UNPKG

@ark-ui/vue

Version:

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

40 lines (37 loc) 1.43 kB
import * as listbox from '@zag-js/listbox'; 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 useListbox = (props, emit) => { const id = useId(); const env = useEnvironmentContext(DEFAULT_ENVIRONMENT); const locale = useLocaleContext(DEFAULT_LOCALE); const context = computed(() => { const localProps = toValue(props); return { id, dir: locale.value.dir, value: localProps.modelValue, getRootNode: env?.value.getRootNode, ...cleanProps(localProps), onValueChange: (details) => { emit?.("valueChange", details); emit?.("update:modelValue", details.value); localProps.onValueChange?.(details); }, onHighlightChange: (details) => { emit?.("highlightChange", details); localProps.onHighlightChange?.(details); }, onSelect: (details) => { emit?.("select", details); localProps.onSelect?.(details); } }; }); const service = useMachine(listbox.machine, context); return computed(() => listbox.connect(service, normalizeProps)); }; export { useListbox };