stimeo-ui 0.18.1 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -0
  3. data/dist/cable/index.js +6 -5
  4. data/dist/controllers/alert_dialog_controller.js +324 -76
  5. data/dist/controllers/announcer_controller.js +22 -9
  6. data/dist/controllers/auto_submit_controller.js +103 -16
  7. data/dist/controllers/avatar_controller.js +29 -9
  8. data/dist/controllers/breadcrumb_controller.js +205 -17
  9. data/dist/controllers/bulk_select_controller.js +31 -13
  10. data/dist/controllers/calendar_controller.js +242 -7
  11. data/dist/controllers/carousel_controller.js +70 -15
  12. data/dist/controllers/character_counter_controller.js +103 -16
  13. data/dist/controllers/clipboard_controller.js +6 -5
  14. data/dist/controllers/collapsible_controller.js +35 -8
  15. data/dist/controllers/combobox_controller.js +155 -26
  16. data/dist/controllers/command_palette_controller.js +445 -117
  17. data/dist/controllers/conditional_fields_controller.js +84 -44
  18. data/dist/controllers/confirm_controller.js +303 -68
  19. data/dist/controllers/context_menu_controller.js +59 -30
  20. data/dist/controllers/countdown_controller.js +6 -5
  21. data/dist/controllers/currency_input_controller.js +72 -7
  22. data/dist/controllers/data_grid_controller.js +273 -51
  23. data/dist/controllers/date_range_picker_controller.js +27 -5
  24. data/dist/controllers/dialog_controller.js +330 -79
  25. data/dist/controllers/direct_upload_controller.js +6 -5
  26. data/dist/controllers/dismissible_controller.js +68 -18
  27. data/dist/controllers/drawer_controller.js +362 -92
  28. data/dist/controllers/dropdown_controller.js +64 -52
  29. data/dist/controllers/editable_controller.js +140 -12
  30. data/dist/controllers/empty_state_controller.js +193 -9
  31. data/dist/controllers/file_dropzone_controller.js +313 -23
  32. data/dist/controllers/filter_controller.js +194 -5
  33. data/dist/controllers/flash_controller.js +205 -17
  34. data/dist/controllers/focus_controller.js +273 -57
  35. data/dist/controllers/form_field_controller.js +71 -14
  36. data/dist/controllers/form_validation_controller.js +11 -7
  37. data/dist/controllers/frame_loading_controller.js +75 -20
  38. data/dist/controllers/hover_card_controller.js +108 -39
  39. data/dist/controllers/input_mask_controller.js +95 -37
  40. data/dist/controllers/intersection_controller.js +61 -29
  41. data/dist/controllers/lazy_frame_controller.js +61 -29
  42. data/dist/controllers/listbox_controller.js +174 -83
  43. data/dist/controllers/local_time_controller.js +1 -7
  44. data/dist/controllers/menu_controller.js +45 -24
  45. data/dist/controllers/menubar_controller.js +232 -52
  46. data/dist/controllers/meter_controller.js +31 -9
  47. data/dist/controllers/multi_select_controller.js +195 -62
  48. data/dist/controllers/navigation_menu_controller.js +1 -1
  49. data/dist/controllers/nested_form_controller.js +275 -21
  50. data/dist/controllers/network_status_controller.js +31 -9
  51. data/dist/controllers/number_input_controller.js +180 -71
  52. data/dist/controllers/otp_controller.js +97 -11
  53. data/dist/controllers/overflow_menu_controller.js +190 -24
  54. data/dist/controllers/pagination_controller.js +186 -24
  55. data/dist/controllers/password_reveal_controller.js +25 -4
  56. data/dist/controllers/password_strength_controller.js +6 -5
  57. data/dist/controllers/popover_controller.js +466 -42
  58. data/dist/controllers/progress_controller.js +31 -9
  59. data/dist/controllers/radio_group_controller.js +279 -88
  60. data/dist/controllers/range_slider_controller.js +72 -54
  61. data/dist/controllers/rating_controller.js +58 -10
  62. data/dist/controllers/read_more_controller.js +191 -25
  63. data/dist/controllers/relative_time_controller.js +1 -7
  64. data/dist/controllers/roving_controller.js +204 -8
  65. data/dist/controllers/scroll_area_controller.js +61 -19
  66. data/dist/controllers/scroll_visibility_controller.js +25 -4
  67. data/dist/controllers/scrollspy_controller.js +240 -32
  68. data/dist/controllers/separator_controller.js +97 -58
  69. data/dist/controllers/sidebar_controller.js +368 -127
  70. data/dist/controllers/skeleton_controller.js +75 -20
  71. data/dist/controllers/slider_controller.js +72 -54
  72. data/dist/controllers/sortable_controller.js +6 -5
  73. data/dist/controllers/spinner_controller.js +75 -20
  74. data/dist/controllers/step_indicator_controller.js +181 -6
  75. data/dist/controllers/stepper_controller.js +188 -6
  76. data/dist/controllers/sticky_observer_controller.js +86 -33
  77. data/dist/controllers/submit_once_controller.js +31 -9
  78. data/dist/controllers/switch_controller.js +84 -44
  79. data/dist/controllers/tabs_controller.js +189 -3
  80. data/dist/controllers/tags_input_controller.js +293 -29
  81. data/dist/controllers/theme_controller.js +192 -5
  82. data/dist/controllers/time_picker_controller.js +77 -42
  83. data/dist/controllers/toast_controller.js +478 -16
  84. data/dist/controllers/toggle_group_controller.js +282 -79
  85. data/dist/controllers/toolbar_controller.js +276 -15
  86. data/dist/controllers/tooltip_controller.js +244 -46
  87. data/dist/controllers/tree_view_controller.js +287 -57
  88. data/dist/index.js +2258 -1016
  89. data/lib/stimeo/ui/version.rb +1 -1
  90. metadata +2 -2
@@ -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
 
@@ -197,6 +218,7 @@ var EscapeLayer = class _EscapeLayer {
197
218
  );
198
219
  #active = null;
199
220
  activate(ownerDocument = document, options) {
221
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
200
222
  this.deactivate();
201
223
  let registry2 = _EscapeLayer.#registries.get(ownerDocument);
202
224
  if (!registry2) {
@@ -204,7 +226,6 @@ var EscapeLayer = class _EscapeLayer {
204
226
  _EscapeLayer.#registries.set(ownerDocument, registry2);
205
227
  ownerDocument.addEventListener("keydown", registry2.onKeydown);
206
228
  }
207
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
208
229
  registry2.stack.push(entry);
209
230
  this.#active = { document: ownerDocument, entry };
210
231
  }
@@ -253,11 +274,16 @@ var EscapeLayer = class _EscapeLayer {
253
274
  }
254
275
  };
255
276
 
277
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
256
278
  function inheritsFieldsetDisabled(control) {
257
279
  let fieldset = control.closest("fieldset[disabled]");
258
280
  while (fieldset) {
259
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
260
- if (!legend?.contains(control)) return true;
281
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
282
+ const legend = Array.from(fieldset.children).find(
283
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
284
+ );
285
+ if (!legend?.contains(control)) return true;
286
+ }
261
287
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
262
288
  }
263
289
  return false;
@@ -330,11 +356,10 @@ function hasNativeTabStop(element) {
330
356
  if (element.tagName === "AUDIO" || element.tagName === "VIDEO") {
331
357
  return element.hasAttribute("controls");
332
358
  }
333
- if (element instanceof HTMLElement && element.tagName === "SUMMARY") {
334
- const details = element.parentElement;
335
- return details instanceof HTMLDetailsElement && Array.from(details.children).find((child) => child.tagName === "SUMMARY") === element;
336
- }
337
- return false;
359
+ const details = element.parentElement;
360
+ return details instanceof HTMLDetailsElement && Array.from(details.children).find(
361
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "summary"
362
+ ) === element;
338
363
  }
339
364
  function hasEditableTabStop(element) {
340
365
  const value = element.getAttribute("contenteditable")?.toLowerCase();
@@ -397,27 +422,42 @@ function isSequentialStop(element) {
397
422
  function ownedScope(element) {
398
423
  if (element.shadowRoot) return Array.from(element.shadowRoot.children);
399
424
  if (element instanceof HTMLSlotElement) {
400
- const assigned = element.assignedElements();
401
- return assigned.length > 0 ? assigned : Array.from(element.children);
425
+ return element.assignedNodes().length > 0 ? element.assignedElements() : Array.from(element.children);
402
426
  }
403
427
  return null;
404
428
  }
405
429
  function orderScope(elements, walk) {
406
430
  const entries = [];
407
431
  const visit = (element) => {
408
- 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;
409
434
  const tabindex = authoredTabindex(element) ?? 0;
410
- const stop = (element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element) ? element : null;
411
- if (stop) walk.tree.push(stop);
435
+ let stop = null;
436
+ if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
437
+ stop = element;
438
+ walk.tree.push(element);
439
+ }
412
440
  const owned = ownedScope(element);
413
441
  if (owned) {
414
442
  if (tabindex >= 0) {
415
443
  entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
444
+ } else if (walk.from && flatTreeContains(element, walk.from) && (!isFrom || walk.side === "on")) {
445
+ walk.fromAt = walk.tree.length;
446
+ walk.excludedScope = element;
447
+ orderScope(owned, {
448
+ from: walk.from,
449
+ side: walk.side,
450
+ tree: walk.tree,
451
+ fromAt: -1,
452
+ excludedScope: null
453
+ });
416
454
  }
455
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
417
456
  return;
418
457
  }
419
458
  if (stop) entries.push({ tabindex, stops: [stop] });
420
459
  for (const child of Array.from(element.children)) visit(child);
460
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
421
461
  };
422
462
  for (const element of elements) visit(element);
423
463
  const positive = entries.filter((entry) => entry.tabindex > 0);
@@ -425,31 +465,139 @@ function orderScope(elements, walk) {
425
465
  const rest = entries.filter((entry) => entry.tabindex === 0);
426
466
  return [...positive, ...rest].flatMap((entry) => entry.stops);
427
467
  }
428
- function passesRadioRule(element, tree, focusedRadio) {
468
+ function passesRadioRule(element, tree, focusedRadio, backward) {
429
469
  if (!isRadio(element) || element.checked) return true;
430
470
  const group = tree.filter(
431
471
  (stop) => stop === element || isRadio(stop) && sameRadioGroup(stop, element)
432
472
  );
433
473
  if (group.some((radio) => radio.checked)) return false;
434
- if (group[0] !== element) return false;
474
+ if (group[backward ? group.length - 1 : 0] !== element) return false;
435
475
  return focusedRadio === null || !sameRadioGroup(focusedRadio, element);
436
476
  }
437
477
  function startIndex(order, walk, backward, isStop) {
438
478
  if (walk.fromAt < 0) return backward ? order.length - 1 : 0;
439
- const at = order.indexOf(walk.from);
479
+ const at = walk.side === "on" ? order.indexOf(walk.from) : -1;
440
480
  if (at >= 0) return backward ? at - 1 : at + 1;
441
481
  const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
442
482
  if (neighbour) return order.indexOf(neighbour);
443
- return backward ? order.length - 1 : 0;
483
+ return backward ? -1 : order.length;
444
484
  }
445
- function sequentialTabStops(root, from = null, backward = false) {
446
- const walk = { from, tree: [], fromAt: from === root ? 0 : -1 };
485
+ function movesWithin(root, from, backward, wrap, radioContext, side = "on", position = { holder: null }) {
486
+ const walk = {
487
+ from,
488
+ side,
489
+ tree: [],
490
+ fromAt: from === root ? 0 : -1,
491
+ excludedScope: null
492
+ };
447
493
  const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
448
- const focusedRadio = walk.fromAt >= 0 && isRadio(from) ? from : null;
449
- const isStop = (element) => passesRadioRule(element, walk.tree, focusedRadio);
450
- const start = startIndex(order, walk, backward, isStop);
451
- const visited = backward ? [...order.slice(0, start + 1).reverse(), ...order.slice(start + 1).reverse()] : [...order.slice(start), ...order.slice(0, start)];
452
- return visited.filter((element) => element !== from && isStop(element));
494
+ const radios = radioContext ?? {
495
+ tree: walk.tree,
496
+ focusedRadio: side === "on" && walk.fromAt >= 0 && isRadio(from) ? from : null
497
+ };
498
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio, backward);
499
+ const holds = (element) => position.holder !== null && flatTreeContains(element, position.holder);
500
+ const start = startIndex(
501
+ order,
502
+ walk,
503
+ backward,
504
+ (element) => order.includes(element) && isStop(element) && !holds(element)
505
+ );
506
+ const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
507
+ const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
508
+ const excluded = walk.excludedScope;
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
+ ) : [];
518
+ return [
519
+ ...prefix,
520
+ ...[...remaining, ...wrapped].filter(
521
+ (element) => (side !== "on" || element !== from) && isStop(element)
522
+ )
523
+ ];
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
+ }
593
+ function sequentialTabStops(root, from = null, backward = false) {
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
+ });
453
601
  }
454
602
  function deepActiveElement(ownerDocument) {
455
603
  let active = ownerDocument.activeElement;
@@ -613,7 +761,7 @@ var INERT_MARK = `data-${FOCUS_TRAP_OWNER}-inert`;
613
761
  var OVERFLOW_MARK = `data-${FOCUS_TRAP_OWNER}-overflow`;
614
762
  var FocusTrap = class _FocusTrap {
615
763
  static #registries = sharedRegistry(
616
- "stimeo-ui.focus-trap.registry.v2",
764
+ "stimeo-ui.focus-trap.registry.v3",
617
765
  () => /* @__PURE__ */ new WeakMap()
618
766
  );
619
767
  static #TEXT_ENTRY_TYPES = /* @__PURE__ */ new Set([
@@ -636,15 +784,20 @@ var FocusTrap = class _FocusTrap {
636
784
  constructor(getContainer, options = {}) {
637
785
  this.#getContainer = getContainer;
638
786
  this.#options = options;
639
- this.#entry = {
787
+ this.#entry = this.#createEntry();
788
+ }
789
+ #createEntry() {
790
+ const entry = {
640
791
  container: () => this.#readContainer(),
641
792
  moveFocus: (backward) => this.#moveFocus(backward),
642
793
  refocus: () => this.#refocus(),
643
794
  isolated: false,
644
795
  lockedBody: null,
645
796
  openedFrom: null,
646
- handedBack: []
797
+ handedBack: [],
798
+ returnTargets: () => this.#activeReturnTargets(entry)
647
799
  };
800
+ return entry;
648
801
  }
649
802
  get active() {
650
803
  return this.#activeState;
@@ -652,6 +805,7 @@ var FocusTrap = class _FocusTrap {
652
805
  activate() {
653
806
  if (this.#activeState) return;
654
807
  _FocusTrap.#sweep();
808
+ this.#entry = this.#createEntry();
655
809
  this.#activeState = true;
656
810
  this.#openers = _FocusTrap.#focusPath();
657
811
  this.#entry.openedFrom = _FocusTrap.#trapHolding(this.#openers[0] ?? null);
@@ -668,14 +822,21 @@ var FocusTrap = class _FocusTrap {
668
822
  this.#returnTabindexes();
669
823
  return;
670
824
  }
825
+ const entry = this.#entry;
826
+ const parent = entry.openedFrom;
827
+ const openers = this.#openers;
828
+ this.#openers = [];
671
829
  this.#activeState = false;
672
830
  this.#escapeLayer.deactivate();
673
- const above = this.#leaveStack();
674
- this.#unlockScroll();
831
+ const above = this.#leaveStack(entry);
832
+ this.#unlockScroll(entry);
675
833
  _FocusTrap.#reconcileBackground(document);
676
- const wayBack = this.#handOver(above);
677
- if (restoreFocus) this.#restore(wayBack, above);
678
- this.#returnTabindexes();
834
+ try {
835
+ const wayBack = this.#handOver(entry, openers, above);
836
+ if (restoreFocus) this.#restore(wayBack, above, parent);
837
+ } finally {
838
+ this.#returnTabindexes();
839
+ }
679
840
  }
680
841
  connect() {
681
842
  this.#gate.cancel();
@@ -711,18 +872,27 @@ var FocusTrap = class _FocusTrap {
711
872
  document.addEventListener("keydown", registry2.onKeydown);
712
873
  document.addEventListener("turbo:morph", registry2.onMorph);
713
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 });
714
880
  }
715
881
  registry2.stack.push(this.#entry);
716
882
  }
717
- #leaveStack() {
883
+ #leaveStack(entry) {
718
884
  const registry2 = _FocusTrap.#registry(document);
719
- const index = registry2.stack.indexOf(this.#entry);
885
+ const index = registry2.stack.indexOf(entry);
720
886
  const above = registry2.stack.slice(index + 1);
721
887
  registry2.stack.splice(index, 1);
722
888
  if (registry2.stack.length === 0) {
723
889
  document.removeEventListener("keydown", registry2.onKeydown);
724
890
  document.removeEventListener("turbo:morph", registry2.onMorph);
725
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;
726
896
  }
727
897
  return above;
728
898
  }
@@ -740,24 +910,26 @@ var FocusTrap = class _FocusTrap {
740
910
  }
741
911
  return null;
742
912
  }
743
- #handOver(above) {
744
- const entry = this.#entry;
745
- const wayBack = [
746
- ...this.#openers,
747
- this.#options.fallbackFocus?.() ?? null,
748
- ...entry.handedBack
749
- ].filter((element) => element !== null);
750
- for (const trap of above) {
751
- if (trap.openedFrom !== entry) continue;
752
- trap.handedBack = [...trap.handedBack, ...wayBack];
753
- trap.openedFrom = entry.openedFrom;
754
- }
755
- this.#openers = [];
913
+ #handOver(entry, openers, above) {
914
+ const wayBack = [...openers];
915
+ const parent = entry.openedFrom;
916
+ const handedBack = entry.handedBack;
756
917
  entry.openedFrom = null;
757
918
  entry.handedBack = [];
919
+ try {
920
+ const fallback = this.#options.fallbackFocus?.();
921
+ if (fallback) wayBack.push(fallback);
922
+ } finally {
923
+ wayBack.push(...handedBack);
924
+ for (const trap of above) {
925
+ if (trap.openedFrom !== entry) continue;
926
+ trap.handedBack = [...trap.handedBack, ...wayBack];
927
+ trap.openedFrom = parent;
928
+ }
929
+ }
758
930
  return wayBack;
759
931
  }
760
- #restore(wayBack, above) {
932
+ #restore(wayBack, above, parent) {
761
933
  const active = deepActiveElement(document);
762
934
  const released = this.#readContainer();
763
935
  const holds = (trap) => {
@@ -768,7 +940,7 @@ var FocusTrap = class _FocusTrap {
768
940
  return !released || !flatTreeContains(released, active) || flatTreeContains(released, container);
769
941
  };
770
942
  if (above.some(holds)) return;
771
- for (const element of wayBack) {
943
+ for (const element of _FocusTrap.#returnCandidates(wayBack, parent)) {
772
944
  if (this.#activeState) return;
773
945
  if (!element.isConnected || !canTakeFocus(element) || !isRenderedForFocus(element)) continue;
774
946
  const before = deepActiveElement(document);
@@ -778,6 +950,19 @@ var FocusTrap = class _FocusTrap {
778
950
  }
779
951
  _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack)?.refocus();
780
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
+ }
781
966
  #refocus() {
782
967
  if (!this.#flag(this.#options.autoFocus, true)) return;
783
968
  if (flatTreeContains(this.#getContainer(), deepActiveElement(document))) return;
@@ -792,8 +977,8 @@ var FocusTrap = class _FocusTrap {
792
977
  const focused = deepActiveElement(document);
793
978
  for (const [element, loan] of [...this.#tabindexLoans]) {
794
979
  if (element === focused) continue;
795
- loan.returnAll();
796
980
  this.#tabindexLoans.delete(element);
981
+ loan.returnAll();
797
982
  }
798
983
  }
799
984
  #lockScroll() {
@@ -804,11 +989,11 @@ var FocusTrap = class _FocusTrap {
804
989
  this.#entry.lockedBody = body;
805
990
  body.style.overflow = "hidden";
806
991
  }
807
- #unlockScroll() {
992
+ #unlockScroll(entry) {
808
993
  const registry2 = _FocusTrap.#registry(document);
809
- const body = this.#entry.lockedBody;
810
- this.#entry.lockedBody = null;
811
- if (!registry2.scrollLocks.delete(this.#entry) || !body) return;
994
+ const body = entry.lockedBody;
995
+ entry.lockedBody = null;
996
+ if (!registry2.scrollLocks.delete(entry) || !body) return;
812
997
  if (!_FocusTrap.#isLocked(registry2, body)) _FocusTrap.#unlock(body);
813
998
  }
814
999
  static #isLocked(registry2, body) {
@@ -838,8 +1023,12 @@ var FocusTrap = class _FocusTrap {
838
1023
  stack: [],
839
1024
  onKeydown: (event) => _FocusTrap.#resolveTab(registry2.stack, event),
840
1025
  onMorph: () => morphs.schedule(),
1026
+ onPointerDown: (event) => _FocusTrap.#recordPress(registry2, event),
1027
+ onFocusIn: (event) => _FocusTrap.#recordFocus(registry2, event),
841
1028
  inerted: /* @__PURE__ */ new Set(),
842
- scrollLocks: /* @__PURE__ */ new Set()
1029
+ scrollLocks: /* @__PURE__ */ new Set(),
1030
+ start: null,
1031
+ pressed: false
843
1032
  };
844
1033
  _FocusTrap.#registries.set(ownerDocument, registry2);
845
1034
  return registry2;
@@ -937,7 +1126,7 @@ var FocusTrap = class _FocusTrap {
937
1126
  return _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack) === this.#entry;
938
1127
  }
939
1128
  #moveFocus(backward) {
940
- const from = deepActiveElement(document);
1129
+ const from = _FocusTrap.#start();
941
1130
  for (const stop of sequentialTabStops(this.#getContainer(), from, backward)) {
942
1131
  if (!this.#ownsTab()) return;
943
1132
  const before = deepActiveElement(document);
@@ -948,18 +1137,66 @@ var FocusTrap = class _FocusTrap {
948
1137
  return;
949
1138
  }
950
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
+ }
951
1187
  static #selectOnEntry(stop) {
952
1188
  if (stop instanceof HTMLInputElement && _FocusTrap.#TEXT_ENTRY_TYPES.has(stop.type)) {
953
1189
  stop.select();
954
1190
  }
955
1191
  }
956
1192
  #focusInitial() {
1193
+ const container = this.#readContainer();
1194
+ if (!container) return;
957
1195
  const preferred = this.#options.initialFocus?.();
958
1196
  if (preferred) {
959
1197
  preferred.focus();
960
1198
  return;
961
1199
  }
962
- const container = this.#getContainer();
963
1200
  const [first] = sequentialTabStops(container);
964
1201
  if (first) {
965
1202
  first.focus();
@@ -1204,10 +1441,13 @@ var DrawerController = class extends Controller {
1204
1441
  static events = ["close", "open"];
1205
1442
  #activePanel = null;
1206
1443
  #openState = false;
1207
- #trap = new FocusTrap(() => this.#activePanel ?? this.panelTarget, {
1208
- onEscape: () => this.#close("escape"),
1209
- fallbackFocus: () => this.hasTriggerTarget ? this.triggerTarget : null
1210
- });
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
+ );
1211
1451
  #transition = new TransitionCompletion();
1212
1452
  #connected = false;
1213
1453
  #reporting = false;
@@ -1223,7 +1463,8 @@ var DrawerController = class extends Controller {
1223
1463
  this.#connected = true;
1224
1464
  this.#activePanel = this.hasPanelTarget ? this.panelTarget : null;
1225
1465
  this.#reflectPlacement();
1226
- 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);
1227
1468
  this.#applyClosedState();
1228
1469
  if (restored) this.openValue = false;
1229
1470
  if (shouldOpen) this.#open("api");
@@ -1249,23 +1490,26 @@ var DrawerController = class extends Controller {
1249
1490
  this.#hiddenLease.return(panel);
1250
1491
  this.#placementLease.return(panel);
1251
1492
  }
1252
- if (panel !== this.#activePanel || stays && this.panelTarget === panel) return;
1493
+ if (!this.#connected || panel !== this.#activePanel || stays && this.panelTarget === panel)
1494
+ return;
1253
1495
  this.#transition.cancel();
1254
1496
  this.#activePanel = null;
1255
- if (!this.#connected) {
1256
- this.#trap.deactivate({ restoreFocus: false });
1257
- return;
1258
- }
1259
- this.#openState = false;
1260
1497
  if (stays) {
1261
1498
  this.#writeState(panel, "closed");
1262
1499
  this.#writeHidden(panel, true);
1263
1500
  }
1264
- this.#writeOverlayState("closed");
1265
- this.#writeOverlayHidden(true);
1266
- this.openValue = false;
1267
- if (this.hasPanelTarget) this.#adoptPanel(this.panelTarget);
1268
- 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
+ }
1269
1513
  }
1270
1514
  overlayTargetConnected() {
1271
1515
  this.#morphRender.schedule();
@@ -1286,17 +1530,26 @@ var DrawerController = class extends Controller {
1286
1530
  this.#open(stateReasonFor(event));
1287
1531
  }
1288
1532
  #open(reason) {
1289
- 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;
1290
1539
  this.#transition.cancel();
1291
- this.#activePanel = this.panelTarget;
1292
- this.#writeHidden(this.panelTarget, false);
1540
+ this.#activePanel = panel;
1541
+ if (panel) this.#writeHidden(panel, false);
1293
1542
  this.#writeOverlayHidden(false);
1294
- void this.panelTarget.offsetWidth;
1543
+ void panel?.offsetWidth;
1295
1544
  this.#setState("open");
1296
1545
  this.openValue = true;
1297
1546
  if (this.#reporting) this.dispatch("open", { detail: { reason }, cancelable: false });
1298
- if (!this.#isOpen) return;
1299
- 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
+ }
1300
1553
  }
1301
1554
  close(event) {
1302
1555
  if (!this.#connected) return;
@@ -1307,11 +1560,21 @@ var DrawerController = class extends Controller {
1307
1560
  if (this.hasOverlayTarget && event.target === this.overlayTarget) this.#close("outside");
1308
1561
  }
1309
1562
  #close(reason) {
1310
- 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
+ }
1311
1568
  this.openValue = false;
1312
1569
  this.#setState("closed");
1313
1570
  if (this.#reporting) this.dispatch("close", { detail: { reason }, cancelable: false });
1314
- 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
+ }
1315
1578
  this.#hideAfterTransition();
1316
1579
  }
1317
1580
  #reflectPlacement() {
@@ -1319,10 +1582,15 @@ var DrawerController = class extends Controller {
1319
1582
  }
1320
1583
  #adoptPanel(panel) {
1321
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
+ }
1322
1590
  this.#activePanel = panel;
1323
1591
  this.#placementLease.write(panel, this.#placement);
1324
- if (panel.getAttribute("data-state") === "open") {
1325
- this.#openState = true;
1592
+ if (this.#openState) {
1593
+ this.#writeState(panel, "open");
1326
1594
  this.#writeHidden(panel, false);
1327
1595
  this.#writeOverlayState("open");
1328
1596
  this.#writeOverlayHidden(false);
@@ -1331,7 +1599,6 @@ var DrawerController = class extends Controller {
1331
1599
  else this.#trap.activate();
1332
1600
  return;
1333
1601
  }
1334
- this.#openState = false;
1335
1602
  this.#writeState(panel, "closed");
1336
1603
  this.#writeHidden(panel, true);
1337
1604
  this.#writeOverlayState("closed");
@@ -1341,10 +1608,13 @@ var DrawerController = class extends Controller {
1341
1608
  }
1342
1609
  #repair() {
1343
1610
  this.#reflectPlacement();
1344
- if (!this.hasPanelTarget) return;
1345
- this.#setState(this.#openState ? "open" : "closed");
1611
+ const state = this.#openState ? "open" : "closed";
1346
1612
  const hidden = !this.#openState && !this.#trap.active;
1347
- 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);
1348
1618
  this.#writeOverlayHidden(hidden);
1349
1619
  }
1350
1620
  get #placement() {
@@ -1384,7 +1654,7 @@ var DrawerController = class extends Controller {
1384
1654
  this.#trap.deactivate();
1385
1655
  }
1386
1656
  get #isOpen() {
1387
- return this.hasPanelTarget && this.panelTarget.getAttribute("data-state") === "open";
1657
+ return this.#openState;
1388
1658
  }
1389
1659
  };
1390
1660