UNPKG

@ark-ui/vue

Version:

A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.

46 lines (45 loc) 1.7 kB
import { DEFAULT_ENVIRONMENT, useEnvironmentContext } from "../../providers/environment/use-environment-context.js"; import { DEFAULT_LOCALE, useLocaleContext } from "../../providers/locale/use-locale-context.js"; import { cleanProps } from "../../utils/clean-props.js"; import * as collapsible from "@zag-js/collapsible"; import { computed, ref, toValue, useId, watch } from "vue"; import { normalizeProps as normalizeProps$1, useMachine } from "@zag-js/vue"; //#region src/components/collapsible/use-collapsible.ts var useCollapsible = (props = {}, emits) => { const id = useId(); const env = useEnvironmentContext(DEFAULT_ENVIRONMENT); const locale = useLocaleContext(DEFAULT_LOCALE); const context = computed(() => { const localeProps = toValue(props); return { id, dir: locale.value.dir, getRootNode: env?.value.getRootNode, ...cleanProps(localeProps), onExitComplete: () => { emits?.("exitComplete"); localeProps.onExitComplete?.(); }, onOpenChange: (details) => { emits?.("openChange", details); emits?.("update:open", details.open); localeProps.onOpenChange?.(details); } }; }); const service = useMachine(collapsible.machine, context); const api = computed(() => collapsible.connect(service, normalizeProps$1)); const wasVisible = ref(false); watch(() => api.value.visible, () => { if (api.value.visible) wasVisible.value = true; }); return computed(() => { const localeProps = toValue(props); return { ...api.value, unmounted: !api.value.visible && !wasVisible.value && localeProps.lazyMount || localeProps.unmountOnExit && !api.value.visible && wasVisible.value }; }); }; //#endregion export { useCollapsible };