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
@@ -54,6 +54,7 @@ var EscapeLayer = class _EscapeLayer {
54
54
  );
55
55
  #active = null;
56
56
  activate(ownerDocument = document, options) {
57
+ const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
57
58
  this.deactivate();
58
59
  let registry = _EscapeLayer.#registries.get(ownerDocument);
59
60
  if (!registry) {
@@ -61,7 +62,6 @@ var EscapeLayer = class _EscapeLayer {
61
62
  _EscapeLayer.#registries.set(ownerDocument, registry);
62
63
  ownerDocument.addEventListener("keydown", registry.onKeydown);
63
64
  }
64
- const entry = { onDismiss: options.onDismiss, claims: options.claims ?? null };
65
65
  registry.stack.push(entry);
66
66
  this.#active = { document: ownerDocument, entry };
67
67
  }
@@ -110,11 +110,16 @@ var EscapeLayer = class _EscapeLayer {
110
110
  }
111
111
  };
112
112
 
113
+ var HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
113
114
  function inheritsFieldsetDisabled(control) {
114
115
  let fieldset = control.closest("fieldset[disabled]");
115
116
  while (fieldset) {
116
- const legend = Array.from(fieldset.children).find((child) => child.tagName === "LEGEND");
117
- if (!legend?.contains(control)) return true;
117
+ if (fieldset.namespaceURI === HTML_NAMESPACE && fieldset.localName === "fieldset") {
118
+ const legend = Array.from(fieldset.children).find(
119
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "legend"
120
+ );
121
+ if (!legend?.contains(control)) return true;
122
+ }
118
123
  fieldset = fieldset.parentElement?.closest("fieldset[disabled]") ?? null;
119
124
  }
120
125
  return false;
@@ -187,11 +192,10 @@ function hasNativeTabStop(element) {
187
192
  if (element.tagName === "AUDIO" || element.tagName === "VIDEO") {
188
193
  return element.hasAttribute("controls");
189
194
  }
190
- if (element instanceof HTMLElement && element.tagName === "SUMMARY") {
191
- const details = element.parentElement;
192
- return details instanceof HTMLDetailsElement && Array.from(details.children).find((child) => child.tagName === "SUMMARY") === element;
193
- }
194
- return false;
195
+ const details = element.parentElement;
196
+ return details instanceof HTMLDetailsElement && Array.from(details.children).find(
197
+ (child) => child.namespaceURI === HTML_NAMESPACE && child.localName === "summary"
198
+ ) === element;
195
199
  }
196
200
  function hasEditableTabStop(element) {
197
201
  const value = element.getAttribute("contenteditable")?.toLowerCase();
@@ -254,27 +258,42 @@ function isSequentialStop(element) {
254
258
  function ownedScope(element) {
255
259
  if (element.shadowRoot) return Array.from(element.shadowRoot.children);
256
260
  if (element instanceof HTMLSlotElement) {
257
- const assigned = element.assignedElements();
258
- return assigned.length > 0 ? assigned : Array.from(element.children);
261
+ return element.assignedNodes().length > 0 ? element.assignedElements() : Array.from(element.children);
259
262
  }
260
263
  return null;
261
264
  }
262
265
  function orderScope(elements, walk) {
263
266
  const entries = [];
264
267
  const visit = (element) => {
265
- if (element === walk.from) walk.fromAt = walk.tree.length;
268
+ const isFrom = element === walk.from;
269
+ if (isFrom && walk.side !== "after") walk.fromAt = walk.tree.length;
266
270
  const tabindex = authoredTabindex(element) ?? 0;
267
- const stop = (element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element) ? element : null;
268
- if (stop) walk.tree.push(stop);
271
+ let stop = null;
272
+ if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
273
+ stop = element;
274
+ walk.tree.push(element);
275
+ }
269
276
  const owned = ownedScope(element);
270
277
  if (owned) {
271
278
  if (tabindex >= 0) {
272
279
  entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
280
+ } else if (walk.from && flatTreeContains(element, walk.from) && (!isFrom || walk.side === "on")) {
281
+ walk.fromAt = walk.tree.length;
282
+ walk.excludedScope = element;
283
+ orderScope(owned, {
284
+ from: walk.from,
285
+ side: walk.side,
286
+ tree: walk.tree,
287
+ fromAt: -1,
288
+ excludedScope: null
289
+ });
273
290
  }
291
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
274
292
  return;
275
293
  }
276
294
  if (stop) entries.push({ tabindex, stops: [stop] });
277
295
  for (const child of Array.from(element.children)) visit(child);
296
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
278
297
  };
279
298
  for (const element of elements) visit(element);
280
299
  const positive = entries.filter((entry) => entry.tabindex > 0);
@@ -282,31 +301,139 @@ function orderScope(elements, walk) {
282
301
  const rest = entries.filter((entry) => entry.tabindex === 0);
283
302
  return [...positive, ...rest].flatMap((entry) => entry.stops);
284
303
  }
285
- function passesRadioRule(element, tree, focusedRadio) {
304
+ function passesRadioRule(element, tree, focusedRadio, backward) {
286
305
  if (!isRadio(element) || element.checked) return true;
287
306
  const group = tree.filter(
288
307
  (stop) => stop === element || isRadio(stop) && sameRadioGroup(stop, element)
289
308
  );
290
309
  if (group.some((radio) => radio.checked)) return false;
291
- if (group[0] !== element) return false;
310
+ if (group[backward ? group.length - 1 : 0] !== element) return false;
292
311
  return focusedRadio === null || !sameRadioGroup(focusedRadio, element);
293
312
  }
294
313
  function startIndex(order, walk, backward, isStop) {
295
314
  if (walk.fromAt < 0) return backward ? order.length - 1 : 0;
296
- const at = order.indexOf(walk.from);
315
+ const at = walk.side === "on" ? order.indexOf(walk.from) : -1;
297
316
  if (at >= 0) return backward ? at - 1 : at + 1;
298
317
  const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
299
318
  if (neighbour) return order.indexOf(neighbour);
300
- return backward ? order.length - 1 : 0;
319
+ return backward ? -1 : order.length;
301
320
  }
302
- function sequentialTabStops(root, from = null, backward = false) {
303
- const walk = { from, tree: [], fromAt: from === root ? 0 : -1 };
321
+ function movesWithin(root, from, backward, wrap, radioContext, side = "on", position = { holder: null }) {
322
+ const walk = {
323
+ from,
324
+ side,
325
+ tree: [],
326
+ fromAt: from === root ? 0 : -1,
327
+ excludedScope: null
328
+ };
304
329
  const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
305
- const focusedRadio = walk.fromAt >= 0 && isRadio(from) ? from : null;
306
- const isStop = (element) => passesRadioRule(element, walk.tree, focusedRadio);
307
- const start = startIndex(order, walk, backward, isStop);
308
- const visited = backward ? [...order.slice(0, start + 1).reverse(), ...order.slice(start + 1).reverse()] : [...order.slice(start), ...order.slice(0, start)];
309
- return visited.filter((element) => element !== from && isStop(element));
330
+ const radios = radioContext ?? {
331
+ tree: walk.tree,
332
+ focusedRadio: side === "on" && walk.fromAt >= 0 && isRadio(from) ? from : null
333
+ };
334
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio, backward);
335
+ const holds = (element) => position.holder !== null && flatTreeContains(element, position.holder);
336
+ const start = startIndex(
337
+ order,
338
+ walk,
339
+ backward,
340
+ (element) => order.includes(element) && isStop(element) && !holds(element)
341
+ );
342
+ const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
343
+ const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
344
+ const excluded = walk.excludedScope;
345
+ const prefix = excluded && !(backward && from === excluded) ? movesWithin(
346
+ excluded,
347
+ from === excluded ? null : from,
348
+ backward,
349
+ false,
350
+ radios,
351
+ side,
352
+ position
353
+ ) : [];
354
+ return [
355
+ ...prefix,
356
+ ...[...remaining, ...wrapped].filter(
357
+ (element) => (side !== "on" || element !== from) && isStop(element)
358
+ )
359
+ ];
360
+ }
361
+ function flatParentNode(node) {
362
+ const parent = node.parentNode;
363
+ if (parent instanceof Element && parent.shadowRoot) {
364
+ for (const slot of parent.shadowRoot.querySelectorAll("slot")) {
365
+ if (slot.assignedNodes().includes(node)) return slot;
366
+ }
367
+ }
368
+ return parent instanceof ShadowRoot ? parent.host : parent;
369
+ }
370
+ function flatChildNodes(node) {
371
+ if (node instanceof Element && node.shadowRoot) return Array.from(node.shadowRoot.childNodes);
372
+ if (node instanceof HTMLSlotElement) {
373
+ const assigned = node.assignedNodes();
374
+ return assigned.length > 0 ? assigned : Array.from(node.childNodes);
375
+ }
376
+ return Array.from(node.childNodes);
377
+ }
378
+ function flatNodeAfter(node, root) {
379
+ for (let current = node; current && current !== root; ) {
380
+ const parent = flatParentNode(current);
381
+ if (!parent) return null;
382
+ const siblings = flatChildNodes(parent);
383
+ const index = siblings.indexOf(current);
384
+ const next = index >= 0 ? siblings[index + 1] : void 0;
385
+ if (next) return next;
386
+ current = parent;
387
+ }
388
+ return null;
389
+ }
390
+ function flatElementBefore(node, root) {
391
+ for (let current = node; current && current !== root; ) {
392
+ const parent = flatParentNode(current);
393
+ if (!parent) return null;
394
+ const siblings = flatChildNodes(parent);
395
+ for (let index = siblings.indexOf(current) - 1; index >= 0; index--) {
396
+ const sibling = siblings[index];
397
+ if (sibling instanceof Element) return sibling;
398
+ }
399
+ current = parent;
400
+ }
401
+ return null;
402
+ }
403
+ function elementAfterPosition(container, offset, root) {
404
+ const owner = container instanceof ShadowRoot ? container.host : container;
405
+ const child = container.childNodes[offset];
406
+ const previous = container.childNodes[offset - 1];
407
+ let node = child ?? (previous ? flatNodeAfter(previous, root) : flatChildNodes(owner)[0] ?? flatNodeAfter(owner, root));
408
+ while (node && !(node instanceof Element)) {
409
+ node = flatChildNodes(node)[0] ?? flatNodeAfter(node, root);
410
+ }
411
+ return node;
412
+ }
413
+ function elementBeforePosition(container, offset, root) {
414
+ const child = container.childNodes[offset];
415
+ if (child) return flatElementBefore(child, root);
416
+ const previous = container.childNodes[offset - 1];
417
+ if (previous) return previous instanceof Element ? previous : flatElementBefore(previous, root);
418
+ return flatElementBefore(container instanceof ShadowRoot ? container.host : container, root);
419
+ }
420
+ function elementHoldingPosition(container, offset) {
421
+ if (container instanceof Element && container.shadowRoot) {
422
+ const near = container.childNodes[offset] ?? container.childNodes[offset - 1];
423
+ return near ? flatParentNode(near) : container;
424
+ }
425
+ if (container instanceof Element) return container;
426
+ if (container instanceof ShadowRoot) return container.host;
427
+ return flatParentNode(container);
428
+ }
429
+ function sequentialTabStops(root, from = null, backward = false) {
430
+ if (!(from instanceof Range)) return movesWithin(root, from, backward, true);
431
+ const container = backward ? from.startContainer : from.endContainer;
432
+ const offset = backward ? from.startOffset : from.endOffset;
433
+ const anchor = backward ? elementBeforePosition(container, offset, root) : elementAfterPosition(container, offset, root);
434
+ return movesWithin(root, anchor, backward, true, void 0, backward ? "after" : "before", {
435
+ holder: elementHoldingPosition(container, offset)
436
+ });
310
437
  }
311
438
  function deepActiveElement(ownerDocument) {
312
439
  let active = ownerDocument.activeElement;
@@ -477,7 +604,7 @@ var INERT_MARK = `data-${FOCUS_TRAP_OWNER}-inert`;
477
604
  var OVERFLOW_MARK = `data-${FOCUS_TRAP_OWNER}-overflow`;
478
605
  var FocusTrap = class _FocusTrap {
479
606
  static #registries = sharedRegistry(
480
- "stimeo-ui.focus-trap.registry.v2",
607
+ "stimeo-ui.focus-trap.registry.v3",
481
608
  () => /* @__PURE__ */ new WeakMap()
482
609
  );
483
610
  static #TEXT_ENTRY_TYPES = /* @__PURE__ */ new Set([
@@ -500,15 +627,20 @@ var FocusTrap = class _FocusTrap {
500
627
  constructor(getContainer, options = {}) {
501
628
  this.#getContainer = getContainer;
502
629
  this.#options = options;
503
- this.#entry = {
630
+ this.#entry = this.#createEntry();
631
+ }
632
+ #createEntry() {
633
+ const entry = {
504
634
  container: () => this.#readContainer(),
505
635
  moveFocus: (backward) => this.#moveFocus(backward),
506
636
  refocus: () => this.#refocus(),
507
637
  isolated: false,
508
638
  lockedBody: null,
509
639
  openedFrom: null,
510
- handedBack: []
640
+ handedBack: [],
641
+ returnTargets: () => this.#activeReturnTargets(entry)
511
642
  };
643
+ return entry;
512
644
  }
513
645
  get active() {
514
646
  return this.#activeState;
@@ -516,6 +648,7 @@ var FocusTrap = class _FocusTrap {
516
648
  activate() {
517
649
  if (this.#activeState) return;
518
650
  _FocusTrap.#sweep();
651
+ this.#entry = this.#createEntry();
519
652
  this.#activeState = true;
520
653
  this.#openers = _FocusTrap.#focusPath();
521
654
  this.#entry.openedFrom = _FocusTrap.#trapHolding(this.#openers[0] ?? null);
@@ -532,14 +665,21 @@ var FocusTrap = class _FocusTrap {
532
665
  this.#returnTabindexes();
533
666
  return;
534
667
  }
668
+ const entry = this.#entry;
669
+ const parent = entry.openedFrom;
670
+ const openers = this.#openers;
671
+ this.#openers = [];
535
672
  this.#activeState = false;
536
673
  this.#escapeLayer.deactivate();
537
- const above = this.#leaveStack();
538
- this.#unlockScroll();
674
+ const above = this.#leaveStack(entry);
675
+ this.#unlockScroll(entry);
539
676
  _FocusTrap.#reconcileBackground(document);
540
- const wayBack = this.#handOver(above);
541
- if (restoreFocus) this.#restore(wayBack, above);
542
- this.#returnTabindexes();
677
+ try {
678
+ const wayBack = this.#handOver(entry, openers, above);
679
+ if (restoreFocus) this.#restore(wayBack, above, parent);
680
+ } finally {
681
+ this.#returnTabindexes();
682
+ }
543
683
  }
544
684
  connect() {
545
685
  this.#gate.cancel();
@@ -575,18 +715,27 @@ var FocusTrap = class _FocusTrap {
575
715
  document.addEventListener("keydown", registry.onKeydown);
576
716
  document.addEventListener("turbo:morph", registry.onMorph);
577
717
  document.addEventListener("turbo:morph-element", registry.onMorph);
718
+ document.addEventListener("pointerdown", registry.onPointerDown, {
719
+ capture: true,
720
+ passive: true
721
+ });
722
+ document.addEventListener("focusin", registry.onFocusIn, { capture: true });
578
723
  }
579
724
  registry.stack.push(this.#entry);
580
725
  }
581
- #leaveStack() {
726
+ #leaveStack(entry) {
582
727
  const registry = _FocusTrap.#registry(document);
583
- const index = registry.stack.indexOf(this.#entry);
728
+ const index = registry.stack.indexOf(entry);
584
729
  const above = registry.stack.slice(index + 1);
585
730
  registry.stack.splice(index, 1);
586
731
  if (registry.stack.length === 0) {
587
732
  document.removeEventListener("keydown", registry.onKeydown);
588
733
  document.removeEventListener("turbo:morph", registry.onMorph);
589
734
  document.removeEventListener("turbo:morph-element", registry.onMorph);
735
+ document.removeEventListener("pointerdown", registry.onPointerDown, { capture: true });
736
+ document.removeEventListener("focusin", registry.onFocusIn, { capture: true });
737
+ registry.start = null;
738
+ registry.pressed = false;
590
739
  }
591
740
  return above;
592
741
  }
@@ -604,24 +753,26 @@ var FocusTrap = class _FocusTrap {
604
753
  }
605
754
  return null;
606
755
  }
607
- #handOver(above) {
608
- const entry = this.#entry;
609
- const wayBack = [
610
- ...this.#openers,
611
- this.#options.fallbackFocus?.() ?? null,
612
- ...entry.handedBack
613
- ].filter((element) => element !== null);
614
- for (const trap of above) {
615
- if (trap.openedFrom !== entry) continue;
616
- trap.handedBack = [...trap.handedBack, ...wayBack];
617
- trap.openedFrom = entry.openedFrom;
618
- }
619
- this.#openers = [];
756
+ #handOver(entry, openers, above) {
757
+ const wayBack = [...openers];
758
+ const parent = entry.openedFrom;
759
+ const handedBack = entry.handedBack;
620
760
  entry.openedFrom = null;
621
761
  entry.handedBack = [];
762
+ try {
763
+ const fallback = this.#options.fallbackFocus?.();
764
+ if (fallback) wayBack.push(fallback);
765
+ } finally {
766
+ wayBack.push(...handedBack);
767
+ for (const trap of above) {
768
+ if (trap.openedFrom !== entry) continue;
769
+ trap.handedBack = [...trap.handedBack, ...wayBack];
770
+ trap.openedFrom = parent;
771
+ }
772
+ }
622
773
  return wayBack;
623
774
  }
624
- #restore(wayBack, above) {
775
+ #restore(wayBack, above, parent) {
625
776
  const active = deepActiveElement(document);
626
777
  const released = this.#readContainer();
627
778
  const holds = (trap) => {
@@ -632,7 +783,7 @@ var FocusTrap = class _FocusTrap {
632
783
  return !released || !flatTreeContains(released, active) || flatTreeContains(released, container);
633
784
  };
634
785
  if (above.some(holds)) return;
635
- for (const element of wayBack) {
786
+ for (const element of _FocusTrap.#returnCandidates(wayBack, parent)) {
636
787
  if (this.#activeState) return;
637
788
  if (!element.isConnected || !canTakeFocus(element) || !isRenderedForFocus(element)) continue;
638
789
  const before = deepActiveElement(document);
@@ -642,6 +793,19 @@ var FocusTrap = class _FocusTrap {
642
793
  }
643
794
  _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack)?.refocus();
644
795
  }
796
+ *#activeReturnTargets(entry) {
797
+ if (!this.#activeState || this.#entry !== entry) return;
798
+ yield* this.#openers;
799
+ const fallback = this.#options.fallbackFocus?.();
800
+ if (fallback) yield fallback;
801
+ yield* entry.handedBack;
802
+ }
803
+ static *#returnCandidates(wayBack, parent) {
804
+ yield* wayBack;
805
+ for (let entry = parent; entry && !_FocusTrap.#takesTab(entry); entry = entry.openedFrom) {
806
+ yield* entry.returnTargets?.() ?? [];
807
+ }
808
+ }
645
809
  #refocus() {
646
810
  if (!this.#flag(this.#options.autoFocus, true)) return;
647
811
  if (flatTreeContains(this.#getContainer(), deepActiveElement(document))) return;
@@ -656,8 +820,8 @@ var FocusTrap = class _FocusTrap {
656
820
  const focused = deepActiveElement(document);
657
821
  for (const [element, loan] of [...this.#tabindexLoans]) {
658
822
  if (element === focused) continue;
659
- loan.returnAll();
660
823
  this.#tabindexLoans.delete(element);
824
+ loan.returnAll();
661
825
  }
662
826
  }
663
827
  #lockScroll() {
@@ -668,11 +832,11 @@ var FocusTrap = class _FocusTrap {
668
832
  this.#entry.lockedBody = body;
669
833
  body.style.overflow = "hidden";
670
834
  }
671
- #unlockScroll() {
835
+ #unlockScroll(entry) {
672
836
  const registry = _FocusTrap.#registry(document);
673
- const body = this.#entry.lockedBody;
674
- this.#entry.lockedBody = null;
675
- if (!registry.scrollLocks.delete(this.#entry) || !body) return;
837
+ const body = entry.lockedBody;
838
+ entry.lockedBody = null;
839
+ if (!registry.scrollLocks.delete(entry) || !body) return;
676
840
  if (!_FocusTrap.#isLocked(registry, body)) _FocusTrap.#unlock(body);
677
841
  }
678
842
  static #isLocked(registry, body) {
@@ -702,8 +866,12 @@ var FocusTrap = class _FocusTrap {
702
866
  stack: [],
703
867
  onKeydown: (event) => _FocusTrap.#resolveTab(registry.stack, event),
704
868
  onMorph: () => morphs.schedule(),
869
+ onPointerDown: (event) => _FocusTrap.#recordPress(registry, event),
870
+ onFocusIn: (event) => _FocusTrap.#recordFocus(registry, event),
705
871
  inerted: /* @__PURE__ */ new Set(),
706
- scrollLocks: /* @__PURE__ */ new Set()
872
+ scrollLocks: /* @__PURE__ */ new Set(),
873
+ start: null,
874
+ pressed: false
707
875
  };
708
876
  _FocusTrap.#registries.set(ownerDocument, registry);
709
877
  return registry;
@@ -801,7 +969,7 @@ var FocusTrap = class _FocusTrap {
801
969
  return _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack) === this.#entry;
802
970
  }
803
971
  #moveFocus(backward) {
804
- const from = deepActiveElement(document);
972
+ const from = _FocusTrap.#start();
805
973
  for (const stop of sequentialTabStops(this.#getContainer(), from, backward)) {
806
974
  if (!this.#ownsTab()) return;
807
975
  const before = deepActiveElement(document);
@@ -812,18 +980,66 @@ var FocusTrap = class _FocusTrap {
812
980
  return;
813
981
  }
814
982
  }
983
+ static #start() {
984
+ const active = deepActiveElement(document);
985
+ const start = _FocusTrap.#registry(document).start;
986
+ if (active && active !== document.body) {
987
+ const at = start ? elementHoldingPosition(start.startContainer, start.startOffset) : null;
988
+ const pointInside = !(active instanceof HTMLElement && isTabStop(active)) && flatTreeContains(active, at);
989
+ return pointInside ? start : active;
990
+ }
991
+ return start ?? active;
992
+ }
993
+ static #recordPress(registry, event) {
994
+ const path = event.composedPath();
995
+ const target = path[0];
996
+ const owner = target.ownerDocument ?? document;
997
+ const roots = path.filter((node) => node instanceof ShadowRoot);
998
+ const caret = _FocusTrap.#caretAt(owner, event.clientX, event.clientY, roots);
999
+ const range = owner.createRange();
1000
+ const fits = caret !== null && target.contains(caret.node) && caret.offset <= (caret.node instanceof CharacterData ? caret.node.length : caret.node.childNodes.length);
1001
+ if (fits) range.setStart(caret.node, caret.offset);
1002
+ else range.setStart(target, 0);
1003
+ range.collapse(true);
1004
+ registry.start = range;
1005
+ registry.pressed = true;
1006
+ }
1007
+ static #recordFocus(registry, event) {
1008
+ const target = event.composedPath()[0];
1009
+ const point = registry.pressed ? registry.start : null;
1010
+ const at = point ? elementHoldingPosition(point.startContainer, point.startOffset) : null;
1011
+ const keep = !(target instanceof HTMLElement && isTabStop(target)) && flatTreeContains(target, at);
1012
+ registry.pressed = false;
1013
+ if (keep) return;
1014
+ const range = target.ownerDocument.createRange();
1015
+ range.selectNode(target);
1016
+ registry.start = range;
1017
+ }
1018
+ static #caretAt(owner, x, y, roots) {
1019
+ if (typeof owner.caretPositionFromPoint === "function") {
1020
+ const position = owner.caretPositionFromPoint(x, y, { shadowRoots: roots });
1021
+ return position ? { node: position.offsetNode, offset: position.offset } : null;
1022
+ }
1023
+ const legacy = owner;
1024
+ if (typeof legacy.caretRangeFromPoint === "function") {
1025
+ const range = legacy.caretRangeFromPoint(x, y);
1026
+ return range ? { node: range.startContainer, offset: range.startOffset } : null;
1027
+ }
1028
+ return null;
1029
+ }
815
1030
  static #selectOnEntry(stop) {
816
1031
  if (stop instanceof HTMLInputElement && _FocusTrap.#TEXT_ENTRY_TYPES.has(stop.type)) {
817
1032
  stop.select();
818
1033
  }
819
1034
  }
820
1035
  #focusInitial() {
1036
+ const container = this.#readContainer();
1037
+ if (!container) return;
821
1038
  const preferred = this.#options.initialFocus?.();
822
1039
  if (preferred) {
823
1040
  preferred.focus();
824
1041
  return;
825
1042
  }
826
- const container = this.#getContainer();
827
1043
  const [first] = sequentialTabStops(container);
828
1044
  if (first) {
829
1045
  first.focus();