stimeo-ui 0.2.1 → 0.3.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 (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +104 -0
  3. data/dist/controllers/accordion_controller.js +10 -0
  4. data/dist/controllers/breadcrumb_controller.js +225 -13
  5. data/dist/controllers/calendar_controller.js +89 -22
  6. data/dist/controllers/carousel_controller.js +47 -6
  7. data/dist/controllers/collapsible_controller.js +2 -2
  8. data/dist/controllers/color_picker_controller.js +46 -7
  9. data/dist/controllers/combobox_controller.js +162 -23
  10. data/dist/controllers/command_palette_controller.js +194 -17
  11. data/dist/controllers/context_menu_controller.js +32 -10
  12. data/dist/controllers/data_grid_controller.js +82 -4
  13. data/dist/controllers/date_range_picker_controller.js +27 -3
  14. data/dist/controllers/editable_controller.js +1 -0
  15. data/dist/controllers/form_validation_controller.js +1 -1
  16. data/dist/controllers/intersection_controller.js +36 -11
  17. data/dist/controllers/lazy_frame_controller.js +31 -10
  18. data/dist/controllers/listbox_controller.js +257 -53
  19. data/dist/controllers/local_time_controller.js +2 -2
  20. data/dist/controllers/menu_controller.js +104 -17
  21. data/dist/controllers/menubar_controller.js +415 -63
  22. data/dist/controllers/multi_select_controller.js +312 -29
  23. data/dist/controllers/navigation_menu_controller.js +154 -27
  24. data/dist/controllers/number_input_controller.js +7 -0
  25. data/dist/controllers/otp_controller.js +18 -1
  26. data/dist/controllers/overflow_indicator_controller.js +81 -13
  27. data/dist/controllers/overflow_menu_controller.js +381 -57
  28. data/dist/controllers/pagination_controller.js +163 -32
  29. data/dist/controllers/persist_controller.js +6 -6
  30. data/dist/controllers/pointer_drag_controller.js +9 -1
  31. data/dist/controllers/popover_controller.js +2 -2
  32. data/dist/controllers/radio_group_controller.js +22 -3
  33. data/dist/controllers/range_slider_controller.js +32 -6
  34. data/dist/controllers/rating_controller.js +16 -2
  35. data/dist/controllers/read_more_controller.js +63 -19
  36. data/dist/controllers/resizable_controller.js +65 -1
  37. data/dist/controllers/roving_controller.js +17 -2
  38. data/dist/controllers/scroll_area_controller.js +86 -12
  39. data/dist/controllers/scroll_restore_controller.js +1 -1
  40. data/dist/controllers/scroll_visibility_controller.js +33 -3
  41. data/dist/controllers/scrollspy_controller.js +346 -73
  42. data/dist/controllers/separator_controller.js +9 -0
  43. data/dist/controllers/skeleton_controller.js +1 -1
  44. data/dist/controllers/slider_controller.js +32 -6
  45. data/dist/controllers/sortable_controller.js +34 -3
  46. data/dist/controllers/spinner_controller.js +1 -1
  47. data/dist/controllers/stick_to_bottom_controller.js +2 -1
  48. data/dist/controllers/sticky_observer_controller.js +32 -11
  49. data/dist/controllers/switch_controller.js +1 -0
  50. data/dist/controllers/tabs_controller.js +26 -3
  51. data/dist/controllers/tags_input_controller.js +22 -2
  52. data/dist/controllers/theme_controller.js +22 -3
  53. data/dist/controllers/time_picker_controller.js +20 -1
  54. data/dist/controllers/toast_controller.js +4 -5
  55. data/dist/controllers/toggle_group_controller.js +23 -2
  56. data/dist/controllers/toolbar_controller.js +230 -31
  57. data/dist/controllers/tree_view_controller.js +467 -51
  58. data/dist/index.js +3514 -689
  59. data/lib/stimeo/ui/version.rb +2 -3
  60. metadata +2 -2
@@ -13,6 +13,7 @@ var IntersectionWatcher = class {
13
13
  #onEntries;
14
14
  #observer = null;
15
15
  #active = false;
16
+ #usingPlatformDefaults = false;
16
17
  constructor(onEntries) {
17
18
  this.#onEntries = onEntries;
18
19
  }
@@ -20,15 +21,22 @@ var IntersectionWatcher = class {
20
21
  get active() {
21
22
  return this.#active;
22
23
  }
24
+ /** Whether the live observer discarded configured options after construction failed. */
25
+ get usingPlatformDefaults() {
26
+ return this.#usingPlatformDefaults;
27
+ }
23
28
  /**
24
29
  * (Re)creates the observer and observes `targets`. Returns `false` — leaving
25
30
  * the watcher inert — without `IntersectionObserver` support (very old
26
31
  * browsers; the caller's no-JS fallback stays in charge) or with no targets.
32
+ * If initial construction with the configured options fails, the watcher
33
+ * warns and retries once with the same root and platform defaults.
27
34
  *
28
- * @throws Whatever the platform throws for an invalid `rootMargin`/`threshold`
29
- * or a failing `observe()`. The exception is passed through unchanged, but
30
- * the watcher rolls back first: every target observed so far is released and
31
- * `active` stays `false`, so a caller that retries starts from a clean slate.
35
+ * @throws The fallback constructor error if both construction attempts fail,
36
+ * or whatever the platform throws from `observe()`. The exception is passed
37
+ * through unchanged, but the watcher rolls back first: every target observed
38
+ * so far is released and `active` stays `false`, so a caller that retries
39
+ * starts from a clean slate.
32
40
  */
33
41
  start(targets, options = {}) {
34
42
  this.stop();
@@ -38,12 +46,23 @@ var IntersectionWatcher = class {
38
46
  const root = "root" in options ? options.root ?? null : options.rootSelector ? document.querySelector(options.rootSelector) : null;
39
47
  let observer = null;
40
48
  try {
41
- observer = new IntersectionObserver(
42
- (entries) => {
43
- if (this.#active && this.#observer === observer) this.#onEntries(entries);
44
- },
45
- { root, rootMargin: options.rootMargin, threshold: options.threshold }
46
- );
49
+ const onEntries = (entries) => {
50
+ if (this.#active && this.#observer === observer) this.#onEntries(entries);
51
+ };
52
+ try {
53
+ observer = new IntersectionObserver(onEntries, {
54
+ root,
55
+ rootMargin: options.rootMargin,
56
+ threshold: options.threshold
57
+ });
58
+ } catch (error) {
59
+ console.warn(
60
+ "Stimeo UI: IntersectionObserver could not be constructed with the configured options; retrying with platform defaults.",
61
+ error
62
+ );
63
+ observer = new IntersectionObserver(onEntries, { root });
64
+ this.#usingPlatformDefaults = true;
65
+ }
47
66
  for (const target of list) observer.observe(target);
48
67
  this.#observer = observer;
49
68
  this.#active = true;
@@ -52,6 +71,7 @@ var IntersectionWatcher = class {
52
71
  observer?.disconnect();
53
72
  this.#observer = null;
54
73
  this.#active = false;
74
+ this.#usingPlatformDefaults = false;
55
75
  throw error;
56
76
  }
57
77
  }
@@ -78,6 +98,7 @@ var IntersectionWatcher = class {
78
98
  this.#active = false;
79
99
  this.#observer?.disconnect();
80
100
  this.#observer = null;
101
+ this.#usingPlatformDefaults = false;
81
102
  }
82
103
  };
83
104
 
@@ -96,6 +117,8 @@ var IntersectionController = class extends Controller {
96
117
  static events = ["enter", "exit", "change", "passed"];
97
118
  /** Shared IO plumbing (support guard, root resolution, active guard, re-arm). */
98
119
  #watcher = new IntersectionWatcher((entries) => this.#onIntersect(entries));
120
+ /** Threshold actually installed in the live observer (0 after option fallback). */
121
+ #effectiveThreshold = 0;
99
122
  /** Bumped by `refresh()`: an in-flight batch becomes stale and stops. */
100
123
  #generation = 0;
101
124
  #onIntersect(entries) {
@@ -103,7 +126,7 @@ var IntersectionController = class extends Controller {
103
126
  for (const entry of entries) {
104
127
  if (!this.#watcher.active || this.#generation !== generation) return;
105
128
  const ratio = entry.intersectionRatio;
106
- const threshold = this.#clampedThreshold();
129
+ const threshold = this.#effectiveThreshold;
107
130
  const intersecting = threshold > 0 ? entry.isIntersecting && ratio >= threshold - RATIO_EPSILON : entry.isIntersecting;
108
131
  this.element.style.setProperty(RATIO_PROPERTY, String(ratio));
109
132
  this.dispatch("change", { detail: { intersecting, ratio } });
@@ -113,11 +136,13 @@ var IntersectionController = class extends Controller {
113
136
  }
114
137
  connect() {
115
138
  if (this.onceValue && this.element.getAttribute("data-intersecting") === "true") return;
139
+ this.#effectiveThreshold = this.#clampedThreshold();
116
140
  this.#watcher.start(this.element, {
117
141
  rootSelector: this.rootSelectorValue,
118
142
  rootMargin: this.rootMarginValue,
119
143
  threshold: this.#thresholds()
120
144
  });
145
+ if (this.#watcher.usingPlatformDefaults) this.#effectiveThreshold = 0;
121
146
  }
122
147
  disconnect() {
123
148
  this.#watcher.stop();
@@ -7,6 +7,7 @@ var IntersectionWatcher = class {
7
7
  #onEntries;
8
8
  #observer = null;
9
9
  #active = false;
10
+ #usingPlatformDefaults = false;
10
11
  constructor(onEntries) {
11
12
  this.#onEntries = onEntries;
12
13
  }
@@ -14,15 +15,22 @@ var IntersectionWatcher = class {
14
15
  get active() {
15
16
  return this.#active;
16
17
  }
18
+ /** Whether the live observer discarded configured options after construction failed. */
19
+ get usingPlatformDefaults() {
20
+ return this.#usingPlatformDefaults;
21
+ }
17
22
  /**
18
23
  * (Re)creates the observer and observes `targets`. Returns `false` — leaving
19
24
  * the watcher inert — without `IntersectionObserver` support (very old
20
25
  * browsers; the caller's no-JS fallback stays in charge) or with no targets.
26
+ * If initial construction with the configured options fails, the watcher
27
+ * warns and retries once with the same root and platform defaults.
21
28
  *
22
- * @throws Whatever the platform throws for an invalid `rootMargin`/`threshold`
23
- * or a failing `observe()`. The exception is passed through unchanged, but
24
- * the watcher rolls back first: every target observed so far is released and
25
- * `active` stays `false`, so a caller that retries starts from a clean slate.
29
+ * @throws The fallback constructor error if both construction attempts fail,
30
+ * or whatever the platform throws from `observe()`. The exception is passed
31
+ * through unchanged, but the watcher rolls back first: every target observed
32
+ * so far is released and `active` stays `false`, so a caller that retries
33
+ * starts from a clean slate.
26
34
  */
27
35
  start(targets, options = {}) {
28
36
  this.stop();
@@ -32,12 +40,23 @@ var IntersectionWatcher = class {
32
40
  const root = "root" in options ? options.root ?? null : options.rootSelector ? document.querySelector(options.rootSelector) : null;
33
41
  let observer = null;
34
42
  try {
35
- observer = new IntersectionObserver(
36
- (entries) => {
37
- if (this.#active && this.#observer === observer) this.#onEntries(entries);
38
- },
39
- { root, rootMargin: options.rootMargin, threshold: options.threshold }
40
- );
43
+ const onEntries = (entries) => {
44
+ if (this.#active && this.#observer === observer) this.#onEntries(entries);
45
+ };
46
+ try {
47
+ observer = new IntersectionObserver(onEntries, {
48
+ root,
49
+ rootMargin: options.rootMargin,
50
+ threshold: options.threshold
51
+ });
52
+ } catch (error) {
53
+ console.warn(
54
+ "Stimeo UI: IntersectionObserver could not be constructed with the configured options; retrying with platform defaults.",
55
+ error
56
+ );
57
+ observer = new IntersectionObserver(onEntries, { root });
58
+ this.#usingPlatformDefaults = true;
59
+ }
41
60
  for (const target of list) observer.observe(target);
42
61
  this.#observer = observer;
43
62
  this.#active = true;
@@ -46,6 +65,7 @@ var IntersectionWatcher = class {
46
65
  observer?.disconnect();
47
66
  this.#observer = null;
48
67
  this.#active = false;
68
+ this.#usingPlatformDefaults = false;
49
69
  throw error;
50
70
  }
51
71
  }
@@ -72,6 +92,7 @@ var IntersectionWatcher = class {
72
92
  this.#active = false;
73
93
  this.#observer?.disconnect();
74
94
  this.#observer = null;
95
+ this.#usingPlatformDefaults = false;
75
96
  }
76
97
  };
77
98
 
@@ -2,6 +2,45 @@ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  // src/controllers/listbox_controller.ts
4
4
 
5
+ // src/utils/arrow_step.ts
6
+ function isReservedArrowChord(event, allow = []) {
7
+ if (!event.key.startsWith("Arrow")) return false;
8
+ return event.altKey && !allow.includes("alt") || event.ctrlKey && !allow.includes("ctrl") || event.metaKey && !allow.includes("meta") || event.shiftKey && !allow.includes("shift");
9
+ }
10
+
11
+ // src/utils/microtask_coalescer.ts
12
+ var MicrotaskCoalescer = class {
13
+ #run;
14
+ #queued = false;
15
+ #active = false;
16
+ #generation = 0;
17
+ /** @param run - the single reconciliation pass, invoked at most once per batch. */
18
+ constructor(run) {
19
+ this.#run = run;
20
+ }
21
+ /** Opens the window in which {@link schedule} is honoured; call from `connect()`. */
22
+ activate() {
23
+ this.#active = true;
24
+ }
25
+ /** Closes the window and drops any pending pass; call from `disconnect()`. */
26
+ cancel() {
27
+ this.#active = false;
28
+ this.#queued = false;
29
+ this.#generation += 1;
30
+ }
31
+ /** Requests one pass after the batch settles. Idempotent; inert outside the window. */
32
+ schedule() {
33
+ if (!this.#active || this.#queued) return;
34
+ this.#queued = true;
35
+ const generation = this.#generation;
36
+ queueMicrotask(() => {
37
+ if (generation !== this.#generation || !this.#queued || !this.#active) return;
38
+ this.#queued = false;
39
+ this.#run();
40
+ });
41
+ }
42
+ };
43
+
5
44
  // src/utils/option_scroll.ts
6
45
  function scrollOptionIntoView(list, option) {
7
46
  if (list.scrollHeight <= list.clientHeight) return;
@@ -67,27 +106,135 @@ var SafeTimeout = class extends TimerRegistry {
67
106
  }
68
107
  };
69
108
 
109
+ // src/utils/typeahead.ts
110
+ var TYPEAHEAD_RESET_MS = 500;
111
+ var Typeahead = class {
112
+ /** Timer registry for the pending idle reset; private so `reset()` is the only exit. */
113
+ #timers = new SafeTimeout();
114
+ /** Idle window before the query resets, in milliseconds. */
115
+ #resetMs;
116
+ /** The accumulated lowercase query, empty when idle. */
117
+ #query = "";
118
+ /** Id of the pending reset timer, `0` when none is scheduled. */
119
+ #timerId = 0;
120
+ /** @param options - Overrides for the idle window. */
121
+ constructor({ resetMs = TYPEAHEAD_RESET_MS } = {}) {
122
+ this.#resetMs = resetMs;
123
+ }
124
+ /** The query a search would currently run with; empty while idle. */
125
+ get query() {
126
+ return this.#query;
127
+ }
128
+ /**
129
+ * Folds `key` into the query, restarts the idle window, and returns the query to
130
+ * search with. A repeated character collapses the query to that one character.
131
+ */
132
+ push(key) {
133
+ const char = key.toLowerCase();
134
+ const repeated = this.#query.length > 0 && [...this.#query].every((c) => c === char);
135
+ this.#query = repeated ? char : this.#query + char;
136
+ this.#timers.clear(this.#timerId);
137
+ this.#timerId = this.#timers.set(() => this.reset(), this.#resetMs);
138
+ return this.#query;
139
+ }
140
+ /** Clears the query and cancels the pending idle reset. */
141
+ reset() {
142
+ this.#query = "";
143
+ this.#timers.clear(this.#timerId);
144
+ this.#timerId = 0;
145
+ }
146
+ };
147
+ function isTypeaheadKey(event) {
148
+ return event.key.length === 1 && event.key !== " " && !event.ctrlKey && !event.metaKey && !event.altKey && !event.isComposing;
149
+ }
150
+ function typeaheadLabel(element, fallbackText) {
151
+ const label = element.getAttribute("aria-label")?.trim();
152
+ if (label) return label.toLowerCase();
153
+ const text = element.textContent ?? "";
154
+ return text.trim().toLowerCase();
155
+ }
156
+ function findTypeaheadMatch(items, from, query, label = (item) => typeaheadLabel(item)) {
157
+ if (query === "") return -1;
158
+ const count = items.length;
159
+ for (let step = 1; step <= count; step += 1) {
160
+ const index = ((from + step) % count + count) % count;
161
+ const candidate = items[index];
162
+ if (candidate && label(candidate).startsWith(query)) return index;
163
+ }
164
+ return -1;
165
+ }
166
+
70
167
  // src/controllers/listbox_controller.ts
71
- var TYPEAHEAD_TIMEOUT = 500;
72
168
  var ListboxController = class extends Controller {
73
169
  static targets = ["trigger", "value", "list", "option", "field"];
74
170
  static actions = ["close", "onTriggerKeydown", "open", "select", "toggle"];
75
171
  static events = ["change"];
76
- /** Index of the active option, or -1 when none is active. */
77
- #activeIndex = -1;
78
- /** Accumulated typeahead query, reset after {@link TYPEAHEAD_TIMEOUT} ms. */
79
- #typeahead = "";
80
- #typeaheadTimer = 0;
81
- #timers = new SafeTimeout();
82
- /** Starts closed and registers the outside-click listener. */
172
+ /** Stable ID of the active option; DOM targets are resolved afresh before use. */
173
+ #activeId = null;
174
+ /** Target ID order captured while an option is active, used only for removal fallback. */
175
+ #activeOrder = [];
176
+ #connected = false;
177
+ /** Collapses one mutation batch of target callbacks into a single pass. */
178
+ #reconcile = new MicrotaskCoalescer(() => this.#reconcileActive());
179
+ /** Accumulated typeahead query and its idle-reset timer. */
180
+ #typeahead = new Typeahead();
181
+ /** Establishes the ARIA baseline, starts closed, and listens for outside clicks. */
83
182
  connect() {
183
+ this.#normalizeSelection();
84
184
  this.close();
85
- document.addEventListener("click", this.#onOutsideClick);
185
+ document.addEventListener("click", this.#onOutsideClick, true);
186
+ this.#connected = true;
187
+ this.#reconcile.activate();
188
+ }
189
+ /**
190
+ * Establishes an inactive baseline for a late option, re-resolves active
191
+ * identity, and re-applies the selection baseline.
192
+ */
193
+ optionTargetConnected(option) {
194
+ option.removeAttribute("data-active");
195
+ if (!this.#connected) return;
196
+ this.#normalizeSelection();
197
+ this.#queueOptionReconciliation();
198
+ }
199
+ /** Removes controller-owned active state and reconciles the surviving targets. */
200
+ optionTargetDisconnected(option) {
201
+ option.removeAttribute("data-active");
202
+ if (this.#connected) this.#queueOptionReconciliation();
203
+ }
204
+ /**
205
+ * Brings the authored DOM to the shape the APG requires, and derives the state
206
+ * that follows from the initial selection.
207
+ *
208
+ * Three things happen, and only these three — which option is chosen is the
209
+ * author's, and is never changed:
210
+ *
211
+ * 1. Every option gets an explicit value. An absent `aria-selected` means "not
212
+ * selectable" in ARIA, so a forgotten attribute hides a selectable option
213
+ * from assistive technology.
214
+ * 2. At most one stays `true`. The first in DOM order wins, since that is the
215
+ * only deterministic reading of "which one did the author mean".
216
+ * 3. The trigger label and the hidden field are derived from that selection.
217
+ * Without this the widget announces a choice it does not submit: the popup
218
+ * says "Banana", the trigger still says "Choose…", and the form posts "".
219
+ *
220
+ * No `change` fires — nothing changed, this is the initial state being told
221
+ * properly. The scan is the `option` target set: a `role="option"` without the
222
+ * target is outside the contract and is neither counted nor written.
223
+ */
224
+ #normalizeSelection() {
225
+ const options = this.optionTargets;
226
+ const selected = options.find((option) => option.getAttribute("aria-selected") === "true");
227
+ for (const option of options) {
228
+ option.setAttribute("aria-selected", option === selected ? "true" : "false");
229
+ }
230
+ if (selected) this.#applySelection(selected);
86
231
  }
87
232
  /** Removes the document listener and clears the typeahead timer. */
88
233
  disconnect() {
89
- document.removeEventListener("click", this.#onOutsideClick);
90
- this.#timers.clearAll();
234
+ this.#connected = false;
235
+ this.#reconcile.cancel();
236
+ document.removeEventListener("click", this.#onOutsideClick, true);
237
+ this.#typeahead.reset();
91
238
  }
92
239
  /**
93
240
  * Toggles the list on a real mouse click. Keyboard activation of the
@@ -102,9 +249,14 @@ var ListboxController = class extends Controller {
102
249
  this.close();
103
250
  }
104
251
  }
105
- /** Routes trigger keyboard interaction per the APG select-only model. */
252
+ /** Yields claimed keys; otherwise routes the APG select-only keyboard model. */
106
253
  onTriggerKeydown(event) {
107
- const length = this.optionTargets.length;
254
+ if (event.defaultPrevented) return;
255
+ if (isReservedArrowChord(event)) return;
256
+ if (!this.#isClosed) this.#reconcileActive();
257
+ const options = this.optionTargets;
258
+ const length = options.length;
259
+ const activeIndex = this.#findActiveIndex(options);
108
260
  if (this.#isClosed) {
109
261
  switch (event.key) {
110
262
  case "Enter":
@@ -123,13 +275,11 @@ var ListboxController = class extends Controller {
123
275
  switch (event.key) {
124
276
  case "ArrowDown":
125
277
  event.preventDefault();
126
- this.#setActive(this.#activeIndex < 0 ? 0 : (this.#activeIndex + 1) % length);
278
+ this.#setActive(activeIndex < 0 ? 0 : (activeIndex + 1) % length);
127
279
  break;
128
280
  case "ArrowUp":
129
281
  event.preventDefault();
130
- this.#setActive(
131
- this.#activeIndex < 0 ? length - 1 : (this.#activeIndex - 1 + length) % length
132
- );
282
+ this.#setActive(activeIndex < 0 ? length - 1 : (activeIndex - 1 + length) % length);
133
283
  break;
134
284
  case "Home":
135
285
  event.preventDefault();
@@ -145,7 +295,7 @@ var ListboxController = class extends Controller {
145
295
  this.#commitActive();
146
296
  break;
147
297
  case "Escape":
148
- if (event.defaultPrevented || event.isComposing) break;
298
+ if (event.isComposing) break;
149
299
  event.preventDefault();
150
300
  this.close();
151
301
  this.triggerTarget.focus();
@@ -154,9 +304,9 @@ var ListboxController = class extends Controller {
154
304
  this.close();
155
305
  break;
156
306
  default:
157
- if (this.#isPrintable(event)) {
307
+ if (isTypeaheadKey(event)) {
158
308
  event.preventDefault();
159
- this.#typeaheadTo(event.key);
309
+ this.#typeaheadTo(options, activeIndex, event.key);
160
310
  }
161
311
  break;
162
312
  }
@@ -164,7 +314,7 @@ var ListboxController = class extends Controller {
164
314
  /** Selects the clicked option and closes, returning focus to the trigger. */
165
315
  select(event) {
166
316
  const option = event.currentTarget.closest('[role="option"]');
167
- if (!option) return;
317
+ if (!option || !this.optionTargets.includes(option)) return;
168
318
  this.#selectOption(option);
169
319
  this.close();
170
320
  this.triggerTarget.focus();
@@ -189,28 +339,41 @@ var ListboxController = class extends Controller {
189
339
  this.listTarget.hidden = true;
190
340
  this.triggerTarget.setAttribute("aria-expanded", "false");
191
341
  this.#setActive(-1);
192
- this.#resetTypeahead();
342
+ this.#typeahead.reset();
193
343
  }
194
344
  /** Commits the active option (keyboard) and closes, returning focus. */
195
345
  #commitActive() {
196
- const option = this.#activeIndex < 0 ? void 0 : this.optionTargets[this.#activeIndex];
346
+ this.#reconcileActive();
347
+ const options = this.optionTargets;
348
+ const activeIndex = this.#findActiveIndex(options);
349
+ const option = activeIndex < 0 ? void 0 : options[activeIndex];
197
350
  if (option) this.#selectOption(option);
198
351
  this.close();
199
352
  this.triggerTarget.focus();
200
353
  }
201
354
  /** Applies selection: `aria-selected`, trigger label, hidden field, `change`. */
202
355
  #selectOption(option) {
356
+ const { value, fieldChanged } = this.#applySelection(option);
357
+ if (fieldChanged) {
358
+ this.fieldTarget.dispatchEvent(new Event("change", { bubbles: true }));
359
+ }
360
+ this.dispatch("change", { detail: { value, option } });
361
+ }
362
+ /**
363
+ * Writes `option` into `aria-selected`, the trigger label and the hidden field.
364
+ * Emits nothing — {@link #normalizeSelection} reuses this at connect, where the
365
+ * state is being described rather than changed.
366
+ */
367
+ #applySelection(option) {
203
368
  for (const candidate of this.optionTargets) {
204
369
  candidate.setAttribute("aria-selected", candidate === option ? "true" : "false");
205
370
  }
206
371
  const label = (option.textContent ?? "").trim();
207
372
  const value = option.dataset.value ?? label;
208
373
  if (this.hasValueTarget) this.valueTarget.textContent = label;
209
- if (this.hasFieldTarget && this.fieldTarget.value !== value) {
210
- this.fieldTarget.value = value;
211
- this.fieldTarget.dispatchEvent(new Event("change", { bubbles: true }));
212
- }
213
- this.dispatch("change", { detail: { value, option } });
374
+ const fieldChanged = this.hasFieldTarget && this.fieldTarget.value !== value;
375
+ if (fieldChanged) this.fieldTarget.value = value;
376
+ return { value, fieldChanged };
214
377
  }
215
378
  /**
216
379
  * Marks the option at `index` active via `data-active` and the trigger's
@@ -218,47 +381,88 @@ var ListboxController = class extends Controller {
218
381
  * set to empty, per the APG).
219
382
  */
220
383
  #setActive(index) {
221
- this.#activeIndex = index;
222
- const active = index < 0 ? null : this.optionTargets[index];
223
- for (const option of this.optionTargets) {
384
+ const options = this.optionTargets;
385
+ const active = index < 0 ? null : options[index] ?? null;
386
+ for (const option of options) {
387
+ const marked = option.hasAttribute("data-active");
224
388
  if (option === active) {
225
- option.setAttribute("data-active", "");
226
- } else {
389
+ if (!marked) option.setAttribute("data-active", "");
390
+ } else if (marked) {
227
391
  option.removeAttribute("data-active");
228
392
  }
229
393
  }
230
394
  if (active?.id) {
395
+ this.#activeId = active.id;
231
396
  this.triggerTarget.setAttribute("aria-activedescendant", active.id);
232
397
  } else {
398
+ this.#activeId = null;
233
399
  this.triggerTarget.removeAttribute("aria-activedescendant");
234
400
  }
401
+ this.#activeOrder = active ? options.map((option) => option.id).filter(Boolean) : [];
235
402
  if (active && this.hasListTarget) scrollOptionIntoView(this.listTarget, active);
236
403
  }
237
- /** Appends a character to the typeahead query and activates the first match. */
238
- #typeaheadTo(char) {
239
- this.#timers.clear(this.#typeaheadTimer);
240
- this.#typeahead += char.toLowerCase();
241
- this.#typeaheadTimer = this.#timers.set(() => {
242
- this.#typeahead = "";
243
- }, TYPEAHEAD_TIMEOUT);
244
- const index = this.optionTargets.findIndex(
245
- (option) => (option.textContent ?? "").trim().toLowerCase().startsWith(this.#typeahead)
246
- );
247
- if (index !== -1) this.#setActive(index);
404
+ /** Resolves active state against the current target collection. */
405
+ #reconcileActive() {
406
+ if (!this.hasTriggerTarget || this.#isClosed) {
407
+ if (this.hasTriggerTarget) this.#setActive(-1);
408
+ return;
409
+ }
410
+ const options = this.optionTargets;
411
+ const currentIndex = this.#findActiveIndex(options);
412
+ if (currentIndex >= 0) {
413
+ const active = options[currentIndex] ?? null;
414
+ const marked = options.filter((option) => option.hasAttribute("data-active"));
415
+ const stateMatches = this.#activeId === (active?.id || null) && marked.length === 1 && marked[0] === active && this.triggerTarget.getAttribute("aria-activedescendant") === (active?.id || null);
416
+ if (stateMatches) {
417
+ this.#activeOrder = options.map((option) => option.id).filter(Boolean);
418
+ } else {
419
+ this.#setActive(currentIndex);
420
+ }
421
+ return;
422
+ }
423
+ const activeId = this.triggerTarget.getAttribute("aria-activedescendant") ?? this.#activeId;
424
+ this.#setActive(activeId ? this.#findFallbackIndex(options, activeId) : -1);
425
+ }
426
+ /** Finds the live target carrying the stable ID, or the active marker for an ID-less option. */
427
+ #findActiveIndex(options) {
428
+ const activeId = (this.hasTriggerTarget ? this.triggerTarget.getAttribute("aria-activedescendant") : null) ?? this.#activeId;
429
+ if (activeId) return options.findIndex((option) => option.id === activeId);
430
+ return options.findIndex((option) => option.hasAttribute("data-active"));
248
431
  }
249
- /** Clears the typeahead query and its pending reset timer. */
250
- #resetTypeahead() {
251
- this.#timers.clear(this.#typeaheadTimer);
252
- this.#typeahead = "";
432
+ /** Chooses a surviving former successor, then a former predecessor. */
433
+ #findFallbackIndex(options, activeId) {
434
+ const oldIndex = this.#activeOrder.indexOf(activeId);
435
+ if (oldIndex < 0) return -1;
436
+ const indexesById = new Map(options.map((option, index) => [option.id, index]));
437
+ for (let index = oldIndex + 1; index < this.#activeOrder.length; index += 1) {
438
+ const fallback = indexesById.get(this.#activeOrder[index] ?? "");
439
+ if (fallback !== void 0) return fallback;
440
+ }
441
+ for (let index = oldIndex - 1; index >= 0; index -= 1) {
442
+ const fallback = indexesById.get(this.#activeOrder[index] ?? "");
443
+ if (fallback !== void 0) return fallback;
444
+ }
445
+ return -1;
253
446
  }
254
- /** Closes the list when a click lands outside the controller element. */
447
+ /** Coalesces all target callbacks from one MutationObserver batch. */
448
+ #queueOptionReconciliation() {
449
+ this.#reconcile.schedule();
450
+ }
451
+ /**
452
+ * Advances the typeahead query and activates the next matching option.
453
+ *
454
+ * The search resumes just after the active option so repeating a character
455
+ * cycles through the options starting with it, rather than re-activating the
456
+ * same first match on every press.
457
+ */
458
+ #typeaheadTo(options, activeIndex, char) {
459
+ const index = findTypeaheadMatch(options, activeIndex, this.#typeahead.push(char));
460
+ if (index !== -1) this.#setActive(index);
461
+ }
462
+ /** Closes on an outside click before an inside handler can detach its target. */
255
463
  #onOutsideClick = (event) => {
256
464
  if (!this.#isClosed && !this.element.contains(event.target)) this.close();
257
465
  };
258
- /** Whether `event.key` is a single printable character (no modifier chord). */
259
- #isPrintable(event) {
260
- return event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey;
261
- }
262
466
  /** Whether the list is currently hidden. */
263
467
  get #isClosed() {
264
468
  return !this.hasListTarget || this.listTarget.hidden !== false;
@@ -32,8 +32,8 @@ var LocalTimeController = class extends Controller {
32
32
  return Number.isNaN(ms) ? null : new Date(ms);
33
33
  }
34
34
  /**
35
- * Reads a timezone-less date-time as UTC — the documented input contract
36
- * since `Date.parse` would otherwise interpret e.g. `"2026-06-08T12:30:00"` in
35
+ * Reads a timezone-less date-time as UTC — the input contract of this
36
+ * controller — since `Date.parse` would otherwise read `"2026-06-08T12:30:00"` in
37
37
  * the *runtime's* local zone, contradicting "the server emits UTC". Values that
38
38
  * already carry `Z` or a `±hh:mm` offset (and bare `YYYY-MM-DD` dates, already
39
39
  * parsed as UTC) are returned unchanged.