@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,192 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Object options · multiple · validated';
|
|
4
|
+
export const description = 'Multiple selection with required + min/max validation and a rendered message.';
|
|
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[]>([ users.value[ 1 ] as User ]);
|
|
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, searchQuery,
|
|
37
|
+
clear, isSelected, canSelectMore, selectedCount, valid, validationMessage,
|
|
38
|
+
cssAnchorName, popupStyle, triggerProps, inputProps, listboxProps, getOptionProps,
|
|
39
|
+
setContainerRef, setTriggerRef, setDropdownRef, setInputRef, setListRef, setOptionRef,
|
|
40
|
+
}"
|
|
41
|
+
v-model="selected"
|
|
42
|
+
:options="users"
|
|
43
|
+
:option-label="(u: User) => u.name"
|
|
44
|
+
multiple
|
|
45
|
+
required
|
|
46
|
+
:min-length="2"
|
|
47
|
+
:max-length="4"
|
|
48
|
+
>
|
|
49
|
+
<fieldset
|
|
50
|
+
:ref="setContainerRef"
|
|
51
|
+
class="fieldset"
|
|
52
|
+
>
|
|
53
|
+
<legend :id="labelId" class="fieldset-legend">Reviewers (multiple, 2 to 4 required)</legend>
|
|
54
|
+
<button
|
|
55
|
+
:ref="setTriggerRef"
|
|
56
|
+
v-bind="triggerProps"
|
|
57
|
+
:aria-labelledby="labelId"
|
|
58
|
+
type="button"
|
|
59
|
+
class="input flex w-full cursor-pointer items-center justify-between"
|
|
60
|
+
:class="valid ? '' : 'input-error'"
|
|
61
|
+
:style="{ anchorName: cssAnchorName }"
|
|
62
|
+
>
|
|
63
|
+
<span :class="{ 'text-base-content/70': selectedCount === 0 }">
|
|
64
|
+
{{ selectedCount ? `${ selectedCount } selected` : 'Select reviewers…' }}
|
|
65
|
+
</span>
|
|
66
|
+
<svg
|
|
67
|
+
class="size-4 opacity-70"
|
|
68
|
+
fill="none"
|
|
69
|
+
stroke="currentColor"
|
|
70
|
+
viewBox="0 0 24 24"
|
|
71
|
+
>
|
|
72
|
+
<path
|
|
73
|
+
stroke-linecap="round"
|
|
74
|
+
stroke-linejoin="round"
|
|
75
|
+
stroke-width="2"
|
|
76
|
+
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
|
77
|
+
/>
|
|
78
|
+
</svg>
|
|
79
|
+
</button>
|
|
80
|
+
<p
|
|
81
|
+
class="fieldset-label"
|
|
82
|
+
:class="valid ? 'text-success' : 'text-error'"
|
|
83
|
+
>
|
|
84
|
+
{{ valid ? 'Looks good.' : validationMessage }}
|
|
85
|
+
</p>
|
|
86
|
+
</fieldset>
|
|
87
|
+
|
|
88
|
+
<div
|
|
89
|
+
:ref="setDropdownRef"
|
|
90
|
+
popover="manual"
|
|
91
|
+
class="
|
|
92
|
+
cbx-popup rounded-box border border-base-content/5 bg-base-200
|
|
93
|
+
shadow-xl
|
|
94
|
+
"
|
|
95
|
+
:style="[popupStyle, {
|
|
96
|
+
left: 'calc(anchor(left) - 0.25rem)',
|
|
97
|
+
width: 'calc(anchor-size(width) + 0.5rem)',
|
|
98
|
+
}]"
|
|
99
|
+
>
|
|
100
|
+
<div class="flex items-center gap-2 border-b border-base-content/10 p-2">
|
|
101
|
+
<input
|
|
102
|
+
:ref="setInputRef"
|
|
103
|
+
v-bind="inputProps"
|
|
104
|
+
:value="searchQuery"
|
|
105
|
+
type="text"
|
|
106
|
+
class="input flex-1 input-sm"
|
|
107
|
+
placeholder="Search reviewers…"
|
|
108
|
+
aria-label="Search reviewers"
|
|
109
|
+
/>
|
|
110
|
+
<button
|
|
111
|
+
type="button"
|
|
112
|
+
class="btn btn-ghost btn-sm"
|
|
113
|
+
@click="clear"
|
|
114
|
+
>
|
|
115
|
+
Clear
|
|
116
|
+
</button>
|
|
117
|
+
</div>
|
|
118
|
+
|
|
119
|
+
<ul
|
|
120
|
+
:ref="setListRef"
|
|
121
|
+
v-bind="listboxProps"
|
|
122
|
+
class="menu max-h-60 w-full flex-nowrap gap-0.5 overflow-y-auto"
|
|
123
|
+
>
|
|
124
|
+
<li
|
|
125
|
+
v-for="(user, index) in filteredOptions"
|
|
126
|
+
:key="user.id"
|
|
127
|
+
>
|
|
128
|
+
<button
|
|
129
|
+
:ref="(el) => setOptionRef(user, el)"
|
|
130
|
+
type="button"
|
|
131
|
+
v-bind="getOptionProps(user, index)"
|
|
132
|
+
class="justify-between shadow-none"
|
|
133
|
+
:class="{
|
|
134
|
+
'menu-active': isSelected(user),
|
|
135
|
+
'menu-focus': index === highlightedIndex,
|
|
136
|
+
}"
|
|
137
|
+
:disabled="!canSelectMore && !isSelected(user)"
|
|
138
|
+
>
|
|
139
|
+
<span class="flex flex-col items-start gap-0.5">
|
|
140
|
+
<span>{{ user.name }}</span>
|
|
141
|
+
<span class="text-xs opacity-70">{{ user.role }}</span>
|
|
142
|
+
</span>
|
|
143
|
+
<svg
|
|
144
|
+
v-if="isSelected(user)"
|
|
145
|
+
class="size-4"
|
|
146
|
+
fill="none"
|
|
147
|
+
stroke="currentColor"
|
|
148
|
+
viewBox="0 0 24 24"
|
|
149
|
+
>
|
|
150
|
+
<path
|
|
151
|
+
stroke-linecap="round"
|
|
152
|
+
stroke-linejoin="round"
|
|
153
|
+
stroke-width="2"
|
|
154
|
+
d="M4.5 12.75l6 6 9-13.5"
|
|
155
|
+
/>
|
|
156
|
+
</svg>
|
|
157
|
+
</button>
|
|
158
|
+
</li>
|
|
159
|
+
</ul>
|
|
160
|
+
</div>
|
|
161
|
+
</HeadlessCombobox>
|
|
162
|
+
</div>
|
|
163
|
+
</template>
|
|
164
|
+
|
|
165
|
+
<style scoped>
|
|
166
|
+
/* Popup opens below the control: slide + fade via the Popover API. */
|
|
167
|
+
.cbx-popup {
|
|
168
|
+
inset: auto;
|
|
169
|
+
position-try: flip-block;
|
|
170
|
+
opacity: 0;
|
|
171
|
+
margin-block-start: 0;
|
|
172
|
+
transition:
|
|
173
|
+
opacity 0.15s ease,
|
|
174
|
+
margin-block-start 0.15s ease,
|
|
175
|
+
overlay 0.15s ease allow-discrete,
|
|
176
|
+
display 0.15s ease allow-discrete;
|
|
177
|
+
|
|
178
|
+
&:not(:popover-open) {
|
|
179
|
+
display: none;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
&:popover-open {
|
|
183
|
+
opacity: 1;
|
|
184
|
+
margin-block-start: 0.5rem;
|
|
185
|
+
|
|
186
|
+
@starting-style {
|
|
187
|
+
opacity: 0;
|
|
188
|
+
margin-block-start: 0;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
</style>
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Object options · single · with filter';
|
|
4
|
+
export const description = 'Object options with a searchable input.';
|
|
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[ 1 ] ?? 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, searchQuery,
|
|
37
|
+
clear, isSelected, cssAnchorName, popupStyle, triggerProps, inputProps,
|
|
38
|
+
listboxProps, getOptionProps, setContainerRef, setTriggerRef, setDropdownRef, setInputRef,
|
|
39
|
+
setListRef, setOptionRef,
|
|
40
|
+
}"
|
|
41
|
+
v-model="selected"
|
|
42
|
+
:options="users"
|
|
43
|
+
:option-label="(u: User) => u.name"
|
|
44
|
+
>
|
|
45
|
+
<fieldset
|
|
46
|
+
:ref="setContainerRef"
|
|
47
|
+
class="fieldset"
|
|
48
|
+
>
|
|
49
|
+
<legend :id="labelId" class="fieldset-legend">Assign user (single, with filter)</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 v-if="selected">{{ selected.name }}</span>
|
|
59
|
+
<span v-else class="text-base-content/70">Select a user…</span>
|
|
60
|
+
<svg
|
|
61
|
+
class="size-4 opacity-70"
|
|
62
|
+
fill="none"
|
|
63
|
+
stroke="currentColor"
|
|
64
|
+
viewBox="0 0 24 24"
|
|
65
|
+
>
|
|
66
|
+
<path
|
|
67
|
+
stroke-linecap="round"
|
|
68
|
+
stroke-linejoin="round"
|
|
69
|
+
stroke-width="2"
|
|
70
|
+
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
|
71
|
+
/>
|
|
72
|
+
</svg>
|
|
73
|
+
</button>
|
|
74
|
+
</fieldset>
|
|
75
|
+
|
|
76
|
+
<div
|
|
77
|
+
:ref="setDropdownRef"
|
|
78
|
+
popover="manual"
|
|
79
|
+
class="
|
|
80
|
+
cbx-popup rounded-box border border-base-content/5 bg-base-200
|
|
81
|
+
shadow-xl
|
|
82
|
+
"
|
|
83
|
+
:style="[popupStyle, {
|
|
84
|
+
left: 'calc(anchor(left) - 0.25rem)',
|
|
85
|
+
width: 'calc(anchor-size(width) + 0.5rem)',
|
|
86
|
+
}]"
|
|
87
|
+
>
|
|
88
|
+
<div class="flex items-center gap-2 border-b border-base-content/10 p-2">
|
|
89
|
+
<input
|
|
90
|
+
:ref="setInputRef"
|
|
91
|
+
v-bind="inputProps"
|
|
92
|
+
:value="searchQuery"
|
|
93
|
+
type="text"
|
|
94
|
+
class="input flex-1 input-sm"
|
|
95
|
+
placeholder="Search users…"
|
|
96
|
+
aria-label="Search users"
|
|
97
|
+
/>
|
|
98
|
+
<button
|
|
99
|
+
type="button"
|
|
100
|
+
class="btn btn-ghost btn-sm"
|
|
101
|
+
@click="clear"
|
|
102
|
+
>
|
|
103
|
+
Clear
|
|
104
|
+
</button>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
<ul
|
|
108
|
+
:ref="setListRef"
|
|
109
|
+
v-bind="listboxProps"
|
|
110
|
+
class="menu max-h-60 w-full flex-nowrap gap-0.5 overflow-y-auto"
|
|
111
|
+
>
|
|
112
|
+
<li
|
|
113
|
+
v-for="(user, index) in filteredOptions"
|
|
114
|
+
:key="user.id"
|
|
115
|
+
>
|
|
116
|
+
<button
|
|
117
|
+
:ref="(el) => setOptionRef(user, el)"
|
|
118
|
+
type="button"
|
|
119
|
+
v-bind="getOptionProps(user, index)"
|
|
120
|
+
class="justify-between shadow-none"
|
|
121
|
+
:class="{
|
|
122
|
+
'menu-active': isSelected(user),
|
|
123
|
+
'menu-focus': index === highlightedIndex,
|
|
124
|
+
}"
|
|
125
|
+
>
|
|
126
|
+
<span class="flex flex-col items-start gap-0.5">
|
|
127
|
+
<span>{{ user.name }}</span>
|
|
128
|
+
<span class="text-xs opacity-70">{{ user.role }}</span>
|
|
129
|
+
</span>
|
|
130
|
+
<svg
|
|
131
|
+
v-if="isSelected(user)"
|
|
132
|
+
class="size-4"
|
|
133
|
+
fill="none"
|
|
134
|
+
stroke="currentColor"
|
|
135
|
+
viewBox="0 0 24 24"
|
|
136
|
+
>
|
|
137
|
+
<path
|
|
138
|
+
stroke-linecap="round"
|
|
139
|
+
stroke-linejoin="round"
|
|
140
|
+
stroke-width="2"
|
|
141
|
+
d="M4.5 12.75l6 6 9-13.5"
|
|
142
|
+
/>
|
|
143
|
+
</svg>
|
|
144
|
+
</button>
|
|
145
|
+
</li>
|
|
146
|
+
|
|
147
|
+
<li
|
|
148
|
+
v-if="filteredOptions.length === 0"
|
|
149
|
+
class="
|
|
150
|
+
pointer-events-none p-4 text-center text-sm text-base-content/70
|
|
151
|
+
"
|
|
152
|
+
role="presentation"
|
|
153
|
+
>
|
|
154
|
+
No users found.
|
|
155
|
+
</li>
|
|
156
|
+
</ul>
|
|
157
|
+
</div>
|
|
158
|
+
</HeadlessCombobox>
|
|
159
|
+
</div>
|
|
160
|
+
</template>
|
|
161
|
+
|
|
162
|
+
<style scoped>
|
|
163
|
+
/* Popup opens below the control: slide + fade via the Popover API. */
|
|
164
|
+
.cbx-popup {
|
|
165
|
+
inset: auto;
|
|
166
|
+
position-try: flip-block;
|
|
167
|
+
opacity: 0;
|
|
168
|
+
margin-block-start: 0;
|
|
169
|
+
transition:
|
|
170
|
+
opacity 0.15s ease,
|
|
171
|
+
margin-block-start 0.15s ease,
|
|
172
|
+
overlay 0.15s ease allow-discrete,
|
|
173
|
+
display 0.15s ease allow-discrete;
|
|
174
|
+
|
|
175
|
+
&:not(:popover-open) {
|
|
176
|
+
display: none;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
&:popover-open {
|
|
180
|
+
opacity: 1;
|
|
181
|
+
margin-block-start: 0.5rem;
|
|
182
|
+
|
|
183
|
+
@starting-style {
|
|
184
|
+
opacity: 0;
|
|
185
|
+
margin-block-start: 0;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
</style>
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Object options · single · separate value';
|
|
4
|
+
export const description = 'Options carry a label and a value; the model holds the value only.';
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { HeadlessCombobox } from '@pdanpdan/headless-combobox-vue';
|
|
9
|
+
import { computed, ref, useId } from 'vue';
|
|
10
|
+
|
|
11
|
+
interface Framework {
|
|
12
|
+
label: string;
|
|
13
|
+
value: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const frameworks: Framework[] = [
|
|
17
|
+
{ label: 'Vue', value: 1 },
|
|
18
|
+
{ label: 'React', value: 2 },
|
|
19
|
+
{ label: 'Svelte', value: 3 },
|
|
20
|
+
{ label: 'Solid', value: 4 },
|
|
21
|
+
{ label: 'Angular', value: 5 },
|
|
22
|
+
{ label: 'Preact', value: 6 },
|
|
23
|
+
{ label: 'Lit', value: 8 },
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
const selected = ref<number | null>(3);
|
|
27
|
+
const labelId = useId();
|
|
28
|
+
|
|
29
|
+
const selectedLabel = computed(
|
|
30
|
+
() => frameworks.find((framework) => framework.value === selected.value)?.label ?? 'Select a framework…',
|
|
31
|
+
);
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<template>
|
|
35
|
+
<div class="w-full max-w-sm">
|
|
36
|
+
<HeadlessCombobox
|
|
37
|
+
v-slot="{
|
|
38
|
+
filteredOptions, highlightedIndex, isSelected,
|
|
39
|
+
cssAnchorName, popupStyle, triggerProps, listboxProps,
|
|
40
|
+
getOptionProps, setContainerRef, setTriggerRef, setDropdownRef, setListRef, setOptionRef,
|
|
41
|
+
}"
|
|
42
|
+
v-model="selected"
|
|
43
|
+
:options="frameworks"
|
|
44
|
+
:option-label="(framework: Framework) => framework.label"
|
|
45
|
+
:option-value="(framework: Framework) => framework.value"
|
|
46
|
+
>
|
|
47
|
+
<fieldset
|
|
48
|
+
:ref="setContainerRef"
|
|
49
|
+
class="fieldset"
|
|
50
|
+
>
|
|
51
|
+
<legend :id="labelId" class="fieldset-legend">Framework id (single, separate value)</legend>
|
|
52
|
+
<button
|
|
53
|
+
:ref="setTriggerRef"
|
|
54
|
+
v-bind="triggerProps"
|
|
55
|
+
:aria-labelledby="labelId"
|
|
56
|
+
type="button"
|
|
57
|
+
class="input flex w-full cursor-pointer items-center justify-between"
|
|
58
|
+
:style="{ anchorName: cssAnchorName }"
|
|
59
|
+
>
|
|
60
|
+
<span :class="{ 'text-base-content/70': selected == null }">{{ selectedLabel }}</span>
|
|
61
|
+
<svg
|
|
62
|
+
class="size-4 opacity-70"
|
|
63
|
+
fill="none"
|
|
64
|
+
stroke="currentColor"
|
|
65
|
+
viewBox="0 0 24 24"
|
|
66
|
+
>
|
|
67
|
+
<path
|
|
68
|
+
stroke-linecap="round"
|
|
69
|
+
stroke-linejoin="round"
|
|
70
|
+
stroke-width="2"
|
|
71
|
+
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
|
72
|
+
/>
|
|
73
|
+
</svg>
|
|
74
|
+
</button>
|
|
75
|
+
</fieldset>
|
|
76
|
+
|
|
77
|
+
<ul
|
|
78
|
+
:ref="(el) => { setDropdownRef(el); setListRef(el); }"
|
|
79
|
+
v-bind="listboxProps"
|
|
80
|
+
popover="manual"
|
|
81
|
+
class="
|
|
82
|
+
cbx-popup menu max-h-60 flex-nowrap gap-0.5 rounded-box border
|
|
83
|
+
border-base-content/5 bg-base-200 shadow-xl
|
|
84
|
+
"
|
|
85
|
+
:style="[popupStyle, {
|
|
86
|
+
left: 'calc(anchor(left) - 0.25rem)',
|
|
87
|
+
width: 'calc(anchor-size(width) + 0.5rem)',
|
|
88
|
+
}]"
|
|
89
|
+
>
|
|
90
|
+
<li
|
|
91
|
+
v-for="(framework, index) in filteredOptions"
|
|
92
|
+
:key="framework.value"
|
|
93
|
+
>
|
|
94
|
+
<button
|
|
95
|
+
:ref="(el) => setOptionRef(framework, el)"
|
|
96
|
+
type="button"
|
|
97
|
+
v-bind="getOptionProps(framework, index)"
|
|
98
|
+
class="justify-between shadow-none"
|
|
99
|
+
:class="{
|
|
100
|
+
'menu-active': isSelected(framework),
|
|
101
|
+
'menu-focus': index === highlightedIndex,
|
|
102
|
+
}"
|
|
103
|
+
>
|
|
104
|
+
<span>{{ framework.label }}</span>
|
|
105
|
+
<span
|
|
106
|
+
class="text-xs"
|
|
107
|
+
:class="isSelected(framework) || index === highlightedIndex ? `
|
|
108
|
+
text-current
|
|
109
|
+
` : `text-base-content/70`"
|
|
110
|
+
>{{ framework.value }}</span>
|
|
111
|
+
</button>
|
|
112
|
+
</li>
|
|
113
|
+
</ul>
|
|
114
|
+
</HeadlessCombobox>
|
|
115
|
+
</div>
|
|
116
|
+
</template>
|
|
117
|
+
|
|
118
|
+
<style scoped>
|
|
119
|
+
/* Popup opens below the control: slide + fade via the Popover API. */
|
|
120
|
+
.cbx-popup {
|
|
121
|
+
inset: auto;
|
|
122
|
+
position-try: flip-block;
|
|
123
|
+
opacity: 0;
|
|
124
|
+
margin-block-start: 0;
|
|
125
|
+
|
|
126
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
127
|
+
transition:
|
|
128
|
+
opacity 0.15s ease,
|
|
129
|
+
margin-block-start 0.15s ease,
|
|
130
|
+
overlay 0.15s ease allow-discrete,
|
|
131
|
+
display 0.15s ease allow-discrete;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
&:not(:popover-open) {
|
|
135
|
+
display: none;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
&:popover-open {
|
|
139
|
+
opacity: 1;
|
|
140
|
+
margin-block-start: 0.5rem;
|
|
141
|
+
|
|
142
|
+
@starting-style {
|
|
143
|
+
opacity: 0;
|
|
144
|
+
margin-block-start: 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
</style>
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/* eslint-disable import/first -- the exports below precede the setup imports in the compiled module */
|
|
3
|
+
export const title = 'Text options · single · internal state';
|
|
4
|
+
export const description = 'An initial value seeds the internal selection; without a change handler the component keeps it internally.';
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { HeadlessCombobox } from '@pdanpdan/headless-combobox-vue';
|
|
9
|
+
import { useId } from 'vue';
|
|
10
|
+
|
|
11
|
+
const frameworks = [
|
|
12
|
+
'Vue',
|
|
13
|
+
'React',
|
|
14
|
+
'Svelte',
|
|
15
|
+
'Solid',
|
|
16
|
+
'Angular',
|
|
17
|
+
'Preact',
|
|
18
|
+
'Lit',
|
|
19
|
+
'Alpine',
|
|
20
|
+
'Ember',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const labelId = useId();
|
|
24
|
+
</script>
|
|
25
|
+
|
|
26
|
+
<template>
|
|
27
|
+
<div class="w-full max-w-sm">
|
|
28
|
+
<HeadlessCombobox
|
|
29
|
+
v-slot="{
|
|
30
|
+
filteredOptions, highlightedIndex, isSelected, selectedList,
|
|
31
|
+
cssAnchorName, popupStyle, triggerProps, listboxProps,
|
|
32
|
+
getOptionProps, setContainerRef, setTriggerRef, setDropdownRef, setListRef, setOptionRef,
|
|
33
|
+
}"
|
|
34
|
+
:options="frameworks"
|
|
35
|
+
model-value="Vue"
|
|
36
|
+
>
|
|
37
|
+
<fieldset
|
|
38
|
+
:ref="setContainerRef"
|
|
39
|
+
class="fieldset"
|
|
40
|
+
>
|
|
41
|
+
<legend :id="labelId" class="fieldset-legend">Framework (single, no v-model)</legend>
|
|
42
|
+
<button
|
|
43
|
+
:ref="setTriggerRef"
|
|
44
|
+
v-bind="triggerProps"
|
|
45
|
+
:aria-labelledby="labelId"
|
|
46
|
+
type="button"
|
|
47
|
+
class="input flex w-full cursor-pointer items-center justify-between"
|
|
48
|
+
:style="{ anchorName: cssAnchorName }"
|
|
49
|
+
>
|
|
50
|
+
<span :class="{ 'text-base-content/70': selectedList.length === 0 }">
|
|
51
|
+
{{ selectedList[ 0 ] ?? 'Select a framework…' }}
|
|
52
|
+
</span>
|
|
53
|
+
<svg
|
|
54
|
+
class="size-4 opacity-70"
|
|
55
|
+
fill="none"
|
|
56
|
+
stroke="currentColor"
|
|
57
|
+
viewBox="0 0 24 24"
|
|
58
|
+
>
|
|
59
|
+
<path
|
|
60
|
+
stroke-linecap="round"
|
|
61
|
+
stroke-linejoin="round"
|
|
62
|
+
stroke-width="2"
|
|
63
|
+
d="M8 9l4-4 4 4m0 6l-4 4-4-4"
|
|
64
|
+
/>
|
|
65
|
+
</svg>
|
|
66
|
+
</button>
|
|
67
|
+
</fieldset>
|
|
68
|
+
|
|
69
|
+
<ul
|
|
70
|
+
:ref="(el) => { setDropdownRef(el); setListRef(el); }"
|
|
71
|
+
v-bind="listboxProps"
|
|
72
|
+
popover="manual"
|
|
73
|
+
class="
|
|
74
|
+
cbx-popup menu max-h-60 flex-nowrap gap-0.5 rounded-box border
|
|
75
|
+
border-base-content/5 bg-base-200 shadow-xl
|
|
76
|
+
"
|
|
77
|
+
:style="[popupStyle, {
|
|
78
|
+
left: 'calc(anchor(left) - 0.25rem)',
|
|
79
|
+
width: 'calc(anchor-size(width) + 0.5rem)',
|
|
80
|
+
}]"
|
|
81
|
+
>
|
|
82
|
+
<li
|
|
83
|
+
v-for="(framework, index) in filteredOptions"
|
|
84
|
+
:key="framework"
|
|
85
|
+
>
|
|
86
|
+
<button
|
|
87
|
+
:ref="(el) => setOptionRef(framework, el)"
|
|
88
|
+
type="button"
|
|
89
|
+
v-bind="getOptionProps(framework, index)"
|
|
90
|
+
class="justify-between shadow-none"
|
|
91
|
+
:class="{
|
|
92
|
+
'menu-active': isSelected(framework),
|
|
93
|
+
'menu-focus': index === highlightedIndex,
|
|
94
|
+
}"
|
|
95
|
+
>
|
|
96
|
+
<span>{{ framework }}</span>
|
|
97
|
+
<svg
|
|
98
|
+
v-if="isSelected(framework)"
|
|
99
|
+
class="size-4"
|
|
100
|
+
fill="none"
|
|
101
|
+
stroke="currentColor"
|
|
102
|
+
viewBox="0 0 24 24"
|
|
103
|
+
>
|
|
104
|
+
<path
|
|
105
|
+
stroke-linecap="round"
|
|
106
|
+
stroke-linejoin="round"
|
|
107
|
+
stroke-width="2"
|
|
108
|
+
d="M4.5 12.75l6 6 9-13.5"
|
|
109
|
+
/>
|
|
110
|
+
</svg>
|
|
111
|
+
</button>
|
|
112
|
+
</li>
|
|
113
|
+
</ul>
|
|
114
|
+
</HeadlessCombobox>
|
|
115
|
+
</div>
|
|
116
|
+
</template>
|
|
117
|
+
|
|
118
|
+
<style scoped>
|
|
119
|
+
/* Popup opens below the control: slide + fade via the Popover API. */
|
|
120
|
+
.cbx-popup {
|
|
121
|
+
inset: auto;
|
|
122
|
+
position-try: flip-block;
|
|
123
|
+
opacity: 0;
|
|
124
|
+
margin-block-start: 0;
|
|
125
|
+
|
|
126
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
127
|
+
transition:
|
|
128
|
+
opacity 0.15s ease,
|
|
129
|
+
margin-block-start 0.15s ease,
|
|
130
|
+
overlay 0.15s ease allow-discrete,
|
|
131
|
+
display 0.15s ease allow-discrete;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
&:not(:popover-open) {
|
|
135
|
+
display: none;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
&:popover-open {
|
|
139
|
+
opacity: 1;
|
|
140
|
+
margin-block-start: 0.5rem;
|
|
141
|
+
|
|
142
|
+
@starting-style {
|
|
143
|
+
opacity: 0;
|
|
144
|
+
margin-block-start: 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
</style>
|