@dumiflex/wp-kit 0.3.0 → 0.4.0
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/CHANGELOG.md +77 -51
- package/LICENSE +674 -674
- package/README.md +73 -76
- package/package.json +78 -77
- package/src/Avatar.vue +83 -83
- package/src/Badge.vue +97 -97
- package/src/Banner.vue +118 -118
- package/src/BookmarkButton.vue +27 -27
- package/src/BundleMeta.vue +51 -51
- package/src/Button.vue +103 -103
- package/src/Card.vue +68 -68
- package/src/Checkbox.vue +109 -107
- package/src/Chip.vue +163 -97
- package/src/ChoiceCards.vue +133 -133
- package/src/CloseButton.vue +69 -69
- package/src/CodeBlock.vue +119 -119
- package/src/ColorField.vue +188 -206
- package/src/ColorPicker.vue +201 -201
- package/src/Combobox.vue +331 -334
- package/src/ConfirmDialog.vue +165 -163
- package/src/ConfirmModal.vue +147 -147
- package/src/ConstraintMatrixPreview.vue +708 -708
- package/src/ContextMenu.vue +386 -379
- package/src/CopyButton.vue +156 -156
- package/src/DataTable.vue +292 -292
- package/src/DatePicker.vue +234 -234
- package/src/DescriptionList.vue +73 -73
- package/src/DiffView.vue +68 -68
- package/src/Disclosure.vue +166 -166
- package/src/DragHandle.vue +49 -49
- package/src/Drawer.vue +131 -131
- package/src/DropZone.vue +196 -196
- package/src/DropdownMenu.vue +72 -72
- package/src/EmptyState.vue +111 -111
- package/src/Field.vue +28 -28
- package/src/HsvPicker.vue +208 -208
- package/src/Icon.vue +39 -39
- package/src/IconPicker.vue +209 -209
- package/src/ImageGrid.vue +125 -125
- package/src/InlineEdit.vue +183 -183
- package/src/Input.vue +99 -99
- package/src/Kbd.vue +56 -56
- package/src/Lightbox.vue +173 -173
- package/src/ListFilter.vue +85 -85
- package/src/ListRow.vue +204 -204
- package/src/LoadingMark.vue +175 -161
- package/src/Logo.vue +60 -60
- package/src/Modal.vue +78 -76
- package/src/ModalShell.vue +109 -107
- package/src/Pagination.vue +106 -106
- package/src/Popover.vue +180 -171
- package/src/PostCard.vue +210 -211
- package/src/ProgressBar.vue +90 -90
- package/src/RadioGroup.vue +177 -177
- package/src/RangeSlider.vue +183 -183
- package/src/RelativeDate.vue +35 -35
- package/src/ResizeHandle.vue +143 -143
- package/src/SearchInput.vue +192 -199
- package/src/Select.vue +644 -641
- package/src/Separator.vue +54 -54
- package/src/Skeleton.vue +82 -82
- package/src/Slider.vue +198 -198
- package/src/SortableList.vue +260 -260
- package/src/SplitPane.vue +68 -68
- package/src/StarButton.vue +40 -40
- package/src/Stepper.vue +141 -141
- package/src/SupportLinks.vue +93 -93
- package/src/TagInput.vue +297 -300
- package/src/Textarea.vue +57 -57
- package/src/Timeline.vue +92 -92
- package/src/ToastCard.vue +119 -119
- package/src/ToastHost.vue +40 -40
- package/src/Toggle.vue +36 -36
- package/src/TokenizedValue.vue +310 -310
- package/src/Toolbar.vue +110 -110
- package/src/VirtualList.vue +87 -87
- package/src/VisuallyHidden.vue +29 -29
- package/src/WpButton.vue +75 -75
- package/src/WpCheck.vue +104 -102
- package/src/WpIconButton.vue +107 -107
- package/src/WpImageZoom.vue +544 -544
- package/src/WpNumber.vue +178 -183
- package/src/WpSegmented.vue +175 -175
- package/src/WpSelect.vue +93 -89
- package/src/WpSwitch.vue +163 -163
- package/src/WpTabs.vue +188 -188
- package/src/_modal-motion.css +33 -33
- package/src/_skeleton.css +50 -50
- package/src/axis-color.ts +42 -42
- package/src/button.css +169 -169
- package/src/calendar.ts +59 -59
- package/src/clipboard.ts +50 -50
- package/src/colour-presets.ts +12 -12
- package/src/diff.ts +51 -51
- package/src/drop-zone.ts +41 -41
- package/src/field.css +50 -0
- package/src/flip.ts +237 -237
- package/src/floating-layer.ts +48 -0
- package/src/floating.ts +99 -99
- package/src/hsv.ts +100 -100
- package/src/icon-catalog.ts +52 -52
- package/src/image-zoom.ts +93 -93
- package/src/kind-colour.ts +28 -0
- package/src/logo-paths.ts +21 -21
- package/src/matrix-axis.ts +91 -91
- package/src/pagination.ts +27 -27
- package/src/platform-keys.ts +29 -29
- package/src/rich-tokenize.ts +357 -357
- package/src/select-types.ts +32 -32
- package/src/slider-math.ts +45 -45
- package/src/sortable.ts +22 -22
- package/src/theme/fonts/OFL-Fraunces.txt +93 -93
- package/src/theme/fonts/OFL-JetBrainsMono.txt +93 -93
- package/src/theme/fonts/OFL-Manrope.txt +93 -93
- package/src/theme/looks.css +428 -428
- package/src/theme/palettes.css +254 -254
- package/src/theme/tokens.css +2896 -2894
- package/src/tokens.ts +56 -56
- package/src/tooltip.css +43 -43
- package/src/tooltip.ts +146 -146
- package/src/useDialogFocus.ts +73 -73
- package/src/useGrowableField.ts +357 -357
- package/src/useModalKeyShield.ts +116 -116
- package/src/useToast.ts +48 -48
- package/src/wp-logo.svg +14 -14
- package/src/ChipToggle.vue +0 -92
- package/src/KindChip.vue +0 -53
- package/src/RatingBadge.vue +0 -42
package/src/Combobox.vue
CHANGED
|
@@ -1,334 +1,331 @@
|
|
|
1
|
-
<script setup lang="ts" generic="T extends string | number">
|
|
2
|
-
/**
|
|
3
|
-
* A select you can type in: the list filters as you type, the arrows light
|
|
4
|
-
* an option and Enter takes it. With `multiple` the picks sit as chips in
|
|
5
|
-
* the box, the list stays open and marks what is picked, and Backspace in
|
|
6
|
-
* the empty box takes the last pick off. The list floats in a kit Popover,
|
|
7
|
-
* so it follows the box on scroll. Use WpSelect for a short fixed list and
|
|
8
|
-
* TagInput when people may type their own words.
|
|
9
|
-
*
|
|
10
|
-
* <Combobox v-model="model" :options="models" aria-label="Model" />
|
|
11
|
-
* <Combobox v-model="cats" multiple :options="categories" aria-label="Categories" />
|
|
12
|
-
*
|
|
13
|
-
* APG "combobox with list autocomplete": focus stays in the text field and
|
|
14
|
-
* the lit option is named by aria-activedescendant.
|
|
15
|
-
*/
|
|
16
|
-
import { computed, nextTick, ref, useId, watch } from "vue";
|
|
17
|
-
import Popover from "./Popover.vue";
|
|
18
|
-
|
|
19
|
-
export interface ComboOption<V> { value: V; label: string; hint?: string; disabled?: boolean }
|
|
20
|
-
|
|
21
|
-
const props = withDefaults(defineProps<{
|
|
22
|
-
modelValue: T | null | T[];
|
|
23
|
-
options: ComboOption<T>[];
|
|
24
|
-
multiple?: boolean | undefined;
|
|
25
|
-
placeholder?: string | undefined;
|
|
26
|
-
size?: "sm" | "md" | "lg" | undefined;
|
|
27
|
-
disabled?: boolean | undefined;
|
|
28
|
-
invalid?: boolean | undefined;
|
|
29
|
-
ariaLabel?: string | undefined;
|
|
30
|
-
/** Show a ✕ that clears a single pick. */
|
|
31
|
-
clearable?: boolean | undefined;
|
|
32
|
-
/** What the list says when nothing matches. */
|
|
33
|
-
emptyText?: string | undefined;
|
|
34
|
-
/** How many options the list shows at once (it scrolls past that). */
|
|
35
|
-
limit?: number | undefined;
|
|
36
|
-
}>(), {
|
|
37
|
-
multiple: false, placeholder: "Choose…", size: "md", ariaLabel: "Choose",
|
|
38
|
-
clearable: false, emptyText: "No matches", limit: 50,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const emit = defineEmits<{ (e: "update:modelValue", v: T | null | T[]): void }>();
|
|
42
|
-
|
|
43
|
-
const uid = useId();
|
|
44
|
-
const listId = `wp-combo-list-${uid}`;
|
|
45
|
-
const inputEl = ref<HTMLInputElement | null>(null);
|
|
46
|
-
const query = ref("");
|
|
47
|
-
const typing = ref(false);
|
|
48
|
-
const wanted = ref(false);
|
|
49
|
-
const active = ref(-1);
|
|
50
|
-
const said = ref("");
|
|
51
|
-
|
|
52
|
-
const picks = computed<T[]>(() => {
|
|
53
|
-
const v = props.modelValue;
|
|
54
|
-
if (Array.isArray(v)) return v;
|
|
55
|
-
return v === null || v === undefined ? [] : [v];
|
|
56
|
-
});
|
|
57
|
-
const isPicked = (v: T): boolean => picks.value.includes(v);
|
|
58
|
-
const labelOf = (v: T): string => props.options.find((o) => o.value === v)?.label ?? String(v);
|
|
59
|
-
const single = computed(() => (!props.multiple && picks.value.length ? labelOf(picks.value[0]) : ""));
|
|
60
|
-
|
|
61
|
-
const matches = computed(() => {
|
|
62
|
-
const q = typing.value ? query.value.trim().toLowerCase() : "";
|
|
63
|
-
const hit = q ? props.options.filter((o) => o.label.toLowerCase().includes(q)) : props.options;
|
|
64
|
-
return [...hit.filter((o) => o.label.toLowerCase().startsWith(q)), ...hit.filter((o) => !o.label.toLowerCase().startsWith(q))]
|
|
65
|
-
.slice(0, props.limit);
|
|
66
|
-
});
|
|
67
|
-
const open = computed(() => wanted.value && !props.disabled);
|
|
68
|
-
const activeId = computed(() => (open.value && active.value >= 0 ? `${listId}-${active.value}` : undefined));
|
|
69
|
-
|
|
70
|
-
// The text field shows the single pick's label while you are not typing.
|
|
71
|
-
watch(single, (l) => { if (!typing.value) query.value = l; }, { immediate: true });
|
|
72
|
-
|
|
73
|
-
function announce(text: string): void {
|
|
74
|
-
said.value = "";
|
|
75
|
-
void nextTick(() => { said.value = text; });
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function choose(o: ComboOption<T>): void {
|
|
79
|
-
if (o.disabled) return;
|
|
80
|
-
if (props.multiple) {
|
|
81
|
-
const on = isPicked(o.value);
|
|
82
|
-
emit("update:modelValue", on ? picks.value.filter((v) => v !== o.value) : [...picks.value, o.value]);
|
|
83
|
-
announce(`${on ? "Removed" : "Added"} ${o.label}`);
|
|
84
|
-
query.value = "";
|
|
85
|
-
typing.value = false;
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
emit("update:modelValue", o.value);
|
|
89
|
-
typing.value = false;
|
|
90
|
-
query.value = o.label;
|
|
91
|
-
wanted.value = false;
|
|
92
|
-
active.value = -1;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function removeAt(i: number, refocus = false): void {
|
|
96
|
-
const v = picks.value[i];
|
|
97
|
-
if (v === undefined || props.disabled) return;
|
|
98
|
-
emit("update:modelValue", picks.value.filter((_, j) => j !== i));
|
|
99
|
-
announce(`Removed ${labelOf(v)}`);
|
|
100
|
-
if (refocus) inputEl.value?.focus();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function clear(): void {
|
|
104
|
-
emit("update:modelValue", props.multiple ? [] : null);
|
|
105
|
-
query.value = "";
|
|
106
|
-
typing.value = false;
|
|
107
|
-
announce("Cleared");
|
|
108
|
-
inputEl.value?.focus();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function step(dir: 1 | -1): void {
|
|
112
|
-
const n = matches.value.length;
|
|
113
|
-
if (!n) return;
|
|
114
|
-
let i = active.value;
|
|
115
|
-
for (let k = 0; k < n; k++) {
|
|
116
|
-
i = i < 0 ? (dir > 0 ? 0 : n - 1) : (i + dir + n) % n;
|
|
117
|
-
if (!matches.value[i].disabled) break;
|
|
118
|
-
}
|
|
119
|
-
active.value = i;
|
|
120
|
-
void nextTick(() => document.getElementById(`${listId}-${i}`)?.scrollIntoView?.({ block: "nearest" }));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function onKeydown(ev: KeyboardEvent): void {
|
|
124
|
-
if (props.disabled) return;
|
|
125
|
-
switch (ev.key) {
|
|
126
|
-
case "ArrowDown":
|
|
127
|
-
case "ArrowUp":
|
|
128
|
-
ev.preventDefault();
|
|
129
|
-
if (!wanted.value) { wanted.value = true; if (ev.altKey) return; }
|
|
130
|
-
step(ev.key === "ArrowDown" ? 1 : -1);
|
|
131
|
-
return;
|
|
132
|
-
case "Enter": {
|
|
133
|
-
const o = open.value && active.value >= 0 ? matches.value[active.value] : undefined;
|
|
134
|
-
if (!o) return;
|
|
135
|
-
ev.preventDefault();
|
|
136
|
-
choose(o);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
case "Escape":
|
|
140
|
-
if (typing.value && !props.multiple) { typing.value = false; query.value = single.value; }
|
|
141
|
-
return;
|
|
142
|
-
case "Backspace":
|
|
143
|
-
if (props.multiple && query.value === "" && picks.value.length) {
|
|
144
|
-
ev.preventDefault();
|
|
145
|
-
removeAt(picks.value.length - 1);
|
|
146
|
-
}
|
|
147
|
-
return;
|
|
148
|
-
default:
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function onInput(): void {
|
|
153
|
-
typing.value = true;
|
|
154
|
-
wanted.value = true;
|
|
155
|
-
active.value = matches.value.findIndex((o) => !o.disabled);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function onBlur(): void {
|
|
159
|
-
wanted.value = false;
|
|
160
|
-
active.value = -1;
|
|
161
|
-
if (!props.multiple) { typing.value = false; query.value = single.value; }
|
|
162
|
-
else query.value = "";
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function pick(o: ComboOption<T>): void {
|
|
166
|
-
choose(o);
|
|
167
|
-
inputEl.value?.focus();
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function setOpen(v: boolean): void {
|
|
171
|
-
if (!v) { wanted.value = false; active.value = -1; }
|
|
172
|
-
}
|
|
173
|
-
</script>
|
|
174
|
-
|
|
175
|
-
<!-- Popover's autofocus prop is off here: focus stays in the box you type in. -->
|
|
176
|
-
<!-- eslint-disable vuejs-accessibility/no-autofocus -->
|
|
177
|
-
<template>
|
|
178
|
-
<Popover :open="open" role="group" match-width :autofocus="false" :label="`${ariaLabel} choices`" @update:open="setOpen">
|
|
179
|
-
<template #trigger>
|
|
180
|
-
<!-- A click on the box's padding focuses the text field, which has the keys. -->
|
|
181
|
-
<!-- eslint-disable-next-line vuejs-accessibility/click-events-have-key-events, vuejs-accessibility/no-static-element-interactions -->
|
|
182
|
-
<div
|
|
183
|
-
class="wp-combo"
|
|
184
|
-
:
|
|
185
|
-
:data-
|
|
186
|
-
:data-
|
|
187
|
-
:data-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
<
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
:
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
:aria-
|
|
211
|
-
:aria-
|
|
212
|
-
:aria-
|
|
213
|
-
:aria-
|
|
214
|
-
:
|
|
215
|
-
:
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
@
|
|
219
|
-
@
|
|
220
|
-
@
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
@
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
<
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
:aria-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
<!--
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
:
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
:aria-
|
|
251
|
-
:
|
|
252
|
-
|
|
253
|
-
@
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
<
|
|
257
|
-
|
|
258
|
-
<span
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
</
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
.
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
.wp-combo[data-size="
|
|
279
|
-
.wp-combo
|
|
280
|
-
.wp-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
.wp-
|
|
296
|
-
.wp-combo__remove
|
|
297
|
-
.wp-combo__remove
|
|
298
|
-
.wp-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
323
|
-
.wp-
|
|
324
|
-
.wp-
|
|
325
|
-
.wp-combo__opt
|
|
326
|
-
.wp-
|
|
327
|
-
.wp-
|
|
328
|
-
|
|
329
|
-
.wp-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
.wp-combo { transition: none; }
|
|
333
|
-
}
|
|
334
|
-
</style>
|
|
1
|
+
<script setup lang="ts" generic="T extends string | number">
|
|
2
|
+
/**
|
|
3
|
+
* A select you can type in: the list filters as you type, the arrows light
|
|
4
|
+
* an option and Enter takes it. With `multiple` the picks sit as chips in
|
|
5
|
+
* the box, the list stays open and marks what is picked, and Backspace in
|
|
6
|
+
* the empty box takes the last pick off. The list floats in a kit Popover,
|
|
7
|
+
* so it follows the box on scroll. Use WpSelect for a short fixed list and
|
|
8
|
+
* TagInput when people may type their own words.
|
|
9
|
+
*
|
|
10
|
+
* <Combobox v-model="model" :options="models" aria-label="Model" />
|
|
11
|
+
* <Combobox v-model="cats" multiple :options="categories" aria-label="Categories" />
|
|
12
|
+
*
|
|
13
|
+
* APG "combobox with list autocomplete": focus stays in the text field and
|
|
14
|
+
* the lit option is named by aria-activedescendant.
|
|
15
|
+
*/
|
|
16
|
+
import { computed, nextTick, ref, useId, watch } from "vue";
|
|
17
|
+
import Popover from "./Popover.vue";
|
|
18
|
+
|
|
19
|
+
export interface ComboOption<V> { value: V; label: string; hint?: string; disabled?: boolean }
|
|
20
|
+
|
|
21
|
+
const props = withDefaults(defineProps<{
|
|
22
|
+
modelValue: T | null | T[];
|
|
23
|
+
options: ComboOption<T>[];
|
|
24
|
+
multiple?: boolean | undefined;
|
|
25
|
+
placeholder?: string | undefined;
|
|
26
|
+
size?: "sm" | "md" | "lg" | undefined;
|
|
27
|
+
disabled?: boolean | undefined;
|
|
28
|
+
invalid?: boolean | undefined;
|
|
29
|
+
ariaLabel?: string | undefined;
|
|
30
|
+
/** Show a ✕ that clears a single pick. */
|
|
31
|
+
clearable?: boolean | undefined;
|
|
32
|
+
/** What the list says when nothing matches. */
|
|
33
|
+
emptyText?: string | undefined;
|
|
34
|
+
/** How many options the list shows at once (it scrolls past that). */
|
|
35
|
+
limit?: number | undefined;
|
|
36
|
+
}>(), {
|
|
37
|
+
multiple: false, placeholder: "Choose…", size: "md", ariaLabel: "Choose",
|
|
38
|
+
clearable: false, emptyText: "No matches", limit: 50,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const emit = defineEmits<{ (e: "update:modelValue", v: T | null | T[]): void }>();
|
|
42
|
+
|
|
43
|
+
const uid = useId();
|
|
44
|
+
const listId = `wp-combo-list-${uid}`;
|
|
45
|
+
const inputEl = ref<HTMLInputElement | null>(null);
|
|
46
|
+
const query = ref("");
|
|
47
|
+
const typing = ref(false);
|
|
48
|
+
const wanted = ref(false);
|
|
49
|
+
const active = ref(-1);
|
|
50
|
+
const said = ref("");
|
|
51
|
+
|
|
52
|
+
const picks = computed<T[]>(() => {
|
|
53
|
+
const v = props.modelValue;
|
|
54
|
+
if (Array.isArray(v)) return v;
|
|
55
|
+
return v === null || v === undefined ? [] : [v];
|
|
56
|
+
});
|
|
57
|
+
const isPicked = (v: T): boolean => picks.value.includes(v);
|
|
58
|
+
const labelOf = (v: T): string => props.options.find((o) => o.value === v)?.label ?? String(v);
|
|
59
|
+
const single = computed(() => (!props.multiple && picks.value.length ? labelOf(picks.value[0]) : ""));
|
|
60
|
+
|
|
61
|
+
const matches = computed(() => {
|
|
62
|
+
const q = typing.value ? query.value.trim().toLowerCase() : "";
|
|
63
|
+
const hit = q ? props.options.filter((o) => o.label.toLowerCase().includes(q)) : props.options;
|
|
64
|
+
return [...hit.filter((o) => o.label.toLowerCase().startsWith(q)), ...hit.filter((o) => !o.label.toLowerCase().startsWith(q))]
|
|
65
|
+
.slice(0, props.limit);
|
|
66
|
+
});
|
|
67
|
+
const open = computed(() => wanted.value && !props.disabled);
|
|
68
|
+
const activeId = computed(() => (open.value && active.value >= 0 ? `${listId}-${active.value}` : undefined));
|
|
69
|
+
|
|
70
|
+
// The text field shows the single pick's label while you are not typing.
|
|
71
|
+
watch(single, (l) => { if (!typing.value) query.value = l; }, { immediate: true });
|
|
72
|
+
|
|
73
|
+
function announce(text: string): void {
|
|
74
|
+
said.value = "";
|
|
75
|
+
void nextTick(() => { said.value = text; });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function choose(o: ComboOption<T>): void {
|
|
79
|
+
if (o.disabled) return;
|
|
80
|
+
if (props.multiple) {
|
|
81
|
+
const on = isPicked(o.value);
|
|
82
|
+
emit("update:modelValue", on ? picks.value.filter((v) => v !== o.value) : [...picks.value, o.value]);
|
|
83
|
+
announce(`${on ? "Removed" : "Added"} ${o.label}`);
|
|
84
|
+
query.value = "";
|
|
85
|
+
typing.value = false;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
emit("update:modelValue", o.value);
|
|
89
|
+
typing.value = false;
|
|
90
|
+
query.value = o.label;
|
|
91
|
+
wanted.value = false;
|
|
92
|
+
active.value = -1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function removeAt(i: number, refocus = false): void {
|
|
96
|
+
const v = picks.value[i];
|
|
97
|
+
if (v === undefined || props.disabled) return;
|
|
98
|
+
emit("update:modelValue", picks.value.filter((_, j) => j !== i));
|
|
99
|
+
announce(`Removed ${labelOf(v)}`);
|
|
100
|
+
if (refocus) inputEl.value?.focus();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function clear(): void {
|
|
104
|
+
emit("update:modelValue", props.multiple ? [] : null);
|
|
105
|
+
query.value = "";
|
|
106
|
+
typing.value = false;
|
|
107
|
+
announce("Cleared");
|
|
108
|
+
inputEl.value?.focus();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function step(dir: 1 | -1): void {
|
|
112
|
+
const n = matches.value.length;
|
|
113
|
+
if (!n) return;
|
|
114
|
+
let i = active.value;
|
|
115
|
+
for (let k = 0; k < n; k++) {
|
|
116
|
+
i = i < 0 ? (dir > 0 ? 0 : n - 1) : (i + dir + n) % n;
|
|
117
|
+
if (!matches.value[i].disabled) break;
|
|
118
|
+
}
|
|
119
|
+
active.value = i;
|
|
120
|
+
void nextTick(() => document.getElementById(`${listId}-${i}`)?.scrollIntoView?.({ block: "nearest" }));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function onKeydown(ev: KeyboardEvent): void {
|
|
124
|
+
if (props.disabled) return;
|
|
125
|
+
switch (ev.key) {
|
|
126
|
+
case "ArrowDown":
|
|
127
|
+
case "ArrowUp":
|
|
128
|
+
ev.preventDefault();
|
|
129
|
+
if (!wanted.value) { wanted.value = true; if (ev.altKey) return; }
|
|
130
|
+
step(ev.key === "ArrowDown" ? 1 : -1);
|
|
131
|
+
return;
|
|
132
|
+
case "Enter": {
|
|
133
|
+
const o = open.value && active.value >= 0 ? matches.value[active.value] : undefined;
|
|
134
|
+
if (!o) return;
|
|
135
|
+
ev.preventDefault();
|
|
136
|
+
choose(o);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
case "Escape":
|
|
140
|
+
if (typing.value && !props.multiple) { typing.value = false; query.value = single.value; }
|
|
141
|
+
return;
|
|
142
|
+
case "Backspace":
|
|
143
|
+
if (props.multiple && query.value === "" && picks.value.length) {
|
|
144
|
+
ev.preventDefault();
|
|
145
|
+
removeAt(picks.value.length - 1);
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
default:
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function onInput(): void {
|
|
153
|
+
typing.value = true;
|
|
154
|
+
wanted.value = true;
|
|
155
|
+
active.value = matches.value.findIndex((o) => !o.disabled);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function onBlur(): void {
|
|
159
|
+
wanted.value = false;
|
|
160
|
+
active.value = -1;
|
|
161
|
+
if (!props.multiple) { typing.value = false; query.value = single.value; }
|
|
162
|
+
else query.value = "";
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function pick(o: ComboOption<T>): void {
|
|
166
|
+
choose(o);
|
|
167
|
+
inputEl.value?.focus();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function setOpen(v: boolean): void {
|
|
171
|
+
if (!v) { wanted.value = false; active.value = -1; }
|
|
172
|
+
}
|
|
173
|
+
</script>
|
|
174
|
+
|
|
175
|
+
<!-- Popover's autofocus prop is off here: focus stays in the box you type in. -->
|
|
176
|
+
<!-- eslint-disable vuejs-accessibility/no-autofocus -->
|
|
177
|
+
<template>
|
|
178
|
+
<Popover :open="open" role="group" match-width :autofocus="false" :label="`${ariaLabel} choices`" @update:open="setOpen">
|
|
179
|
+
<template #trigger>
|
|
180
|
+
<!-- A click on the box's padding focuses the text field, which has the keys. -->
|
|
181
|
+
<!-- eslint-disable-next-line vuejs-accessibility/click-events-have-key-events, vuejs-accessibility/no-static-element-interactions -->
|
|
182
|
+
<div
|
|
183
|
+
class="wp-combo wp-field-box"
|
|
184
|
+
:class="`wp-field-box--${size}`"
|
|
185
|
+
:data-size="size"
|
|
186
|
+
:data-multiple="multiple || undefined"
|
|
187
|
+
:data-disabled="disabled || undefined"
|
|
188
|
+
:data-invalid="invalid || undefined"
|
|
189
|
+
@click="inputEl?.focus(); wanted = true"
|
|
190
|
+
>
|
|
191
|
+
<template v-if="multiple">
|
|
192
|
+
<span v-for="(v, i) in picks" :key="String(v)" class="wp-combo__chip">
|
|
193
|
+
<span class="wp-combo__chip-text">{{ labelOf(v) }}</span>
|
|
194
|
+
<button
|
|
195
|
+
type="button"
|
|
196
|
+
class="wp-combo__remove"
|
|
197
|
+
:aria-label="`Remove ${labelOf(v)}`"
|
|
198
|
+
:disabled="disabled"
|
|
199
|
+
@click.stop="removeAt(i, true)"
|
|
200
|
+
><i class="pi pi-times" aria-hidden="true" /></button>
|
|
201
|
+
</span>
|
|
202
|
+
</template>
|
|
203
|
+
<input
|
|
204
|
+
ref="inputEl"
|
|
205
|
+
v-model="query"
|
|
206
|
+
class="wp-combo__field"
|
|
207
|
+
type="text"
|
|
208
|
+
role="combobox"
|
|
209
|
+
aria-autocomplete="list"
|
|
210
|
+
:aria-label="ariaLabel"
|
|
211
|
+
:aria-expanded="open"
|
|
212
|
+
:aria-controls="open ? listId : undefined"
|
|
213
|
+
:aria-activedescendant="activeId"
|
|
214
|
+
:aria-invalid="invalid || undefined"
|
|
215
|
+
:placeholder="multiple && picks.length ? '' : placeholder"
|
|
216
|
+
:disabled="disabled"
|
|
217
|
+
autocomplete="off"
|
|
218
|
+
@focus="wanted = true"
|
|
219
|
+
@blur="onBlur"
|
|
220
|
+
@keydown="onKeydown"
|
|
221
|
+
@input="onInput"
|
|
222
|
+
>
|
|
223
|
+
<button
|
|
224
|
+
v-if="clearable && picks.length && !disabled"
|
|
225
|
+
type="button"
|
|
226
|
+
class="wp-combo__clear"
|
|
227
|
+
aria-label="Clear"
|
|
228
|
+
@mousedown.prevent
|
|
229
|
+
@click.stop="clear"
|
|
230
|
+
><i class="pi pi-times" aria-hidden="true" /></button>
|
|
231
|
+
<i class="pi pi-chevron-down wp-combo__caret" aria-hidden="true" />
|
|
232
|
+
<span class="wp-combo__sr" aria-live="polite">{{ said }}</span>
|
|
233
|
+
</div>
|
|
234
|
+
</template>
|
|
235
|
+
<ul
|
|
236
|
+
:id="listId"
|
|
237
|
+
class="wp-combo__list"
|
|
238
|
+
role="listbox"
|
|
239
|
+
:aria-label="`${ariaLabel} choices`"
|
|
240
|
+
:aria-multiselectable="multiple || undefined"
|
|
241
|
+
>
|
|
242
|
+
<!-- Options are lit with aria-activedescendant; the text field keeps focus and the keys. -->
|
|
243
|
+
<!-- eslint-disable-next-line vuejs-accessibility/click-events-have-key-events, vuejs-accessibility/interactive-supports-focus -->
|
|
244
|
+
<li
|
|
245
|
+
v-for="(o, i) in matches"
|
|
246
|
+
:id="`${listId}-${i}`"
|
|
247
|
+
:key="String(o.value)"
|
|
248
|
+
class="wp-combo__opt"
|
|
249
|
+
role="option"
|
|
250
|
+
:aria-selected="isPicked(o.value)"
|
|
251
|
+
:aria-disabled="o.disabled || undefined"
|
|
252
|
+
:data-active="i === active || undefined"
|
|
253
|
+
@mousedown.prevent
|
|
254
|
+
@click="pick(o)"
|
|
255
|
+
>
|
|
256
|
+
<i class="pi pi-check wp-combo__tick" :data-on="isPicked(o.value) || undefined" aria-hidden="true" />
|
|
257
|
+
<span class="wp-combo__opt-text">
|
|
258
|
+
<span>{{ o.label }}</span>
|
|
259
|
+
<span v-if="o.hint" class="wp-combo__hint">{{ o.hint }}</span>
|
|
260
|
+
</span>
|
|
261
|
+
</li>
|
|
262
|
+
<li v-if="!matches.length" class="wp-combo__empty" role="presentation">{{ emptyText }}</li>
|
|
263
|
+
</ul>
|
|
264
|
+
</Popover>
|
|
265
|
+
</template>
|
|
266
|
+
|
|
267
|
+
<style scoped src="./field.css"></style>
|
|
268
|
+
|
|
269
|
+
<style scoped>
|
|
270
|
+
.wp-combo {
|
|
271
|
+
--_h: var(--wp-field-h);
|
|
272
|
+
position: relative;
|
|
273
|
+
display: flex; flex-wrap: wrap; align-items: center; gap: 4px;
|
|
274
|
+
width: 100%; padding: 3px 28px 3px var(--wp-space-3);
|
|
275
|
+
cursor: text;
|
|
276
|
+
font-family: var(--wp-font-sans); font-weight: 400; line-height: 1;
|
|
277
|
+
}
|
|
278
|
+
.wp-combo[data-size="sm"] { padding: 2px 24px 2px var(--wp-space-2); gap: 3px; }
|
|
279
|
+
.wp-combo[data-size="lg"] { padding: 5px 32px 5px var(--wp-space-4); }
|
|
280
|
+
.wp-combo__chip {
|
|
281
|
+
display: inline-flex; align-items: center; gap: 2px; max-width: 100%; box-sizing: border-box;
|
|
282
|
+
height: 20px; padding: 0 2px 0 7px;
|
|
283
|
+
border: 1px solid var(--wp-border-strong); border-radius: var(--wp-radius-cap, 999px); background: var(--wp-bg-3);
|
|
284
|
+
color: var(--wp-text); font-size: var(--wp-text-xs); line-height: 1;
|
|
285
|
+
}
|
|
286
|
+
.wp-combo[data-size="sm"] .wp-combo__chip { height: 16px; padding-left: 5px; font-size: 10.5px; }
|
|
287
|
+
.wp-combo__chip-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
288
|
+
.wp-combo__remove, .wp-combo__clear {
|
|
289
|
+
display: inline-grid; place-items: center; width: 16px; height: 16px; padding: 0;
|
|
290
|
+
border: 0; border-radius: min(50%, var(--wp-radius-cap, 999px)); background: transparent; color: var(--wp-text-muted); cursor: pointer;
|
|
291
|
+
}
|
|
292
|
+
.wp-combo__remove { margin: -2px 0; position: relative; }
|
|
293
|
+
.wp-combo__remove::before { content: ""; position: absolute; inset: -2px; }
|
|
294
|
+
.wp-combo__remove .pi, .wp-combo__clear .pi { font-size: 8px; }
|
|
295
|
+
.wp-combo__clear { position: absolute; right: 24px; top: 50%; transform: translateY(-50%); }
|
|
296
|
+
.wp-combo__remove:hover:not(:disabled), .wp-combo__clear:hover { color: var(--wp-text); }
|
|
297
|
+
.wp-combo__remove:focus-visible, .wp-combo__clear:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--wp-border-focus); }
|
|
298
|
+
.wp-combo__caret {
|
|
299
|
+
position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
|
|
300
|
+
color: var(--wp-text-muted); font-size: 10px; pointer-events: none;
|
|
301
|
+
}
|
|
302
|
+
.wp-combo[data-size="sm"] .wp-combo__caret { right: 8px; font-size: 9px; }
|
|
303
|
+
.wp-combo__field {
|
|
304
|
+
flex: 1 0 6ch; min-width: 6ch; height: calc(var(--_h) - 8px); margin: 0; padding: 0 2px;
|
|
305
|
+
border: 0; outline: none; background: transparent; color: inherit; font: inherit;
|
|
306
|
+
text-overflow: ellipsis;
|
|
307
|
+
}
|
|
308
|
+
.wp-combo__field::placeholder { color: var(--wp-text-dim); }
|
|
309
|
+
.wp-combo__field:disabled { cursor: not-allowed; }
|
|
310
|
+
.wp-combo__sr {
|
|
311
|
+
position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
|
|
312
|
+
clip: rect(0 0 0 0); white-space: nowrap; border: 0;
|
|
313
|
+
}
|
|
314
|
+
.wp-combo__list { margin: calc(var(--wp-space-2) * -1); padding: 0; list-style: none; max-height: 280px; overflow: auto; }
|
|
315
|
+
.wp-combo__opt {
|
|
316
|
+
display: flex; align-items: flex-start; gap: var(--wp-space-2);
|
|
317
|
+
padding: 6px var(--wp-space-4) 6px var(--wp-space-2); border-radius: var(--wp-radius-sm);
|
|
318
|
+
color: var(--wp-text); font-size: var(--wp-text-sm); cursor: pointer;
|
|
319
|
+
}
|
|
320
|
+
.wp-combo__opt:hover, .wp-combo__opt[data-active] { background: var(--wp-row-hover); }
|
|
321
|
+
.wp-combo__opt[data-active] { box-shadow: inset 2px 0 0 var(--wp-accent); }
|
|
322
|
+
.wp-combo__opt[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }
|
|
323
|
+
.wp-combo__tick { width: 14px; margin-top: 2px; font-size: 10px; color: var(--wp-accent); visibility: hidden; }
|
|
324
|
+
.wp-combo__tick[data-on] { visibility: visible; }
|
|
325
|
+
.wp-combo__opt-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
|
|
326
|
+
.wp-combo__hint { color: var(--wp-text-muted); font-size: var(--wp-text-xs); }
|
|
327
|
+
.wp-combo__empty { padding: 6px var(--wp-space-4); color: var(--wp-text-muted); font-size: var(--wp-text-sm); }
|
|
328
|
+
@media (prefers-reduced-motion: reduce) {
|
|
329
|
+
.wp-combo { transition: none; }
|
|
330
|
+
}
|
|
331
|
+
</style>
|