stimeo-ui 0.18.2 → 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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -0
  3. data/dist/controllers/alert_dialog_controller.js +246 -42
  4. data/dist/controllers/auto_submit_controller.js +26 -5
  5. data/dist/controllers/avatar_controller.js +29 -9
  6. data/dist/controllers/breadcrumb_controller.js +205 -17
  7. data/dist/controllers/bulk_select_controller.js +25 -4
  8. data/dist/controllers/calendar_controller.js +242 -7
  9. data/dist/controllers/carousel_controller.js +70 -15
  10. data/dist/controllers/character_counter_controller.js +26 -5
  11. data/dist/controllers/collapsible_controller.js +35 -8
  12. data/dist/controllers/combobox_controller.js +84 -20
  13. data/dist/controllers/command_palette_controller.js +296 -77
  14. data/dist/controllers/confirm_controller.js +225 -34
  15. data/dist/controllers/context_menu_controller.js +58 -29
  16. data/dist/controllers/currency_input_controller.js +1 -1
  17. data/dist/controllers/data_grid_controller.js +196 -9
  18. data/dist/controllers/date_range_picker_controller.js +25 -4
  19. data/dist/controllers/dialog_controller.js +252 -45
  20. data/dist/controllers/dismissible_controller.js +57 -11
  21. data/dist/controllers/drawer_controller.js +284 -58
  22. data/dist/controllers/dropdown_controller.js +63 -51
  23. data/dist/controllers/editable_controller.js +69 -6
  24. data/dist/controllers/empty_state_controller.js +187 -4
  25. data/dist/controllers/file_dropzone_controller.js +300 -12
  26. data/dist/controllers/filter_controller.js +194 -5
  27. data/dist/controllers/flash_controller.js +199 -12
  28. data/dist/controllers/focus_controller.js +195 -23
  29. data/dist/controllers/form_field_controller.js +65 -9
  30. data/dist/controllers/frame_loading_controller.js +25 -4
  31. data/dist/controllers/hover_card_controller.js +107 -38
  32. data/dist/controllers/input_mask_controller.js +1 -1
  33. data/dist/controllers/listbox_controller.js +94 -36
  34. data/dist/controllers/menu_controller.js +44 -23
  35. data/dist/controllers/menubar_controller.js +228 -46
  36. data/dist/controllers/meter_controller.js +25 -4
  37. data/dist/controllers/multi_select_controller.js +110 -42
  38. data/dist/controllers/nested_form_controller.js +258 -9
  39. data/dist/controllers/network_status_controller.js +25 -4
  40. data/dist/controllers/number_input_controller.js +26 -5
  41. data/dist/controllers/otp_controller.js +26 -5
  42. data/dist/controllers/overflow_menu_controller.js +183 -22
  43. data/dist/controllers/pagination_controller.js +179 -22
  44. data/dist/controllers/password_reveal_controller.js +25 -4
  45. data/dist/controllers/popover_controller.js +454 -34
  46. data/dist/controllers/progress_controller.js +25 -4
  47. data/dist/controllers/radio_group_controller.js +195 -40
  48. data/dist/controllers/rating_controller.js +58 -10
  49. data/dist/controllers/read_more_controller.js +191 -25
  50. data/dist/controllers/roving_controller.js +197 -6
  51. data/dist/controllers/scroll_area_controller.js +50 -12
  52. data/dist/controllers/scroll_visibility_controller.js +25 -4
  53. data/dist/controllers/scrollspy_controller.js +179 -3
  54. data/dist/controllers/separator_controller.js +25 -4
  55. data/dist/controllers/sidebar_controller.js +290 -93
  56. data/dist/controllers/skeleton_controller.js +25 -4
  57. data/dist/controllers/spinner_controller.js +25 -4
  58. data/dist/controllers/step_indicator_controller.js +181 -6
  59. data/dist/controllers/stepper_controller.js +188 -6
  60. data/dist/controllers/sticky_observer_controller.js +25 -4
  61. data/dist/controllers/submit_once_controller.js +25 -4
  62. data/dist/controllers/tabs_controller.js +189 -3
  63. data/dist/controllers/tags_input_controller.js +210 -9
  64. data/dist/controllers/theme_controller.js +192 -5
  65. data/dist/controllers/toast_controller.js +478 -16
  66. data/dist/controllers/toggle_group_controller.js +198 -35
  67. data/dist/controllers/toolbar_controller.js +198 -7
  68. data/dist/controllers/tooltip_controller.js +243 -45
  69. data/dist/controllers/tree_view_controller.js +200 -8
  70. data/dist/index.js +1764 -739
  71. data/lib/stimeo/ui/version.rb +1 -1
  72. metadata +2 -2
@@ -24,16 +24,23 @@ function actionSource(source) {
24
24
 
25
25
  function setAttributeIfChanged(element, name, value, beforeWrite) {
26
26
  if (element.getAttribute(name) === value) return false;
27
+ beforeWrite?.();
27
28
  if (value === null) element.removeAttribute(name);
28
29
  else element.setAttribute(name, value);
29
30
  return true;
30
31
  }
31
32
 
32
- function syncActiveOption(options, active) {
33
+ function syncActiveOption(options, active, lease) {
33
34
  let written = 0;
34
35
  for (const option of options) {
35
36
  const next = option === active ? "true" : "false";
36
- if (setAttributeIfChanged(option, "aria-selected", next)) written += 1;
37
+ if (lease) {
38
+ if (option.getAttribute("aria-selected") === next) continue;
39
+ lease.adopt(option, { ifChanged: true });
40
+ lease.write(option, next, () => {
41
+ written += 1;
42
+ });
43
+ } else if (setAttributeIfChanged(option, "aria-selected", next)) written += 1;
37
44
  }
38
45
  return written;
39
46
  }
@@ -114,14 +121,34 @@ var AttributeLease = class {
114
121
  const names = registry.names;
115
122
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
116
123
  }
117
- write(element, value) {
124
+ write(element, value, beforeWrite) {
118
125
  const stack = attributeStack(element, this.#attribute);
119
126
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
120
127
  this.#held.add(element);
121
128
  const shows = !this.#covered(element, stack, hold);
122
129
  hold.written = value;
123
130
  this.#mark(element, stack);
124
- if (shows) this.#reflect(element, value);
131
+ if (shows) this.#reflect(element, value, beforeWrite);
132
+ }
133
+ adopt(element, options = {}) {
134
+ const known = registry.stacks.get(element)?.has(this.#attribute);
135
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
136
+ const stack = attributeStack(element, this.#attribute);
137
+ const hold = this.#take(stack);
138
+ if (!hold) return false;
139
+ const current = element.getAttribute(this.#attribute);
140
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
141
+ return false;
142
+ }
143
+ this.#held.add(element);
144
+ hold.original = current;
145
+ hold.linked = false;
146
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
147
+ if (!above.linked) break;
148
+ above.original = current;
149
+ }
150
+ this.#mark(element, stack);
151
+ return true;
125
152
  }
126
153
  return(element) {
127
154
  const held = this.#held.delete(element);
@@ -187,8 +214,8 @@ var AttributeLease = class {
187
214
  setAttributeIfChanged(element, name, recorded);
188
215
  }
189
216
  }
190
- #reflect(element, value) {
191
- setAttributeIfChanged(element, this.#attribute, value);
217
+ #reflect(element, value, beforeWrite) {
218
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
192
219
  }
193
220
  };
194
221
 
@@ -306,7 +333,7 @@ var CompositionTracker = class {
306
333
  this.#confirmedTarget = null;
307
334
  if (confirmed === null || confirmed !== event.target) return false;
308
335
  const inputType = event.inputType;
309
- return !inputType || isCompositionInputType(inputType);
336
+ return inputType ? isCompositionInputType(inputType) : event.isTrusted === true;
310
337
  }
311
338
  #release(target) {
312
339
  target.removeEventListener("compositionstart", this.#handleStart);
@@ -697,7 +724,8 @@ function ownedScope(element) {
697
724
  function orderScope(elements, walk) {
698
725
  const entries = [];
699
726
  const visit = (element) => {
700
- if (element === walk.from) walk.fromAt = walk.tree.length;
727
+ const isFrom = element === walk.from;
728
+ if (isFrom && walk.side !== "after") walk.fromAt = walk.tree.length;
701
729
  const tabindex = authoredTabindex(element) ?? 0;
702
730
  let stop = null;
703
731
  if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
@@ -708,15 +736,23 @@ function orderScope(elements, walk) {
708
736
  if (owned) {
709
737
  if (tabindex >= 0) {
710
738
  entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
711
- } else if (walk.from && flatTreeContains(element, walk.from)) {
739
+ } else if (walk.from && flatTreeContains(element, walk.from) && (!isFrom || walk.side === "on")) {
712
740
  walk.fromAt = walk.tree.length;
713
741
  walk.excludedScope = element;
714
- orderScope(owned, { from: walk.from, tree: walk.tree, fromAt: -1, excludedScope: null });
742
+ orderScope(owned, {
743
+ from: walk.from,
744
+ side: walk.side,
745
+ tree: walk.tree,
746
+ fromAt: -1,
747
+ excludedScope: null
748
+ });
715
749
  }
750
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
716
751
  return;
717
752
  }
718
753
  if (stop) entries.push({ tabindex, stops: [stop] });
719
754
  for (const child of Array.from(element.children)) visit(child);
755
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
720
756
  };
721
757
  for (const element of elements) visit(element);
722
758
  const positive = entries.filter((entry) => entry.tabindex > 0);
@@ -724,26 +760,27 @@ function orderScope(elements, walk) {
724
760
  const rest = entries.filter((entry) => entry.tabindex === 0);
725
761
  return [...positive, ...rest].flatMap((entry) => entry.stops);
726
762
  }
727
- function passesRadioRule(element, tree, focusedRadio) {
763
+ function passesRadioRule(element, tree, focusedRadio, backward) {
728
764
  if (!isRadio(element) || element.checked) return true;
729
765
  const group = tree.filter(
730
766
  (stop) => stop === element || isRadio(stop) && sameRadioGroup(stop, element)
731
767
  );
732
768
  if (group.some((radio) => radio.checked)) return false;
733
- if (group[0] !== element) return false;
769
+ if (group[backward ? group.length - 1 : 0] !== element) return false;
734
770
  return focusedRadio === null || !sameRadioGroup(focusedRadio, element);
735
771
  }
736
772
  function startIndex(order, walk, backward, isStop) {
737
773
  if (walk.fromAt < 0) return backward ? order.length - 1 : 0;
738
- const at = order.indexOf(walk.from);
774
+ const at = walk.side === "on" ? order.indexOf(walk.from) : -1;
739
775
  if (at >= 0) return backward ? at - 1 : at + 1;
740
776
  const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
741
777
  if (neighbour) return order.indexOf(neighbour);
742
778
  return backward ? -1 : order.length;
743
779
  }
744
- function movesWithin(root, from, backward, wrap, radioContext) {
780
+ function movesWithin(root, from, backward, wrap, radioContext, side = "on", position = { holder: null }) {
745
781
  const walk = {
746
782
  from,
783
+ side,
747
784
  tree: [],
748
785
  fromAt: from === root ? 0 : -1,
749
786
  excludedScope: null
@@ -751,26 +788,111 @@ function movesWithin(root, from, backward, wrap, radioContext) {
751
788
  const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
752
789
  const radios = radioContext ?? {
753
790
  tree: walk.tree,
754
- focusedRadio: walk.fromAt >= 0 && isRadio(from) ? from : null
791
+ focusedRadio: side === "on" && walk.fromAt >= 0 && isRadio(from) ? from : null
755
792
  };
756
- const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio);
793
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio, backward);
794
+ const holds = (element) => position.holder !== null && flatTreeContains(element, position.holder);
757
795
  const start = startIndex(
758
796
  order,
759
797
  walk,
760
798
  backward,
761
- (element) => order.includes(element) && isStop(element)
799
+ (element) => order.includes(element) && isStop(element) && !holds(element)
762
800
  );
763
801
  const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
764
802
  const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
765
803
  const excluded = walk.excludedScope;
766
- const prefix = excluded && !(backward && from === excluded) ? movesWithin(excluded, from === excluded ? null : from, backward, false, radios) : [];
804
+ const prefix = excluded && !(backward && from === excluded) ? movesWithin(
805
+ excluded,
806
+ from === excluded ? null : from,
807
+ backward,
808
+ false,
809
+ radios,
810
+ side,
811
+ position
812
+ ) : [];
767
813
  return [
768
814
  ...prefix,
769
- ...[...remaining, ...wrapped].filter((element) => element !== from && isStop(element))
815
+ ...[...remaining, ...wrapped].filter(
816
+ (element) => (side !== "on" || element !== from) && isStop(element)
817
+ )
770
818
  ];
771
819
  }
820
+ function flatParentNode(node) {
821
+ const parent = node.parentNode;
822
+ if (parent instanceof Element && parent.shadowRoot) {
823
+ for (const slot of parent.shadowRoot.querySelectorAll("slot")) {
824
+ if (slot.assignedNodes().includes(node)) return slot;
825
+ }
826
+ }
827
+ return parent instanceof ShadowRoot ? parent.host : parent;
828
+ }
829
+ function flatChildNodes(node) {
830
+ if (node instanceof Element && node.shadowRoot) return Array.from(node.shadowRoot.childNodes);
831
+ if (node instanceof HTMLSlotElement) {
832
+ const assigned = node.assignedNodes();
833
+ return assigned.length > 0 ? assigned : Array.from(node.childNodes);
834
+ }
835
+ return Array.from(node.childNodes);
836
+ }
837
+ function flatNodeAfter(node, root) {
838
+ for (let current = node; current && current !== root; ) {
839
+ const parent = flatParentNode(current);
840
+ if (!parent) return null;
841
+ const siblings = flatChildNodes(parent);
842
+ const index = siblings.indexOf(current);
843
+ const next = index >= 0 ? siblings[index + 1] : void 0;
844
+ if (next) return next;
845
+ current = parent;
846
+ }
847
+ return null;
848
+ }
849
+ function flatElementBefore(node, root) {
850
+ for (let current = node; current && current !== root; ) {
851
+ const parent = flatParentNode(current);
852
+ if (!parent) return null;
853
+ const siblings = flatChildNodes(parent);
854
+ for (let index = siblings.indexOf(current) - 1; index >= 0; index--) {
855
+ const sibling = siblings[index];
856
+ if (sibling instanceof Element) return sibling;
857
+ }
858
+ current = parent;
859
+ }
860
+ return null;
861
+ }
862
+ function elementAfterPosition(container, offset, root) {
863
+ const owner = container instanceof ShadowRoot ? container.host : container;
864
+ const child = container.childNodes[offset];
865
+ const previous = container.childNodes[offset - 1];
866
+ let node = child ?? (previous ? flatNodeAfter(previous, root) : flatChildNodes(owner)[0] ?? flatNodeAfter(owner, root));
867
+ while (node && !(node instanceof Element)) {
868
+ node = flatChildNodes(node)[0] ?? flatNodeAfter(node, root);
869
+ }
870
+ return node;
871
+ }
872
+ function elementBeforePosition(container, offset, root) {
873
+ const child = container.childNodes[offset];
874
+ if (child) return flatElementBefore(child, root);
875
+ const previous = container.childNodes[offset - 1];
876
+ if (previous) return previous instanceof Element ? previous : flatElementBefore(previous, root);
877
+ return flatElementBefore(container instanceof ShadowRoot ? container.host : container, root);
878
+ }
879
+ function elementHoldingPosition(container, offset) {
880
+ if (container instanceof Element && container.shadowRoot) {
881
+ const near = container.childNodes[offset] ?? container.childNodes[offset - 1];
882
+ return near ? flatParentNode(near) : container;
883
+ }
884
+ if (container instanceof Element) return container;
885
+ if (container instanceof ShadowRoot) return container.host;
886
+ return flatParentNode(container);
887
+ }
772
888
  function sequentialTabStops(root, from = null, backward = false) {
773
- return movesWithin(root, from, backward, true);
889
+ if (!(from instanceof Range)) return movesWithin(root, from, backward, true);
890
+ const container = backward ? from.startContainer : from.endContainer;
891
+ const offset = backward ? from.startOffset : from.endOffset;
892
+ const anchor = backward ? elementBeforePosition(container, offset, root) : elementAfterPosition(container, offset, root);
893
+ return movesWithin(root, anchor, backward, true, void 0, backward ? "after" : "before", {
894
+ holder: elementHoldingPosition(container, offset)
895
+ });
774
896
  }
775
897
  function deepActiveElement(ownerDocument) {
776
898
  let active = ownerDocument.activeElement;
@@ -934,7 +1056,7 @@ var INERT_MARK = `data-${FOCUS_TRAP_OWNER}-inert`;
934
1056
  var OVERFLOW_MARK = `data-${FOCUS_TRAP_OWNER}-overflow`;
935
1057
  var FocusTrap = class _FocusTrap {
936
1058
  static #registries = sharedRegistry(
937
- "stimeo-ui.focus-trap.registry.v2",
1059
+ "stimeo-ui.focus-trap.registry.v3",
938
1060
  () => /* @__PURE__ */ new WeakMap()
939
1061
  );
940
1062
  static #TEXT_ENTRY_TYPES = /* @__PURE__ */ new Set([
@@ -960,15 +1082,17 @@ var FocusTrap = class _FocusTrap {
960
1082
  this.#entry = this.#createEntry();
961
1083
  }
962
1084
  #createEntry() {
963
- return {
1085
+ const entry = {
964
1086
  container: () => this.#readContainer(),
965
1087
  moveFocus: (backward) => this.#moveFocus(backward),
966
1088
  refocus: () => this.#refocus(),
967
1089
  isolated: false,
968
1090
  lockedBody: null,
969
1091
  openedFrom: null,
970
- handedBack: []
1092
+ handedBack: [],
1093
+ returnTargets: () => this.#activeReturnTargets(entry)
971
1094
  };
1095
+ return entry;
972
1096
  }
973
1097
  get active() {
974
1098
  return this.#activeState;
@@ -994,6 +1118,7 @@ var FocusTrap = class _FocusTrap {
994
1118
  return;
995
1119
  }
996
1120
  const entry = this.#entry;
1121
+ const parent = entry.openedFrom;
997
1122
  const openers = this.#openers;
998
1123
  this.#openers = [];
999
1124
  this.#activeState = false;
@@ -1003,7 +1128,7 @@ var FocusTrap = class _FocusTrap {
1003
1128
  _FocusTrap.#reconcileBackground(document);
1004
1129
  try {
1005
1130
  const wayBack = this.#handOver(entry, openers, above);
1006
- if (restoreFocus) this.#restore(wayBack, above);
1131
+ if (restoreFocus) this.#restore(wayBack, above, parent);
1007
1132
  } finally {
1008
1133
  this.#returnTabindexes();
1009
1134
  }
@@ -1042,6 +1167,11 @@ var FocusTrap = class _FocusTrap {
1042
1167
  document.addEventListener("keydown", registry2.onKeydown);
1043
1168
  document.addEventListener("turbo:morph", registry2.onMorph);
1044
1169
  document.addEventListener("turbo:morph-element", registry2.onMorph);
1170
+ document.addEventListener("pointerdown", registry2.onPointerDown, {
1171
+ capture: true,
1172
+ passive: true
1173
+ });
1174
+ document.addEventListener("focusin", registry2.onFocusIn, { capture: true });
1045
1175
  }
1046
1176
  registry2.stack.push(this.#entry);
1047
1177
  }
@@ -1054,6 +1184,10 @@ var FocusTrap = class _FocusTrap {
1054
1184
  document.removeEventListener("keydown", registry2.onKeydown);
1055
1185
  document.removeEventListener("turbo:morph", registry2.onMorph);
1056
1186
  document.removeEventListener("turbo:morph-element", registry2.onMorph);
1187
+ document.removeEventListener("pointerdown", registry2.onPointerDown, { capture: true });
1188
+ document.removeEventListener("focusin", registry2.onFocusIn, { capture: true });
1189
+ registry2.start = null;
1190
+ registry2.pressed = false;
1057
1191
  }
1058
1192
  return above;
1059
1193
  }
@@ -1090,7 +1224,7 @@ var FocusTrap = class _FocusTrap {
1090
1224
  }
1091
1225
  return wayBack;
1092
1226
  }
1093
- #restore(wayBack, above) {
1227
+ #restore(wayBack, above, parent) {
1094
1228
  const active = deepActiveElement(document);
1095
1229
  const released = this.#readContainer();
1096
1230
  const holds = (trap) => {
@@ -1101,7 +1235,7 @@ var FocusTrap = class _FocusTrap {
1101
1235
  return !released || !flatTreeContains(released, active) || flatTreeContains(released, container);
1102
1236
  };
1103
1237
  if (above.some(holds)) return;
1104
- for (const element of wayBack) {
1238
+ for (const element of _FocusTrap.#returnCandidates(wayBack, parent)) {
1105
1239
  if (this.#activeState) return;
1106
1240
  if (!element.isConnected || !canTakeFocus(element) || !isRenderedForFocus(element)) continue;
1107
1241
  const before = deepActiveElement(document);
@@ -1111,6 +1245,19 @@ var FocusTrap = class _FocusTrap {
1111
1245
  }
1112
1246
  _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack)?.refocus();
1113
1247
  }
1248
+ *#activeReturnTargets(entry) {
1249
+ if (!this.#activeState || this.#entry !== entry) return;
1250
+ yield* this.#openers;
1251
+ const fallback = this.#options.fallbackFocus?.();
1252
+ if (fallback) yield fallback;
1253
+ yield* entry.handedBack;
1254
+ }
1255
+ static *#returnCandidates(wayBack, parent) {
1256
+ yield* wayBack;
1257
+ for (let entry = parent; entry && !_FocusTrap.#takesTab(entry); entry = entry.openedFrom) {
1258
+ yield* entry.returnTargets?.() ?? [];
1259
+ }
1260
+ }
1114
1261
  #refocus() {
1115
1262
  if (!this.#flag(this.#options.autoFocus, true)) return;
1116
1263
  if (flatTreeContains(this.#getContainer(), deepActiveElement(document))) return;
@@ -1125,8 +1272,8 @@ var FocusTrap = class _FocusTrap {
1125
1272
  const focused = deepActiveElement(document);
1126
1273
  for (const [element, loan] of [...this.#tabindexLoans]) {
1127
1274
  if (element === focused) continue;
1128
- loan.returnAll();
1129
1275
  this.#tabindexLoans.delete(element);
1276
+ loan.returnAll();
1130
1277
  }
1131
1278
  }
1132
1279
  #lockScroll() {
@@ -1171,8 +1318,12 @@ var FocusTrap = class _FocusTrap {
1171
1318
  stack: [],
1172
1319
  onKeydown: (event) => _FocusTrap.#resolveTab(registry2.stack, event),
1173
1320
  onMorph: () => morphs.schedule(),
1321
+ onPointerDown: (event) => _FocusTrap.#recordPress(registry2, event),
1322
+ onFocusIn: (event) => _FocusTrap.#recordFocus(registry2, event),
1174
1323
  inerted: /* @__PURE__ */ new Set(),
1175
- scrollLocks: /* @__PURE__ */ new Set()
1324
+ scrollLocks: /* @__PURE__ */ new Set(),
1325
+ start: null,
1326
+ pressed: false
1176
1327
  };
1177
1328
  _FocusTrap.#registries.set(ownerDocument, registry2);
1178
1329
  return registry2;
@@ -1270,7 +1421,7 @@ var FocusTrap = class _FocusTrap {
1270
1421
  return _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack) === this.#entry;
1271
1422
  }
1272
1423
  #moveFocus(backward) {
1273
- const from = deepActiveElement(document);
1424
+ const from = _FocusTrap.#start();
1274
1425
  for (const stop of sequentialTabStops(this.#getContainer(), from, backward)) {
1275
1426
  if (!this.#ownsTab()) return;
1276
1427
  const before = deepActiveElement(document);
@@ -1281,18 +1432,66 @@ var FocusTrap = class _FocusTrap {
1281
1432
  return;
1282
1433
  }
1283
1434
  }
1435
+ static #start() {
1436
+ const active = deepActiveElement(document);
1437
+ const start = _FocusTrap.#registry(document).start;
1438
+ if (active && active !== document.body) {
1439
+ const at = start ? elementHoldingPosition(start.startContainer, start.startOffset) : null;
1440
+ const pointInside = !(active instanceof HTMLElement && isTabStop(active)) && flatTreeContains(active, at);
1441
+ return pointInside ? start : active;
1442
+ }
1443
+ return start ?? active;
1444
+ }
1445
+ static #recordPress(registry2, event) {
1446
+ const path = event.composedPath();
1447
+ const target = path[0];
1448
+ const owner = target.ownerDocument ?? document;
1449
+ const roots = path.filter((node) => node instanceof ShadowRoot);
1450
+ const caret = _FocusTrap.#caretAt(owner, event.clientX, event.clientY, roots);
1451
+ const range = owner.createRange();
1452
+ const fits = caret !== null && target.contains(caret.node) && caret.offset <= (caret.node instanceof CharacterData ? caret.node.length : caret.node.childNodes.length);
1453
+ if (fits) range.setStart(caret.node, caret.offset);
1454
+ else range.setStart(target, 0);
1455
+ range.collapse(true);
1456
+ registry2.start = range;
1457
+ registry2.pressed = true;
1458
+ }
1459
+ static #recordFocus(registry2, event) {
1460
+ const target = event.composedPath()[0];
1461
+ const point = registry2.pressed ? registry2.start : null;
1462
+ const at = point ? elementHoldingPosition(point.startContainer, point.startOffset) : null;
1463
+ const keep = !(target instanceof HTMLElement && isTabStop(target)) && flatTreeContains(target, at);
1464
+ registry2.pressed = false;
1465
+ if (keep) return;
1466
+ const range = target.ownerDocument.createRange();
1467
+ range.selectNode(target);
1468
+ registry2.start = range;
1469
+ }
1470
+ static #caretAt(owner, x, y, roots) {
1471
+ if (typeof owner.caretPositionFromPoint === "function") {
1472
+ const position = owner.caretPositionFromPoint(x, y, { shadowRoots: roots });
1473
+ return position ? { node: position.offsetNode, offset: position.offset } : null;
1474
+ }
1475
+ const legacy = owner;
1476
+ if (typeof legacy.caretRangeFromPoint === "function") {
1477
+ const range = legacy.caretRangeFromPoint(x, y);
1478
+ return range ? { node: range.startContainer, offset: range.startOffset } : null;
1479
+ }
1480
+ return null;
1481
+ }
1284
1482
  static #selectOnEntry(stop) {
1285
1483
  if (stop instanceof HTMLInputElement && _FocusTrap.#TEXT_ENTRY_TYPES.has(stop.type)) {
1286
1484
  stop.select();
1287
1485
  }
1288
1486
  }
1289
1487
  #focusInitial() {
1488
+ const container = this.#readContainer();
1489
+ if (!container) return;
1290
1490
  const preferred = this.#options.initialFocus?.();
1291
1491
  if (preferred) {
1292
1492
  preferred.focus();
1293
1493
  return;
1294
1494
  }
1295
- const container = this.#getContainer();
1296
1495
  const [first] = sequentialTabStops(container);
1297
1496
  if (first) {
1298
1497
  first.focus();
@@ -1393,7 +1592,13 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1393
1592
  #connected = false;
1394
1593
  #lived = new LivedMark(this.identifier);
1395
1594
  #dialog = null;
1595
+ #openState = false;
1596
+ #seedOnDialogArrival = false;
1396
1597
  #hidden = new AttributeLease("hidden", this.identifier);
1598
+ #optionHidden = new AttributeLease("hidden", this.identifier);
1599
+ #optionSelected = new AttributeLease("aria-selected", this.identifier);
1600
+ #knownOptions = /* @__PURE__ */ new Set();
1601
+ #optionDisabled = new AttributeLease("aria-disabled", this.identifier);
1397
1602
  #expanded = new AttributeLease("aria-expanded", this.identifier);
1398
1603
  #activeDescendant = new AttributeLease(
1399
1604
  "aria-activedescendant",
@@ -1418,19 +1623,24 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1418
1623
  });
1419
1624
  #listeners = new ListenerSet();
1420
1625
  connect() {
1626
+ const options = new Set(this.optionTargets);
1627
+ const departed = [...this.#knownOptions].filter((option) => !options.has(option));
1628
+ for (const option of departed) this.#returnOption(option);
1421
1629
  const connection = this.#lived.connect(this.element);
1422
1630
  this.#trap.connect();
1423
1631
  this.#listeners.add(document, "keydown", this.#onGlobalKeydown);
1424
1632
  if (this.hasInputTarget) this.#composition.observe(this.inputTarget);
1425
1633
  this.#syncOptionSemantics();
1634
+ this.#dialog = this.hasDialogTarget ? this.dialogTarget : null;
1635
+ this.#openState = this.#dialog ? !this.#dialog.hidden : this.#openState;
1426
1636
  if (connection === "reconnect" && this.#isOpen) {
1637
+ if (departed.length > 0) this.#reconcileActive();
1427
1638
  this.#trap.activate();
1428
1639
  } else {
1429
1640
  const shouldOpen = connection !== "restored" && (this.#isOpen || this.openValue);
1430
1641
  this.#resetToClosedState();
1431
1642
  if (shouldOpen) this.open();
1432
1643
  }
1433
- this.#dialog = this.hasDialogTarget ? this.dialogTarget : null;
1434
1644
  this.#connected = true;
1435
1645
  this.#reconcile.activate();
1436
1646
  this.#morphRender.observe(this.element);
@@ -1445,16 +1655,38 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1445
1655
  this.#trap.disconnect(this);
1446
1656
  }
1447
1657
  optionTargetConnected(option) {
1658
+ const original = option.getAttribute(_CommandPaletteController.#ORIGINAL_ARIA_DISABLED);
1659
+ if (original !== null) {
1660
+ option.removeAttribute(_CommandPaletteController.#ORIGINAL_ARIA_DISABLED);
1661
+ if (option.getAttribute("aria-disabled") === "true") {
1662
+ if (original === _CommandPaletteController.#ABSENT_ARIA_DISABLED) {
1663
+ option.removeAttribute("aria-disabled");
1664
+ } else {
1665
+ option.setAttribute("aria-disabled", original);
1666
+ }
1667
+ }
1668
+ }
1448
1669
  this.#syncOptionSemanticsFor(option);
1449
- option.setAttribute("aria-selected", "false");
1450
- option.removeAttribute("data-active");
1670
+ if (!this.#knownOptions.has(option)) {
1671
+ this.#knownOptions.add(option);
1672
+ syncActiveOption([option], null, this.#optionSelected);
1673
+ option.removeAttribute("data-active");
1674
+ }
1451
1675
  if (this.#connected) this.#queueOptionReconciliation();
1452
1676
  }
1453
1677
  optionTargetDisconnected(option) {
1454
- option.setAttribute("aria-selected", "false");
1455
- option.removeAttribute("data-active");
1678
+ if (!this.scope.containsElement(option) || !this.optionTargets.includes(option)) {
1679
+ this.#returnOption(option);
1680
+ }
1456
1681
  if (this.#connected) this.#queueOptionReconciliation();
1457
1682
  }
1683
+ #returnOption(option) {
1684
+ this.#knownOptions.delete(option);
1685
+ this.#optionHidden.return(option);
1686
+ this.#optionDisabled.return(option);
1687
+ this.#optionSelected.return(option);
1688
+ option.removeAttribute("data-active");
1689
+ }
1458
1690
  inputTargetConnected(input) {
1459
1691
  this.#composition.observe(input);
1460
1692
  if (this.#connected) this.#queueOptionReconciliation();
@@ -1490,11 +1722,14 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1490
1722
  }
1491
1723
  }
1492
1724
  open() {
1493
- if (!this.hasDialogTarget || this.#isOpen) return;
1494
- this.#hidden.write(this.dialogTarget, null);
1725
+ this.#adoptDialog();
1726
+ if (this.#isOpen) return;
1727
+ this.#openState = true;
1728
+ if (this.#dialog) this.#hidden.write(this.#dialog, null);
1495
1729
  this.openValue = true;
1496
1730
  if (this.hasInputTarget) this.#expanded.write(this.inputTarget, "true");
1497
- this.#resetFilter();
1731
+ this.#seedOnDialogArrival = this.#dialog === null;
1732
+ if (!this.#seedOnDialogArrival) this.#resetFilter();
1498
1733
  this.#trap.activate();
1499
1734
  }
1500
1735
  close() {
@@ -1504,20 +1739,17 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1504
1739
  const dialog = this.hasDialogTarget ? this.dialogTarget : null;
1505
1740
  if (dialog === this.#dialog) return;
1506
1741
  this.#dialog = dialog;
1507
- if (!this.#trap.active) {
1508
- if (dialog) this.#hidden.write(dialog, "");
1509
- return;
1510
- }
1511
- if (!dialog) {
1512
- this.#resetToClosedState();
1513
- this.#trap.deactivate();
1514
- return;
1742
+ if (dialog) {
1743
+ this.#hidden.write(dialog, this.#openState ? null : "");
1744
+ if (this.#seedOnDialogArrival) {
1745
+ this.#seedOnDialogArrival = false;
1746
+ this.#resetFilter();
1747
+ }
1515
1748
  }
1516
- this.#hidden.write(dialog, null);
1517
1749
  this.#trap.refreshContainer();
1518
1750
  }
1519
1751
  #close(restoreFocus) {
1520
- if (!this.hasDialogTarget || !this.#isOpen) return;
1752
+ if (!this.#isOpen) return;
1521
1753
  this.#resetToClosedState();
1522
1754
  this.#trap.deactivate({ restoreFocus });
1523
1755
  }
@@ -1532,10 +1764,10 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1532
1764
  const searchText = (option.dataset.searchValue || option.textContent || "").trim().toLowerCase();
1533
1765
  const matches = searchText.includes(query);
1534
1766
  if (matches) {
1535
- option.removeAttribute("hidden");
1536
- if (this.#isNavigable(option)) hasSelectableMatch = true;
1537
- } else {
1538
- option.setAttribute("hidden", "true");
1767
+ this.#optionHidden.return(option);
1768
+ if (!option.hasAttribute("hidden") && this.#isNavigable(option)) hasSelectableMatch = true;
1769
+ } else if (!option.hasAttribute("hidden")) {
1770
+ this.#optionHidden.write(option, "true");
1539
1771
  }
1540
1772
  }
1541
1773
  if (this.hasEmptyTarget) {
@@ -1573,6 +1805,8 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1573
1805
  if (isReservedArrowChord(event)) return;
1574
1806
  if ((event.key === "Home" || event.key === "End") && hasModifierChord(event)) return;
1575
1807
  if (this.#composition.isComposing(event) || !this.#isOpen) return;
1808
+ this.#adoptDialog();
1809
+ if (!this.#dialog) return;
1576
1810
  this.#reconcileActive();
1577
1811
  switch (event.key) {
1578
1812
  case "ArrowDown":
@@ -1613,7 +1847,7 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1613
1847
  this.#syncOptionSemantics();
1614
1848
  const visible = this.#visibleOptions;
1615
1849
  const activeOption = visible[index] ?? null;
1616
- syncActiveOption(this.optionTargets, activeOption);
1850
+ syncActiveOption(this.optionTargets, activeOption, this.#optionSelected);
1617
1851
  for (const option of this.optionTargets) {
1618
1852
  if (option === activeOption) option.setAttribute("data-active", "true");
1619
1853
  else option.removeAttribute("data-active");
@@ -1693,7 +1927,7 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1693
1927
  #resetFilter() {
1694
1928
  if (this.hasInputTarget) this.inputTarget.value = "";
1695
1929
  for (const option of this.optionTargets) {
1696
- option.removeAttribute("hidden");
1930
+ this.#optionHidden.return(option);
1697
1931
  }
1698
1932
  const hasSelectableOption = this.#visibleOptions.length > 0;
1699
1933
  this.#reflectEmptyState();
@@ -1714,28 +1948,13 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1714
1948
  for (const option of this.optionTargets) this.#syncOptionSemanticsFor(option);
1715
1949
  }
1716
1950
  #syncOptionSemanticsFor(option) {
1717
- const originalAttribute = _CommandPaletteController.#ORIGINAL_ARIA_DISABLED;
1718
- const originalValue = option.getAttribute(originalAttribute);
1719
- if (option.dataset.disabled === "true") {
1720
- if (originalValue === null) {
1721
- option.setAttribute(
1722
- originalAttribute,
1723
- option.getAttribute("aria-disabled") ?? _CommandPaletteController.#ABSENT_ARIA_DISABLED
1724
- );
1725
- }
1726
- option.setAttribute("aria-disabled", "true");
1727
- return;
1728
- }
1729
- if (originalValue === null) return;
1730
- if (originalValue === _CommandPaletteController.#ABSENT_ARIA_DISABLED) {
1731
- option.removeAttribute("aria-disabled");
1732
- } else {
1733
- option.setAttribute("aria-disabled", originalValue);
1951
+ if (option.dataset.disabled !== "true") this.#optionDisabled.return(option);
1952
+ else if (option.getAttribute("aria-disabled") !== "true") {
1953
+ this.#optionDisabled.write(option, "true");
1734
1954
  }
1735
- option.removeAttribute(originalAttribute);
1736
1955
  }
1737
1956
  get #isOpen() {
1738
- return this.hasDialogTarget && !this.dialogTarget.hidden;
1957
+ return this.#openState;
1739
1958
  }
1740
1959
  #onGlobalKeydown = (event) => {
1741
1960
  if (event.defaultPrevented) return;
@@ -1765,13 +1984,13 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
1765
1984
  return null;
1766
1985
  }
1767
1986
  #resetToClosedState() {
1987
+ this.#openState = false;
1988
+ this.#seedOnDialogArrival = false;
1768
1989
  this.#activeId = null;
1769
1990
  this.#activeOrder = [];
1770
1991
  this.openValue = false;
1771
- for (const option of this.optionTargets) {
1772
- option.setAttribute("aria-selected", "false");
1773
- option.removeAttribute("data-active");
1774
- }
1992
+ syncActiveOption(this.optionTargets, null, this.#optionSelected);
1993
+ for (const option of this.optionTargets) option.removeAttribute("data-active");
1775
1994
  if (this.hasDialogTarget) {
1776
1995
  this.#hidden.write(this.dialogTarget, "");
1777
1996
  }