@dumiflex/wp-kit 0.3.0 → 0.5.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.
Files changed (130) hide show
  1. package/CHANGELOG.md +92 -51
  2. package/LICENSE +674 -674
  3. package/README.md +73 -76
  4. package/package.json +78 -77
  5. package/src/Avatar.vue +83 -83
  6. package/src/Badge.vue +97 -97
  7. package/src/Banner.vue +118 -118
  8. package/src/BookmarkButton.vue +27 -27
  9. package/src/BundleMeta.vue +51 -51
  10. package/src/Button.vue +103 -103
  11. package/src/Card.vue +68 -68
  12. package/src/Checkbox.vue +109 -107
  13. package/src/Chip.vue +163 -97
  14. package/src/ChoiceCards.vue +133 -133
  15. package/src/CloseButton.vue +69 -69
  16. package/src/CodeBlock.vue +146 -119
  17. package/src/ColorField.vue +188 -206
  18. package/src/ColorPicker.vue +201 -201
  19. package/src/Combobox.vue +331 -334
  20. package/src/ConfirmDialog.vue +165 -163
  21. package/src/ConfirmModal.vue +147 -147
  22. package/src/ConstraintMatrixPreview.vue +708 -708
  23. package/src/ContextMenu.vue +386 -379
  24. package/src/CopyButton.vue +156 -156
  25. package/src/DataTable.vue +293 -292
  26. package/src/DatePicker.vue +234 -234
  27. package/src/DescriptionList.vue +73 -73
  28. package/src/DiffView.vue +68 -68
  29. package/src/Disclosure.vue +166 -166
  30. package/src/DragHandle.vue +49 -49
  31. package/src/Drawer.vue +131 -131
  32. package/src/DropZone.vue +196 -196
  33. package/src/DropdownMenu.vue +72 -72
  34. package/src/EmptyState.vue +111 -111
  35. package/src/Field.vue +28 -28
  36. package/src/HsvPicker.vue +208 -208
  37. package/src/Icon.vue +39 -39
  38. package/src/IconPicker.vue +209 -209
  39. package/src/ImageGrid.vue +127 -125
  40. package/src/InlineEdit.vue +183 -183
  41. package/src/Input.vue +99 -99
  42. package/src/Kbd.vue +56 -56
  43. package/src/Lightbox.vue +176 -173
  44. package/src/ListFilter.vue +85 -85
  45. package/src/ListRow.vue +204 -204
  46. package/src/LoadingMark.vue +217 -161
  47. package/src/Logo.vue +60 -60
  48. package/src/Modal.vue +78 -76
  49. package/src/ModalShell.vue +109 -107
  50. package/src/Pagination.vue +106 -106
  51. package/src/Popover.vue +180 -171
  52. package/src/PostCard.vue +210 -211
  53. package/src/ProgressBar.vue +90 -90
  54. package/src/RadioGroup.vue +177 -177
  55. package/src/RangeSlider.vue +183 -183
  56. package/src/RelativeDate.vue +35 -35
  57. package/src/ResizeHandle.vue +143 -143
  58. package/src/SearchInput.vue +192 -199
  59. package/src/Select.vue +644 -641
  60. package/src/Separator.vue +54 -54
  61. package/src/Skeleton.vue +98 -82
  62. package/src/Slider.vue +198 -198
  63. package/src/SortableList.vue +260 -260
  64. package/src/SplitPane.vue +68 -68
  65. package/src/StarButton.vue +40 -40
  66. package/src/Stepper.vue +141 -141
  67. package/src/SupportLinks.vue +93 -93
  68. package/src/TagInput.vue +297 -300
  69. package/src/Textarea.vue +57 -57
  70. package/src/Timeline.vue +92 -92
  71. package/src/ToastCard.vue +119 -119
  72. package/src/ToastHost.vue +40 -40
  73. package/src/Toggle.vue +36 -36
  74. package/src/TokenizedValue.vue +310 -310
  75. package/src/Toolbar.vue +110 -110
  76. package/src/VirtualList.vue +87 -87
  77. package/src/VisuallyHidden.vue +29 -29
  78. package/src/WpButton.vue +75 -75
  79. package/src/WpCheck.vue +104 -102
  80. package/src/WpIconButton.vue +107 -107
  81. package/src/WpImageZoom.vue +544 -544
  82. package/src/WpNumber.vue +178 -183
  83. package/src/WpSegmented.vue +175 -175
  84. package/src/WpSelect.vue +93 -89
  85. package/src/WpSwitch.vue +163 -163
  86. package/src/WpTabs.vue +188 -188
  87. package/src/_modal-motion.css +33 -33
  88. package/src/_skeleton.css +50 -50
  89. package/src/axis-color.ts +42 -42
  90. package/src/button.css +169 -169
  91. package/src/calendar.ts +59 -59
  92. package/src/clipboard.ts +50 -50
  93. package/src/colour-presets.ts +12 -12
  94. package/src/diff.ts +51 -51
  95. package/src/drop-zone.ts +41 -41
  96. package/src/field.css +50 -0
  97. package/src/flip.ts +237 -237
  98. package/src/floating-layer.ts +48 -0
  99. package/src/floating.ts +99 -99
  100. package/src/highlight.ts +199 -0
  101. package/src/hsv.ts +100 -100
  102. package/src/icon-catalog.ts +52 -52
  103. package/src/image-zoom.ts +93 -93
  104. package/src/kind-colour.ts +28 -0
  105. package/src/loading-mark-motion.ts +249 -0
  106. package/src/logo-paths.ts +21 -21
  107. package/src/matrix-axis.ts +91 -91
  108. package/src/pagination.ts +27 -27
  109. package/src/platform-keys.ts +29 -29
  110. package/src/rich-tokenize.ts +357 -357
  111. package/src/select-types.ts +32 -32
  112. package/src/slider-math.ts +45 -45
  113. package/src/sortable.ts +22 -22
  114. package/src/theme/fonts/OFL-Fraunces.txt +93 -93
  115. package/src/theme/fonts/OFL-JetBrainsMono.txt +93 -93
  116. package/src/theme/fonts/OFL-Manrope.txt +93 -93
  117. package/src/theme/looks.css +428 -428
  118. package/src/theme/palettes.css +254 -254
  119. package/src/theme/tokens.css +2896 -2894
  120. package/src/tokens.ts +56 -56
  121. package/src/tooltip.css +43 -43
  122. package/src/tooltip.ts +146 -146
  123. package/src/useDialogFocus.ts +73 -73
  124. package/src/useGrowableField.ts +357 -357
  125. package/src/useModalKeyShield.ts +116 -116
  126. package/src/useToast.ts +48 -48
  127. package/src/wp-logo.svg +14 -14
  128. package/src/ChipToggle.vue +0 -92
  129. package/src/KindChip.vue +0 -53
  130. 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
- :data-size="size"
185
- :data-multiple="multiple || undefined"
186
- :data-disabled="disabled || undefined"
187
- :data-invalid="invalid || undefined"
188
- @click="inputEl?.focus(); wanted = true"
189
- >
190
- <template v-if="multiple">
191
- <span v-for="(v, i) in picks" :key="String(v)" class="wp-combo__chip">
192
- <span class="wp-combo__chip-text">{{ labelOf(v) }}</span>
193
- <button
194
- type="button"
195
- class="wp-combo__remove"
196
- :aria-label="`Remove ${labelOf(v)}`"
197
- :disabled="disabled"
198
- @click.stop="removeAt(i, true)"
199
- ><i class="pi pi-times" aria-hidden="true" /></button>
200
- </span>
201
- </template>
202
- <input
203
- ref="inputEl"
204
- v-model="query"
205
- class="wp-combo__field"
206
- type="text"
207
- role="combobox"
208
- aria-autocomplete="list"
209
- :aria-label="ariaLabel"
210
- :aria-expanded="open"
211
- :aria-controls="open ? listId : undefined"
212
- :aria-activedescendant="activeId"
213
- :aria-invalid="invalid || undefined"
214
- :placeholder="multiple && picks.length ? '' : placeholder"
215
- :disabled="disabled"
216
- autocomplete="off"
217
- @focus="wanted = true"
218
- @blur="onBlur"
219
- @keydown="onKeydown"
220
- @input="onInput"
221
- >
222
- <button
223
- v-if="clearable && picks.length && !disabled"
224
- type="button"
225
- class="wp-combo__clear"
226
- aria-label="Clear"
227
- @mousedown.prevent
228
- @click.stop="clear"
229
- ><i class="pi pi-times" aria-hidden="true" /></button>
230
- <i class="pi pi-chevron-down wp-combo__caret" aria-hidden="true" />
231
- <span class="wp-combo__sr" aria-live="polite">{{ said }}</span>
232
- </div>
233
- </template>
234
- <ul
235
- :id="listId"
236
- class="wp-combo__list"
237
- role="listbox"
238
- :aria-label="`${ariaLabel} choices`"
239
- :aria-multiselectable="multiple || undefined"
240
- >
241
- <!-- Options are lit with aria-activedescendant; the text field keeps focus and the keys. -->
242
- <!-- eslint-disable-next-line vuejs-accessibility/click-events-have-key-events, vuejs-accessibility/interactive-supports-focus -->
243
- <li
244
- v-for="(o, i) in matches"
245
- :id="`${listId}-${i}`"
246
- :key="String(o.value)"
247
- class="wp-combo__opt"
248
- role="option"
249
- :aria-selected="isPicked(o.value)"
250
- :aria-disabled="o.disabled || undefined"
251
- :data-active="i === active || undefined"
252
- @mousedown.prevent
253
- @click="pick(o)"
254
- >
255
- <i class="pi pi-check wp-combo__tick" :data-on="isPicked(o.value) || undefined" aria-hidden="true" />
256
- <span class="wp-combo__opt-text">
257
- <span>{{ o.label }}</span>
258
- <span v-if="o.hint" class="wp-combo__hint">{{ o.hint }}</span>
259
- </span>
260
- </li>
261
- <li v-if="!matches.length" class="wp-combo__empty" role="presentation">{{ emptyText }}</li>
262
- </ul>
263
- </Popover>
264
- </template>
265
-
266
- <style scoped>
267
- .wp-combo {
268
- --_h: var(--wp-control-h-md);
269
- position: relative;
270
- display: flex; flex-wrap: wrap; align-items: center; gap: 4px; box-sizing: border-box;
271
- width: 100%; min-height: var(--_h); padding: 3px 28px 3px var(--wp-space-3);
272
- border: 1px solid var(--wp-border); border-radius: var(--wp-radius);
273
- background: var(--wp-bg-1); color: var(--wp-text); cursor: text;
274
- font: 400 var(--wp-text-sm)/1 var(--wp-font-sans);
275
- transition: border-color var(--wp-motion-hover), box-shadow var(--wp-motion-hover);
276
- }
277
- .wp-combo[data-size="sm"] { --_h: var(--wp-control-h-sm); padding: 2px 24px 2px var(--wp-space-2); border-radius: var(--wp-radius-sm); font-size: var(--wp-text-xs); gap: 3px; }
278
- .wp-combo[data-size="lg"] { --_h: var(--wp-control-h-lg); padding: 5px 32px 5px var(--wp-space-4); }
279
- .wp-combo:hover { border-color: var(--wp-border-strong); }
280
- .wp-combo:focus-within { border-color: var(--wp-border-focus); box-shadow: 0 0 0 3px var(--wp-focus-ring); }
281
- .wp-combo[data-invalid] { border-color: var(--wp-error-border); }
282
- .wp-combo[data-disabled] { opacity: 0.45; cursor: not-allowed; }
283
- .wp-combo__chip {
284
- display: inline-flex; align-items: center; gap: 2px; max-width: 100%; box-sizing: border-box;
285
- height: 20px; padding: 0 2px 0 7px;
286
- border: 1px solid var(--wp-border-strong); border-radius: var(--wp-radius-cap, 999px); background: var(--wp-bg-3);
287
- color: var(--wp-text); font-size: var(--wp-text-xs); line-height: 1;
288
- }
289
- .wp-combo[data-size="sm"] .wp-combo__chip { height: 16px; padding-left: 5px; font-size: 10.5px; }
290
- .wp-combo__chip-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
291
- .wp-combo__remove, .wp-combo__clear {
292
- display: inline-grid; place-items: center; width: 16px; height: 16px; padding: 0;
293
- border: 0; border-radius: min(50%, var(--wp-radius-cap, 999px)); background: transparent; color: var(--wp-text-muted); cursor: pointer;
294
- }
295
- .wp-combo__remove { margin: -2px 0; position: relative; }
296
- .wp-combo__remove::before { content: ""; position: absolute; inset: -2px; }
297
- .wp-combo__remove .pi, .wp-combo__clear .pi { font-size: 8px; }
298
- .wp-combo__clear { position: absolute; right: 24px; top: 50%; transform: translateY(-50%); }
299
- .wp-combo__remove:hover:not(:disabled), .wp-combo__clear:hover { color: var(--wp-text); }
300
- .wp-combo__remove:focus-visible, .wp-combo__clear:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--wp-border-focus); }
301
- .wp-combo__caret {
302
- position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
303
- color: var(--wp-text-muted); font-size: 10px; pointer-events: none;
304
- }
305
- .wp-combo[data-size="sm"] .wp-combo__caret { right: 8px; font-size: 9px; }
306
- .wp-combo__field {
307
- flex: 1 0 6ch; min-width: 6ch; height: calc(var(--_h) - 8px); margin: 0; padding: 0 2px;
308
- border: 0; outline: none; background: transparent; color: inherit; font: inherit;
309
- text-overflow: ellipsis;
310
- }
311
- .wp-combo__field::placeholder { color: var(--wp-text-dim); }
312
- .wp-combo__field:disabled { cursor: not-allowed; }
313
- .wp-combo__sr {
314
- position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
315
- clip: rect(0 0 0 0); white-space: nowrap; border: 0;
316
- }
317
- .wp-combo__list { margin: calc(var(--wp-space-2) * -1); padding: 0; list-style: none; max-height: 280px; overflow: auto; }
318
- .wp-combo__opt {
319
- display: flex; align-items: flex-start; gap: var(--wp-space-2);
320
- padding: 6px var(--wp-space-4) 6px var(--wp-space-2); border-radius: var(--wp-radius-sm);
321
- color: var(--wp-text); font-size: var(--wp-text-sm); cursor: pointer;
322
- }
323
- .wp-combo__opt:hover, .wp-combo__opt[data-active] { background: var(--wp-row-hover); }
324
- .wp-combo__opt[data-active] { box-shadow: inset 2px 0 0 var(--wp-accent); }
325
- .wp-combo__opt[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }
326
- .wp-combo__tick { width: 14px; margin-top: 2px; font-size: 10px; color: var(--wp-accent); visibility: hidden; }
327
- .wp-combo__tick[data-on] { visibility: visible; }
328
- .wp-combo__opt-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
329
- .wp-combo__hint { color: var(--wp-text-muted); font-size: var(--wp-text-xs); }
330
- .wp-combo__empty { padding: 6px var(--wp-space-4); color: var(--wp-text-muted); font-size: var(--wp-text-sm); }
331
- @media (prefers-reduced-motion: reduce) {
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>