@pdanpdan/headless-combobox-vue 0.0.0-stage → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +330 -2
- package/dist/HeadlessCombobox.vue.d.ts +27 -0
- package/dist/HeadlessCombobox.vue.d.ts.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +183 -0
- package/dist/index.mjs.map +1 -0
- package/dist/useHeadlessCombobox.d.ts +61 -0
- package/dist/useHeadlessCombobox.d.ts.map +1 -0
- package/examples/ComposableProgrammatic.vue +195 -0
- package/examples/ComposableSingle.vue +206 -0
- package/examples/NiceSelect.vue +149 -0
- package/examples/ObjectMultipleValidated.vue +192 -0
- package/examples/ObjectSingle.vue +189 -0
- package/examples/ObjectValueSingle.vue +148 -0
- package/examples/TextInternal.vue +148 -0
- package/examples/TextMultiple.vue +171 -0
- package/examples/TextMultipleChips.vue +192 -0
- package/examples/TextMultipleCustom.vue +162 -0
- package/examples/TextSelectOnTab.vue +148 -0
- package/examples/TextSingle.vue +144 -0
- package/examples/TextTypeahead.vue +184 -0
- package/examples/TextTypeaheadChips.vue +218 -0
- package/package.json +79 -3
- package/src/HeadlessCombobox.vue +124 -0
- package/src/index.ts +15 -0
- package/src/useHeadlessCombobox.ts +239 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["_useModel","_renderSlot","_unref","PROP_KEYS","resolveProps","base","isRef","source","resolved","key","value","useHeadlessCombobox","resolvedProps","computed","propsSource","instance","getCurrentInstance","generatedId","useId","store","createCombobox","emit","nextTick","toRaw","snapshot","shallowRef","unsubscribe","watch","next","onMounted","onUnmounted","option","index","returnFocus","event","el","HeadlessCombobox_default","defineComponent","\"modelValue\"","\"modelModifiers\"","props","__props","model","useModel","renderSlot","_ctx","unref","isOpen","multiple","disabled","readonly","searchQuery","filteredOptions","highlightedIndex","alignmentOffset","cssAnchorName","popupStyle","selectedCount","selectedList","canSelectMore","isSelected","valid","errors","validationMessage","triggerProps","inputProps","comboboxInputProps","listboxProps","getOptionProps","setSearchQuery","setHighlightedIndex","toggle","open","close","select","clear","focusInput","handleKeydown","setContainerRef","setTriggerRef","setDropdownRef","setInputRef","setListRef","setOptionRef"],"sources":["../src/useHeadlessCombobox.ts","../src/HeadlessCombobox.vue"],"sourcesContent":["import type {\n HeadlessComboboxProps,\n HeadlessComboboxSnapshot,\n} from '@pdanpdan/headless-combobox-core';\nimport type { ComputedRef, Ref } from 'vue';\n\nimport { createCombobox } from '@pdanpdan/headless-combobox-core';\nimport {\n computed,\n getCurrentInstance,\n isRef,\n nextTick,\n onMounted,\n onUnmounted,\n shallowRef,\n toRaw,\n useId,\n watch,\n} from 'vue';\n\nexport type {\n HeadlessComboboxComboboxInputProps,\n HeadlessComboboxErrorCode,\n HeadlessComboboxInputProps,\n HeadlessComboboxListboxProps,\n HeadlessComboboxOptionProps,\n HeadlessComboboxPopupStyle,\n HeadlessComboboxProps,\n HeadlessComboboxTriggerProps,\n} from '@pdanpdan/headless-combobox-core';\n\n/** Scope exposed by the default slot: plain values, no refs. */\nexport type HeadlessComboboxSlotProps<O, V, Q = string> = HeadlessComboboxSnapshot<O, V, Q>;\n\n/**\n * Accepted forms for `useHeadlessCombobox` props: a plain props object (fields\n * may be refs, which are unwrapped and tracked), a ref to a props object, or a\n * getter. Normalized internally, so no `reactive()` wrapper needed by callers.\n */\nexport type HeadlessComboboxPropsSource<O, V = O, Q = string>\n = | HeadlessComboboxProps<O, V, Q>\n | Ref<HeadlessComboboxProps<O, V, Q>>\n | (() => HeadlessComboboxProps<O, V, Q>)\n | { [K in keyof HeadlessComboboxProps<O, V, Q>]: HeadlessComboboxProps<O, V, Q>[ K ] | Ref<HeadlessComboboxProps<O, V, Q>[ K ]> };\n\n/** Return value of `useHeadlessCombobox`: Vue refs for state, stable actions. */\nexport interface HeadlessComboboxScope<O, V, Q = string> {\n // State\n isOpen: Ref<boolean>;\n multiple: ComputedRef<boolean>;\n disabled: ComputedRef<boolean>;\n readonly: ComputedRef<boolean>;\n searchQuery: Ref<Q | undefined>;\n filteredOptions: ComputedRef<O[]>;\n highlightedIndex: Ref<number>;\n alignmentOffset: Ref<number>;\n cssAnchorName: string;\n popupStyle: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>[ 'popupStyle' ]>;\n selectedCount: ComputedRef<number>;\n selectedList: ComputedRef<V[]>;\n canSelectMore: ComputedRef<boolean>;\n isSelected: (option: O) => boolean;\n valid: ComputedRef<boolean>;\n errors: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>[ 'errors' ]>;\n validationMessage: ComputedRef<string>;\n // ARIA prop bags (spread with v-bind)\n triggerProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>[ 'triggerProps' ]>;\n inputProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>[ 'inputProps' ]>;\n comboboxInputProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>[ 'comboboxInputProps' ]>;\n listboxProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>[ 'listboxProps' ]>;\n getOptionProps: (option: O, index: number) => HeadlessComboboxSnapshot<O, V, Q>[ 'getOptionProps' ] extends (option: O, index: number) => infer R\n ? R\n : never;\n // Actions\n setSearchQuery: (value: Q | undefined) => void;\n setHighlightedIndex: (index: number) => void;\n toggle: () => void;\n open: () => Promise<void>;\n close: (returnFocus?: boolean) => void;\n select: (option: O) => void;\n clear: () => void;\n focusInput: () => void;\n handleKeydown: (event: Event) => void;\n // Element wiring\n setContainerRef: (el: unknown) => void;\n setTriggerRef: (el: unknown) => void;\n setDropdownRef: (el: unknown) => void;\n setInputRef: (el: unknown) => void;\n setListRef: (el: unknown) => void;\n setOptionRef: (option: O, el: unknown) => void;\n}\n\n/** The props fields read from a props source; a fixed list keeps tracking deterministic. */\nconst PROP_KEYS = [\n 'modelValue',\n 'options',\n 'multiple',\n 'minLength',\n 'maxLength',\n 'required',\n 'disabled',\n 'readonly',\n 'closeOnSelect',\n 'closeOnClickOutside',\n 'clickOutsideFilter',\n 'selectOnTab',\n 'optionValue',\n 'optionLabel',\n 'optionFilter',\n 'id',\n 'alignSelected',\n 'inputOnOpen',\n 'errorMessages',\n] as const;\n\nfunction resolveProps<O, V, Q>(source: HeadlessComboboxPropsSource<O, V, Q>): HeadlessComboboxProps<O, V, Q> {\n const base = isRef(source)\n ? source.value\n : typeof source === 'function'\n ? source()\n : source;\n const resolved: Record<string, unknown> = {};\n for (const key of PROP_KEYS) {\n const value = (base as Record<string, unknown>)[ key ];\n resolved[ key ] = isRef(value)\n ? value.value\n : value;\n }\n return resolved as unknown as HeadlessComboboxProps<O, V, Q>;\n}\n\n/**\n * Vue adapter for the framework-independent combobox core.\n *\n * Returns Vue refs backed by the core snapshot. Call it from `setup()`; it\n * mounts the document listeners and uses Vue's `useId()` for default ids.\n */\nexport function useHeadlessCombobox<O, V = O, Q = string>(\n propsSource: HeadlessComboboxPropsSource<O, V, Q>,\n emit: (value: V | V[] | null) => void,\n): HeadlessComboboxScope<O, V, Q> {\n const resolvedProps = computed(() => resolveProps(propsSource));\n const instance = getCurrentInstance();\n const generatedId = instance\n ? useId()\n : undefined;\n\n const store = createCombobox<O, V, Q>({\n props: resolvedProps.value,\n onChange: emit,\n runtime: {\n nextTick: () => nextTick(),\n unwrap: (value) => toRaw(value),\n getDocument: () => (typeof document === 'undefined'\n ? null\n : document),\n ...(generatedId != null\n ? {\n createId: () => generatedId,\n }\n : {}),\n },\n });\n\n const snapshot = shallowRef(store.getSnapshot());\n const unsubscribe = store.subscribe(() => {\n snapshot.value = store.getSnapshot();\n });\n\n // Props may be Vue-reactive (component props) or contain refs (composable\n // callers). The computed tracks both; push each change into the core.\n watch(resolvedProps, (next) => store.setProps(next), { immediate: true });\n\n if (instance) {\n onMounted(() => store.mount());\n onUnmounted(() => {\n unsubscribe();\n store.destroy();\n });\n } else {\n store.mount();\n }\n\n const isSelected = (option: O) => store.getSnapshot().isSelected(option);\n const getOptionProps = (option: O, index: number) =>\n store.getSnapshot().getOptionProps(option, index);\n\n return {\n isOpen: computed({\n get: () => snapshot.value.isOpen,\n set: (value) => store.setState({ isOpen: value }),\n }),\n multiple: computed(() => snapshot.value.multiple),\n disabled: computed(() => snapshot.value.disabled),\n readonly: computed(() => snapshot.value.readonly),\n searchQuery: computed({\n get: () => snapshot.value.searchQuery,\n set: (value) => store.getSnapshot().setSearchQuery(value),\n }),\n filteredOptions: computed(() => snapshot.value.filteredOptions),\n highlightedIndex: computed({\n get: () => snapshot.value.highlightedIndex,\n set: (value) => store.setState({ highlightedIndex: value }),\n }),\n alignmentOffset: computed({\n get: () => snapshot.value.alignmentOffset,\n set: (value) => store.setState({ alignmentOffset: value }),\n }),\n cssAnchorName: snapshot.value.cssAnchorName,\n popupStyle: computed(() => snapshot.value.popupStyle),\n selectedCount: computed(() => snapshot.value.selectedCount),\n selectedList: computed(() => snapshot.value.selectedList),\n canSelectMore: computed(() => snapshot.value.canSelectMore),\n isSelected,\n valid: computed(() => snapshot.value.valid),\n errors: computed(() => snapshot.value.errors),\n validationMessage: computed(() => snapshot.value.validationMessage),\n triggerProps: computed(() => snapshot.value.triggerProps),\n inputProps: computed(() => snapshot.value.inputProps),\n comboboxInputProps: computed(() => snapshot.value.comboboxInputProps),\n listboxProps: computed(() => snapshot.value.listboxProps),\n getOptionProps,\n setSearchQuery: (value) => store.getSnapshot().setSearchQuery(value),\n setHighlightedIndex: (index) => store.getSnapshot().setHighlightedIndex(index),\n toggle: () => store.getSnapshot().toggle(),\n open: () => store.getSnapshot().open(),\n close: (returnFocus) => store.getSnapshot().close(returnFocus),\n select: (option) => store.getSnapshot().select(option),\n clear: () => store.getSnapshot().clear(),\n focusInput: () => store.getSnapshot().focusInput(),\n handleKeydown: (event) => store.getSnapshot().handleKeydown(event),\n setContainerRef: (el) => store.getSnapshot().setContainerRef(el),\n setTriggerRef: (el) => store.getSnapshot().setTriggerRef(el),\n setDropdownRef: (el) => store.getSnapshot().setDropdownRef(el),\n setInputRef: (el) => store.getSnapshot().setInputRef(el),\n setListRef: (el) => store.getSnapshot().setListRef(el),\n setOptionRef: (option, el) => store.getSnapshot().setOptionRef(option, el),\n };\n}\n","<script setup lang=\"ts\" generic=\"O, V = O, Q = string\">\nimport type { HeadlessComboboxProps, HeadlessComboboxSlotProps } from './useHeadlessCombobox';\n\nimport { useHeadlessCombobox } from './useHeadlessCombobox';\n\nexport type {\n HeadlessComboboxComboboxInputProps,\n HeadlessComboboxErrorCode,\n HeadlessComboboxInputProps,\n HeadlessComboboxListboxProps,\n HeadlessComboboxOptionProps,\n HeadlessComboboxPopupStyle,\n HeadlessComboboxProps,\n HeadlessComboboxPropsSource,\n HeadlessComboboxScope,\n HeadlessComboboxSlotProps,\n HeadlessComboboxTriggerProps,\n} from './useHeadlessCombobox';\n\nconst props = withDefaults(defineProps<Omit<HeadlessComboboxProps<O, V, Q>, 'modelValue'>>(), {\n // defaults to auto based on `multiple`.\n closeOnSelect: null,\n // Vue casts absent boolean props to `false`; the default must be explicit.\n closeOnClickOutside: true,\n inputOnOpen: 'select',\n});\n\ndefineSlots<{\n default: (props: HeadlessComboboxSlotProps<O, V, Q>) => unknown;\n}>();\n\n// No parent `v-model`? The model falls back to local state (`defineModel`\n// keeps the last set value internally) so the component still works.\nconst model = defineModel<V | V[] | null>('modelValue', { default: null });\n\nconst {\n isOpen,\n multiple,\n disabled,\n readonly,\n searchQuery,\n filteredOptions,\n highlightedIndex,\n alignmentOffset,\n cssAnchorName,\n popupStyle,\n selectedCount,\n selectedList,\n canSelectMore,\n isSelected,\n valid,\n errors,\n validationMessage,\n triggerProps,\n inputProps,\n comboboxInputProps,\n listboxProps,\n getOptionProps,\n setSearchQuery,\n setHighlightedIndex,\n toggle,\n open,\n close,\n select,\n clear,\n focusInput,\n handleKeydown,\n setContainerRef,\n setTriggerRef,\n setDropdownRef,\n setInputRef,\n setListRef,\n setOptionRef,\n} = useHeadlessCombobox<O, V, Q>(\n // The getter keeps the props reactive: reading `props` inside it tracks\n // every field, and the model ref is read on each access too.\n () => ({ ...props, modelValue: model.value }) as unknown as HeadlessComboboxProps<O, V, Q>,\n (value) => {\n model.value = value;\n },\n);\n</script>\n\n<template>\n <slot\n :is-open=\"isOpen\"\n :multiple=\"multiple === true\"\n :disabled=\"disabled === true\"\n :readonly=\"readonly === true\"\n :search-query=\"(searchQuery as Q | undefined)\"\n :filtered-options=\"filteredOptions\"\n :highlighted-index=\"highlightedIndex\"\n :alignment-offset=\"alignmentOffset\"\n :css-anchor-name=\"cssAnchorName\"\n :popup-style=\"popupStyle\"\n :selected-count=\"selectedCount\"\n :selected-list=\"selectedList\"\n :can-select-more=\"canSelectMore\"\n :is-selected=\"isSelected\"\n :valid=\"valid\"\n :errors=\"errors\"\n :validation-message=\"validationMessage\"\n :trigger-props=\"triggerProps\"\n :input-props=\"inputProps\"\n :combobox-input-props=\"comboboxInputProps\"\n :listbox-props=\"listboxProps\"\n :get-option-props=\"getOptionProps\"\n :set-search-query=\"setSearchQuery\"\n :set-highlighted-index=\"setHighlightedIndex\"\n :toggle=\"toggle\"\n :open=\"open\"\n :close=\"close\"\n :select=\"select\"\n :clear=\"clear\"\n :focus-input=\"focusInput\"\n :handle-keydown=\"handleKeydown\"\n :set-container-ref=\"setContainerRef\"\n :set-trigger-ref=\"setTriggerRef\"\n :set-dropdown-ref=\"setDropdownRef\"\n :set-input-ref=\"setInputRef\"\n :set-list-ref=\"setListRef\"\n :set-option-ref=\"setOptionRef\"\n />\n</template>\n"],"mappings":";;;AA6FA,IAAMG,IAAY;CAChB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,SAASC,EAAsB,GAA8E;CAC3G,IAAMC,IAAOC,EAAMC,CAAM,IACrBA,EAAO,QACP,OAAOA,KAAW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A;GACjB,YAAarD,EAAAsD,CAAA;GACb,eAAgBtD,EAAAuD,CAAA;GAChB,cAAevD,EAAAwD,CAAA;GACf,eAAiBxD,EAAAyD,CAAA;GACjB,YAAazD,EAAA0D,CAAA;GACb,OAAO1D,EAAA2D,CAAA;GACP,QAAQ3D,EAAA4D,CAAA;GACR,mBAAoB5D,EAAA6D,CAAA;GACpB,cAAe7D,EAAA8D,CAAA;GACf,YAAa9D,EAAA+D,CAAA;GACb,oBAAsB/D,EAAAgE,CAAA;GACtB,cAAehE,EAAAiE,CAAA;GACf,gBAAkBjE,EAAAkE,CAAA;GAClB,gBAAkBlE,EAAAmE,CAAA;GAClB,qBAAuBnE,EAAAoE,CAAA;GACvB,QAAQpE,EAAAqE,CAAA;GACR,MAAMrE,EAAAsE,CAAA;GACN,OAAOtE,EAAAuE,CAAA;GACP,QAAQvE,EAAAwE,CAAA;GACR,OAAOxE,EAAAyE,CAAA;GACP,YAAazE,EAAA0E,CAAA;GACb,eAAgB1E,EAAA2E,CAAA;GAChB,iBAAmB3E,EAAA4E,CAAA;GACnB,eAAiB5E,EAAA6E,CAAA;GACjB,gBAAkB7E,EAAA8E,CAAA;GAClB,aAAe9E,EAAA+E,CAAA;GACf,YAAc/E,EAAAgF,CAAA;GACd,cAAgBhF,EAAAiF,CAAA"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { HeadlessComboboxProps, HeadlessComboboxSnapshot } from '../../headless-combobox-core/src/index.ts';
|
|
2
|
+
import { ComputedRef, Ref } from 'vue';
|
|
3
|
+
export type { HeadlessComboboxComboboxInputProps, HeadlessComboboxErrorCode, HeadlessComboboxInputProps, HeadlessComboboxListboxProps, HeadlessComboboxOptionProps, HeadlessComboboxPopupStyle, HeadlessComboboxProps, HeadlessComboboxTriggerProps, } from '../../headless-combobox-core/src/index.ts';
|
|
4
|
+
/** Scope exposed by the default slot: plain values, no refs. */
|
|
5
|
+
export type HeadlessComboboxSlotProps<O, V, Q = string> = HeadlessComboboxSnapshot<O, V, Q>;
|
|
6
|
+
/**
|
|
7
|
+
* Accepted forms for `useHeadlessCombobox` props: a plain props object (fields
|
|
8
|
+
* may be refs, which are unwrapped and tracked), a ref to a props object, or a
|
|
9
|
+
* getter. Normalized internally, so no `reactive()` wrapper needed by callers.
|
|
10
|
+
*/
|
|
11
|
+
export type HeadlessComboboxPropsSource<O, V = O, Q = string> = HeadlessComboboxProps<O, V, Q> | Ref<HeadlessComboboxProps<O, V, Q>> | (() => HeadlessComboboxProps<O, V, Q>) | {
|
|
12
|
+
[K in keyof HeadlessComboboxProps<O, V, Q>]: HeadlessComboboxProps<O, V, Q>[K] | Ref<HeadlessComboboxProps<O, V, Q>[K]>;
|
|
13
|
+
};
|
|
14
|
+
/** Return value of `useHeadlessCombobox`: Vue refs for state, stable actions. */
|
|
15
|
+
export interface HeadlessComboboxScope<O, V, Q = string> {
|
|
16
|
+
isOpen: Ref<boolean>;
|
|
17
|
+
multiple: ComputedRef<boolean>;
|
|
18
|
+
disabled: ComputedRef<boolean>;
|
|
19
|
+
readonly: ComputedRef<boolean>;
|
|
20
|
+
searchQuery: Ref<Q | undefined>;
|
|
21
|
+
filteredOptions: ComputedRef<O[]>;
|
|
22
|
+
highlightedIndex: Ref<number>;
|
|
23
|
+
alignmentOffset: Ref<number>;
|
|
24
|
+
cssAnchorName: string;
|
|
25
|
+
popupStyle: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>['popupStyle']>;
|
|
26
|
+
selectedCount: ComputedRef<number>;
|
|
27
|
+
selectedList: ComputedRef<V[]>;
|
|
28
|
+
canSelectMore: ComputedRef<boolean>;
|
|
29
|
+
isSelected: (option: O) => boolean;
|
|
30
|
+
valid: ComputedRef<boolean>;
|
|
31
|
+
errors: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>['errors']>;
|
|
32
|
+
validationMessage: ComputedRef<string>;
|
|
33
|
+
triggerProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>['triggerProps']>;
|
|
34
|
+
inputProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>['inputProps']>;
|
|
35
|
+
comboboxInputProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>['comboboxInputProps']>;
|
|
36
|
+
listboxProps: ComputedRef<HeadlessComboboxSnapshot<O, V, Q>['listboxProps']>;
|
|
37
|
+
getOptionProps: (option: O, index: number) => HeadlessComboboxSnapshot<O, V, Q>['getOptionProps'] extends (option: O, index: number) => infer R ? R : never;
|
|
38
|
+
setSearchQuery: (value: Q | undefined) => void;
|
|
39
|
+
setHighlightedIndex: (index: number) => void;
|
|
40
|
+
toggle: () => void;
|
|
41
|
+
open: () => Promise<void>;
|
|
42
|
+
close: (returnFocus?: boolean) => void;
|
|
43
|
+
select: (option: O) => void;
|
|
44
|
+
clear: () => void;
|
|
45
|
+
focusInput: () => void;
|
|
46
|
+
handleKeydown: (event: Event) => void;
|
|
47
|
+
setContainerRef: (el: unknown) => void;
|
|
48
|
+
setTriggerRef: (el: unknown) => void;
|
|
49
|
+
setDropdownRef: (el: unknown) => void;
|
|
50
|
+
setInputRef: (el: unknown) => void;
|
|
51
|
+
setListRef: (el: unknown) => void;
|
|
52
|
+
setOptionRef: (option: O, el: unknown) => void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Vue adapter for the framework-independent combobox core.
|
|
56
|
+
*
|
|
57
|
+
* Returns Vue refs backed by the core snapshot. Call it from `setup()`; it
|
|
58
|
+
* mounts the document listeners and uses Vue's `useId()` for default ids.
|
|
59
|
+
*/
|
|
60
|
+
export declare function useHeadlessCombobox<O, V = O, Q = string>(propsSource: HeadlessComboboxPropsSource<O, V, Q>, emit: (value: V | V[] | null) => void): HeadlessComboboxScope<O, V, Q>;
|
|
61
|
+
//# sourceMappingURL=useHeadlessCombobox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useHeadlessCombobox.d.ts","sourceRoot":"","sources":["../src/useHeadlessCombobox.ts"],"names":[],"mappings":"AA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AAgB,mBAAmB,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EACtD,WAAW,EAAE,2BAA2B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EACjD,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,KAAK,IAAI,GACpC,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAkGhC"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Composable · programmatic control';
|
|
4
|
+
export const description = 'open, close, select, and clear are plain functions, so the buttons outside the widget can call them. clickOutsideFilter keeps the popup open while the panel is used.';
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { useHeadlessCombobox } from '@pdanpdan/headless-combobox-vue';
|
|
9
|
+
import { ref, useId } from 'vue';
|
|
10
|
+
|
|
11
|
+
interface User {
|
|
12
|
+
id: number;
|
|
13
|
+
name: string;
|
|
14
|
+
role: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const users = [
|
|
18
|
+
{ id: 1, name: 'Wade Cooper', role: 'Admin' },
|
|
19
|
+
{ id: 2, name: 'Arlene Mccoy', role: 'Editor' },
|
|
20
|
+
{ id: 3, name: 'Devon Webb', role: 'Viewer' },
|
|
21
|
+
{ id: 4, name: 'Tom Cook', role: 'Editor' },
|
|
22
|
+
{ id: 5, name: 'Tanya Fox', role: 'Admin' },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
const selected = ref<User | null>(null);
|
|
26
|
+
const labelId = useId();
|
|
27
|
+
|
|
28
|
+
// The composable escapes the slot: actions are plain functions, so they can be
|
|
29
|
+
// called from anywhere, here from buttons outside the widget entirely. The
|
|
30
|
+
// control panel is outside the click-outside boundary, so the filter keeps the
|
|
31
|
+
// popup open when it is clicked.
|
|
32
|
+
const {
|
|
33
|
+
isOpen,
|
|
34
|
+
highlightedIndex,
|
|
35
|
+
isSelected,
|
|
36
|
+
cssAnchorName,
|
|
37
|
+
popupStyle,
|
|
38
|
+
triggerProps,
|
|
39
|
+
listboxProps,
|
|
40
|
+
getOptionProps,
|
|
41
|
+
open,
|
|
42
|
+
close,
|
|
43
|
+
select,
|
|
44
|
+
clear,
|
|
45
|
+
setContainerRef,
|
|
46
|
+
setTriggerRef,
|
|
47
|
+
setDropdownRef,
|
|
48
|
+
setListRef,
|
|
49
|
+
setOptionRef,
|
|
50
|
+
} = useHeadlessCombobox<User>(
|
|
51
|
+
{
|
|
52
|
+
modelValue: selected,
|
|
53
|
+
options: users,
|
|
54
|
+
optionLabel: (user: User) => user.name,
|
|
55
|
+
clickOutsideFilter: (target: EventTarget | null) =>
|
|
56
|
+
!(target instanceof HTMLElement && target.closest('.cbx-controls')),
|
|
57
|
+
},
|
|
58
|
+
(value) => {
|
|
59
|
+
selected.value = value as User | null;
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
</script>
|
|
63
|
+
|
|
64
|
+
<template>
|
|
65
|
+
<div class="w-full max-w-sm">
|
|
66
|
+
<div class="flex flex-col gap-3">
|
|
67
|
+
<p class="text-xs text-base-content/80">Drive it from anywhere:</p>
|
|
68
|
+
<div class="cbx-controls flex flex-wrap items-center gap-2">
|
|
69
|
+
<button
|
|
70
|
+
type="button"
|
|
71
|
+
class="btn btn-sm"
|
|
72
|
+
@click="open()"
|
|
73
|
+
>
|
|
74
|
+
Open
|
|
75
|
+
</button>
|
|
76
|
+
<button
|
|
77
|
+
type="button"
|
|
78
|
+
class="btn btn-sm"
|
|
79
|
+
@click="close()"
|
|
80
|
+
>
|
|
81
|
+
Close
|
|
82
|
+
</button>
|
|
83
|
+
<button
|
|
84
|
+
type="button"
|
|
85
|
+
class="btn btn-sm"
|
|
86
|
+
@click="select(users[ 1 ]!)"
|
|
87
|
+
>
|
|
88
|
+
Select Arlene
|
|
89
|
+
</button>
|
|
90
|
+
<button
|
|
91
|
+
type="button"
|
|
92
|
+
class="btn btn-sm"
|
|
93
|
+
@click="clear()"
|
|
94
|
+
>
|
|
95
|
+
Clear
|
|
96
|
+
</button>
|
|
97
|
+
</div>
|
|
98
|
+
|
|
99
|
+
<p class="text-xs text-base-content/80">
|
|
100
|
+
Selected: {{ selected?.name ?? 'none' }} · popup {{ isOpen ? 'open' : 'closed' }}
|
|
101
|
+
</p>
|
|
102
|
+
|
|
103
|
+
<fieldset
|
|
104
|
+
:ref="setContainerRef"
|
|
105
|
+
class="fieldset"
|
|
106
|
+
>
|
|
107
|
+
<legend :id="labelId" class="fieldset-legend">Member (composable, programmatic)</legend>
|
|
108
|
+
<button
|
|
109
|
+
:ref="setTriggerRef"
|
|
110
|
+
v-bind="triggerProps"
|
|
111
|
+
:aria-labelledby="labelId"
|
|
112
|
+
type="button"
|
|
113
|
+
class="input flex w-full cursor-pointer items-center justify-between"
|
|
114
|
+
:style="{ anchorName: cssAnchorName }"
|
|
115
|
+
>
|
|
116
|
+
<span :class="{ 'text-base-content/70': selected == null }">{{ selected?.name ?? 'Select a member…' }}</span>
|
|
117
|
+
<svg
|
|
118
|
+
class="size-4 opacity-70"
|
|
119
|
+
fill="none"
|
|
120
|
+
stroke="currentColor"
|
|
121
|
+
viewBox="0 0 24 24"
|
|
122
|
+
>
|
|
123
|
+
<path
|
|
124
|
+
stroke-linecap="round"
|
|
125
|
+
stroke-linejoin="round"
|
|
126
|
+
stroke-width="2"
|
|
127
|
+
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
|
128
|
+
/>
|
|
129
|
+
</svg>
|
|
130
|
+
</button>
|
|
131
|
+
|
|
132
|
+
<ul
|
|
133
|
+
:ref="(el) => { setDropdownRef(el); setListRef(el); }"
|
|
134
|
+
v-bind="listboxProps"
|
|
135
|
+
popover="manual"
|
|
136
|
+
class="
|
|
137
|
+
cbx-popup menu max-h-60 flex-nowrap gap-0.5 rounded-box border
|
|
138
|
+
border-base-content/5 bg-base-200 shadow-xl
|
|
139
|
+
"
|
|
140
|
+
:style="popupStyle"
|
|
141
|
+
>
|
|
142
|
+
<li
|
|
143
|
+
v-for="(user, index) in users"
|
|
144
|
+
:key="user.id"
|
|
145
|
+
>
|
|
146
|
+
<button
|
|
147
|
+
:ref="(el) => setOptionRef(user, el)"
|
|
148
|
+
type="button"
|
|
149
|
+
v-bind="getOptionProps(user, index)"
|
|
150
|
+
class="justify-between shadow-none"
|
|
151
|
+
:class="{
|
|
152
|
+
'menu-active': isSelected(user),
|
|
153
|
+
'menu-focus': index === highlightedIndex,
|
|
154
|
+
}"
|
|
155
|
+
>
|
|
156
|
+
<span class="flex flex-col items-start">
|
|
157
|
+
<span>{{ user.name }}</span>
|
|
158
|
+
<span class="text-xs opacity-70">{{ user.role }}</span>
|
|
159
|
+
</span>
|
|
160
|
+
</button>
|
|
161
|
+
</li>
|
|
162
|
+
</ul>
|
|
163
|
+
</fieldset>
|
|
164
|
+
</div>
|
|
165
|
+
</div>
|
|
166
|
+
</template>
|
|
167
|
+
|
|
168
|
+
<style scoped>
|
|
169
|
+
/* Popup opens below the control: slide + fade via the Popover API. */
|
|
170
|
+
.cbx-popup {
|
|
171
|
+
inset: auto;
|
|
172
|
+
position-try: flip-block;
|
|
173
|
+
opacity: 0;
|
|
174
|
+
margin-block-start: 0;
|
|
175
|
+
transition:
|
|
176
|
+
opacity 0.15s ease,
|
|
177
|
+
margin-block-start 0.15s ease,
|
|
178
|
+
overlay 0.15s ease allow-discrete,
|
|
179
|
+
display 0.15s ease allow-discrete;
|
|
180
|
+
|
|
181
|
+
&:not(:popover-open) {
|
|
182
|
+
display: none;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
&:popover-open {
|
|
186
|
+
opacity: 1;
|
|
187
|
+
margin-block-start: 0.5rem;
|
|
188
|
+
|
|
189
|
+
@starting-style {
|
|
190
|
+
opacity: 0;
|
|
191
|
+
margin-block-start: 0;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
</style>
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Composable · single · typeahead';
|
|
4
|
+
export const description = 'The same behavior without the component. useHeadlessCombobox exposes the state, ARIA bags, and actions; wire the markup with plain refs.';
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { useHeadlessCombobox } from '@pdanpdan/headless-combobox-vue';
|
|
9
|
+
import { ref, useId } from 'vue';
|
|
10
|
+
|
|
11
|
+
const languages = [
|
|
12
|
+
'JavaScript',
|
|
13
|
+
'TypeScript',
|
|
14
|
+
'Python',
|
|
15
|
+
'Rust',
|
|
16
|
+
'Go',
|
|
17
|
+
'Java',
|
|
18
|
+
'C#',
|
|
19
|
+
'C++',
|
|
20
|
+
'Ruby',
|
|
21
|
+
'PHP',
|
|
22
|
+
'Swift',
|
|
23
|
+
'Kotlin',
|
|
24
|
+
'Dart',
|
|
25
|
+
'Elixir',
|
|
26
|
+
'Haskell',
|
|
27
|
+
'Scala',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
const selected = ref<string | null>('TypeScript');
|
|
31
|
+
const labelId = useId();
|
|
32
|
+
|
|
33
|
+
// Same state machine as <HeadlessCombobox>, with no slot indirection. Props accept
|
|
34
|
+
// plain values or refs (unwrapped and tracked internally); the callback
|
|
35
|
+
// receives every update:modelValue payload.
|
|
36
|
+
const {
|
|
37
|
+
isOpen,
|
|
38
|
+
filteredOptions,
|
|
39
|
+
highlightedIndex,
|
|
40
|
+
searchQuery,
|
|
41
|
+
toggle,
|
|
42
|
+
open,
|
|
43
|
+
isSelected,
|
|
44
|
+
handleKeydown,
|
|
45
|
+
cssAnchorName,
|
|
46
|
+
popupStyle,
|
|
47
|
+
comboboxInputProps,
|
|
48
|
+
listboxProps,
|
|
49
|
+
getOptionProps,
|
|
50
|
+
setContainerRef,
|
|
51
|
+
setTriggerRef,
|
|
52
|
+
setInputRef,
|
|
53
|
+
setDropdownRef,
|
|
54
|
+
setListRef,
|
|
55
|
+
setOptionRef,
|
|
56
|
+
} = useHeadlessCombobox<string>(
|
|
57
|
+
{
|
|
58
|
+
modelValue: selected,
|
|
59
|
+
options: languages,
|
|
60
|
+
},
|
|
61
|
+
(value) => {
|
|
62
|
+
selected.value = value as string | null;
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
<template>
|
|
68
|
+
<div class="w-full max-w-sm">
|
|
69
|
+
<fieldset
|
|
70
|
+
:ref="setContainerRef"
|
|
71
|
+
class="fieldset"
|
|
72
|
+
>
|
|
73
|
+
<legend :id="labelId" class="fieldset-legend">Language (composable, typeahead)</legend>
|
|
74
|
+
<!-- eslint-disable-next-line vue-a11y/click-events-have-key-events, vue-a11y/no-static-element-interactions -- the inner input is the keyboard-operable control; this only widens the pointer target -->
|
|
75
|
+
<div
|
|
76
|
+
class="input relative flex w-full items-center"
|
|
77
|
+
:style="{ anchorName: cssAnchorName }"
|
|
78
|
+
@click.self="open"
|
|
79
|
+
>
|
|
80
|
+
<input
|
|
81
|
+
:ref="(el) => { setTriggerRef(el); setInputRef(el); }"
|
|
82
|
+
v-bind="comboboxInputProps"
|
|
83
|
+
:aria-labelledby="labelId"
|
|
84
|
+
:value="searchQuery ?? (selected ?? '')"
|
|
85
|
+
type="text"
|
|
86
|
+
class="w-full pe-6"
|
|
87
|
+
placeholder="Search a language…"
|
|
88
|
+
@keydown="(e) => { if (e.key !== ' ') { handleKeydown(e); } }"
|
|
89
|
+
/>
|
|
90
|
+
<button
|
|
91
|
+
type="button"
|
|
92
|
+
tabindex="-1"
|
|
93
|
+
class="
|
|
94
|
+
absolute top-1/2 right-2 -translate-y-1/2 cursor-pointer
|
|
95
|
+
text-base-content/70
|
|
96
|
+
"
|
|
97
|
+
aria-label="Toggle suggestions"
|
|
98
|
+
@click.stop="toggle"
|
|
99
|
+
>
|
|
100
|
+
<svg
|
|
101
|
+
class="
|
|
102
|
+
size-4
|
|
103
|
+
motion-safe:transition-transform
|
|
104
|
+
"
|
|
105
|
+
:class="{ 'rotate-180': isOpen }"
|
|
106
|
+
fill="none"
|
|
107
|
+
stroke="currentColor"
|
|
108
|
+
viewBox="0 0 24 24"
|
|
109
|
+
>
|
|
110
|
+
<path
|
|
111
|
+
stroke-linecap="round"
|
|
112
|
+
stroke-linejoin="round"
|
|
113
|
+
stroke-width="2"
|
|
114
|
+
d="M19 9l-7 7-7-7"
|
|
115
|
+
/>
|
|
116
|
+
</svg>
|
|
117
|
+
</button>
|
|
118
|
+
</div>
|
|
119
|
+
|
|
120
|
+
<ul
|
|
121
|
+
:ref="(el) => { setDropdownRef(el); setListRef(el); }"
|
|
122
|
+
v-bind="listboxProps"
|
|
123
|
+
popover="manual"
|
|
124
|
+
class="
|
|
125
|
+
cbx-popup menu max-h-60 flex-nowrap gap-0.5 rounded-box border
|
|
126
|
+
border-base-content/5 bg-base-200 shadow-xl
|
|
127
|
+
"
|
|
128
|
+
:style="[popupStyle, {
|
|
129
|
+
left: 'calc(anchor(left) - 0.25rem)',
|
|
130
|
+
width: 'calc(anchor-size(width) + 0.5rem)',
|
|
131
|
+
}]"
|
|
132
|
+
>
|
|
133
|
+
<li
|
|
134
|
+
v-for="(language, index) in filteredOptions"
|
|
135
|
+
:key="language"
|
|
136
|
+
>
|
|
137
|
+
<button
|
|
138
|
+
:ref="(el) => setOptionRef(language, el)"
|
|
139
|
+
type="button"
|
|
140
|
+
v-bind="getOptionProps(language, index)"
|
|
141
|
+
class="justify-between shadow-none"
|
|
142
|
+
:class="{
|
|
143
|
+
'menu-active': isSelected(language),
|
|
144
|
+
'menu-focus': index === highlightedIndex,
|
|
145
|
+
}"
|
|
146
|
+
>
|
|
147
|
+
<span>{{ language }}</span>
|
|
148
|
+
<svg
|
|
149
|
+
v-if="isSelected(language)"
|
|
150
|
+
class="size-4"
|
|
151
|
+
fill="none"
|
|
152
|
+
stroke="currentColor"
|
|
153
|
+
viewBox="0 0 24 24"
|
|
154
|
+
>
|
|
155
|
+
<path
|
|
156
|
+
stroke-linecap="round"
|
|
157
|
+
stroke-linejoin="round"
|
|
158
|
+
stroke-width="2"
|
|
159
|
+
d="M4.5 12.75l6 6 9-13.5"
|
|
160
|
+
/>
|
|
161
|
+
</svg>
|
|
162
|
+
</button>
|
|
163
|
+
</li>
|
|
164
|
+
|
|
165
|
+
<li
|
|
166
|
+
v-if="filteredOptions.length === 0"
|
|
167
|
+
class="
|
|
168
|
+
pointer-events-none p-4 text-center text-sm text-base-content/70
|
|
169
|
+
"
|
|
170
|
+
role="presentation"
|
|
171
|
+
>
|
|
172
|
+
No matches.
|
|
173
|
+
</li>
|
|
174
|
+
</ul>
|
|
175
|
+
</fieldset>
|
|
176
|
+
</div>
|
|
177
|
+
</template>
|
|
178
|
+
|
|
179
|
+
<style scoped>
|
|
180
|
+
/* Popup opens below the control: slide + fade via the Popover API. */
|
|
181
|
+
.cbx-popup {
|
|
182
|
+
inset: auto;
|
|
183
|
+
position-try: flip-block;
|
|
184
|
+
opacity: 0;
|
|
185
|
+
margin-block-start: 0;
|
|
186
|
+
transition:
|
|
187
|
+
opacity 0.15s ease,
|
|
188
|
+
margin-block-start 0.15s ease,
|
|
189
|
+
overlay 0.15s ease allow-discrete,
|
|
190
|
+
display 0.15s ease allow-discrete;
|
|
191
|
+
|
|
192
|
+
&:not(:popover-open) {
|
|
193
|
+
display: none;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
&:popover-open {
|
|
197
|
+
opacity: 1;
|
|
198
|
+
margin-block-start: 0.5rem;
|
|
199
|
+
|
|
200
|
+
@starting-style {
|
|
201
|
+
opacity: 0;
|
|
202
|
+
margin-block-start: 0;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
</style>
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Select alignment';
|
|
4
|
+
export const description = 'No search input; the dropdown aligns so the selected option overlays the trigger.';
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { HeadlessCombobox } from '@pdanpdan/headless-combobox-vue';
|
|
9
|
+
import { ref, useId } from 'vue';
|
|
10
|
+
|
|
11
|
+
interface User {
|
|
12
|
+
id: number;
|
|
13
|
+
name: string;
|
|
14
|
+
role: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const users = ref<User[]>([
|
|
18
|
+
{ id: 1, name: 'Wade Cooper', role: 'Admin' },
|
|
19
|
+
{ id: 2, name: 'Arlene Mccoy', role: 'Editor' },
|
|
20
|
+
{ id: 3, name: 'Devon Webb', role: 'Viewer' },
|
|
21
|
+
{ id: 4, name: 'Tom Cook', role: 'Editor' },
|
|
22
|
+
{ id: 5, name: 'Tanya Fox', role: 'Admin' },
|
|
23
|
+
{ id: 6, name: 'Hellen Schmidt', role: 'Viewer' },
|
|
24
|
+
{ id: 7, name: 'Caroline Schultz', role: 'Admin' },
|
|
25
|
+
{ id: 8, name: 'Mason Heaney', role: 'Editor' },
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const selected = ref<User | null>(users.value[ 3 ] ?? null);
|
|
29
|
+
const labelId = useId();
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<template>
|
|
33
|
+
<div class="w-full max-w-sm">
|
|
34
|
+
<HeadlessCombobox
|
|
35
|
+
v-slot="{
|
|
36
|
+
filteredOptions, highlightedIndex, isSelected,
|
|
37
|
+
cssAnchorName, popupStyle, triggerProps, listboxProps,
|
|
38
|
+
getOptionProps, setContainerRef, setTriggerRef, setDropdownRef, setListRef, setOptionRef,
|
|
39
|
+
}"
|
|
40
|
+
v-model="selected"
|
|
41
|
+
:options="users"
|
|
42
|
+
:option-label="(u: User) => u.name"
|
|
43
|
+
align-selected
|
|
44
|
+
>
|
|
45
|
+
<fieldset
|
|
46
|
+
:ref="setContainerRef"
|
|
47
|
+
class="fieldset"
|
|
48
|
+
>
|
|
49
|
+
<legend :id="labelId" class="fieldset-legend">Assign user</legend>
|
|
50
|
+
<button
|
|
51
|
+
:ref="setTriggerRef"
|
|
52
|
+
v-bind="triggerProps"
|
|
53
|
+
:aria-labelledby="labelId"
|
|
54
|
+
type="button"
|
|
55
|
+
class="input flex w-full cursor-pointer items-center justify-between"
|
|
56
|
+
:style="{ anchorName: cssAnchorName }"
|
|
57
|
+
>
|
|
58
|
+
<span :class="{ 'text-base-content/70': selected == null }">{{ selected?.name ?? 'Select a user…' }}</span>
|
|
59
|
+
<svg
|
|
60
|
+
class="size-4 opacity-70"
|
|
61
|
+
fill="none"
|
|
62
|
+
stroke="currentColor"
|
|
63
|
+
viewBox="0 0 24 24"
|
|
64
|
+
>
|
|
65
|
+
<path
|
|
66
|
+
stroke-linecap="round"
|
|
67
|
+
stroke-linejoin="round"
|
|
68
|
+
stroke-width="2"
|
|
69
|
+
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
|
70
|
+
/>
|
|
71
|
+
</svg>
|
|
72
|
+
</button>
|
|
73
|
+
</fieldset>
|
|
74
|
+
|
|
75
|
+
<ul
|
|
76
|
+
:ref="(el) => { setDropdownRef(el); setListRef(el); }"
|
|
77
|
+
v-bind="listboxProps"
|
|
78
|
+
popover="manual"
|
|
79
|
+
class="
|
|
80
|
+
cbx-popup menu max-h-60 flex-nowrap gap-0.5 rounded-box border
|
|
81
|
+
border-base-content/5 bg-base-200 shadow-xl
|
|
82
|
+
"
|
|
83
|
+
:style="[popupStyle, {
|
|
84
|
+
// Menu padding (0.5rem) + popup border (1px) per side, minus the
|
|
85
|
+
// field's own border (1px per side): the option content box then
|
|
86
|
+
// covers the field's content box exactly.
|
|
87
|
+
left: 'calc(anchor(left) - 0.5rem)',
|
|
88
|
+
width: 'calc(anchor-size(width) + 1rem)',
|
|
89
|
+
}]"
|
|
90
|
+
>
|
|
91
|
+
<li
|
|
92
|
+
v-for="(user, index) in filteredOptions"
|
|
93
|
+
:key="user.id"
|
|
94
|
+
>
|
|
95
|
+
<button
|
|
96
|
+
:ref="(el) => setOptionRef(user, el)"
|
|
97
|
+
type="button"
|
|
98
|
+
v-bind="getOptionProps(user, index)"
|
|
99
|
+
class="flex min-h-10 items-center justify-between shadow-none"
|
|
100
|
+
:class="{
|
|
101
|
+
'menu-active': isSelected(user),
|
|
102
|
+
'menu-focus': index === highlightedIndex,
|
|
103
|
+
}"
|
|
104
|
+
>
|
|
105
|
+
<span>{{ user.name }}</span>
|
|
106
|
+
<svg
|
|
107
|
+
v-if="isSelected(user)"
|
|
108
|
+
class="size-4"
|
|
109
|
+
fill="none"
|
|
110
|
+
stroke="currentColor"
|
|
111
|
+
viewBox="0 0 24 24"
|
|
112
|
+
>
|
|
113
|
+
<path
|
|
114
|
+
stroke-linecap="round"
|
|
115
|
+
stroke-linejoin="round"
|
|
116
|
+
stroke-width="2"
|
|
117
|
+
d="M4.5 12.75l6 6 9-13.5"
|
|
118
|
+
/>
|
|
119
|
+
</svg>
|
|
120
|
+
</button>
|
|
121
|
+
</li>
|
|
122
|
+
</ul>
|
|
123
|
+
</HeadlessCombobox>
|
|
124
|
+
</div>
|
|
125
|
+
</template>
|
|
126
|
+
|
|
127
|
+
<style scoped>
|
|
128
|
+
/* Popup covers the control: zoom + fade via the Popover API.
|
|
129
|
+
`scale` composes with the inline `translate` alignment offset. */
|
|
130
|
+
.cbx-popup {
|
|
131
|
+
opacity: 0;
|
|
132
|
+
transition:
|
|
133
|
+
opacity 0.3s ease,
|
|
134
|
+
overlay 0.3s ease allow-discrete,
|
|
135
|
+
display 0.3s ease allow-discrete;
|
|
136
|
+
|
|
137
|
+
&:not(:popover-open) {
|
|
138
|
+
display: none;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
&:popover-open {
|
|
142
|
+
opacity: 1;
|
|
143
|
+
|
|
144
|
+
@starting-style {
|
|
145
|
+
opacity: 0;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
</style>
|