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
@@ -2,6 +2,7 @@ import { Controller } from '@hotwired/stimulus';
2
2
 
3
3
  function setAttributeIfChanged(element, name, value, beforeWrite) {
4
4
  if (element.getAttribute(name) === value) return false;
5
+ beforeWrite?.();
5
6
  if (value === null) element.removeAttribute(name);
6
7
  else element.setAttribute(name, value);
7
8
  return true;
@@ -75,14 +76,34 @@ var AttributeLease = class {
75
76
  const names = registry.names;
76
77
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
77
78
  }
78
- write(element, value) {
79
+ write(element, value, beforeWrite) {
79
80
  const stack = attributeStack(element, this.#attribute);
80
81
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
81
82
  this.#held.add(element);
82
83
  const shows = !this.#covered(element, stack, hold);
83
84
  hold.written = value;
84
85
  this.#mark(element, stack);
85
- if (shows) this.#reflect(element, value);
86
+ if (shows) this.#reflect(element, value, beforeWrite);
87
+ }
88
+ adopt(element, options = {}) {
89
+ const known = registry.stacks.get(element)?.has(this.#attribute);
90
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
91
+ const stack = attributeStack(element, this.#attribute);
92
+ const hold = this.#take(stack);
93
+ if (!hold) return false;
94
+ const current = element.getAttribute(this.#attribute);
95
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
96
+ return false;
97
+ }
98
+ this.#held.add(element);
99
+ hold.original = current;
100
+ hold.linked = false;
101
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
102
+ if (!above.linked) break;
103
+ above.original = current;
104
+ }
105
+ this.#mark(element, stack);
106
+ return true;
86
107
  }
87
108
  return(element) {
88
109
  const held = this.#held.delete(element);
@@ -148,8 +169,8 @@ var AttributeLease = class {
148
169
  setAttributeIfChanged(element, name, recorded);
149
170
  }
150
171
  }
151
- #reflect(element, value) {
152
- setAttributeIfChanged(element, this.#attribute, value);
172
+ #reflect(element, value, beforeWrite) {
173
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
153
174
  }
154
175
  };
155
176
 
@@ -408,7 +429,8 @@ function ownedScope(element) {
408
429
  function orderScope(elements, walk) {
409
430
  const entries = [];
410
431
  const visit = (element) => {
411
- if (element === walk.from) walk.fromAt = walk.tree.length;
432
+ const isFrom = element === walk.from;
433
+ if (isFrom && walk.side !== "after") walk.fromAt = walk.tree.length;
412
434
  const tabindex = authoredTabindex(element) ?? 0;
413
435
  let stop = null;
414
436
  if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
@@ -419,15 +441,23 @@ function orderScope(elements, walk) {
419
441
  if (owned) {
420
442
  if (tabindex >= 0) {
421
443
  entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
422
- } else if (walk.from && flatTreeContains(element, walk.from)) {
444
+ } else if (walk.from && flatTreeContains(element, walk.from) && (!isFrom || walk.side === "on")) {
423
445
  walk.fromAt = walk.tree.length;
424
446
  walk.excludedScope = element;
425
- orderScope(owned, { from: walk.from, tree: walk.tree, fromAt: -1, excludedScope: null });
447
+ orderScope(owned, {
448
+ from: walk.from,
449
+ side: walk.side,
450
+ tree: walk.tree,
451
+ fromAt: -1,
452
+ excludedScope: null
453
+ });
426
454
  }
455
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
427
456
  return;
428
457
  }
429
458
  if (stop) entries.push({ tabindex, stops: [stop] });
430
459
  for (const child of Array.from(element.children)) visit(child);
460
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
431
461
  };
432
462
  for (const element of elements) visit(element);
433
463
  const positive = entries.filter((entry) => entry.tabindex > 0);
@@ -435,26 +465,27 @@ function orderScope(elements, walk) {
435
465
  const rest = entries.filter((entry) => entry.tabindex === 0);
436
466
  return [...positive, ...rest].flatMap((entry) => entry.stops);
437
467
  }
438
- function passesRadioRule(element, tree, focusedRadio) {
468
+ function passesRadioRule(element, tree, focusedRadio, backward) {
439
469
  if (!isRadio(element) || element.checked) return true;
440
470
  const group = tree.filter(
441
471
  (stop) => stop === element || isRadio(stop) && sameRadioGroup(stop, element)
442
472
  );
443
473
  if (group.some((radio) => radio.checked)) return false;
444
- if (group[0] !== element) return false;
474
+ if (group[backward ? group.length - 1 : 0] !== element) return false;
445
475
  return focusedRadio === null || !sameRadioGroup(focusedRadio, element);
446
476
  }
447
477
  function startIndex(order, walk, backward, isStop) {
448
478
  if (walk.fromAt < 0) return backward ? order.length - 1 : 0;
449
- const at = order.indexOf(walk.from);
479
+ const at = walk.side === "on" ? order.indexOf(walk.from) : -1;
450
480
  if (at >= 0) return backward ? at - 1 : at + 1;
451
481
  const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
452
482
  if (neighbour) return order.indexOf(neighbour);
453
483
  return backward ? -1 : order.length;
454
484
  }
455
- function movesWithin(root, from, backward, wrap, radioContext) {
485
+ function movesWithin(root, from, backward, wrap, radioContext, side = "on", position = { holder: null }) {
456
486
  const walk = {
457
487
  from,
488
+ side,
458
489
  tree: [],
459
490
  fromAt: from === root ? 0 : -1,
460
491
  excludedScope: null
@@ -462,26 +493,111 @@ function movesWithin(root, from, backward, wrap, radioContext) {
462
493
  const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
463
494
  const radios = radioContext ?? {
464
495
  tree: walk.tree,
465
- focusedRadio: walk.fromAt >= 0 && isRadio(from) ? from : null
496
+ focusedRadio: side === "on" && walk.fromAt >= 0 && isRadio(from) ? from : null
466
497
  };
467
- const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio);
498
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio, backward);
499
+ const holds = (element) => position.holder !== null && flatTreeContains(element, position.holder);
468
500
  const start = startIndex(
469
501
  order,
470
502
  walk,
471
503
  backward,
472
- (element) => order.includes(element) && isStop(element)
504
+ (element) => order.includes(element) && isStop(element) && !holds(element)
473
505
  );
474
506
  const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
475
507
  const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
476
508
  const excluded = walk.excludedScope;
477
- const prefix = excluded && !(backward && from === excluded) ? movesWithin(excluded, from === excluded ? null : from, backward, false, radios) : [];
509
+ const prefix = excluded && !(backward && from === excluded) ? movesWithin(
510
+ excluded,
511
+ from === excluded ? null : from,
512
+ backward,
513
+ false,
514
+ radios,
515
+ side,
516
+ position
517
+ ) : [];
478
518
  return [
479
519
  ...prefix,
480
- ...[...remaining, ...wrapped].filter((element) => element !== from && isStop(element))
520
+ ...[...remaining, ...wrapped].filter(
521
+ (element) => (side !== "on" || element !== from) && isStop(element)
522
+ )
481
523
  ];
482
524
  }
525
+ function flatParentNode(node) {
526
+ const parent = node.parentNode;
527
+ if (parent instanceof Element && parent.shadowRoot) {
528
+ for (const slot of parent.shadowRoot.querySelectorAll("slot")) {
529
+ if (slot.assignedNodes().includes(node)) return slot;
530
+ }
531
+ }
532
+ return parent instanceof ShadowRoot ? parent.host : parent;
533
+ }
534
+ function flatChildNodes(node) {
535
+ if (node instanceof Element && node.shadowRoot) return Array.from(node.shadowRoot.childNodes);
536
+ if (node instanceof HTMLSlotElement) {
537
+ const assigned = node.assignedNodes();
538
+ return assigned.length > 0 ? assigned : Array.from(node.childNodes);
539
+ }
540
+ return Array.from(node.childNodes);
541
+ }
542
+ function flatNodeAfter(node, root) {
543
+ for (let current = node; current && current !== root; ) {
544
+ const parent = flatParentNode(current);
545
+ if (!parent) return null;
546
+ const siblings = flatChildNodes(parent);
547
+ const index = siblings.indexOf(current);
548
+ const next = index >= 0 ? siblings[index + 1] : void 0;
549
+ if (next) return next;
550
+ current = parent;
551
+ }
552
+ return null;
553
+ }
554
+ function flatElementBefore(node, root) {
555
+ for (let current = node; current && current !== root; ) {
556
+ const parent = flatParentNode(current);
557
+ if (!parent) return null;
558
+ const siblings = flatChildNodes(parent);
559
+ for (let index = siblings.indexOf(current) - 1; index >= 0; index--) {
560
+ const sibling = siblings[index];
561
+ if (sibling instanceof Element) return sibling;
562
+ }
563
+ current = parent;
564
+ }
565
+ return null;
566
+ }
567
+ function elementAfterPosition(container, offset, root) {
568
+ const owner = container instanceof ShadowRoot ? container.host : container;
569
+ const child = container.childNodes[offset];
570
+ const previous = container.childNodes[offset - 1];
571
+ let node = child ?? (previous ? flatNodeAfter(previous, root) : flatChildNodes(owner)[0] ?? flatNodeAfter(owner, root));
572
+ while (node && !(node instanceof Element)) {
573
+ node = flatChildNodes(node)[0] ?? flatNodeAfter(node, root);
574
+ }
575
+ return node;
576
+ }
577
+ function elementBeforePosition(container, offset, root) {
578
+ const child = container.childNodes[offset];
579
+ if (child) return flatElementBefore(child, root);
580
+ const previous = container.childNodes[offset - 1];
581
+ if (previous) return previous instanceof Element ? previous : flatElementBefore(previous, root);
582
+ return flatElementBefore(container instanceof ShadowRoot ? container.host : container, root);
583
+ }
584
+ function elementHoldingPosition(container, offset) {
585
+ if (container instanceof Element && container.shadowRoot) {
586
+ const near = container.childNodes[offset] ?? container.childNodes[offset - 1];
587
+ return near ? flatParentNode(near) : container;
588
+ }
589
+ if (container instanceof Element) return container;
590
+ if (container instanceof ShadowRoot) return container.host;
591
+ return flatParentNode(container);
592
+ }
483
593
  function sequentialTabStops(root, from = null, backward = false) {
484
- return movesWithin(root, from, backward, true);
594
+ if (!(from instanceof Range)) return movesWithin(root, from, backward, true);
595
+ const container = backward ? from.startContainer : from.endContainer;
596
+ const offset = backward ? from.startOffset : from.endOffset;
597
+ const anchor = backward ? elementBeforePosition(container, offset, root) : elementAfterPosition(container, offset, root);
598
+ return movesWithin(root, anchor, backward, true, void 0, backward ? "after" : "before", {
599
+ holder: elementHoldingPosition(container, offset)
600
+ });
485
601
  }
486
602
  function deepActiveElement(ownerDocument) {
487
603
  let active = ownerDocument.activeElement;
@@ -645,7 +761,7 @@ var INERT_MARK = `data-${FOCUS_TRAP_OWNER}-inert`;
645
761
  var OVERFLOW_MARK = `data-${FOCUS_TRAP_OWNER}-overflow`;
646
762
  var FocusTrap = class _FocusTrap {
647
763
  static #registries = sharedRegistry(
648
- "stimeo-ui.focus-trap.registry.v2",
764
+ "stimeo-ui.focus-trap.registry.v3",
649
765
  () => /* @__PURE__ */ new WeakMap()
650
766
  );
651
767
  static #TEXT_ENTRY_TYPES = /* @__PURE__ */ new Set([
@@ -671,15 +787,17 @@ var FocusTrap = class _FocusTrap {
671
787
  this.#entry = this.#createEntry();
672
788
  }
673
789
  #createEntry() {
674
- return {
790
+ const entry = {
675
791
  container: () => this.#readContainer(),
676
792
  moveFocus: (backward) => this.#moveFocus(backward),
677
793
  refocus: () => this.#refocus(),
678
794
  isolated: false,
679
795
  lockedBody: null,
680
796
  openedFrom: null,
681
- handedBack: []
797
+ handedBack: [],
798
+ returnTargets: () => this.#activeReturnTargets(entry)
682
799
  };
800
+ return entry;
683
801
  }
684
802
  get active() {
685
803
  return this.#activeState;
@@ -705,6 +823,7 @@ var FocusTrap = class _FocusTrap {
705
823
  return;
706
824
  }
707
825
  const entry = this.#entry;
826
+ const parent = entry.openedFrom;
708
827
  const openers = this.#openers;
709
828
  this.#openers = [];
710
829
  this.#activeState = false;
@@ -714,7 +833,7 @@ var FocusTrap = class _FocusTrap {
714
833
  _FocusTrap.#reconcileBackground(document);
715
834
  try {
716
835
  const wayBack = this.#handOver(entry, openers, above);
717
- if (restoreFocus) this.#restore(wayBack, above);
836
+ if (restoreFocus) this.#restore(wayBack, above, parent);
718
837
  } finally {
719
838
  this.#returnTabindexes();
720
839
  }
@@ -753,6 +872,11 @@ var FocusTrap = class _FocusTrap {
753
872
  document.addEventListener("keydown", registry2.onKeydown);
754
873
  document.addEventListener("turbo:morph", registry2.onMorph);
755
874
  document.addEventListener("turbo:morph-element", registry2.onMorph);
875
+ document.addEventListener("pointerdown", registry2.onPointerDown, {
876
+ capture: true,
877
+ passive: true
878
+ });
879
+ document.addEventListener("focusin", registry2.onFocusIn, { capture: true });
756
880
  }
757
881
  registry2.stack.push(this.#entry);
758
882
  }
@@ -765,6 +889,10 @@ var FocusTrap = class _FocusTrap {
765
889
  document.removeEventListener("keydown", registry2.onKeydown);
766
890
  document.removeEventListener("turbo:morph", registry2.onMorph);
767
891
  document.removeEventListener("turbo:morph-element", registry2.onMorph);
892
+ document.removeEventListener("pointerdown", registry2.onPointerDown, { capture: true });
893
+ document.removeEventListener("focusin", registry2.onFocusIn, { capture: true });
894
+ registry2.start = null;
895
+ registry2.pressed = false;
768
896
  }
769
897
  return above;
770
898
  }
@@ -801,7 +929,7 @@ var FocusTrap = class _FocusTrap {
801
929
  }
802
930
  return wayBack;
803
931
  }
804
- #restore(wayBack, above) {
932
+ #restore(wayBack, above, parent) {
805
933
  const active = deepActiveElement(document);
806
934
  const released = this.#readContainer();
807
935
  const holds = (trap) => {
@@ -812,7 +940,7 @@ var FocusTrap = class _FocusTrap {
812
940
  return !released || !flatTreeContains(released, active) || flatTreeContains(released, container);
813
941
  };
814
942
  if (above.some(holds)) return;
815
- for (const element of wayBack) {
943
+ for (const element of _FocusTrap.#returnCandidates(wayBack, parent)) {
816
944
  if (this.#activeState) return;
817
945
  if (!element.isConnected || !canTakeFocus(element) || !isRenderedForFocus(element)) continue;
818
946
  const before = deepActiveElement(document);
@@ -822,6 +950,19 @@ var FocusTrap = class _FocusTrap {
822
950
  }
823
951
  _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack)?.refocus();
824
952
  }
953
+ *#activeReturnTargets(entry) {
954
+ if (!this.#activeState || this.#entry !== entry) return;
955
+ yield* this.#openers;
956
+ const fallback = this.#options.fallbackFocus?.();
957
+ if (fallback) yield fallback;
958
+ yield* entry.handedBack;
959
+ }
960
+ static *#returnCandidates(wayBack, parent) {
961
+ yield* wayBack;
962
+ for (let entry = parent; entry && !_FocusTrap.#takesTab(entry); entry = entry.openedFrom) {
963
+ yield* entry.returnTargets?.() ?? [];
964
+ }
965
+ }
825
966
  #refocus() {
826
967
  if (!this.#flag(this.#options.autoFocus, true)) return;
827
968
  if (flatTreeContains(this.#getContainer(), deepActiveElement(document))) return;
@@ -836,8 +977,8 @@ var FocusTrap = class _FocusTrap {
836
977
  const focused = deepActiveElement(document);
837
978
  for (const [element, loan] of [...this.#tabindexLoans]) {
838
979
  if (element === focused) continue;
839
- loan.returnAll();
840
980
  this.#tabindexLoans.delete(element);
981
+ loan.returnAll();
841
982
  }
842
983
  }
843
984
  #lockScroll() {
@@ -882,8 +1023,12 @@ var FocusTrap = class _FocusTrap {
882
1023
  stack: [],
883
1024
  onKeydown: (event) => _FocusTrap.#resolveTab(registry2.stack, event),
884
1025
  onMorph: () => morphs.schedule(),
1026
+ onPointerDown: (event) => _FocusTrap.#recordPress(registry2, event),
1027
+ onFocusIn: (event) => _FocusTrap.#recordFocus(registry2, event),
885
1028
  inerted: /* @__PURE__ */ new Set(),
886
- scrollLocks: /* @__PURE__ */ new Set()
1029
+ scrollLocks: /* @__PURE__ */ new Set(),
1030
+ start: null,
1031
+ pressed: false
887
1032
  };
888
1033
  _FocusTrap.#registries.set(ownerDocument, registry2);
889
1034
  return registry2;
@@ -981,7 +1126,7 @@ var FocusTrap = class _FocusTrap {
981
1126
  return _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack) === this.#entry;
982
1127
  }
983
1128
  #moveFocus(backward) {
984
- const from = deepActiveElement(document);
1129
+ const from = _FocusTrap.#start();
985
1130
  for (const stop of sequentialTabStops(this.#getContainer(), from, backward)) {
986
1131
  if (!this.#ownsTab()) return;
987
1132
  const before = deepActiveElement(document);
@@ -992,18 +1137,66 @@ var FocusTrap = class _FocusTrap {
992
1137
  return;
993
1138
  }
994
1139
  }
1140
+ static #start() {
1141
+ const active = deepActiveElement(document);
1142
+ const start = _FocusTrap.#registry(document).start;
1143
+ if (active && active !== document.body) {
1144
+ const at = start ? elementHoldingPosition(start.startContainer, start.startOffset) : null;
1145
+ const pointInside = !(active instanceof HTMLElement && isTabStop(active)) && flatTreeContains(active, at);
1146
+ return pointInside ? start : active;
1147
+ }
1148
+ return start ?? active;
1149
+ }
1150
+ static #recordPress(registry2, event) {
1151
+ const path = event.composedPath();
1152
+ const target = path[0];
1153
+ const owner = target.ownerDocument ?? document;
1154
+ const roots = path.filter((node) => node instanceof ShadowRoot);
1155
+ const caret = _FocusTrap.#caretAt(owner, event.clientX, event.clientY, roots);
1156
+ const range = owner.createRange();
1157
+ const fits = caret !== null && target.contains(caret.node) && caret.offset <= (caret.node instanceof CharacterData ? caret.node.length : caret.node.childNodes.length);
1158
+ if (fits) range.setStart(caret.node, caret.offset);
1159
+ else range.setStart(target, 0);
1160
+ range.collapse(true);
1161
+ registry2.start = range;
1162
+ registry2.pressed = true;
1163
+ }
1164
+ static #recordFocus(registry2, event) {
1165
+ const target = event.composedPath()[0];
1166
+ const point = registry2.pressed ? registry2.start : null;
1167
+ const at = point ? elementHoldingPosition(point.startContainer, point.startOffset) : null;
1168
+ const keep = !(target instanceof HTMLElement && isTabStop(target)) && flatTreeContains(target, at);
1169
+ registry2.pressed = false;
1170
+ if (keep) return;
1171
+ const range = target.ownerDocument.createRange();
1172
+ range.selectNode(target);
1173
+ registry2.start = range;
1174
+ }
1175
+ static #caretAt(owner, x, y, roots) {
1176
+ if (typeof owner.caretPositionFromPoint === "function") {
1177
+ const position = owner.caretPositionFromPoint(x, y, { shadowRoots: roots });
1178
+ return position ? { node: position.offsetNode, offset: position.offset } : null;
1179
+ }
1180
+ const legacy = owner;
1181
+ if (typeof legacy.caretRangeFromPoint === "function") {
1182
+ const range = legacy.caretRangeFromPoint(x, y);
1183
+ return range ? { node: range.startContainer, offset: range.startOffset } : null;
1184
+ }
1185
+ return null;
1186
+ }
995
1187
  static #selectOnEntry(stop) {
996
1188
  if (stop instanceof HTMLInputElement && _FocusTrap.#TEXT_ENTRY_TYPES.has(stop.type)) {
997
1189
  stop.select();
998
1190
  }
999
1191
  }
1000
1192
  #focusInitial() {
1193
+ const container = this.#readContainer();
1194
+ if (!container) return;
1001
1195
  const preferred = this.#options.initialFocus?.();
1002
1196
  if (preferred) {
1003
1197
  preferred.focus();
1004
1198
  return;
1005
1199
  }
1006
- const container = this.#getContainer();
1007
1200
  const [first] = sequentialTabStops(container);
1008
1201
  if (first) {
1009
1202
  first.focus();
@@ -1300,7 +1493,7 @@ var SidebarController = class _SidebarController extends Controller {
1300
1493
  #transition = new TransitionCompletion();
1301
1494
  #connected = false;
1302
1495
  #storageKeyRead = null;
1303
- #expanded = false;
1496
+ #overlayOpen = false;
1304
1497
  #backdrop = {
1305
1498
  state: "closed",
1306
1499
  hidden: true
@@ -1321,10 +1514,15 @@ var SidebarController = class _SidebarController extends Controller {
1321
1514
  this.#mql = this.#matchBreakpoint(this.#mqlQuery);
1322
1515
  if (this.#mql) this.#mediaListeners.add(this.#mql, "change", this.#onMediaChange);
1323
1516
  const mode = this.#computeMode();
1324
- const resume = this.#lived && mode === "overlay" && this.#isOverlayOpen;
1517
+ const startsOpen = this.#activePanel ? this.#activePanel.getAttribute("data-state") === "open" : this.#overlayOpen;
1518
+ const resume = this.#lived && this.#isOverlay && mode === "overlay" && startsOpen;
1325
1519
  this.#lived = true;
1326
- if (resume) this.#trap.activate();
1327
- else this.#applyMode(mode, false);
1520
+ if (resume) {
1521
+ this.#overlayOpen = true;
1522
+ this.#trap.activate();
1523
+ } else {
1524
+ this.#applyMode(mode, false);
1525
+ }
1328
1526
  this.#morphRender.observe(this.element);
1329
1527
  }
1330
1528
  disconnect() {
@@ -1337,32 +1535,16 @@ var SidebarController = class _SidebarController extends Controller {
1337
1535
  this.#trap.disconnect(this);
1338
1536
  this.#activePanel = null;
1339
1537
  }
1340
- panelTargetConnected(panel) {
1341
- if (!this.#connected || this.#activePanel?.isConnected) return;
1342
- this.#adoptPanel(panel);
1538
+ panelTargetConnected(_panel) {
1539
+ this.#adoptPanel();
1343
1540
  }
1344
1541
  panelTargetDisconnected(panel) {
1345
- const stays = this.panelTargets.includes(panel);
1346
- if (!stays) {
1542
+ if (!this.panelTargets.includes(panel)) {
1347
1543
  this.#modeLease.return(panel);
1348
1544
  this.#stateLease.return(panel);
1349
1545
  this.#hiddenLease.return(panel);
1350
1546
  }
1351
- if (panel !== this.#activePanel || stays && this.panelTarget === panel) return;
1352
- this.#transition.cancel();
1353
- this.#activePanel = null;
1354
- if (stays) {
1355
- this.#writeState(panel, "closed");
1356
- this.#writeHidden(panel, true);
1357
- }
1358
- this.#hideBackdrop();
1359
- this.#setExpandedAttr(false);
1360
- if (!this.#connected) {
1361
- this.#trap.deactivate({ restoreFocus: false });
1362
- return;
1363
- }
1364
- if (this.hasPanelTarget) this.#adoptPanel(this.panelTarget);
1365
- else this.#trap.deactivate();
1547
+ this.#adoptPanel();
1366
1548
  }
1367
1549
  triggerTargetConnected() {
1368
1550
  if (this.#connected) this.#reflectExpanded();
@@ -1433,6 +1615,7 @@ var SidebarController = class _SidebarController extends Controller {
1433
1615
  this.#mode = mode;
1434
1616
  if (this.hasPanelTarget) this.#modeLease.write(this.panelTarget, mode);
1435
1617
  if (mode === "inline") {
1618
+ this.#overlayOpen = false;
1436
1619
  this.#transition.cancel();
1437
1620
  if (report) this.#trap.deactivate({ restoreFocus: false });
1438
1621
  if (this.hasPanelTarget) this.#writeHidden(this.panelTarget, false);
@@ -1451,11 +1634,11 @@ var SidebarController = class _SidebarController extends Controller {
1451
1634
  }
1452
1635
  const panel = this.panelTarget;
1453
1636
  this.#modeLease.write(panel, "overlay");
1454
- this.#setOverlayState(this.#expanded ? "open" : "closed");
1455
- const hidden = !this.#expanded && !this.#trap.active;
1637
+ this.#setOverlayState(this.#overlayOpen ? "open" : "closed");
1638
+ const hidden = !this.#overlayOpen && !this.#trap.active;
1456
1639
  this.#writeHidden(panel, hidden);
1457
1640
  this.#writeBackdropHidden(hidden);
1458
- this.#setExpandedAttr(this.#expanded);
1641
+ this.#reflectExpanded();
1459
1642
  }
1460
1643
  #reportReconcile() {
1461
1644
  this.dispatch("reconcile", {
@@ -1463,30 +1646,36 @@ var SidebarController = class _SidebarController extends Controller {
1463
1646
  cancelable: false
1464
1647
  });
1465
1648
  }
1466
- #adoptPanel(panel) {
1649
+ #adoptPanel() {
1650
+ if (!this.#connected) return;
1651
+ const panel = this.hasPanelTarget ? this.panelTarget : null;
1652
+ const previous = this.#activePanel;
1653
+ if (panel === previous) return;
1467
1654
  this.#transition.cancel();
1468
1655
  this.#activePanel = panel;
1469
- this.#modeLease.write(panel, this.#mode);
1656
+ if (previous && this.#isOverlay && this.panelTargets.includes(previous)) {
1657
+ this.#writeState(previous, "closed");
1658
+ this.#writeHidden(previous, true);
1659
+ }
1660
+ if (panel) this.#modeLease.write(panel, this.#mode);
1470
1661
  if (this.#mode === "inline") {
1471
- this.#writeHidden(panel, false);
1472
- this.#writeState(panel, this.#collapsed ? "collapsed" : "expanded");
1662
+ if (panel) this.#writeHidden(panel, false);
1473
1663
  this.#hideBackdrop();
1474
- this.#setExpandedAttr(!this.#collapsed);
1664
+ this.#applyInlineState(this.#collapsed);
1475
1665
  return;
1476
1666
  }
1477
- if (panel.getAttribute("data-state") === "open") {
1478
- this.#writeHidden(panel, false);
1479
- this.#writeBackdropState("open");
1480
- this.#writeBackdropHidden(false);
1481
- this.#setExpandedAttr(true);
1482
- if (this.#trap.active) this.#trap.refreshContainer();
1483
- else this.#trap.activate();
1667
+ if (this.#isOverlayOpen) {
1668
+ if (panel) {
1669
+ this.#writeHidden(panel, false);
1670
+ this.#setOverlayState("open");
1671
+ this.#writeBackdropHidden(false);
1672
+ } else {
1673
+ this.#hideBackdrop();
1674
+ }
1675
+ this.#trap.refreshContainer();
1484
1676
  return;
1485
1677
  }
1486
- this.#writeState(panel, "closed");
1487
- this.#writeHidden(panel, true);
1488
- this.#hideBackdrop();
1489
- this.#setExpandedAttr(false);
1678
+ this.#setOverlayClosedImmediate();
1490
1679
  this.#trap.deactivate();
1491
1680
  }
1492
1681
  #onMediaChange = (event) => {
@@ -1511,32 +1700,46 @@ var SidebarController = class _SidebarController extends Controller {
1511
1700
  if (this.hasPanelTarget) {
1512
1701
  this.#writeState(this.panelTarget, collapsed ? "collapsed" : "expanded");
1513
1702
  }
1514
- this.#setExpandedAttr(!collapsed);
1703
+ this.#reflectExpanded();
1515
1704
  }
1516
1705
  #openOverlay(reason) {
1517
- if (!this.hasPanelTarget || this.#isOverlayOpen) return;
1706
+ this.#adoptPanel();
1707
+ if (this.#isOverlayOpen) return;
1518
1708
  this.#transition.cancel();
1519
- this.#activePanel = this.panelTarget;
1520
- this.#writeHidden(this.panelTarget, false);
1521
- this.#writeBackdropHidden(false);
1522
- void this.panelTarget.offsetWidth;
1523
- this.#setOverlayState("open");
1524
- this.#setExpandedAttr(true);
1709
+ if (this.#activePanel) {
1710
+ this.#writeHidden(this.#activePanel, false);
1711
+ this.#writeBackdropHidden(false);
1712
+ void this.#activePanel.offsetWidth;
1713
+ this.#setOverlayState("open");
1714
+ }
1715
+ this.#overlayOpen = true;
1716
+ this.#reflectExpanded();
1525
1717
  this.#report(true, reason);
1526
- if (!this.#isOverlayOpen) return;
1718
+ if (!this.#connected || !this.#isOverlayOpen) return;
1719
+ this.#adoptPanel();
1527
1720
  this.#trap.activate();
1528
1721
  }
1529
1722
  #closeOverlay(reason) {
1530
- if (!this.hasPanelTarget || !this.#isOverlayOpen) return;
1723
+ if (!this.#isOverlayOpen) return;
1724
+ this.#adoptPanel();
1725
+ if (!this.#connected || !this.#isOverlayOpen) return;
1726
+ this.#overlayOpen = false;
1531
1727
  this.#setOverlayState("closed");
1532
- this.#setExpandedAttr(false);
1728
+ this.#reflectExpanded();
1533
1729
  this.#report(false, reason);
1534
- if (this.#isOverlayOpen) return;
1535
- this.#hideAfterTransition();
1730
+ if (!this.#connected || this.#isOverlayOpen) return;
1731
+ const panel = this.hasPanelTarget ? this.panelTarget : null;
1732
+ if (panel !== this.#activePanel) this.#adoptPanel();
1733
+ else if (panel) this.#hideAfterTransition(panel);
1734
+ else {
1735
+ this.#hideBackdrop();
1736
+ this.#trap.deactivate();
1737
+ }
1536
1738
  }
1537
1739
  #setOverlayClosedImmediate() {
1740
+ this.#overlayOpen = false;
1538
1741
  this.#setOverlayState("closed");
1539
- this.#setExpandedAttr(false);
1742
+ this.#reflectExpanded();
1540
1743
  if (this.hasPanelTarget) this.#writeHidden(this.panelTarget, true);
1541
1744
  this.#hideBackdrop();
1542
1745
  }
@@ -1544,9 +1747,7 @@ var SidebarController = class _SidebarController extends Controller {
1544
1747
  if (this.hasPanelTarget) this.#writeState(this.panelTarget, state);
1545
1748
  this.#writeBackdropState(state);
1546
1749
  }
1547
- #hideAfterTransition() {
1548
- const panel = this.panelTarget;
1549
- this.#activePanel = panel;
1750
+ #hideAfterTransition(panel) {
1550
1751
  this.#transition.wait(panel, () => this.#applyOverlayHidden(panel));
1551
1752
  }
1552
1753
  #applyOverlayHidden(panel) {
@@ -1576,13 +1777,9 @@ var SidebarController = class _SidebarController extends Controller {
1576
1777
  this.#writeBackdropState(this.#backdrop.state);
1577
1778
  this.#writeBackdropHidden(this.#backdrop.hidden);
1578
1779
  }
1579
- #setExpandedAttr(expanded) {
1580
- this.#expanded = expanded;
1581
- this.#reflectExpanded();
1582
- }
1583
1780
  #reflectExpanded() {
1584
1781
  if (this.hasTriggerTarget) {
1585
- this.#expandedLease.write(this.triggerTarget, this.#expanded ? "true" : "false");
1782
+ this.#expandedLease.write(this.triggerTarget, this.#isExpanded ? "true" : "false");
1586
1783
  }
1587
1784
  }
1588
1785
  #report(open, reason) {
@@ -1624,7 +1821,7 @@ var SidebarController = class _SidebarController extends Controller {
1624
1821
  return this.#mode === "overlay";
1625
1822
  }
1626
1823
  get #isOverlayOpen() {
1627
- return this.#isOverlay && this.hasPanelTarget && this.panelTarget.getAttribute("data-state") === "open";
1824
+ return this.#isOverlay && this.#overlayOpen;
1628
1825
  }
1629
1826
  get #safeBreakpoint() {
1630
1827
  return this.#numbers.read(