stimeo-ui 0.18.1 → 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 (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -0
  3. data/dist/cable/index.js +6 -5
  4. data/dist/controllers/alert_dialog_controller.js +324 -76
  5. data/dist/controllers/announcer_controller.js +22 -9
  6. data/dist/controllers/auto_submit_controller.js +103 -16
  7. data/dist/controllers/avatar_controller.js +29 -9
  8. data/dist/controllers/breadcrumb_controller.js +205 -17
  9. data/dist/controllers/bulk_select_controller.js +31 -13
  10. data/dist/controllers/calendar_controller.js +242 -7
  11. data/dist/controllers/carousel_controller.js +70 -15
  12. data/dist/controllers/character_counter_controller.js +103 -16
  13. data/dist/controllers/clipboard_controller.js +6 -5
  14. data/dist/controllers/collapsible_controller.js +35 -8
  15. data/dist/controllers/combobox_controller.js +155 -26
  16. data/dist/controllers/command_palette_controller.js +445 -117
  17. data/dist/controllers/conditional_fields_controller.js +84 -44
  18. data/dist/controllers/confirm_controller.js +303 -68
  19. data/dist/controllers/context_menu_controller.js +59 -30
  20. data/dist/controllers/countdown_controller.js +6 -5
  21. data/dist/controllers/currency_input_controller.js +72 -7
  22. data/dist/controllers/data_grid_controller.js +273 -51
  23. data/dist/controllers/date_range_picker_controller.js +27 -5
  24. data/dist/controllers/dialog_controller.js +330 -79
  25. data/dist/controllers/direct_upload_controller.js +6 -5
  26. data/dist/controllers/dismissible_controller.js +68 -18
  27. data/dist/controllers/drawer_controller.js +362 -92
  28. data/dist/controllers/dropdown_controller.js +64 -52
  29. data/dist/controllers/editable_controller.js +140 -12
  30. data/dist/controllers/empty_state_controller.js +193 -9
  31. data/dist/controllers/file_dropzone_controller.js +313 -23
  32. data/dist/controllers/filter_controller.js +194 -5
  33. data/dist/controllers/flash_controller.js +205 -17
  34. data/dist/controllers/focus_controller.js +273 -57
  35. data/dist/controllers/form_field_controller.js +71 -14
  36. data/dist/controllers/form_validation_controller.js +11 -7
  37. data/dist/controllers/frame_loading_controller.js +75 -20
  38. data/dist/controllers/hover_card_controller.js +108 -39
  39. data/dist/controllers/input_mask_controller.js +95 -37
  40. data/dist/controllers/intersection_controller.js +61 -29
  41. data/dist/controllers/lazy_frame_controller.js +61 -29
  42. data/dist/controllers/listbox_controller.js +174 -83
  43. data/dist/controllers/local_time_controller.js +1 -7
  44. data/dist/controllers/menu_controller.js +45 -24
  45. data/dist/controllers/menubar_controller.js +232 -52
  46. data/dist/controllers/meter_controller.js +31 -9
  47. data/dist/controllers/multi_select_controller.js +195 -62
  48. data/dist/controllers/navigation_menu_controller.js +1 -1
  49. data/dist/controllers/nested_form_controller.js +275 -21
  50. data/dist/controllers/network_status_controller.js +31 -9
  51. data/dist/controllers/number_input_controller.js +180 -71
  52. data/dist/controllers/otp_controller.js +97 -11
  53. data/dist/controllers/overflow_menu_controller.js +190 -24
  54. data/dist/controllers/pagination_controller.js +186 -24
  55. data/dist/controllers/password_reveal_controller.js +25 -4
  56. data/dist/controllers/password_strength_controller.js +6 -5
  57. data/dist/controllers/popover_controller.js +466 -42
  58. data/dist/controllers/progress_controller.js +31 -9
  59. data/dist/controllers/radio_group_controller.js +279 -88
  60. data/dist/controllers/range_slider_controller.js +72 -54
  61. data/dist/controllers/rating_controller.js +58 -10
  62. data/dist/controllers/read_more_controller.js +191 -25
  63. data/dist/controllers/relative_time_controller.js +1 -7
  64. data/dist/controllers/roving_controller.js +204 -8
  65. data/dist/controllers/scroll_area_controller.js +61 -19
  66. data/dist/controllers/scroll_visibility_controller.js +25 -4
  67. data/dist/controllers/scrollspy_controller.js +240 -32
  68. data/dist/controllers/separator_controller.js +97 -58
  69. data/dist/controllers/sidebar_controller.js +368 -127
  70. data/dist/controllers/skeleton_controller.js +75 -20
  71. data/dist/controllers/slider_controller.js +72 -54
  72. data/dist/controllers/sortable_controller.js +6 -5
  73. data/dist/controllers/spinner_controller.js +75 -20
  74. data/dist/controllers/step_indicator_controller.js +181 -6
  75. data/dist/controllers/stepper_controller.js +188 -6
  76. data/dist/controllers/sticky_observer_controller.js +86 -33
  77. data/dist/controllers/submit_once_controller.js +31 -9
  78. data/dist/controllers/switch_controller.js +84 -44
  79. data/dist/controllers/tabs_controller.js +189 -3
  80. data/dist/controllers/tags_input_controller.js +293 -29
  81. data/dist/controllers/theme_controller.js +192 -5
  82. data/dist/controllers/time_picker_controller.js +77 -42
  83. data/dist/controllers/toast_controller.js +478 -16
  84. data/dist/controllers/toggle_group_controller.js +282 -79
  85. data/dist/controllers/toolbar_controller.js +276 -15
  86. data/dist/controllers/tooltip_controller.js +244 -46
  87. data/dist/controllers/tree_view_controller.js +287 -57
  88. data/dist/index.js +2258 -1016
  89. data/lib/stimeo/ui/version.rb +1 -1
  90. metadata +2 -2
@@ -106,14 +106,34 @@ var AttributeLease = class {
106
106
  const names = registry.names;
107
107
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
108
108
  }
109
- write(element, value) {
109
+ write(element, value, beforeWrite) {
110
110
  const stack = attributeStack(element, this.#attribute);
111
111
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
112
112
  this.#held.add(element);
113
113
  const shows = !this.#covered(element, stack, hold);
114
114
  hold.written = value;
115
115
  this.#mark(element, stack);
116
- if (shows) this.#reflect(element, value);
116
+ if (shows) this.#reflect(element, value, beforeWrite);
117
+ }
118
+ adopt(element, options = {}) {
119
+ const known = registry.stacks.get(element)?.has(this.#attribute);
120
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
121
+ const stack = attributeStack(element, this.#attribute);
122
+ const hold = this.#take(stack);
123
+ if (!hold) return false;
124
+ const current = element.getAttribute(this.#attribute);
125
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
126
+ return false;
127
+ }
128
+ this.#held.add(element);
129
+ hold.original = current;
130
+ hold.linked = false;
131
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
132
+ if (!above.linked) break;
133
+ above.original = current;
134
+ }
135
+ this.#mark(element, stack);
136
+ return true;
117
137
  }
118
138
  return(element) {
119
139
  const held = this.#held.delete(element);
@@ -179,50 +199,73 @@ var AttributeLease = class {
179
199
  setAttributeIfChanged(element, name, recorded);
180
200
  }
181
201
  }
182
- #reflect(element, value) {
183
- setAttributeIfChanged(element, this.#attribute, value);
202
+ #reflect(element, value, beforeWrite) {
203
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
184
204
  }
185
205
  };
186
206
 
187
207
  var AuthoredMutationWatcher = class {
188
208
  #onAuthored;
189
- #claims = null;
190
- #observer = null;
209
+ #lifetime = null;
191
210
  constructor(onAuthored) {
192
211
  this.#onAuthored = onAuthored;
193
212
  }
194
213
  observe(root, options) {
195
- this.disconnect();
196
- const observer = new MutationObserver((records) => {
197
- if (this.#observer !== observer) return;
198
- const authored = this.#filter(records);
199
- if (authored.length > 0) this.#onAuthored(authored);
200
- });
201
- this.#observer = observer;
202
- this.#claims = { attributes: /* @__PURE__ */ new Map(), children: /* @__PURE__ */ new Map() };
203
- observer.observe(root, {
204
- attributes: true,
205
- attributeFilter: [...options.attributes],
206
- subtree: options.subtree ?? false,
207
- childList: options.childList ?? false
208
- });
209
- if (options.ancestorAttributes) {
210
- let ancestor = root.parentElement;
211
- while (ancestor) {
212
- observer.observe(ancestor, {
213
- attributes: true,
214
- attributeFilter: [...options.ancestorAttributes]
215
- });
216
- ancestor = ancestor.parentElement;
214
+ const previous = this.#lifetime;
215
+ const lifetime = {
216
+ observer: null,
217
+ active: false,
218
+ claims: { attributes: /* @__PURE__ */ new Map(), children: /* @__PURE__ */ new Map() }
219
+ };
220
+ this.#lifetime = lifetime;
221
+ let observer = null;
222
+ try {
223
+ if (previous) this.#release(previous);
224
+ if (this.#lifetime !== lifetime) return;
225
+ const attributes = [...options.attributes];
226
+ const subtree = options.subtree ?? false;
227
+ const childList = options.childList ?? false;
228
+ const inherited = options.ancestorAttributes;
229
+ const ancestorAttributes = inherited ? [...inherited] : null;
230
+ if (this.#lifetime !== lifetime) return;
231
+ observer = new MutationObserver((records) => {
232
+ const authored = this.#filter(records, lifetime.claims);
233
+ if (this.#lifetime === lifetime && lifetime.active && authored.length > 0) {
234
+ this.#onAuthored(authored);
235
+ }
236
+ });
237
+ if (this.#lifetime !== lifetime) return;
238
+ lifetime.observer = observer;
239
+ observer.observe(root, { attributes: true, attributeFilter: attributes, subtree, childList });
240
+ if (this.#lifetime !== lifetime) {
241
+ this.#release(lifetime, observer);
242
+ return;
243
+ }
244
+ if (ancestorAttributes) {
245
+ let ancestor = root.parentElement;
246
+ while (ancestor) {
247
+ observer.observe(ancestor, { attributes: true, attributeFilter: ancestorAttributes });
248
+ if (this.#lifetime !== lifetime) {
249
+ this.#release(lifetime, observer);
250
+ return;
251
+ }
252
+ ancestor = ancestor.parentElement;
253
+ }
217
254
  }
255
+ lifetime.active = true;
256
+ } catch (error) {
257
+ this.#release(lifetime, observer);
258
+ throw error;
218
259
  }
219
260
  }
220
261
  claim(target, name, value) {
221
- const claims = this.#claims?.attributes;
222
- let attributes = claims?.get(target);
262
+ const lifetime = this.#lifetime;
263
+ if (!lifetime?.active) return;
264
+ const claims = lifetime.claims.attributes;
265
+ let attributes = claims.get(target);
223
266
  if (!attributes) {
224
267
  attributes = /* @__PURE__ */ new Map();
225
- claims?.set(target, attributes);
268
+ claims.set(target, attributes);
226
269
  }
227
270
  const pending = attributes.get(name);
228
271
  attributes.set(name, { value, count: (pending?.count ?? 0) + 1 });
@@ -231,37 +274,49 @@ var AuthoredMutationWatcher = class {
231
274
  this.claim(target, name, target.getAttribute(name));
232
275
  }
233
276
  claimChildList(target, added, removed) {
234
- const claims = this.#claims?.children;
235
- const pending = claims?.get(target) ?? { added: /* @__PURE__ */ new Map(), removed: /* @__PURE__ */ new Map() };
277
+ const lifetime = this.#lifetime;
278
+ if (!lifetime?.active) return;
279
+ const claims = lifetime.claims.children;
280
+ const pending = claims.get(target) ?? { added: /* @__PURE__ */ new Map(), removed: /* @__PURE__ */ new Map() };
236
281
  for (const node of added) pending.added.set(node, (pending.added.get(node) ?? 0) + 1);
237
282
  for (const node of removed) pending.removed.set(node, (pending.removed.get(node) ?? 0) + 1);
238
- claims?.set(target, pending);
283
+ claims.set(target, pending);
239
284
  }
240
285
  take() {
241
- return this.#filter(this.#observer?.takeRecords() ?? []);
286
+ const lifetime = this.#lifetime;
287
+ const observer = lifetime?.observer;
288
+ if (!lifetime?.active || !observer) return [];
289
+ const records = observer.takeRecords();
290
+ const authored = this.#filter(records, lifetime.claims);
291
+ return this.#lifetime === lifetime && lifetime.active ? authored : [];
242
292
  }
243
293
  disconnect() {
244
- this.#observer?.disconnect();
245
- this.#observer = null;
246
- this.#claims = null;
247
- }
248
- #filter(records) {
249
- const authored = records.filter((record) => !this.#consume(record));
250
- this.#claims?.attributes.clear();
251
- this.#claims?.children.clear();
294
+ const lifetime = this.#lifetime;
295
+ if (lifetime) this.#release(lifetime);
296
+ }
297
+ #release(lifetime, observer = lifetime.observer) {
298
+ if (this.#lifetime === lifetime) this.#lifetime = null;
299
+ lifetime.claims.attributes.clear();
300
+ lifetime.claims.children.clear();
301
+ observer?.disconnect();
302
+ }
303
+ #filter(records, claims) {
304
+ const authored = records.filter((record) => !this.#consume(record, claims));
305
+ claims.attributes.clear();
306
+ claims.children.clear();
252
307
  return authored;
253
308
  }
254
- #consume(record) {
309
+ #consume(record, claims) {
255
310
  if (record.type === "attributes") {
256
311
  const target = record.target;
257
- const pending2 = this.#claims?.attributes.get(target)?.get(record.attributeName);
312
+ const pending2 = claims.attributes.get(target)?.get(record.attributeName);
258
313
  if (!pending2 || pending2.count === 0 || target.getAttribute(record.attributeName) !== pending2.value) {
259
314
  return false;
260
315
  }
261
316
  pending2.count -= 1;
262
317
  return true;
263
318
  }
264
- const pending = this.#claims?.children.get(record.target);
319
+ const pending = claims.children.get(record.target);
265
320
  if (!pending) return false;
266
321
  const added = [...record.addedNodes];
267
322
  const removed = [...record.removedNodes];
@@ -499,7 +554,6 @@ var Typeahead = class {
499
554
  #timers = new SafeTimeout();
500
555
  #resetMs;
501
556
  #query = "";
502
- #timerId = 0;
503
557
  constructor({ resetMs = TYPEAHEAD_RESET_MS } = {}) {
504
558
  this.#resetMs = resetMs;
505
559
  }
@@ -510,14 +564,13 @@ var Typeahead = class {
510
564
  const char = key.toLowerCase();
511
565
  const repeated = this.#query.length > 0 && [...this.#query].every((c) => c === char);
512
566
  this.#query = repeated ? char : this.#query + char;
513
- this.#timers.clear(this.#timerId);
514
- this.#timerId = this.#timers.set(() => this.reset(), this.#resetMs);
567
+ this.#timers.clearAll();
568
+ this.#timers.set(() => this.reset(), this.#resetMs);
515
569
  return this.#query;
516
570
  }
517
571
  reset() {
518
572
  this.#query = "";
519
- this.#timers.clear(this.#timerId);
520
- this.#timerId = 0;
573
+ this.#timers.clearAll();
521
574
  }
522
575
  };
523
576
  function isTypeaheadKey(event) {
@@ -555,10 +608,14 @@ var ListboxController = class extends Controller {
555
608
  #activeOrder = [];
556
609
  #reconcile = new MorphRenderWatcher(() => this.#reconcileOptions());
557
610
  #observer = new AuthoredMutationWatcher((records) => {
611
+ this.#adoptOptionWrites(records);
558
612
  if (this.#concernsOptions(records)) this.#reconcile.schedule();
559
613
  });
614
+ #selected = new AttributeLease("aria-selected", this.identifier);
615
+ #managedOptions = /* @__PURE__ */ new Set();
560
616
  #settled = { option: null, value: "" };
561
- #shownList = null;
617
+ #list = null;
618
+ #open = false;
562
619
  #listHidden = new AttributeLease("hidden", this.identifier);
563
620
  #expanded = new AttributeLease("aria-expanded", this.identifier);
564
621
  #activeDescendant = new AttributeLease(
@@ -568,6 +625,11 @@ var ListboxController = class extends Controller {
568
625
  #typeahead = new Typeahead();
569
626
  #listeners = new ListenerSet();
570
627
  connect() {
628
+ const options = new Set(this.optionTargets);
629
+ for (const option of this.#managedOptions) {
630
+ this.#selected.adopt(option, { ifChanged: true });
631
+ if (!options.has(option)) this.#releaseOption(option);
632
+ }
571
633
  this.#normalizeSelection();
572
634
  this.close();
573
635
  this.#listeners.add(document, "click", this.#onOutsideClick, { capture: true });
@@ -576,13 +638,21 @@ var ListboxController = class extends Controller {
576
638
  this.#observeOptions();
577
639
  }
578
640
  optionTargetConnected(option) {
579
- option.removeAttribute("data-active");
641
+ if (!this.#managedOptions.has(option)) option.removeAttribute("data-active");
580
642
  this.#reconcile.schedule();
581
643
  }
582
644
  optionTargetDisconnected(option) {
583
- option.removeAttribute("data-active");
645
+ if (!this.scope.containsElement(option) || !this.optionTargets.includes(option)) {
646
+ this.#flushMutations();
647
+ this.#releaseOption(option);
648
+ }
584
649
  this.#reconcile.schedule();
585
650
  }
651
+ #releaseOption(option) {
652
+ this.#managedOptions.delete(option);
653
+ this.#selected.return(option);
654
+ option.removeAttribute("data-active");
655
+ }
586
656
  triggerTargetConnected() {
587
657
  this.#reconcile.schedule();
588
658
  }
@@ -598,7 +668,7 @@ var ListboxController = class extends Controller {
598
668
  }
599
669
  listTargetDisconnected(list) {
600
670
  if (!this.listTargets.includes(list)) {
601
- if (list === this.#shownList) this.#adoptPageClose();
671
+ if (list === this.#list) this.#reconcileOpenState();
602
672
  this.#listHidden.return(list);
603
673
  }
604
674
  this.#reconcile.schedule();
@@ -631,23 +701,22 @@ var ListboxController = class extends Controller {
631
701
  this.#reportMove();
632
702
  }
633
703
  #reconcileOpenState() {
634
- if (!this.hasListTarget) {
635
- this.#shownList = null;
636
- return;
637
- }
638
- if (this.#shownList && this.listTargets.includes(this.#shownList)) this.#adoptPageClose();
639
- const list = this.listTarget;
640
- if (this.#shownList !== null && list !== this.#shownList) {
641
- this.#listHidden.write(list, null);
642
- this.#shownList = list;
704
+ const previous = this.#list;
705
+ if (previous) this.#open = !previous.hidden;
706
+ const list = this.hasListTarget ? this.listTarget : null;
707
+ if (list !== previous) {
708
+ this.#list = list;
709
+ if (list) {
710
+ this.#listHidden.write(list, this.#open ? null : "");
711
+ if (this.#open && previous === null && this.#activeId === null && this.hasTriggerTarget) {
712
+ this.#activateInitialOption();
713
+ }
714
+ }
643
715
  }
644
716
  if (this.hasTriggerTarget) {
645
- this.#expanded.write(this.triggerTarget, list.hidden ? "false" : "true");
717
+ this.#expanded.write(this.triggerTarget, String(this.#open));
646
718
  }
647
719
  }
648
- #adoptPageClose() {
649
- if (this.#shownList?.hidden) this.#shownList = null;
650
- }
651
720
  #reportMove() {
652
721
  const { option, value } = this.#selection();
653
722
  if (option === this.#settled.option && value === this.#settled.value) return;
@@ -659,6 +728,7 @@ var ListboxController = class extends Controller {
659
728
  return { option, value: option ? this.#optionValue(option) : "" };
660
729
  }
661
730
  disconnect() {
731
+ this.#flushMutations();
662
732
  this.#connectionGeneration += 1;
663
733
  this.#reconcile.disconnect();
664
734
  this.#observer.disconnect();
@@ -677,8 +747,9 @@ var ListboxController = class extends Controller {
677
747
  if (event.defaultPrevented) return;
678
748
  if (isReservedArrowChord(event)) return;
679
749
  if ((event.key === "Home" || event.key === "End") && hasModifierChord(event)) return;
750
+ this.#reconcileOpenState();
680
751
  if (!this.#isClosed) this.#reconcileActive();
681
- const options = this.optionTargets;
752
+ const options = this.hasListTarget ? this.optionTargets : [];
682
753
  const length = options.length;
683
754
  const activeIndex = this.#findActiveIndex(options);
684
755
  if (this.#isClosed) {
@@ -749,25 +820,29 @@ var ListboxController = class extends Controller {
749
820
  }
750
821
  }
751
822
  open() {
752
- if (!this.hasListTarget) return;
753
- this.#listHidden.write(this.listTarget, null);
754
- this.#shownList = this.listTarget;
823
+ if (!this.hasTriggerTarget) return;
824
+ this.#list = this.hasListTarget ? this.listTarget : null;
825
+ this.#open = true;
755
826
  this.#expanded.write(this.triggerTarget, "true");
756
- if (this.optionTargets.length === 0) {
757
- this.#setActive(-1);
758
- return;
827
+ if (this.#list) {
828
+ this.#listHidden.write(this.#list, null);
829
+ this.#activateInitialOption();
759
830
  }
831
+ }
832
+ #activateInitialOption() {
760
833
  const selected = this.optionTargets.findIndex(
761
834
  (option) => option.getAttribute("aria-selected") === "true"
762
835
  );
763
836
  this.#setActive(selected === -1 ? 0 : selected);
764
837
  }
765
838
  close() {
766
- if (!this.hasListTarget) return;
767
- this.#listHidden.write(this.listTarget, "");
768
- this.#shownList = null;
769
- this.#expanded.write(this.triggerTarget, "false");
770
- this.#setActive(-1);
839
+ this.#list = this.hasListTarget ? this.listTarget : null;
840
+ this.#open = false;
841
+ if (this.#list) this.#listHidden.write(this.#list, "");
842
+ if (this.hasTriggerTarget) {
843
+ this.#expanded.write(this.triggerTarget, "false");
844
+ this.#setActive(-1);
845
+ }
771
846
  this.#typeahead.reset();
772
847
  }
773
848
  #commitActive() {
@@ -791,6 +866,7 @@ var ListboxController = class extends Controller {
791
866
  return this.#moves.isLatest(token);
792
867
  }
793
868
  #applySelection(option) {
869
+ this.#flushMutations();
794
870
  for (const candidate of this.optionTargets) {
795
871
  this.#writeSelection(candidate, candidate === option ? "true" : "false");
796
872
  }
@@ -830,8 +906,18 @@ var ListboxController = class extends Controller {
830
906
  const options = new Set(this.optionTargets);
831
907
  return records.some((record) => options.has(record.target));
832
908
  }
909
+ #adoptOptionWrites(records) {
910
+ for (const record of records) {
911
+ const option = record.target;
912
+ if (record.attributeName === "aria-selected" && this.#managedOptions.has(option)) {
913
+ this.#selected.adopt(option);
914
+ }
915
+ }
916
+ }
833
917
  #flushMutations() {
834
- if (this.#concernsOptions(this.#observer.take())) this.#reconcile.schedule();
918
+ const records = this.#observer.take();
919
+ this.#adoptOptionWrites(records);
920
+ if (this.#concernsOptions(records)) this.#reconcile.schedule();
835
921
  }
836
922
  #setActive(index) {
837
923
  const options = this.optionTargets;
@@ -859,6 +945,10 @@ var ListboxController = class extends Controller {
859
945
  if (this.hasTriggerTarget) this.#setActive(-1);
860
946
  return;
861
947
  }
948
+ if (!this.hasListTarget) {
949
+ this.#activeDescendant.write(this.triggerTarget, null);
950
+ return;
951
+ }
862
952
  const options = this.optionTargets;
863
953
  const currentIndex = this.#findActiveIndex(options);
864
954
  if (currentIndex >= 0) {
@@ -902,10 +992,11 @@ var ListboxController = class extends Controller {
902
992
  if (!this.#isClosed && !this.element.contains(event.target)) this.close();
903
993
  };
904
994
  get #isClosed() {
905
- return !this.hasListTarget || this.listTarget.hidden !== false;
995
+ return this.#list ? this.#list.hidden !== false : !this.#open;
906
996
  }
907
997
  #writeSelection(target, value) {
908
- setAttributeIfChanged(target, "aria-selected", value, () => {
998
+ this.#managedOptions.add(target);
999
+ this.#selected.write(target, value, () => {
909
1000
  this.#observer.claim(target, "aria-selected", value);
910
1001
  });
911
1002
  }
@@ -27,13 +27,7 @@ function readDatetime(element) {
27
27
  const minute = Number(minuteText);
28
28
  const second = Number(secondText);
29
29
  if (hour === 24 && (minute !== 0 || second !== 0 || /[1-9]/.test(fraction))) return null;
30
- let offset = zone;
31
- if (zone !== "Z") {
32
- const digits = zone.slice(1).replace(":", "");
33
- const offsetHour = digits.slice(0, 2);
34
- const offsetMinute = digits.slice(2);
35
- offset = `${zone[0]}${offsetHour}:${offsetMinute}`;
36
- }
30
+ const offset = zone.replace(/^([+-]\d{2})(\d{2})$/, "$1:$2");
37
31
  const milliseconds = fraction.slice(0, 3).padEnd(3, "0");
38
32
  const canonical = `${yearText}-${monthText}-${dayText}T${hourText}:${minuteText}:${secondText}.${milliseconds}${offset}`;
39
33
  const instant = Date.parse(canonical);
@@ -17,6 +17,7 @@ function hasModifierChord(event) {
17
17
 
18
18
  function setAttributeIfChanged(element, name, value, beforeWrite) {
19
19
  if (element.getAttribute(name) === value) return false;
20
+ beforeWrite?.();
20
21
  if (value === null) element.removeAttribute(name);
21
22
  else element.setAttribute(name, value);
22
23
  return true;
@@ -90,14 +91,34 @@ var AttributeLease = class {
90
91
  const names = registry.names;
91
92
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
92
93
  }
93
- write(element, value) {
94
+ write(element, value, beforeWrite) {
94
95
  const stack = attributeStack(element, this.#attribute);
95
96
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
96
97
  this.#held.add(element);
97
98
  const shows = !this.#covered(element, stack, hold);
98
99
  hold.written = value;
99
100
  this.#mark(element, stack);
100
- if (shows) this.#reflect(element, value);
101
+ if (shows) this.#reflect(element, value, beforeWrite);
102
+ }
103
+ adopt(element, options = {}) {
104
+ const known = registry.stacks.get(element)?.has(this.#attribute);
105
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
106
+ const stack = attributeStack(element, this.#attribute);
107
+ const hold = this.#take(stack);
108
+ if (!hold) return false;
109
+ const current = element.getAttribute(this.#attribute);
110
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
111
+ return false;
112
+ }
113
+ this.#held.add(element);
114
+ hold.original = current;
115
+ hold.linked = false;
116
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
117
+ if (!above.linked) break;
118
+ above.original = current;
119
+ }
120
+ this.#mark(element, stack);
121
+ return true;
101
122
  }
102
123
  return(element) {
103
124
  const held = this.#held.delete(element);
@@ -163,8 +184,8 @@ var AttributeLease = class {
163
184
  setAttributeIfChanged(element, name, recorded);
164
185
  }
165
186
  }
166
- #reflect(element, value) {
167
- setAttributeIfChanged(element, this.#attribute, value);
187
+ #reflect(element, value, beforeWrite) {
188
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
168
189
  }
169
190
  };
170
191
 
@@ -193,6 +214,7 @@ var EscapeLayer = class _EscapeLayer {
193
214
  );
194
215
  #active = null;
195
216
  activate(ownerDocument = document, options) {
217
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
196
218
  this.deactivate();
197
219
  let registry2 = _EscapeLayer.#registries.get(ownerDocument);
198
220
  if (!registry2) {
@@ -200,7 +222,6 @@ var EscapeLayer = class _EscapeLayer {
200
222
  _EscapeLayer.#registries.set(ownerDocument, registry2);
201
223
  ownerDocument.addEventListener("keydown", registry2.onKeydown);
202
224
  }
203
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
204
225
  registry2.stack.push(entry);
205
226
  this.#active = { document: ownerDocument, entry };
206
227
  }
@@ -331,6 +352,7 @@ var MenuController = class extends Controller {
331
352
  #menuHidden = new AttributeLease("hidden", this.identifier);
332
353
  #expanded = new AttributeLease("aria-expanded", this.identifier);
333
354
  #menu = null;
355
+ #openState = false;
334
356
  #clickOwner = null;
335
357
  #clickVerdicts = new ClickVerdicts();
336
358
  #activated = /* @__PURE__ */ new WeakSet();
@@ -338,6 +360,7 @@ var MenuController = class extends Controller {
338
360
  #reporting = false;
339
361
  #listeners = new ListenerSet();
340
362
  connect() {
363
+ this.#menu = this.hasMenuTarget ? this.menuTarget : null;
341
364
  this.#close("api");
342
365
  this.#listeners.add(this.element, "click", this.#onItemClickCapture, { capture: true });
343
366
  this.#listeners.add(this.element, "click", this.#onDelegatedItemClick);
@@ -383,39 +406,35 @@ var MenuController = class extends Controller {
383
406
  }
384
407
  #open(reason) {
385
408
  this.#timers.clearAll();
386
- if (!this.hasMenuTarget) return;
387
- const menu = this.menuTarget;
409
+ this.#adoptMenu();
388
410
  const was = this.#isOpen;
411
+ this.#openState = true;
389
412
  this.#escapeLayer.activate(document, {
390
413
  onDismiss: () => this.#closeAndRestore("escape"),
391
414
  claims: claimsWhileFocusWithin(this.element)
392
415
  });
393
- this.#menuHidden.write(menu, null);
394
- this.#menu = menu;
395
- if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, "true");
416
+ if (this.#menu) this.#menuHidden.write(this.#menu, null);
417
+ this.#reflectTrigger();
396
418
  if (!was && this.#reporting) this.dispatch("open", { detail: { reason }, cancelable: false });
397
419
  }
398
420
  #close(reason) {
421
+ this.#adoptMenu();
422
+ const was = this.#isOpen;
399
423
  this.#timers.clearAll();
400
424
  this.#escapeLayer.deactivate();
401
- const menu = this.hasMenuTarget ? this.menuTarget : null;
402
- this.#menu = menu;
403
- if (!menu) return;
404
- const was = this.#isOpen;
405
- this.#menuHidden.write(menu, "");
406
- if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, "false");
425
+ this.#openState = false;
426
+ if (this.#menu) this.#menuHidden.write(this.#menu, "");
427
+ this.#reflectTrigger();
407
428
  if (was && this.#reporting) this.dispatch("close", { detail: { reason }, cancelable: false });
408
429
  }
409
430
  #adoptMenu() {
410
431
  if (!this.#connected) return;
411
432
  const menu = this.hasMenuTarget ? this.menuTarget : null;
412
433
  const previous = this.#menu;
434
+ if (previous) this.#openState = !previous.hidden;
413
435
  this.#menu = menu;
414
- if (!menu) {
415
- this.#escapeLayer.deactivate();
416
- } else if (menu !== previous) {
417
- this.#menuHidden.write(menu, previous?.hidden ?? true ? "" : null);
418
- }
436
+ if (!this.#openState) this.#escapeLayer.deactivate();
437
+ if (menu && menu !== previous) this.#menuHidden.write(menu, this.#openState ? null : "");
419
438
  this.#reflectTrigger();
420
439
  }
421
440
  #reflectTrigger() {
@@ -518,11 +537,13 @@ var MenuController = class extends Controller {
518
537
  event.stopImmediatePropagation();
519
538
  };
520
539
  #focusFirst() {
521
- if (!this.#isOpen) return;
540
+ this.#adoptMenu();
541
+ if (!this.#menu || !this.#isOpen) return;
522
542
  this.#navigableItems[0]?.focus();
523
543
  }
524
544
  #focusLast() {
525
- if (!this.#isOpen) return;
545
+ this.#adoptMenu();
546
+ if (!this.#menu || !this.#isOpen) return;
526
547
  const items = this.#navigableItems;
527
548
  items[items.length - 1]?.focus();
528
549
  }
@@ -537,7 +558,7 @@ var MenuController = class extends Controller {
537
558
  return !this.#isNavigable(item) || isAriaDisabled(item);
538
559
  }
539
560
  get #isOpen() {
540
- return this.hasMenuTarget && !this.menuTarget.hidden;
561
+ return this.#menu ? !this.#menu.hidden : this.#openState;
541
562
  }
542
563
  };
543
564