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();
@@ -1256,7 +1493,7 @@ var SidebarController = class _SidebarController extends Controller {
1256
1493
  #transition = new TransitionCompletion();
1257
1494
  #connected = false;
1258
1495
  #storageKeyRead = null;
1259
- #expanded = false;
1496
+ #overlayOpen = false;
1260
1497
  #backdrop = {
1261
1498
  state: "closed",
1262
1499
  hidden: true
@@ -1277,10 +1514,15 @@ var SidebarController = class _SidebarController extends Controller {
1277
1514
  this.#mql = this.#matchBreakpoint(this.#mqlQuery);
1278
1515
  if (this.#mql) this.#mediaListeners.add(this.#mql, "change", this.#onMediaChange);
1279
1516
  const mode = this.#computeMode();
1280
- 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;
1281
1519
  this.#lived = true;
1282
- if (resume) this.#trap.activate();
1283
- else this.#applyMode(mode, false);
1520
+ if (resume) {
1521
+ this.#overlayOpen = true;
1522
+ this.#trap.activate();
1523
+ } else {
1524
+ this.#applyMode(mode, false);
1525
+ }
1284
1526
  this.#morphRender.observe(this.element);
1285
1527
  }
1286
1528
  disconnect() {
@@ -1293,32 +1535,16 @@ var SidebarController = class _SidebarController extends Controller {
1293
1535
  this.#trap.disconnect(this);
1294
1536
  this.#activePanel = null;
1295
1537
  }
1296
- panelTargetConnected(panel) {
1297
- if (!this.#connected || this.#activePanel?.isConnected) return;
1298
- this.#adoptPanel(panel);
1538
+ panelTargetConnected(_panel) {
1539
+ this.#adoptPanel();
1299
1540
  }
1300
1541
  panelTargetDisconnected(panel) {
1301
- const stays = this.panelTargets.includes(panel);
1302
- if (!stays) {
1542
+ if (!this.panelTargets.includes(panel)) {
1303
1543
  this.#modeLease.return(panel);
1304
1544
  this.#stateLease.return(panel);
1305
1545
  this.#hiddenLease.return(panel);
1306
1546
  }
1307
- if (panel !== this.#activePanel || stays && this.panelTarget === panel) return;
1308
- this.#transition.cancel();
1309
- this.#activePanel = null;
1310
- if (stays) {
1311
- this.#writeState(panel, "closed");
1312
- this.#writeHidden(panel, true);
1313
- }
1314
- this.#hideBackdrop();
1315
- this.#setExpandedAttr(false);
1316
- if (!this.#connected) {
1317
- this.#trap.deactivate({ restoreFocus: false });
1318
- return;
1319
- }
1320
- if (this.hasPanelTarget) this.#adoptPanel(this.panelTarget);
1321
- else this.#trap.deactivate();
1547
+ this.#adoptPanel();
1322
1548
  }
1323
1549
  triggerTargetConnected() {
1324
1550
  if (this.#connected) this.#reflectExpanded();
@@ -1389,6 +1615,7 @@ var SidebarController = class _SidebarController extends Controller {
1389
1615
  this.#mode = mode;
1390
1616
  if (this.hasPanelTarget) this.#modeLease.write(this.panelTarget, mode);
1391
1617
  if (mode === "inline") {
1618
+ this.#overlayOpen = false;
1392
1619
  this.#transition.cancel();
1393
1620
  if (report) this.#trap.deactivate({ restoreFocus: false });
1394
1621
  if (this.hasPanelTarget) this.#writeHidden(this.panelTarget, false);
@@ -1407,11 +1634,11 @@ var SidebarController = class _SidebarController extends Controller {
1407
1634
  }
1408
1635
  const panel = this.panelTarget;
1409
1636
  this.#modeLease.write(panel, "overlay");
1410
- this.#setOverlayState(this.#expanded ? "open" : "closed");
1411
- const hidden = !this.#expanded && !this.#trap.active;
1637
+ this.#setOverlayState(this.#overlayOpen ? "open" : "closed");
1638
+ const hidden = !this.#overlayOpen && !this.#trap.active;
1412
1639
  this.#writeHidden(panel, hidden);
1413
1640
  this.#writeBackdropHidden(hidden);
1414
- this.#setExpandedAttr(this.#expanded);
1641
+ this.#reflectExpanded();
1415
1642
  }
1416
1643
  #reportReconcile() {
1417
1644
  this.dispatch("reconcile", {
@@ -1419,30 +1646,36 @@ var SidebarController = class _SidebarController extends Controller {
1419
1646
  cancelable: false
1420
1647
  });
1421
1648
  }
1422
- #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;
1423
1654
  this.#transition.cancel();
1424
1655
  this.#activePanel = panel;
1425
- 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);
1426
1661
  if (this.#mode === "inline") {
1427
- this.#writeHidden(panel, false);
1428
- this.#writeState(panel, this.#collapsed ? "collapsed" : "expanded");
1662
+ if (panel) this.#writeHidden(panel, false);
1429
1663
  this.#hideBackdrop();
1430
- this.#setExpandedAttr(!this.#collapsed);
1664
+ this.#applyInlineState(this.#collapsed);
1431
1665
  return;
1432
1666
  }
1433
- if (panel.getAttribute("data-state") === "open") {
1434
- this.#writeHidden(panel, false);
1435
- this.#writeBackdropState("open");
1436
- this.#writeBackdropHidden(false);
1437
- this.#setExpandedAttr(true);
1438
- if (this.#trap.active) this.#trap.refreshContainer();
1439
- 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();
1440
1676
  return;
1441
1677
  }
1442
- this.#writeState(panel, "closed");
1443
- this.#writeHidden(panel, true);
1444
- this.#hideBackdrop();
1445
- this.#setExpandedAttr(false);
1678
+ this.#setOverlayClosedImmediate();
1446
1679
  this.#trap.deactivate();
1447
1680
  }
1448
1681
  #onMediaChange = (event) => {
@@ -1467,32 +1700,46 @@ var SidebarController = class _SidebarController extends Controller {
1467
1700
  if (this.hasPanelTarget) {
1468
1701
  this.#writeState(this.panelTarget, collapsed ? "collapsed" : "expanded");
1469
1702
  }
1470
- this.#setExpandedAttr(!collapsed);
1703
+ this.#reflectExpanded();
1471
1704
  }
1472
1705
  #openOverlay(reason) {
1473
- if (!this.hasPanelTarget || this.#isOverlayOpen) return;
1706
+ this.#adoptPanel();
1707
+ if (this.#isOverlayOpen) return;
1474
1708
  this.#transition.cancel();
1475
- this.#activePanel = this.panelTarget;
1476
- this.#writeHidden(this.panelTarget, false);
1477
- this.#writeBackdropHidden(false);
1478
- void this.panelTarget.offsetWidth;
1479
- this.#setOverlayState("open");
1480
- 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();
1481
1717
  this.#report(true, reason);
1482
- if (!this.#isOverlayOpen) return;
1718
+ if (!this.#connected || !this.#isOverlayOpen) return;
1719
+ this.#adoptPanel();
1483
1720
  this.#trap.activate();
1484
1721
  }
1485
1722
  #closeOverlay(reason) {
1486
- 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;
1487
1727
  this.#setOverlayState("closed");
1488
- this.#setExpandedAttr(false);
1728
+ this.#reflectExpanded();
1489
1729
  this.#report(false, reason);
1490
- if (this.#isOverlayOpen) return;
1491
- 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
+ }
1492
1738
  }
1493
1739
  #setOverlayClosedImmediate() {
1740
+ this.#overlayOpen = false;
1494
1741
  this.#setOverlayState("closed");
1495
- this.#setExpandedAttr(false);
1742
+ this.#reflectExpanded();
1496
1743
  if (this.hasPanelTarget) this.#writeHidden(this.panelTarget, true);
1497
1744
  this.#hideBackdrop();
1498
1745
  }
@@ -1500,9 +1747,7 @@ var SidebarController = class _SidebarController extends Controller {
1500
1747
  if (this.hasPanelTarget) this.#writeState(this.panelTarget, state);
1501
1748
  this.#writeBackdropState(state);
1502
1749
  }
1503
- #hideAfterTransition() {
1504
- const panel = this.panelTarget;
1505
- this.#activePanel = panel;
1750
+ #hideAfterTransition(panel) {
1506
1751
  this.#transition.wait(panel, () => this.#applyOverlayHidden(panel));
1507
1752
  }
1508
1753
  #applyOverlayHidden(panel) {
@@ -1532,13 +1777,9 @@ var SidebarController = class _SidebarController extends Controller {
1532
1777
  this.#writeBackdropState(this.#backdrop.state);
1533
1778
  this.#writeBackdropHidden(this.#backdrop.hidden);
1534
1779
  }
1535
- #setExpandedAttr(expanded) {
1536
- this.#expanded = expanded;
1537
- this.#reflectExpanded();
1538
- }
1539
1780
  #reflectExpanded() {
1540
1781
  if (this.hasTriggerTarget) {
1541
- this.#expandedLease.write(this.triggerTarget, this.#expanded ? "true" : "false");
1782
+ this.#expandedLease.write(this.triggerTarget, this.#isExpanded ? "true" : "false");
1542
1783
  }
1543
1784
  }
1544
1785
  #report(open, reason) {
@@ -1580,7 +1821,7 @@ var SidebarController = class _SidebarController extends Controller {
1580
1821
  return this.#mode === "overlay";
1581
1822
  }
1582
1823
  get #isOverlayOpen() {
1583
- return this.#isOverlay && this.hasPanelTarget && this.panelTarget.getAttribute("data-state") === "open";
1824
+ return this.#isOverlay && this.#overlayOpen;
1584
1825
  }
1585
1826
  get #safeBreakpoint() {
1586
1827
  return this.#numbers.read(