mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.9 KiB
75 lines
1.9 KiB
2 years ago
|
import { useStorage } from '@vueuse/core'
|
||
|
import { useInjectionState, watch } from '#imports'
|
||
2 years ago
|
|
||
|
interface UseSidebarProps {
|
||
|
hasSidebar?: boolean
|
||
|
isOpen?: boolean
|
||
2 years ago
|
storageKey?: string // if a storageKey is passed, use that key for localStorage
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
/**
|
||
2 years ago
|
* Injection state for sidebars
|
||
|
*
|
||
2 years ago
|
* Use `provideSidebar` to provide the injection state on current component level (will affect all children injections)
|
||
2 years ago
|
* Use `useSidebar` to use the injection state on current component level
|
||
2 years ago
|
*
|
||
|
* If `provideSidebar` is not called explicitly, `useSidebar` will trigger the provider if no injection state can be found
|
||
2 years ago
|
*/
|
||
2 years ago
|
const [setup, use] = useInjectionState((props: UseSidebarProps = {}) => {
|
||
2 years ago
|
let isOpen = ref(props.isOpen ?? false)
|
||
|
let hasSidebar = ref(props.hasSidebar ?? true)
|
||
|
|
||
|
function toggle(state?: boolean) {
|
||
|
isOpen.value = state ?? !isOpen.value
|
||
|
}
|
||
|
|
||
|
function toggleHasSidebar(state?: boolean) {
|
||
|
hasSidebar.value = state ?? !hasSidebar.value
|
||
|
}
|
||
|
|
||
|
if (props.storageKey) {
|
||
|
const storage = toRefs(useStorage(props.storageKey, { isOpen, hasSidebar }, localStorage, { mergeDefaults: true }).value)
|
||
|
isOpen = storage.isOpen
|
||
|
hasSidebar = storage.hasSidebar
|
||
|
}
|
||
2 years ago
|
|
||
|
watch(
|
||
|
hasSidebar,
|
||
|
(nextHasSidebar) => {
|
||
|
if (!nextHasSidebar) toggle(false)
|
||
|
},
|
||
|
{ immediate: true },
|
||
|
)
|
||
|
|
||
|
watch(
|
||
|
isOpen,
|
||
|
(nextIsOpen) => {
|
||
|
if (nextIsOpen && !hasSidebar.value) toggleHasSidebar(true)
|
||
|
},
|
||
|
{ immediate: true },
|
||
|
)
|
||
|
|
||
|
return {
|
||
|
isOpen,
|
||
|
toggle,
|
||
|
hasSidebar,
|
||
|
toggleHasSidebar,
|
||
|
}
|
||
|
}, 'useSidebar')
|
||
|
|
||
2 years ago
|
export const provideSidebar = setup
|
||
|
|
||
2 years ago
|
export function useSidebar(props: UseSidebarProps = {}) {
|
||
|
const state = use()
|
||
|
|
||
|
if (!state) {
|
||
|
return setup(props)
|
||
|
} else {
|
||
|
// set state if props were passed
|
||
|
if (typeof props.isOpen !== 'undefined') state.isOpen.value = props.isOpen
|
||
|
if (typeof props.hasSidebar !== 'undefined') state.hasSidebar.value = props.hasSidebar
|
||
|
}
|
||
|
|
||
|
return state
|
||
|
}
|