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();
@@ -1072,6 +1265,8 @@ var DialogController = class extends Controller {
1072
1265
  });
1073
1266
  #reporting = false;
1074
1267
  #connected = false;
1268
+ #openState = false;
1269
+ #seenDialog = false;
1075
1270
  #dialog = null;
1076
1271
  #hidden = new AttributeLease("hidden", this.identifier);
1077
1272
  #morphRender = new MorphRenderWatcher(() => this.#repair());
@@ -1080,17 +1275,20 @@ var DialogController = class extends Controller {
1080
1275
  this.#connected = true;
1081
1276
  if (this.hasDialogTarget) this.#hidden.write(this.dialogTarget, "");
1082
1277
  this.#dialog = this.hasDialogTarget ? this.dialogTarget : null;
1278
+ this.#seenDialog = this.#dialog !== null;
1279
+ this.#openState = false;
1083
1280
  this.#reporting = true;
1084
1281
  this.#morphRender.observe(this.element);
1085
1282
  }
1086
1283
  disconnect() {
1087
1284
  this.#morphRender.disconnect();
1088
1285
  this.#connected = false;
1286
+ this.#openState = false;
1089
1287
  this.#reporting = false;
1090
1288
  this.#trap.disconnect(this);
1091
1289
  }
1092
1290
  #repair() {
1093
- if (this.hasDialogTarget) this.#hidden.write(this.dialogTarget, this.#trap.active ? null : "");
1291
+ if (this.hasDialogTarget) this.#hidden.write(this.dialogTarget, this.#openState ? null : "");
1094
1292
  }
1095
1293
  dialogTargetConnected() {
1096
1294
  if (this.#connected) this.#adoptDialog();
@@ -1107,39 +1305,48 @@ var DialogController = class extends Controller {
1107
1305
  this.#close(stateReasonFor(event));
1108
1306
  }
1109
1307
  closeOnBackdrop(event) {
1110
- if (event.target === this.dialogTarget) this.#close("outside");
1308
+ if (this.hasDialogTarget && event.target === this.dialogTarget) this.#close("outside");
1111
1309
  }
1112
1310
  #open(reason) {
1113
- if (!this.hasDialogTarget || this.#isOpen) return;
1114
- this.#hidden.write(this.dialogTarget, null);
1311
+ this.#adoptDialog();
1312
+ if (!this.#connected || !this.#seenDialog || this.#isOpen) return;
1313
+ this.#openState = true;
1314
+ if (this.hasDialogTarget) this.#hidden.write(this.dialogTarget, null);
1115
1315
  if (this.#reporting) this.dispatch("open", { detail: { reason }, cancelable: false });
1116
- if (!this.#isOpen) return;
1316
+ if (!this.#isOpen || !this.#connected) return;
1317
+ this.#adoptDialog();
1318
+ if (this.hasDialogTarget && this.dialogTarget.hidden) {
1319
+ this.#openState = false;
1320
+ return;
1321
+ }
1117
1322
  this.#trap.activate();
1118
1323
  }
1119
1324
  #close(reason) {
1120
- if (!this.hasDialogTarget || !this.#isOpen) return;
1121
- this.#hidden.write(this.dialogTarget, "");
1325
+ this.#adoptDialog();
1326
+ if (!this.#isOpen) return;
1327
+ this.#openState = false;
1328
+ if (this.hasDialogTarget) this.#hidden.write(this.dialogTarget, "");
1122
1329
  if (this.#reporting) this.dispatch("close", { detail: { reason }, cancelable: false });
1123
1330
  if (this.#isOpen) return;
1124
1331
  this.#trap.deactivate();
1125
1332
  }
1126
1333
  #adoptDialog() {
1127
1334
  const dialog = this.hasDialogTarget ? this.dialogTarget : null;
1128
- if (dialog === this.#dialog) return;
1129
- this.#dialog = dialog;
1130
- if (!this.#trap.active) {
1131
- if (dialog) this.#hidden.write(dialog, "");
1132
- return;
1335
+ const previous = this.#dialog;
1336
+ if (dialog === previous) return;
1337
+ if (previous) {
1338
+ if (this.dialogTargets.includes(previous)) this.#hidden.write(previous, "");
1339
+ else this.#hidden.return(previous);
1133
1340
  }
1134
- if (!dialog) {
1135
- this.#trap.deactivate();
1136
- return;
1341
+ this.#dialog = dialog;
1342
+ if (dialog) {
1343
+ this.#seenDialog = true;
1344
+ this.#hidden.write(dialog, this.#openState ? null : "");
1137
1345
  }
1138
- this.#hidden.write(dialog, null);
1139
- this.#trap.refreshContainer();
1346
+ if (this.#openState) this.#trap.refreshContainer();
1140
1347
  }
1141
1348
  get #isOpen() {
1142
- return this.hasDialogTarget && !this.dialogTarget.hidden;
1349
+ return this.#openState;
1143
1350
  }
1144
1351
  };
1145
1352
 
@@ -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
 
@@ -240,6 +261,19 @@ function tabStopsWithin(root) {
240
261
  isTabStop
241
262
  );
242
263
  }
264
+ function isRadio(element) {
265
+ return element instanceof HTMLInputElement && element.type === "radio";
266
+ }
267
+ function sameRadioGroup(a, b) {
268
+ return a.name !== "" && a.name === b.name && a.form === b.form && a.getRootNode() === b.getRootNode();
269
+ }
270
+ function isUncheckedRadioInCheckedGroup(element) {
271
+ if (!isRadio(element) || element.checked) return false;
272
+ const root = element.getRootNode();
273
+ return Array.from(root.querySelectorAll("input")).some(
274
+ (other) => isRadio(other) && other.checked && sameRadioGroup(other, element) && isTabStop(other)
275
+ );
276
+ }
243
277
 
244
278
  var ListenerSet = class {
245
279
  #abort = new AbortController();
@@ -260,7 +294,8 @@ var DismissibleController = class extends Controller {
260
294
  };
261
295
  static actions = ["dismiss"];
262
296
  static events = ["dismiss"];
263
- #openDefault = new AttributeLease("data-state", this.identifier);
297
+ #state = new AttributeLease("data-state", this.identifier);
298
+ #hidden = new AttributeLease("hidden", this.identifier);
264
299
  #connected = false;
265
300
  #escapeListeners = new ListenerSet();
266
301
  connect() {
@@ -271,14 +306,19 @@ var DismissibleController = class extends Controller {
271
306
  disconnect() {
272
307
  this.#connected = false;
273
308
  this.#escapeListeners.dispose();
309
+ if (this.element.isConnected && !this.scope.containsElement(this.element)) {
310
+ this.#returnRoot(this.element);
311
+ }
274
312
  }
275
313
  rootTargetConnected(root) {
276
314
  if (!this.#connected) return;
277
- if (!this.rootTargets.includes(this.element)) this.#openDefault.return(this.element);
315
+ if (!this.rootTargets.includes(this.element)) this.#returnRoot(this.element);
278
316
  this.#establishOpen(root);
279
317
  }
280
318
  rootTargetDisconnected(root) {
281
- if (!this.rootTargets.includes(root)) this.#openDefault.return(root);
319
+ if ((root.isConnected || this.element.isConnected) && !this.rootTargets.includes(root)) {
320
+ this.#returnRoot(root);
321
+ }
282
322
  if (this.#connected && this.hasRootTarget) this.#establishOpen(this.rootTarget);
283
323
  }
284
324
  closeOnEscapeValueChanged() {
@@ -302,7 +342,11 @@ var DismissibleController = class extends Controller {
302
342
  }
303
343
  }
304
344
  #establishOpen(root) {
305
- if (!root.hasAttribute("data-state")) this.#openDefault.write(root, "open");
345
+ if (!root.hasAttribute("data-state")) this.#state.write(root, "open");
346
+ }
347
+ #returnRoot(root) {
348
+ this.#state.return(root);
349
+ this.#hidden.return(root);
306
350
  }
307
351
  get #root() {
308
352
  return this.hasRootTarget ? this.rootTarget : this.element;
@@ -311,10 +355,10 @@ var DismissibleController = class extends Controller {
311
355
  const root = this.#root;
312
356
  const mode = this.modeValue === "hide" ? "hide" : "remove";
313
357
  this.#retreatFocus(root);
314
- root.setAttribute("data-state", "closing");
358
+ this.#state.write(root, "closing");
315
359
  this.dispatch("dismiss", { detail: { mode } });
316
360
  if (mode === "hide") {
317
- root.hidden = true;
361
+ this.#hidden.write(root, "");
318
362
  } else {
319
363
  root.remove();
320
364
  }
@@ -335,7 +379,9 @@ var DismissibleController = class extends Controller {
335
379
  if (explicitFallback && !root.contains(explicitFallback) && this.#isExplicitFocusAvailable(explicitFallback)) {
336
380
  yield explicitFallback;
337
381
  }
338
- const candidates = tabStopsWithin(document);
382
+ const candidates = tabStopsWithin(document).filter(
383
+ (element) => !isUncheckedRadioInCheckedGroup(element)
384
+ );
339
385
  for (const element of candidates) {
340
386
  if (element === explicitFallback || root.contains(element) || !(root.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_FOLLOWING)) {
341
387
  continue;