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();
@@ -1248,10 +1441,13 @@ var DrawerController = class extends Controller {
1248
1441
  static events = ["close", "open"];
1249
1442
  #activePanel = null;
1250
1443
  #openState = false;
1251
- #trap = new FocusTrap(() => this.#activePanel ?? this.panelTarget, {
1252
- onEscape: () => this.#close("escape"),
1253
- fallbackFocus: () => this.hasTriggerTarget ? this.triggerTarget : null
1254
- });
1444
+ #trap = new FocusTrap(
1445
+ () => this.#activePanel ?? (this.hasPanelTarget ? this.panelTarget : this.hasOverlayTarget ? this.overlayTarget : this.element),
1446
+ {
1447
+ onEscape: () => this.#close("escape"),
1448
+ fallbackFocus: () => this.hasTriggerTarget ? this.triggerTarget : null
1449
+ }
1450
+ );
1255
1451
  #transition = new TransitionCompletion();
1256
1452
  #connected = false;
1257
1453
  #reporting = false;
@@ -1267,7 +1463,8 @@ var DrawerController = class extends Controller {
1267
1463
  this.#connected = true;
1268
1464
  this.#activePanel = this.hasPanelTarget ? this.panelTarget : null;
1269
1465
  this.#reflectPlacement();
1270
- const shouldOpen = !restored && (this.#isOpen || this.openValue);
1466
+ const startsOpen = this.#activePanel ? this.#activePanel.getAttribute("data-state") === "open" : this.#openState;
1467
+ const shouldOpen = !restored && (startsOpen || this.openValue);
1271
1468
  this.#applyClosedState();
1272
1469
  if (restored) this.openValue = false;
1273
1470
  if (shouldOpen) this.#open("api");
@@ -1293,23 +1490,26 @@ var DrawerController = class extends Controller {
1293
1490
  this.#hiddenLease.return(panel);
1294
1491
  this.#placementLease.return(panel);
1295
1492
  }
1296
- if (panel !== this.#activePanel || stays && this.panelTarget === panel) return;
1493
+ if (!this.#connected || panel !== this.#activePanel || stays && this.panelTarget === panel)
1494
+ return;
1297
1495
  this.#transition.cancel();
1298
1496
  this.#activePanel = null;
1299
- if (!this.#connected) {
1300
- this.#trap.deactivate({ restoreFocus: false });
1301
- return;
1302
- }
1303
- this.#openState = false;
1304
1497
  if (stays) {
1305
1498
  this.#writeState(panel, "closed");
1306
1499
  this.#writeHidden(panel, true);
1307
1500
  }
1308
- this.#writeOverlayState("closed");
1309
- this.#writeOverlayHidden(true);
1310
- this.openValue = false;
1311
- if (this.hasPanelTarget) this.#adoptPanel(this.panelTarget);
1312
- else this.#trap.deactivate();
1501
+ if (this.hasPanelTarget) {
1502
+ this.#adoptPanel(this.panelTarget);
1503
+ return;
1504
+ }
1505
+ if (!this.#openState) {
1506
+ this.#writeOverlayState("closed");
1507
+ this.#writeOverlayHidden(true);
1508
+ this.openValue = false;
1509
+ this.#trap.deactivate();
1510
+ } else {
1511
+ this.#trap.refreshContainer();
1512
+ }
1313
1513
  }
1314
1514
  overlayTargetConnected() {
1315
1515
  this.#morphRender.schedule();
@@ -1330,17 +1530,26 @@ var DrawerController = class extends Controller {
1330
1530
  this.#open(stateReasonFor(event));
1331
1531
  }
1332
1532
  #open(reason) {
1333
- if (!this.hasPanelTarget || this.#isOpen) return;
1533
+ const panel = this.hasPanelTarget ? this.panelTarget : null;
1534
+ if (this.#openState) {
1535
+ if (panel && panel !== this.#activePanel) this.#adoptPanel(panel);
1536
+ return;
1537
+ }
1538
+ if (!panel && !this.hasOverlayTarget) return;
1334
1539
  this.#transition.cancel();
1335
- this.#activePanel = this.panelTarget;
1336
- this.#writeHidden(this.panelTarget, false);
1540
+ this.#activePanel = panel;
1541
+ if (panel) this.#writeHidden(panel, false);
1337
1542
  this.#writeOverlayHidden(false);
1338
- void this.panelTarget.offsetWidth;
1543
+ void panel?.offsetWidth;
1339
1544
  this.#setState("open");
1340
1545
  this.openValue = true;
1341
1546
  if (this.#reporting) this.dispatch("open", { detail: { reason }, cancelable: false });
1342
- if (!this.#isOpen) return;
1343
- this.#trap.activate();
1547
+ if (!this.#connected || !this.#isOpen) return;
1548
+ if (this.hasPanelTarget && this.panelTarget !== this.#activePanel) {
1549
+ this.#adoptPanel(this.panelTarget);
1550
+ } else {
1551
+ this.#trap.activate();
1552
+ }
1344
1553
  }
1345
1554
  close(event) {
1346
1555
  if (!this.#connected) return;
@@ -1351,11 +1560,21 @@ var DrawerController = class extends Controller {
1351
1560
  if (this.hasOverlayTarget && event.target === this.overlayTarget) this.#close("outside");
1352
1561
  }
1353
1562
  #close(reason) {
1354
- if (!this.hasPanelTarget || !this.#isOpen) return;
1563
+ if (!this.#openState) return;
1564
+ if (this.hasPanelTarget && this.panelTarget !== this.#activePanel) {
1565
+ this.#adoptPanel(this.panelTarget);
1566
+ if (!this.#connected || !this.#openState) return;
1567
+ }
1355
1568
  this.openValue = false;
1356
1569
  this.#setState("closed");
1357
1570
  if (this.#reporting) this.dispatch("close", { detail: { reason }, cancelable: false });
1358
- if (this.#isOpen) return;
1571
+ if (!this.#connected || this.#isOpen) return;
1572
+ if (!this.hasPanelTarget) {
1573
+ this.#transition.cancel();
1574
+ this.#writeOverlayHidden(true);
1575
+ this.#trap.deactivate();
1576
+ return;
1577
+ }
1359
1578
  this.#hideAfterTransition();
1360
1579
  }
1361
1580
  #reflectPlacement() {
@@ -1363,10 +1582,15 @@ var DrawerController = class extends Controller {
1363
1582
  }
1364
1583
  #adoptPanel(panel) {
1365
1584
  this.#transition.cancel();
1585
+ const previous = this.#activePanel;
1586
+ if (previous && previous !== panel && this.panelTargets.includes(previous)) {
1587
+ this.#writeState(previous, "closed");
1588
+ this.#writeHidden(previous, true);
1589
+ }
1366
1590
  this.#activePanel = panel;
1367
1591
  this.#placementLease.write(panel, this.#placement);
1368
- if (panel.getAttribute("data-state") === "open") {
1369
- this.#openState = true;
1592
+ if (this.#openState) {
1593
+ this.#writeState(panel, "open");
1370
1594
  this.#writeHidden(panel, false);
1371
1595
  this.#writeOverlayState("open");
1372
1596
  this.#writeOverlayHidden(false);
@@ -1375,7 +1599,6 @@ var DrawerController = class extends Controller {
1375
1599
  else this.#trap.activate();
1376
1600
  return;
1377
1601
  }
1378
- this.#openState = false;
1379
1602
  this.#writeState(panel, "closed");
1380
1603
  this.#writeHidden(panel, true);
1381
1604
  this.#writeOverlayState("closed");
@@ -1385,10 +1608,13 @@ var DrawerController = class extends Controller {
1385
1608
  }
1386
1609
  #repair() {
1387
1610
  this.#reflectPlacement();
1388
- if (!this.hasPanelTarget) return;
1389
- this.#setState(this.#openState ? "open" : "closed");
1611
+ const state = this.#openState ? "open" : "closed";
1390
1612
  const hidden = !this.#openState && !this.#trap.active;
1391
- this.#writeHidden(this.panelTarget, hidden);
1613
+ if (this.hasPanelTarget) {
1614
+ this.#writeState(this.panelTarget, state);
1615
+ this.#writeHidden(this.panelTarget, hidden);
1616
+ }
1617
+ this.#writeOverlayState(state);
1392
1618
  this.#writeOverlayHidden(hidden);
1393
1619
  }
1394
1620
  get #placement() {
@@ -1428,7 +1654,7 @@ var DrawerController = class extends Controller {
1428
1654
  this.#trap.deactivate();
1429
1655
  }
1430
1656
  get #isOpen() {
1431
- return this.hasPanelTarget && this.panelTarget.getAttribute("data-state") === "open";
1657
+ return this.#openState;
1432
1658
  }
1433
1659
  };
1434
1660