UNPKG

@ark-ui/vue

Version:

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

41 lines (38 loc) 1.52 kB
import * as treeView from '@zag-js/tree-view'; import { useMachine, normalizeProps } from '@zag-js/vue'; import { useId, computed, toValue } from 'vue'; import { useEnvironmentContext } 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(); 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); } }; }); const service = useMachine(treeView.machine, context); return computed(() => treeView.connect(service, normalizeProps)); }; export { useTreeView };