loseta 0.17.0 → 0.19.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 (70) hide show
  1. package/README.md +37 -8
  2. package/dist/commands/add.d.ts +8 -0
  3. package/dist/commands/add.js +60 -15
  4. package/dist/commands/add.js.map +1 -1
  5. package/dist/commands/init.js +2 -1
  6. package/dist/commands/init.js.map +1 -1
  7. package/dist/config.d.ts +7 -0
  8. package/dist/config.js +13 -0
  9. package/dist/config.js.map +1 -1
  10. package/dist/imports.d.ts +4 -0
  11. package/dist/imports.js +30 -2
  12. package/dist/imports.js.map +1 -1
  13. package/dist/install.d.ts +23 -3
  14. package/dist/install.js +84 -5
  15. package/dist/install.js.map +1 -1
  16. package/dist/style-switch.js +2 -1
  17. package/dist/style-switch.js.map +1 -1
  18. package/dist/types.d.ts +11 -0
  19. package/dist/ui.js +4 -1
  20. package/dist/ui.js.map +1 -1
  21. package/package.json +4 -3
  22. package/registry/behaviors/calendar.js +855 -0
  23. package/registry/behaviors/carousel.js +280 -0
  24. package/registry/behaviors/code-block.js +106 -0
  25. package/registry/behaviors/combobox.js +237 -0
  26. package/registry/behaviors/command.js +105 -0
  27. package/registry/behaviors/core.js +302 -0
  28. package/registry/behaviors/dialog.js +97 -0
  29. package/registry/behaviors/hover-overlay.js +180 -0
  30. package/registry/behaviors/listbox.js +135 -0
  31. package/registry/behaviors/menu.js +586 -0
  32. package/registry/behaviors/message-scroller.js +147 -0
  33. package/registry/behaviors/popover.js +127 -0
  34. package/registry/behaviors/questionnaire.js +91 -0
  35. package/registry/behaviors/resizable.js +463 -0
  36. package/registry/behaviors/select.js +196 -0
  37. package/registry/behaviors/tabs.js +95 -0
  38. package/registry/behaviors/toast.js +137 -0
  39. package/registry/components/carousel.css +1 -0
  40. package/registry/components/code-block.css +19 -0
  41. package/registry/components/hover-card.css +2 -2
  42. package/registry/components/resizable.css +49 -0
  43. package/registry/components/tooltip.css +4 -1
  44. package/registry/examples/alert-dialog.html +21 -21
  45. package/registry/examples/button-group.html +3 -3
  46. package/registry/examples/calendar.html +387 -326
  47. package/registry/examples/carousel.html +80 -2
  48. package/registry/examples/code-block.html +10 -4
  49. package/registry/examples/combobox.html +2 -0
  50. package/registry/examples/command.html +17 -17
  51. package/registry/examples/context-menu.html +18 -18
  52. package/registry/examples/dialog.html +15 -15
  53. package/registry/examples/drawer.html +22 -22
  54. package/registry/examples/questionnaire.html +2 -2
  55. package/registry/examples/resizable.html +88 -15
  56. package/registry/examples/select.html +7 -0
  57. package/registry/examples/sheet.html +23 -23
  58. package/registry/examples/sidebar.html +11 -11
  59. package/registry/examples/toast.html +11 -0
  60. package/registry/primitives/dialog-shell.css +5 -0
  61. package/registry/registry.json +280 -1
  62. package/registry/schema.json +17 -0
  63. package/registry/styles/luma/resizable.css +10 -0
  64. package/registry/styles/lyra/resizable.css +10 -0
  65. package/registry/styles/maia/resizable.css +10 -0
  66. package/registry/styles/mira/resizable.css +10 -0
  67. package/registry/styles/nova/resizable.css +10 -0
  68. package/registry/styles/rhea/resizable.css +10 -0
  69. package/registry/styles/sera/resizable.css +10 -0
  70. package/registry/styles/vega/resizable.css +10 -0
@@ -0,0 +1,280 @@
1
+ // @ts-check
2
+
3
+ import { emit, init, isRTL, on } from "./core.js";
4
+
5
+ /**
6
+ * @typedef {object} CarouselState
7
+ * @property {number} index
8
+ * @property {number} target
9
+ * @property {boolean} settling
10
+ * @property {number} timer
11
+ */
12
+
13
+ /** @type {WeakMap<HTMLElement, CarouselState>} */
14
+ const states = new WeakMap();
15
+ /** @type {WeakSet<HTMLElement>} */
16
+ const scheduled = new WeakSet();
17
+
18
+ /** @param {HTMLElement} carousel */
19
+ function parts(carousel) {
20
+ const viewport = carousel.querySelector(".carousel-viewport");
21
+ const items = [...carousel.querySelectorAll(".carousel-item")].filter(
22
+ (item) => item instanceof HTMLElement
23
+ );
24
+ const indicators = [...carousel.querySelectorAll(".carousel-indicator")].filter(
25
+ (indicator) => indicator instanceof HTMLElement
26
+ );
27
+ return {
28
+ viewport: viewport instanceof HTMLElement ? viewport : null,
29
+ items,
30
+ indicators
31
+ };
32
+ }
33
+
34
+ /** @param {HTMLElement} carousel */
35
+ function horizontal(carousel) {
36
+ return carousel.getAttribute("data-orientation") !== "vertical";
37
+ }
38
+
39
+ function reducedMotion() {
40
+ return (
41
+ typeof window.matchMedia === "function" &&
42
+ window.matchMedia("(prefers-reduced-motion: reduce)").matches
43
+ );
44
+ }
45
+
46
+ /**
47
+ * @param {HTMLElement} carousel
48
+ * @param {HTMLElement} viewport
49
+ * @param {HTMLElement[]} items
50
+ */
51
+ function indexFromScroll(carousel, viewport, items) {
52
+ if (items.length === 0) return 0;
53
+ const viewportRect = viewport.getBoundingClientRect();
54
+ const horizontalScroll = horizontal(carousel);
55
+ let best = 0;
56
+ let distance = Number.POSITIVE_INFINITY;
57
+
58
+ for (let index = 0; index < items.length; index += 1) {
59
+ const item = items[index];
60
+ if (!item) continue;
61
+ const rect = item.getBoundingClientRect();
62
+ const value = horizontalScroll
63
+ ? Math.abs(rect.left - viewportRect.left) +
64
+ Math.abs(rect.right - viewportRect.right)
65
+ : Math.abs(rect.top - viewportRect.top) +
66
+ Math.abs(rect.bottom - viewportRect.bottom);
67
+ if (value < distance) {
68
+ distance = value;
69
+ best = index;
70
+ }
71
+ }
72
+
73
+ return best;
74
+ }
75
+
76
+ /**
77
+ * @param {HTMLElement} carousel
78
+ * @param {number} index
79
+ */
80
+ function updateIndicators(carousel, index) {
81
+ const { indicators } = parts(carousel);
82
+ for (let cursor = 0; cursor < indicators.length; cursor += 1) {
83
+ const indicator = indicators[cursor];
84
+ if (!indicator) continue;
85
+ indicator.setAttribute("aria-current", String(cursor === index));
86
+ }
87
+ }
88
+
89
+ /**
90
+ * @param {HTMLElement} carousel
91
+ * @param {HTMLElement} viewport
92
+ * @param {HTMLElement[]} items
93
+ */
94
+ function updateOffscreen(carousel, viewport, items) {
95
+ const viewportRect = viewport.getBoundingClientRect();
96
+ const horizontalScroll = horizontal(carousel);
97
+
98
+ for (const item of items) {
99
+ const rect = item.getBoundingClientRect();
100
+ const visible = horizontalScroll
101
+ ? rect.right > viewportRect.left + 1 && rect.left < viewportRect.right - 1
102
+ : rect.bottom > viewportRect.top + 1 && rect.top < viewportRect.bottom - 1;
103
+ item.setAttribute("aria-hidden", String(!visible));
104
+ item.toggleAttribute("inert", !visible);
105
+ }
106
+ }
107
+
108
+ /** @param {HTMLElement} carousel */
109
+ function ensure(carousel) {
110
+ const existing = states.get(carousel);
111
+ if (existing) return existing;
112
+
113
+ const { viewport, items, indicators } = parts(carousel);
114
+ const current = indicators.findIndex(
115
+ (indicator) => indicator.getAttribute("aria-current") === "true"
116
+ );
117
+ const index =
118
+ current >= 0
119
+ ? current
120
+ : viewport
121
+ ? indexFromScroll(carousel, viewport, items)
122
+ : 0;
123
+ const state = { index, target: index, settling: false, timer: 0 };
124
+ states.set(carousel, state);
125
+
126
+ if (viewport) updateOffscreen(carousel, viewport, items);
127
+ updateIndicators(carousel, index);
128
+ return state;
129
+ }
130
+
131
+ /**
132
+ * @param {HTMLElement} carousel
133
+ * @param {CarouselState} state
134
+ */
135
+ function update(carousel, state) {
136
+ const { viewport, items } = parts(carousel);
137
+ if (!viewport) return;
138
+ const index = indexFromScroll(carousel, viewport, items);
139
+ updateOffscreen(carousel, viewport, items);
140
+
141
+ if (state.settling) {
142
+ if (index !== state.target) return;
143
+ state.settling = false;
144
+ if (state.timer !== 0) {
145
+ window.clearTimeout(state.timer);
146
+ state.timer = 0;
147
+ }
148
+ }
149
+
150
+ const changed = index !== state.index;
151
+ state.index = index;
152
+ updateIndicators(carousel, index);
153
+ if (changed) emit(carousel, "change", { index });
154
+ }
155
+
156
+ /**
157
+ * @param {HTMLElement} carousel
158
+ * @param {CarouselState} state
159
+ * @param {number} index
160
+ */
161
+ function go(carousel, state, index) {
162
+ const { items } = parts(carousel);
163
+ if (items.length === 0) return;
164
+
165
+ const clamped = Math.max(0, Math.min(index, items.length - 1));
166
+ const item = items[clamped];
167
+ if (!item) return;
168
+
169
+ const changed = clamped !== state.index;
170
+ state.index = clamped;
171
+ state.target = clamped;
172
+ state.settling = true;
173
+ updateIndicators(carousel, clamped);
174
+
175
+ if (state.timer !== 0) window.clearTimeout(state.timer);
176
+ state.timer = window.setTimeout(() => {
177
+ state.settling = false;
178
+ state.target = state.index;
179
+ state.timer = 0;
180
+ }, 600);
181
+
182
+ const horizontalScroll = horizontal(carousel);
183
+ item.scrollIntoView({
184
+ behavior: reducedMotion() ? "auto" : "smooth",
185
+ block: horizontalScroll ? "nearest" : "start",
186
+ inline: horizontalScroll ? "start" : "nearest"
187
+ });
188
+
189
+ if (changed) emit(carousel, "change", { index: clamped });
190
+ }
191
+
192
+ on(
193
+ "click",
194
+ ".carousel [data-slot='previous'], .carousel [data-slot='next']",
195
+ (event, element) => {
196
+ if (!(element instanceof HTMLElement)) return;
197
+ const carousel = element.closest(".carousel");
198
+ if (!(carousel instanceof HTMLElement)) return;
199
+ const state = ensure(carousel);
200
+ go(
201
+ carousel,
202
+ state,
203
+ state.index + (element.getAttribute("data-slot") === "next" ? 1 : -1)
204
+ );
205
+ event.preventDefault();
206
+ return true;
207
+ }
208
+ );
209
+
210
+ on("click", ".carousel .carousel-indicator", (event, element) => {
211
+ if (!(element instanceof HTMLElement)) return;
212
+ const carousel = element.closest(".carousel");
213
+ if (!(carousel instanceof HTMLElement)) return;
214
+ const { indicators } = parts(carousel);
215
+ const index = indicators.indexOf(element);
216
+ if (index === -1) return;
217
+ const state = ensure(carousel);
218
+ go(carousel, state, index);
219
+ event.preventDefault();
220
+ return true;
221
+ });
222
+
223
+ on("keydown", ".carousel-viewport", (event, element) => {
224
+ if (!(event instanceof KeyboardEvent)) return;
225
+ if (!(element instanceof HTMLElement)) return;
226
+ if (event.target !== element) return;
227
+
228
+ const carousel = element.closest(".carousel");
229
+ if (!(carousel instanceof HTMLElement)) return;
230
+ const state = ensure(carousel);
231
+ const { items } = parts(carousel);
232
+ if (items.length === 0) return;
233
+
234
+ const horizontalScroll = horizontal(carousel);
235
+ const rtl = horizontalScroll && isRTL(carousel);
236
+ const nextKey = horizontalScroll
237
+ ? rtl
238
+ ? "ArrowLeft"
239
+ : "ArrowRight"
240
+ : "ArrowDown";
241
+ const previousKey = horizontalScroll
242
+ ? rtl
243
+ ? "ArrowRight"
244
+ : "ArrowLeft"
245
+ : "ArrowUp";
246
+
247
+ if (event.key === nextKey) {
248
+ go(carousel, state, state.index + 1);
249
+ event.preventDefault();
250
+ return true;
251
+ }
252
+
253
+ if (event.key === previousKey) {
254
+ go(carousel, state, state.index - 1);
255
+ event.preventDefault();
256
+ return true;
257
+ }
258
+
259
+ if (event.key === "Home" || event.key === "End") {
260
+ go(carousel, state, event.key === "Home" ? 0 : items.length - 1);
261
+ event.preventDefault();
262
+ return true;
263
+ }
264
+ });
265
+
266
+ on("scroll", ".carousel-viewport", (_event, element) => {
267
+ if (!(element instanceof HTMLElement)) return;
268
+ const carousel = element.closest(".carousel");
269
+ if (!(carousel instanceof HTMLElement)) return;
270
+ if (scheduled.has(carousel)) return;
271
+
272
+ scheduled.add(carousel);
273
+ requestAnimationFrame(() => {
274
+ scheduled.delete(carousel);
275
+ if (!carousel.isConnected) return;
276
+ update(carousel, ensure(carousel));
277
+ });
278
+ });
279
+
280
+ init();
@@ -0,0 +1,106 @@
1
+ // @ts-check
2
+
3
+ import { init, on } from "./core.js";
4
+
5
+ const FEEDBACK_MS = 1500;
6
+
7
+ /** @type {WeakMap<HTMLElement, number>} */
8
+ const feedbackTimers = new WeakMap();
9
+ /** @type {WeakMap<HTMLElement, { label: string, text: string | null }>} */
10
+ const originals = new WeakMap();
11
+
12
+ /** @param {Element} block */
13
+ function codeText(block) {
14
+ const code = block.querySelector("pre code");
15
+ if (!code) return "";
16
+
17
+ const lines = [...code.querySelectorAll("[data-slot='line']")];
18
+ const text =
19
+ lines.length > 0
20
+ ? lines
21
+ .map((line) => {
22
+ const content = line.querySelector("[data-slot='line-content']");
23
+ return content?.textContent ?? line.textContent ?? "";
24
+ })
25
+ .join("\n")
26
+ : (code.textContent ?? "");
27
+
28
+ return text.replace(/\n+$/, "");
29
+ }
30
+
31
+ /** @param {HTMLElement} button */
32
+ function resetFeedback(button) {
33
+ const timer = feedbackTimers.get(button);
34
+ if (timer !== undefined) {
35
+ window.clearTimeout(timer);
36
+ feedbackTimers.delete(button);
37
+ }
38
+ button.removeAttribute("data-copied");
39
+ const original = originals.get(button);
40
+ if (original) {
41
+ button.setAttribute("aria-label", original.label);
42
+ if (original.text !== null) button.textContent = original.text;
43
+ }
44
+ }
45
+
46
+ /** @param {HTMLElement} button @param {string} label @param {boolean} copied */
47
+ function showFeedback(button, label, copied) {
48
+ if (!originals.has(button)) {
49
+ originals.set(button, {
50
+ label: button.getAttribute("aria-label") ?? "Copy",
51
+ text: button.querySelector("svg") ? null : (button.textContent ?? "")
52
+ });
53
+ }
54
+
55
+ resetFeedback(button);
56
+
57
+ if (copied) {
58
+ button.setAttribute("data-copied", "");
59
+ const original = originals.get(button);
60
+ if (original?.text !== null && original?.text !== undefined) {
61
+ button.textContent = "Copied";
62
+ }
63
+ }
64
+ button.setAttribute("aria-label", label);
65
+
66
+ const timer = window.setTimeout(() => resetFeedback(button), FEEDBACK_MS);
67
+ feedbackTimers.set(button, timer);
68
+ }
69
+
70
+ on("click", ".code-block [data-slot='copy']", (event, target) => {
71
+ if (!(target instanceof HTMLElement)) return;
72
+ const block = target.closest(".code-block");
73
+ if (!block) return;
74
+
75
+ event.preventDefault();
76
+
77
+ const text = codeText(block);
78
+ if (!text) return true;
79
+
80
+ const write = navigator.clipboard?.writeText(text);
81
+ if (!write) {
82
+ showFeedback(target, "Copy failed", false);
83
+ return true;
84
+ }
85
+
86
+ write
87
+ .then(() => showFeedback(target, "Copied", true))
88
+ .catch(() => showFeedback(target, "Copy failed", false));
89
+
90
+ return true;
91
+ });
92
+
93
+ on("click", ".code-block [data-slot='wrap']", (event, target) => {
94
+ if (!(target instanceof HTMLElement)) return;
95
+ const block = target.closest(".code-block");
96
+ const pre = block?.querySelector("pre");
97
+ if (!pre) return;
98
+
99
+ const next = !pre.hasAttribute("data-wrap");
100
+ pre.toggleAttribute("data-wrap", next);
101
+ target.setAttribute("aria-pressed", String(next));
102
+ event.preventDefault();
103
+ return true;
104
+ });
105
+
106
+ init();
@@ -0,0 +1,237 @@
1
+ // @ts-check
2
+
3
+ import { emit, init, on, onDocument, panelFor } from "./core.js";
4
+ import {
5
+ filterOptions,
6
+ firstOption,
7
+ highlight,
8
+ lastOption,
9
+ listboxIn,
10
+ moveHighlight,
11
+ optionsOf,
12
+ resetFilter,
13
+ selectOption,
14
+ selectedOptions,
15
+ selectedValues
16
+ } from "./listbox.js";
17
+
18
+ /** @type {WeakSet<HTMLInputElement>} */
19
+ const suppressFocus = new WeakSet();
20
+
21
+ /** @param {HTMLInputElement} input */
22
+ function rootOf(input) {
23
+ return input.closest(".combobox") ?? input.parentElement ?? input;
24
+ }
25
+
26
+ /** @param {HTMLInputElement} input */
27
+ function parts(input) {
28
+ const panel = panelFor(input);
29
+ const listbox = panel ? listboxIn(panel) : null;
30
+ return { panel, listbox };
31
+ }
32
+
33
+ /** @param {HTMLInputElement} input */
34
+ function isExpanded(input) {
35
+ return input.getAttribute("aria-expanded") === "true";
36
+ }
37
+
38
+ /** @param {HTMLInputElement} input @param {boolean} open */
39
+ function setExpanded(input, open) {
40
+ const { panel } = parts(input);
41
+ input.setAttribute("aria-expanded", String(open));
42
+ if (panel) panel.setAttribute("aria-hidden", String(!open));
43
+ }
44
+
45
+ /** @param {HTMLInputElement} input */
46
+ function multiple(input) {
47
+ const { listbox } = parts(input);
48
+ return listbox?.getAttribute("aria-multiselectable") === "true";
49
+ }
50
+
51
+ /** @param {HTMLInputElement} input */
52
+ function syncHidden(input) {
53
+ if (multiple(input)) return;
54
+ const { listbox } = parts(input);
55
+ if (!listbox) return;
56
+ const hidden = rootOf(input).querySelector("input[data-slot='input']");
57
+ if (hidden instanceof HTMLInputElement) {
58
+ hidden.value = selectedValues(listbox)[0] ?? "";
59
+ }
60
+ }
61
+
62
+ /** @param {HTMLInputElement} input */
63
+ function openCombobox(input) {
64
+ const { listbox } = parts(input);
65
+ if (!listbox) return;
66
+
67
+ resetFilter(listbox);
68
+ setExpanded(input, true);
69
+ const selected = selectedOptions(listbox)[0] ?? firstOption(listbox);
70
+ highlight(listbox, selected, input);
71
+ }
72
+
73
+ /** @param {HTMLInputElement} input @param {boolean} restore */
74
+ function closeCombobox(input, restore) {
75
+ const { listbox } = parts(input);
76
+ setExpanded(input, false);
77
+ if (listbox) highlight(listbox, null, input);
78
+
79
+ if (!restore) return;
80
+ const selected = listbox ? selectedValues(listbox) : [];
81
+ input.value = multiple(input) ? "" : (selected[0] ?? "");
82
+ if (listbox) resetFilter(listbox);
83
+ }
84
+
85
+ /** @param {HTMLInputElement} input */
86
+ function filter(input) {
87
+ const { listbox } = parts(input);
88
+ if (!listbox) return;
89
+
90
+ if (!isExpanded(input)) setExpanded(input, true);
91
+ if (input.value.trim() === "") {
92
+ resetFilter(listbox);
93
+ } else {
94
+ filterOptions(listbox, input.value);
95
+ }
96
+ highlight(listbox, firstOption(listbox), input);
97
+ }
98
+
99
+ /** @param {HTMLInputElement} input @param {Element} option */
100
+ function choose(input, option) {
101
+ const { listbox } = parts(input);
102
+ if (!listbox) return;
103
+
104
+ const isMultiple = multiple(input);
105
+ const values = selectOption(listbox, option, isMultiple);
106
+ const root = rootOf(input);
107
+
108
+ if (isMultiple) {
109
+ input.value = "";
110
+ resetFilter(listbox);
111
+ highlight(listbox, firstOption(listbox), input);
112
+ emit(root, "change", { values });
113
+ syncHidden(input);
114
+ input.focus();
115
+ return;
116
+ }
117
+
118
+ const value = values[0] ?? "";
119
+ input.value = value;
120
+ setExpanded(input, false);
121
+ highlight(listbox, null, input);
122
+ syncHidden(input);
123
+ emit(root, "change", { value });
124
+ if (document.activeElement !== input) {
125
+ suppressFocus.add(input);
126
+ input.focus();
127
+ }
128
+ }
129
+
130
+ /** @param {HTMLInputElement} input */
131
+ function clear(input) {
132
+ const { listbox } = parts(input);
133
+ if (!listbox) return;
134
+
135
+ for (const option of optionsOf(listbox)) {
136
+ option.setAttribute("aria-selected", "false");
137
+ }
138
+ input.value = "";
139
+ resetFilter(listbox);
140
+ highlight(listbox, firstOption(listbox), input);
141
+ syncHidden(input);
142
+ emit(rootOf(input), "change", { value: "", values: [] });
143
+ input.focus();
144
+ }
145
+
146
+ on("focusin", ".combobox [role='combobox']", (_event, target) => {
147
+ if (!(target instanceof HTMLInputElement)) return;
148
+ if (suppressFocus.has(target)) {
149
+ suppressFocus.delete(target);
150
+ return;
151
+ }
152
+ if (!isExpanded(target)) openCombobox(target);
153
+ });
154
+
155
+ on("click", ".combobox [role='combobox']", (_event, target) => {
156
+ if (!(target instanceof HTMLInputElement)) return;
157
+ if (!isExpanded(target)) openCombobox(target);
158
+ });
159
+
160
+ on("input", ".combobox [role='combobox']", (event, target) => {
161
+ if (!(target instanceof HTMLInputElement)) return;
162
+ if (event instanceof InputEvent && event.isComposing) return;
163
+ filter(target);
164
+ });
165
+
166
+ on("click", ".combobox [role='option']", (event, target) => {
167
+ if (!(target instanceof HTMLElement) || target.getAttribute("aria-disabled") === "true") {
168
+ return;
169
+ }
170
+ const input = target.closest(".combobox")?.querySelector("[role='combobox']");
171
+ if (input instanceof HTMLInputElement) choose(input, target);
172
+ event.preventDefault();
173
+ return true;
174
+ });
175
+
176
+ on("click", ".combobox [data-slot='clear']", (event, target) => {
177
+ if (!(target instanceof HTMLElement)) return;
178
+ const input = target.closest(".combobox")?.querySelector("[role='combobox']");
179
+ if (input instanceof HTMLInputElement) clear(input);
180
+ event.preventDefault();
181
+ return true;
182
+ });
183
+
184
+ on("keydown", ".combobox [role='combobox']", (event, target) => {
185
+ if (!(event instanceof KeyboardEvent) || !(target instanceof HTMLInputElement)) return;
186
+ const { listbox } = parts(target);
187
+ if (!listbox) return;
188
+
189
+ if (event.key === "Escape") {
190
+ if (!isExpanded(target)) return;
191
+ closeCombobox(target, true);
192
+ event.preventDefault();
193
+ return true;
194
+ }
195
+
196
+ if (event.key === "ArrowDown" || event.key === "ArrowUp") {
197
+ if (!isExpanded(target)) openCombobox(target);
198
+ const next = moveHighlight(listbox, event.key === "ArrowDown" ? 1 : -1);
199
+ if (next) highlight(listbox, next, target);
200
+ event.preventDefault();
201
+ return true;
202
+ }
203
+
204
+ if (event.key === "Home" || event.key === "End") {
205
+ if (!isExpanded(target)) openCombobox(target);
206
+ highlight(
207
+ listbox,
208
+ event.key === "Home" ? firstOption(listbox) : lastOption(listbox),
209
+ target
210
+ );
211
+ event.preventDefault();
212
+ return true;
213
+ }
214
+
215
+ if (event.key === "Enter") {
216
+ const option = listbox.querySelector("[data-highlighted]");
217
+ if (option instanceof HTMLElement) choose(target, option);
218
+ event.preventDefault();
219
+ return true;
220
+ }
221
+
222
+ if (event.key === "Tab") {
223
+ closeCombobox(target, true);
224
+ }
225
+ }, 25);
226
+
227
+ onDocument("pointerdown", (event) => {
228
+ if (!(event.target instanceof Node)) return;
229
+ for (const input of document.querySelectorAll(".combobox [role='combobox']")) {
230
+ if (!(input instanceof HTMLInputElement) || !isExpanded(input)) continue;
231
+ const root = rootOf(input);
232
+ if (root.contains(event.target)) continue;
233
+ closeCombobox(input, true);
234
+ }
235
+ });
236
+
237
+ init();