stimeo-ui 0.2.0 → 0.2.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +59 -0
  3. data/dist/controllers/alert_dialog_controller.js +318 -0
  4. data/dist/controllers/carousel_controller.js +272 -0
  5. data/dist/controllers/clipboard_controller.js +144 -0
  6. data/dist/controllers/collapsible_controller.js +327 -0
  7. data/dist/controllers/color_picker_controller.js +213 -0
  8. data/dist/controllers/count_up_controller.js +8 -1
  9. data/dist/controllers/currency_input_controller.js +147 -0
  10. data/dist/controllers/data_grid_controller.js +168 -0
  11. data/dist/controllers/date_range_picker_controller.js +417 -0
  12. data/dist/controllers/dismissible_controller.js +117 -0
  13. data/dist/controllers/drawer_controller.js +630 -0
  14. data/dist/controllers/editable_controller.js +168 -0
  15. data/dist/controllers/file_dropzone_controller.js +165 -0
  16. data/dist/controllers/filter_controller.js +86 -0
  17. data/dist/controllers/flash_controller.js +36 -5
  18. data/dist/controllers/highlight_controller.js +6 -4
  19. data/dist/controllers/intersection_controller.js +41 -18
  20. data/dist/controllers/lazy_frame_controller.js +33 -11
  21. data/dist/controllers/masonry_controller.js +142 -0
  22. data/dist/controllers/menubar_controller.js +433 -0
  23. data/dist/controllers/multi_select_controller.js +472 -0
  24. data/dist/controllers/navigation_menu_controller.js +384 -0
  25. data/dist/controllers/overflow_indicator_controller.js +178 -27
  26. data/dist/controllers/password_reveal_controller.js +117 -0
  27. data/dist/controllers/range_slider_controller.js +166 -0
  28. data/dist/controllers/read_more_controller.js +194 -0
  29. data/dist/controllers/scroll_area_controller.js +15 -2
  30. data/dist/controllers/scroll_restore_controller.js +93 -0
  31. data/dist/controllers/scroll_visibility_controller.js +8 -4
  32. data/dist/controllers/scrollspy_controller.js +33 -11
  33. data/dist/controllers/separator_controller.js +87 -0
  34. data/dist/controllers/sidebar_controller.js +761 -0
  35. data/dist/controllers/stepper_controller.js +28 -12
  36. data/dist/controllers/stick_to_bottom_controller.js +8 -4
  37. data/dist/controllers/sticky_observer_controller.js +88 -20
  38. data/dist/controllers/tags_input_controller.js +275 -0
  39. data/dist/controllers/theme_controller.js +20 -10
  40. data/dist/controllers/time_picker_controller.js +212 -0
  41. data/dist/controllers/toast_controller.js +36 -9
  42. data/dist/controllers/transition_controller.js +153 -38
  43. data/dist/controllers/tree_view_controller.js +275 -0
  44. data/dist/index.js +811 -295
  45. data/lib/stimeo/ui/version.rb +1 -1
  46. metadata +28 -2
@@ -0,0 +1,142 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // src/controllers/masonry_controller.ts
4
+
5
+ // src/utils/layout_observer.ts
6
+ var LayoutObserver = class {
7
+ #callback;
8
+ #resizeObserverFactory;
9
+ #resizeObserver = null;
10
+ #observingViewport = false;
11
+ /** Stable bound handler so add/removeEventListener target the same reference. */
12
+ #handleViewportResize = () => {
13
+ this.#callback();
14
+ };
15
+ constructor(callback, options = {}) {
16
+ this.#callback = callback;
17
+ this.#resizeObserverFactory = options.resizeObserverFactory ?? (typeof ResizeObserver === "undefined" ? null : (cb) => new ResizeObserver(cb));
18
+ }
19
+ /**
20
+ * Starts observing an element's size. Repeated calls observe additional
21
+ * elements through the same shared observer. No-ops when no
22
+ * `ResizeObserver` implementation is available.
23
+ */
24
+ observe(element) {
25
+ if (!this.#resizeObserverFactory) return;
26
+ if (!this.#resizeObserver) {
27
+ this.#resizeObserver = this.#resizeObserverFactory(() => {
28
+ this.#callback();
29
+ });
30
+ }
31
+ this.#resizeObserver.observe(element);
32
+ }
33
+ /** Stops observing a single element while leaving any others in place. */
34
+ unobserve(element) {
35
+ this.#resizeObserver?.unobserve(element);
36
+ }
37
+ /** Starts observing viewport resizes. Idempotent: the listener is added once. */
38
+ observeViewport() {
39
+ if (this.#observingViewport) return;
40
+ this.#observingViewport = true;
41
+ window.addEventListener("resize", this.#handleViewportResize);
42
+ }
43
+ /** Stops observing viewport resizes without affecting element observation. */
44
+ unobserveViewport() {
45
+ if (!this.#observingViewport) return;
46
+ this.#observingViewport = false;
47
+ window.removeEventListener("resize", this.#handleViewportResize);
48
+ }
49
+ /**
50
+ * Releases every observation: disconnects the {@link ResizeObserver} and
51
+ * removes the viewport listener. Safe to call multiple times. Call this from a
52
+ * controller's `disconnect()`.
53
+ */
54
+ disconnect() {
55
+ this.#resizeObserver?.disconnect();
56
+ this.#resizeObserver = null;
57
+ this.unobserveViewport();
58
+ }
59
+ };
60
+
61
+ // src/controllers/masonry_controller.ts
62
+ var COLUMNS_PROPERTY = "--stimeo-masonry-columns";
63
+ var MasonryController = class extends Controller {
64
+ static targets = ["item"];
65
+ static values = {
66
+ minColumnWidth: { type: Number, default: 240 },
67
+ gap: { type: Number, default: 16 }
68
+ };
69
+ static events = ["layout"];
70
+ #layout = new LayoutObserver(() => this.#relayout());
71
+ #mutationObserver = null;
72
+ /** Last published column count, so `layout` fires only on real changes. */
73
+ #lastColumns = 0;
74
+ /**
75
+ * Re-pack when a descendant resource finishes loading. Images/iframes report a
76
+ * height of 0 until loaded, which would skew the shortest-column packing if the
77
+ * first pass ran before they settled; `load` does not bubble, so this is bound in
78
+ * the capture phase to catch every descendant.
79
+ */
80
+ #onLoad = () => this.#relayout();
81
+ /** Observes size/content changes and performs the first layout pass. */
82
+ connect() {
83
+ this.#layout.observe(this.element);
84
+ this.#layout.observeViewport();
85
+ if (typeof MutationObserver !== "undefined") {
86
+ this.#mutationObserver = new MutationObserver(() => this.#relayout());
87
+ this.#mutationObserver.observe(this.element, { childList: true, subtree: true });
88
+ }
89
+ this.element.addEventListener("load", this.#onLoad, true);
90
+ this.#relayout();
91
+ }
92
+ /** Releases both observers and the load listener so nothing fires after detach. */
93
+ disconnect() {
94
+ this.#layout.disconnect();
95
+ this.#mutationObserver?.disconnect();
96
+ this.#mutationObserver = null;
97
+ this.element.removeEventListener("load", this.#onLoad, true);
98
+ this.#lastColumns = 0;
99
+ }
100
+ /**
101
+ * Recomputes the column count and assigns every item to the shortest column.
102
+ * Runs automatically on connect, on resize, on item add/remove, and when a
103
+ * descendant resource loads (private — there is no public action; the observers
104
+ * and the capture-phase `load` listener drive it). Items are walked in DOM
105
+ * order; each lands in the column with the least accumulated height, which
106
+ * keeps the packing balanced without reordering the DOM.
107
+ */
108
+ #relayout() {
109
+ const items = this.itemTargets;
110
+ const columns = this.#columnCount();
111
+ const heights = new Array(columns).fill(0);
112
+ for (const item of items) {
113
+ let shortest = 0;
114
+ for (let col = 1; col < columns; col++) {
115
+ if ((heights[col] ?? 0) < (heights[shortest] ?? 0)) shortest = col;
116
+ }
117
+ item.setAttribute("data-column", String(shortest));
118
+ heights[shortest] = (heights[shortest] ?? 0) + item.getBoundingClientRect().height + this.gapValue;
119
+ }
120
+ this.element.style.setProperty(COLUMNS_PROPERTY, String(columns));
121
+ if (columns !== this.#lastColumns) {
122
+ this.#lastColumns = columns;
123
+ this.dispatch("layout", { detail: { columns } });
124
+ }
125
+ }
126
+ /**
127
+ * Derives how many columns fit: `floor((width + gap) / (minColumnWidth + gap))`,
128
+ * never fewer than one. When the width is unmeasurable (detached, or a layout
129
+ * engine that reports `0`), it falls back to a single column so every item still
130
+ * gets a valid `data-column`.
131
+ */
132
+ #columnCount() {
133
+ const width = this.element.getBoundingClientRect().width;
134
+ const denominator = this.minColumnWidthValue + this.gapValue;
135
+ if (width <= 0 || denominator <= 0) return 1;
136
+ return Math.max(1, Math.floor((width + this.gapValue) / denominator));
137
+ }
138
+ };
139
+
140
+ export { MasonryController };
141
+ //# sourceMappingURL=masonry_controller.js.map
142
+ //# sourceMappingURL=masonry_controller.js.map
@@ -0,0 +1,433 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ // src/controllers/menubar_controller.ts
4
+
5
+ // src/utils/escape_layer.ts
6
+ function claimsWhileFocusWithin(element) {
7
+ return () => {
8
+ const active = element.ownerDocument.activeElement;
9
+ return active === null || active === element.ownerDocument.body || element.contains(active);
10
+ };
11
+ }
12
+ var EscapeLayer = class _EscapeLayer {
13
+ static #registries = /* @__PURE__ */ new WeakMap();
14
+ #ownerDocument = null;
15
+ /** Dismissal callback while active; `null` when inactive. */
16
+ #onDismiss = null;
17
+ /** Live predicate deciding whether the layer claims a press; `null` = always. */
18
+ #claims = null;
19
+ /**
20
+ * Activates this layer at the top of its document's Escape stack, installing
21
+ * the document's shared resolver listener if this is its first layer.
22
+ * Re-activating an already-active layer moves it to the top.
23
+ */
24
+ activate(ownerDocument = document, options) {
25
+ this.deactivate();
26
+ let registry = _EscapeLayer.#registries.get(ownerDocument);
27
+ if (!registry) {
28
+ registry = _EscapeLayer.#createRegistry();
29
+ _EscapeLayer.#registries.set(ownerDocument, registry);
30
+ ownerDocument.addEventListener("keydown", registry.onKeydown);
31
+ }
32
+ registry.stack.push(this);
33
+ this.#ownerDocument = ownerDocument;
34
+ this.#onDismiss = options.onDismiss;
35
+ this.#claims = options.claims ?? null;
36
+ }
37
+ /**
38
+ * Removes this layer from its document's Escape stack, uninstalling the
39
+ * shared listener when the stack empties. Safe to call when inactive.
40
+ */
41
+ deactivate() {
42
+ const ownerDocument = this.#ownerDocument;
43
+ if (!ownerDocument) return;
44
+ const registry = _EscapeLayer.#registries.get(ownerDocument);
45
+ if (registry) {
46
+ const index = registry.stack.lastIndexOf(this);
47
+ if (index >= 0) registry.stack.splice(index, 1);
48
+ if (registry.stack.length === 0) {
49
+ ownerDocument.removeEventListener("keydown", registry.onKeydown);
50
+ _EscapeLayer.#registries.delete(ownerDocument);
51
+ }
52
+ }
53
+ this.#ownerDocument = null;
54
+ this.#onDismiss = null;
55
+ this.#claims = null;
56
+ }
57
+ /**
58
+ * Whether this active layer would own a press right now: it is the topmost
59
+ * layer whose {@link EscapeLayerOptions.claims} passes. Exposed for tests
60
+ * and diagnostics — production dismissal goes through the shared listener.
61
+ */
62
+ get ownsEscape() {
63
+ const ownerDocument = this.#ownerDocument;
64
+ if (!ownerDocument) return false;
65
+ const registry = _EscapeLayer.#registries.get(ownerDocument);
66
+ if (!registry) return false;
67
+ return _EscapeLayer.#resolveOwner(registry.stack) === this;
68
+ }
69
+ /** Builds a document's registry with its shared resolver listener. */
70
+ static #createRegistry() {
71
+ const registry = {
72
+ stack: [],
73
+ onKeydown: (event) => {
74
+ if (event.key !== "Escape" || event.defaultPrevented || event.isComposing) return;
75
+ const owner = _EscapeLayer.#resolveOwner(registry.stack);
76
+ if (!owner) return;
77
+ event.preventDefault();
78
+ owner.#onDismiss?.();
79
+ }
80
+ };
81
+ return registry;
82
+ }
83
+ /** The topmost stack layer whose claims predicate passes, or `null`. */
84
+ static #resolveOwner(stack) {
85
+ for (let index = stack.length - 1; index >= 0; index--) {
86
+ const layer = stack[index];
87
+ if (!layer) continue;
88
+ if (layer.#claims && !layer.#claims()) continue;
89
+ return layer;
90
+ }
91
+ return null;
92
+ }
93
+ };
94
+
95
+ // src/utils/roving_tabindex.ts
96
+ var RovingTabindex = class {
97
+ /** Returns the current ordered item elements; called on every operation. */
98
+ #getItems;
99
+ /**
100
+ * @param getItems - Returns the current ordered item elements. Called on every
101
+ * operation so the live target list is always used.
102
+ */
103
+ constructor(getItems) {
104
+ this.#getItems = getItems;
105
+ }
106
+ /** Index of the currently tabbable item (`tabindex="0"`), or `-1` if none. */
107
+ get activeIndex() {
108
+ return this.#getItems().findIndex((item) => item.tabIndex === 0);
109
+ }
110
+ /**
111
+ * Makes exactly the item at `index` tabbable (`tabindex="0"`) and removes every
112
+ * other item from the Tab sequence (`tabindex="-1"`). An out-of-range `index`
113
+ * (e.g. `-1`) leaves all items at `-1`, which a controller can use to express
114
+ * "nothing is currently tabbable".
115
+ *
116
+ * @param index - Position of the item to make tabbable.
117
+ * @param options - Pass `{ focus: true }` to also move DOM focus to that item.
118
+ */
119
+ setActive(index, { focus = false } = {}) {
120
+ const items = this.#getItems();
121
+ items.forEach((item, i) => {
122
+ item.tabIndex = i === index ? 0 : -1;
123
+ });
124
+ if (focus) items[index]?.focus();
125
+ }
126
+ };
127
+
128
+ // src/utils/safe_timeout.ts
129
+ var TimerRegistry = class {
130
+ /** Live timer ids that have not yet been cleared (or, for timeouts, fired). */
131
+ ids = /* @__PURE__ */ new Set();
132
+ /**
133
+ * Cancels a single tracked timer.
134
+ *
135
+ * No-ops if the id is unknown (already cleared, fired, or never owned by this
136
+ * registry), so callers can clear defensively without guarding.
137
+ */
138
+ clear(id) {
139
+ if (this.ids.delete(id)) {
140
+ this.cancel(id);
141
+ }
142
+ }
143
+ /**
144
+ * Cancels every tracked timer. Call this from a controller's `disconnect()`
145
+ * to guarantee no timer outlives the element.
146
+ */
147
+ clearAll() {
148
+ for (const id of this.ids) {
149
+ this.cancel(id);
150
+ }
151
+ this.ids.clear();
152
+ }
153
+ /** Number of timers currently tracked (pending). */
154
+ get size() {
155
+ return this.ids.size;
156
+ }
157
+ };
158
+ var SafeTimeout = class extends TimerRegistry {
159
+ /**
160
+ * Schedules `callback` after `delay` ms and returns the timer id.
161
+ *
162
+ * The id is removed from the registry automatically when the timeout fires,
163
+ * so {@link TimerRegistry.size | size} reflects only still-pending timers.
164
+ */
165
+ set(callback, delay) {
166
+ const id = this.schedule(() => {
167
+ this.ids.delete(id);
168
+ callback();
169
+ }, delay);
170
+ this.ids.add(id);
171
+ return id;
172
+ }
173
+ schedule(callback, delay) {
174
+ return window.setTimeout(callback, delay);
175
+ }
176
+ cancel(id) {
177
+ window.clearTimeout(id);
178
+ }
179
+ };
180
+
181
+ // src/controllers/menubar_controller.ts
182
+ var TYPEAHEAD_RESET_MS = 500;
183
+ var MenubarController = class extends Controller {
184
+ static targets = ["top", "menu", "item"];
185
+ static actions = ["activate", "onItemKeydown", "onTopKeydown", "toggle"];
186
+ /** Roving tabindex across the top-level menuitems (one Tab stop). */
187
+ #roving = new RovingTabindex(() => this.topTargets);
188
+ /** Typeahead buffer and its idle-reset timer (scoped to the open menu). */
189
+ #typeahead = "";
190
+ #typeaheadId = 0;
191
+ #timers = new SafeTimeout();
192
+ /** Escape-stack membership while a menu is open; the shared resolver dismisses via it. */
193
+ #escapeLayer = new EscapeLayer();
194
+ /** Establishes the single tab stop and the closed baseline. */
195
+ connect() {
196
+ const active = this.#roving.activeIndex;
197
+ this.#roving.setActive(active === -1 ? 0 : active);
198
+ this.#closeAllMenus();
199
+ document.addEventListener("click", this.#onOutsideClick);
200
+ }
201
+ /** Removes the document listener, stack membership, and any pending typeahead timer. */
202
+ disconnect() {
203
+ this.#escapeLayer.deactivate();
204
+ document.removeEventListener("click", this.#onOutsideClick);
205
+ this.#timers.clearAll();
206
+ }
207
+ /** Toggles a top item's menu. Bound via `data-action` (click on the top item). */
208
+ toggle(event) {
209
+ const top = event.currentTarget;
210
+ if (this.#isExpanded(top)) {
211
+ this.#closeMenu(top);
212
+ } else {
213
+ this.#openMenu(top, "first");
214
+ }
215
+ }
216
+ /** Keyboard handling while focus is on a top item. */
217
+ onTopKeydown(event) {
218
+ const tops = this.topTargets;
219
+ const index = tops.indexOf(event.currentTarget);
220
+ if (index === -1) return;
221
+ const length = tops.length;
222
+ const anyOpen = this.#isAnyOpen;
223
+ switch (event.key) {
224
+ case "ArrowRight":
225
+ event.preventDefault();
226
+ this.#gotoTop((index + 1) % length, anyOpen);
227
+ break;
228
+ case "ArrowLeft":
229
+ event.preventDefault();
230
+ this.#gotoTop((index - 1 + length) % length, anyOpen);
231
+ break;
232
+ case "ArrowDown":
233
+ event.preventDefault();
234
+ this.#openMenu(tops[index], "first");
235
+ break;
236
+ case "ArrowUp":
237
+ event.preventDefault();
238
+ this.#openMenu(tops[index], "last");
239
+ break;
240
+ case "Home":
241
+ event.preventDefault();
242
+ this.#gotoTop(0, anyOpen);
243
+ break;
244
+ case "End":
245
+ event.preventDefault();
246
+ this.#gotoTop(length - 1, anyOpen);
247
+ break;
248
+ }
249
+ }
250
+ /** Keyboard handling while focus is on a menu item. */
251
+ onItemKeydown(event) {
252
+ const item = event.currentTarget;
253
+ const menu = item.closest("[role='menu']");
254
+ if (!menu) return;
255
+ const items = this.#itemsIn(menu);
256
+ const index = items.indexOf(item);
257
+ const length = items.length;
258
+ switch (event.key) {
259
+ case "ArrowDown":
260
+ event.preventDefault();
261
+ this.#focusAt(items, (index + 1) % length);
262
+ break;
263
+ case "ArrowUp":
264
+ event.preventDefault();
265
+ this.#focusAt(items, (index - 1 + length) % length);
266
+ break;
267
+ case "Home":
268
+ event.preventDefault();
269
+ this.#focusAt(items, 0);
270
+ break;
271
+ case "End":
272
+ event.preventDefault();
273
+ this.#focusAt(items, length - 1);
274
+ break;
275
+ case "ArrowRight":
276
+ event.preventDefault();
277
+ this.#moveToAdjacentMenu(menu, 1);
278
+ break;
279
+ case "ArrowLeft":
280
+ event.preventDefault();
281
+ this.#moveToAdjacentMenu(menu, -1);
282
+ break;
283
+ case "Tab":
284
+ this.#closeAllMenus();
285
+ break;
286
+ default:
287
+ if (this.#isTypeaheadKey(event)) {
288
+ event.preventDefault();
289
+ this.#typeaheadTo(items, index, event.key);
290
+ }
291
+ break;
292
+ }
293
+ }
294
+ /** Closes the owning menu after an item is activated and refocuses its top. */
295
+ activate(event) {
296
+ const item = event.currentTarget;
297
+ const menu = item.closest("[role='menu']");
298
+ const top = this.#topFor(menu);
299
+ this.#closeAllMenus();
300
+ this.#focusTop(top);
301
+ }
302
+ /** Moves the roving focus to top `index`, opening its menu when one was open. */
303
+ #gotoTop(index, reopen) {
304
+ const top = this.topTargets[index];
305
+ if (!top) return;
306
+ if (reopen) {
307
+ this.#openMenu(top, "first");
308
+ } else {
309
+ this.#roving.setActive(index, { focus: true });
310
+ }
311
+ }
312
+ /** Opens `top`'s menu (closing others) and focuses its first/last item. */
313
+ #openMenu(top, focus) {
314
+ if (!top) return;
315
+ this.#closeAllMenus();
316
+ const menu = this.#menuFor(top);
317
+ if (!menu) return;
318
+ menu.hidden = false;
319
+ top.setAttribute("aria-expanded", "true");
320
+ this.#escapeLayer.activate(document, {
321
+ onDismiss: () => this.#dismissOpenMenu(),
322
+ claims: claimsWhileFocusWithin(this.element)
323
+ });
324
+ this.#roving.setActive(this.topTargets.indexOf(top));
325
+ const items = this.#itemsIn(menu);
326
+ this.#focusAt(items, focus === "first" ? 0 : items.length - 1);
327
+ }
328
+ /** Hides `top`'s menu and reflects the collapsed state. */
329
+ #closeMenu(top) {
330
+ if (!top) return;
331
+ const menu = this.#menuFor(top);
332
+ if (menu) menu.hidden = true;
333
+ top.setAttribute("aria-expanded", "false");
334
+ if (!this.#isAnyOpen) this.#escapeLayer.deactivate();
335
+ }
336
+ /** Closes every menu and resets the typeahead buffer. */
337
+ #closeAllMenus() {
338
+ for (const top of this.topTargets) this.#closeMenu(top);
339
+ this.#resetTypeahead();
340
+ }
341
+ /**
342
+ * Escape path, invoked by the shared resolver: pressed inside an open menu it
343
+ * closes that menu and returns focus to its top item (the APG behavior);
344
+ * pressed while focus is on a top item (or fell to the body) it closes every
345
+ * menu without moving focus.
346
+ */
347
+ #dismissOpenMenu() {
348
+ const active = document.activeElement;
349
+ const menu = this.menuTargets.find((candidate) => candidate.contains(active));
350
+ if (menu) {
351
+ const top = this.#topFor(menu);
352
+ this.#closeMenu(top);
353
+ this.#focusTop(top);
354
+ return;
355
+ }
356
+ this.#closeAllMenus();
357
+ }
358
+ /** Opens the menu of the top item `delta` steps from the one owning `menu`. */
359
+ #moveToAdjacentMenu(menu, delta) {
360
+ const top = this.#topFor(menu);
361
+ if (!top) return;
362
+ const tops = this.topTargets;
363
+ const next = (tops.indexOf(top) + delta + tops.length) % tops.length;
364
+ this.#openMenu(tops[next], "first");
365
+ }
366
+ /** Closes when a click lands outside the controller's element. */
367
+ #onOutsideClick = (event) => {
368
+ if (this.#isAnyOpen && !this.element.contains(event.target)) this.#closeAllMenus();
369
+ };
370
+ /** The menu element controlled by `top` (matched by `aria-controls`/`id`). */
371
+ #menuFor(top) {
372
+ const id = top.getAttribute("aria-controls");
373
+ return id ? this.menuTargets.find((menu) => menu.id === id) ?? null : null;
374
+ }
375
+ /** The top item that controls `menu` (reverse of `#menuFor`). */
376
+ #topFor(menu) {
377
+ if (!menu) return null;
378
+ return this.topTargets.find((top) => top.getAttribute("aria-controls") === menu.id) ?? null;
379
+ }
380
+ /** The item targets that live inside `menu`, in DOM order. */
381
+ #itemsIn(menu) {
382
+ return this.itemTargets.filter((item) => menu.contains(item));
383
+ }
384
+ /** Moves DOM focus to the item at `index` (no-op if out of range). */
385
+ #focusAt(items, index) {
386
+ items[index]?.focus();
387
+ }
388
+ /** Returns roving focus to a top item (and makes it the single tab stop). */
389
+ #focusTop(top) {
390
+ if (!top) return;
391
+ this.#roving.setActive(this.topTargets.indexOf(top), { focus: true });
392
+ }
393
+ /** Whether `top`'s menu is currently expanded. */
394
+ #isExpanded(top) {
395
+ return top.getAttribute("aria-expanded") === "true";
396
+ }
397
+ /** Whether any top item's menu is currently open. */
398
+ get #isAnyOpen() {
399
+ return this.topTargets.some((top) => this.#isExpanded(top));
400
+ }
401
+ /**
402
+ * Whether the event is a single printable character usable for typeahead.
403
+ * `Space` is excluded: on a `<button>` menuitem it natively activates the item
404
+ * (Enter/Space → click), so swallowing it for typeahead would break activation.
405
+ */
406
+ #isTypeaheadKey(event) {
407
+ return event.key.length === 1 && event.key !== " " && !event.ctrlKey && !event.metaKey && !event.altKey;
408
+ }
409
+ /** Advances focus to the next item in `items` matching the accumulated buffer. */
410
+ #typeaheadTo(items, current, key) {
411
+ this.#typeahead += key.toLowerCase();
412
+ this.#timers.clear(this.#typeaheadId);
413
+ this.#typeaheadId = this.#timers.set(() => this.#resetTypeahead(), TYPEAHEAD_RESET_MS);
414
+ const count = items.length;
415
+ for (let step = 1; step <= count; step++) {
416
+ const candidate = items[(current + step) % count];
417
+ const label = (candidate?.textContent ?? "").trim().toLowerCase();
418
+ if (label.startsWith(this.#typeahead)) {
419
+ candidate?.focus();
420
+ return;
421
+ }
422
+ }
423
+ }
424
+ /** Clears the typeahead buffer and its timer. */
425
+ #resetTypeahead() {
426
+ this.#typeahead = "";
427
+ this.#timers.clear(this.#typeaheadId);
428
+ }
429
+ };
430
+
431
+ export { MenubarController };
432
+ //# sourceMappingURL=menubar_controller.js.map
433
+ //# sourceMappingURL=menubar_controller.js.map