UNPKG

@ark-ui/vue

Version:

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

54 lines (51 loc) 2.07 kB
import * as treeView from '@zag-js/tree-view'; import { useMachine, normalizeProps } from '@zag-js/vue'; import { useId, computed, toValue } from 'vue'; import { useEnvironmentContext, DEFAULT_ENVIRONMENT } from '../../providers/environment/use-environment-context.js'; import { useLocaleContext, DEFAULT_LOCALE } from '../../providers/locale/use-locale-context.js'; import { cleanProps } from '../../utils/clean-props.js'; const useTreeView = (props, emit) => { 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), onFocusChange: (details) => { emit?.("focusChange", details); emit?.("update:focusedValue", details.focusedValue); localeProps.onFocusChange?.(details); }, onExpandedChange: (details) => { emit?.("expandedChange", details); emit?.("update:expandedValue", details.expandedValue); localeProps.onExpandedChange?.(details); }, onSelectionChange: (details) => { emit?.("selectionChange", details); emit?.("update:selectedValue", details.selectedValue); localeProps.onSelectionChange?.(details); }, onCheckedChange: (details) => { emit?.("checkedChange", details); emit?.("update:checkedValue", details.checkedValue); localeProps.onCheckedChange?.(details); }, onLoadChildrenComplete: (details) => { emit?.("loadChildrenComplete", details); localeProps.onLoadChildrenComplete?.(details); }, onLoadChildrenError: (details) => { emit?.("loadChildrenError", details); localeProps.onLoadChildrenError?.(details); } }; }); const service = useMachine(treeView.machine, context); return computed(() => treeView.connect(service, normalizeProps)); }; export { useTreeView };