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
@@ -33,6 +33,180 @@ function announce(message, options = {}) {
33
33
  window.dispatchEvent(event);
34
34
  }
35
35
 
36
+ function setAttributeIfChanged(element, name, value, beforeWrite) {
37
+ if (element.getAttribute(name) === value) return false;
38
+ beforeWrite?.();
39
+ if (value === null) element.removeAttribute(name);
40
+ else element.setAttribute(name, value);
41
+ return true;
42
+ }
43
+
44
+ function sharedRegistry(key, create) {
45
+ const symbol = Symbol.for(key);
46
+ const scope = globalThis;
47
+ const existing = scope[symbol];
48
+ if (existing !== void 0) return existing;
49
+ const registry2 = create();
50
+ Object.defineProperty(globalThis, symbol, { value: registry2 });
51
+ return registry2;
52
+ }
53
+
54
+ var registry = sharedRegistry(
55
+ "stimeo-ui.attribute-lease.registry.v1",
56
+ () => ({ stacks: /* @__PURE__ */ new WeakMap(), names: /* @__PURE__ */ new Map() })
57
+ );
58
+ function isAttributeValue(value) {
59
+ return value === null || typeof value === "string";
60
+ }
61
+ function readLeaseRecord(element, record) {
62
+ try {
63
+ const parsed = JSON.parse(element.getAttribute(record) ?? "null");
64
+ if (!Array.isArray(parsed)) return void 0;
65
+ const [original, written, ...rest] = parsed;
66
+ const linked = rest[0] !== false;
67
+ const beneath = linked ? rest : rest.slice(1);
68
+ if (!beneath.every((under) => typeof under === "string")) return void 0;
69
+ return isAttributeValue(original) && isAttributeValue(written) ? { original, written, linked, beneath } : void 0;
70
+ } catch {
71
+ return void 0;
72
+ }
73
+ }
74
+ function attributeStack(element, attribute) {
75
+ const byAttribute = registry.stacks.get(element) ?? /* @__PURE__ */ new Map();
76
+ registry.stacks.set(element, byAttribute);
77
+ const known = byAttribute.get(attribute);
78
+ if (known) return known;
79
+ const found = [];
80
+ for (const record of registry.names.get(attribute) ?? []) {
81
+ const read = readLeaseRecord(element, record);
82
+ if (read) found.push({ ...read, record });
83
+ }
84
+ const depth = ({ beneath }) => beneath.filter((name) => found.some((other) => other.record === name)).length;
85
+ const stack = found.sort((one, other) => depth(one) - depth(other)).map(({ record, written, original, linked }, index) => ({
86
+ lease: null,
87
+ record,
88
+ written,
89
+ original,
90
+ linked: linked && index > 0
91
+ }));
92
+ byAttribute.set(attribute, stack);
93
+ return stack;
94
+ }
95
+ function findHold(stack, lease, record) {
96
+ return stack.find((held) => held.lease === lease) ?? stack.find((held) => held.lease === null && held.record === record);
97
+ }
98
+ var AttributeLease = class {
99
+ #attribute;
100
+ #record;
101
+ #base;
102
+ #preserveAuthor;
103
+ #held = /* @__PURE__ */ new Set();
104
+ constructor(attribute, owner, options = {}) {
105
+ this.#attribute = attribute;
106
+ this.#record = `data-${owner}-${attribute}-lease`;
107
+ this.#base = options.base === true;
108
+ this.#preserveAuthor = options.preserveAuthor === true;
109
+ const names = registry.names;
110
+ names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
111
+ }
112
+ write(element, value, beforeWrite) {
113
+ const stack = attributeStack(element, this.#attribute);
114
+ const hold = this.#take(stack) ?? this.#acquire(element, stack);
115
+ this.#held.add(element);
116
+ const shows = !this.#covered(element, stack, hold);
117
+ hold.written = value;
118
+ this.#mark(element, stack);
119
+ if (shows) this.#reflect(element, value, beforeWrite);
120
+ }
121
+ adopt(element, options = {}) {
122
+ const known = registry.stacks.get(element)?.has(this.#attribute);
123
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
124
+ const stack = attributeStack(element, this.#attribute);
125
+ const hold = this.#take(stack);
126
+ if (!hold) return false;
127
+ const current = element.getAttribute(this.#attribute);
128
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
129
+ return false;
130
+ }
131
+ this.#held.add(element);
132
+ hold.original = current;
133
+ hold.linked = false;
134
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
135
+ if (!above.linked) break;
136
+ above.original = current;
137
+ }
138
+ this.#mark(element, stack);
139
+ return true;
140
+ }
141
+ return(element) {
142
+ const held = this.#held.delete(element);
143
+ const known = registry.stacks.get(element)?.has(this.#attribute);
144
+ const stack = held || known || element.hasAttribute(this.#record) ? attributeStack(element, this.#attribute) : [];
145
+ const hold = this.#take(stack);
146
+ if (!hold) return false;
147
+ const carries = element.getAttribute(this.#attribute) === hold.written;
148
+ const owns = carries && !this.#covered(element, stack, hold);
149
+ const index = stack.indexOf(hold);
150
+ stack.splice(index, 1);
151
+ const above = stack[index];
152
+ if (above) above.linked &&= hold.linked;
153
+ this.#mark(element, stack);
154
+ if (!owns) return true;
155
+ const below = hold.linked ? stack[index - 1] : void 0;
156
+ this.#reflect(element, below ? below.written : hold.original);
157
+ return true;
158
+ }
159
+ returnAll() {
160
+ for (const element of Array.from(this.#held)) this.return(element);
161
+ }
162
+ #take(stack) {
163
+ const hold = findHold(stack, this, this.#record);
164
+ if (hold) hold.lease = this;
165
+ return hold;
166
+ }
167
+ #acquire(element, stack) {
168
+ const current = element.getAttribute(this.#attribute);
169
+ const top = stack.at(-1);
170
+ const bottom = stack[0];
171
+ const over = top?.written === current ? top : void 0;
172
+ const from = this.#base ? this.#preserveAuthor && !stack.some((held) => held.written === current) ? void 0 : bottom : over;
173
+ const hold = {
174
+ lease: this,
175
+ record: this.#record,
176
+ written: current,
177
+ original: from ? from.original : current,
178
+ linked: !this.#base && over !== void 0
179
+ };
180
+ if (this.#base && bottom) {
181
+ bottom.linked = true;
182
+ stack.unshift(hold);
183
+ } else stack.push(hold);
184
+ return hold;
185
+ }
186
+ #covered(element, stack, hold) {
187
+ const current = element.getAttribute(this.#attribute);
188
+ return stack.slice(stack.indexOf(hold) + 1).some((above) => above.written === current);
189
+ }
190
+ #mark(element, stack) {
191
+ const records = /* @__PURE__ */ new Map([[this.#record, null]]);
192
+ stack.forEach((hold, index) => {
193
+ const names = stack.slice(0, index).map((under) => under.record);
194
+ const beneath = [...new Set(names.reverse())].filter((name) => name !== hold.record);
195
+ const marker = hold.linked || beneath.length === 0 ? [] : [false];
196
+ records.set(
197
+ hold.record,
198
+ hold.written === hold.original && beneath.length === 0 && index === stack.length - 1 ? null : JSON.stringify([hold.original, hold.written, ...marker, ...beneath])
199
+ );
200
+ });
201
+ for (const [name, recorded] of records) {
202
+ setAttributeIfChanged(element, name, recorded);
203
+ }
204
+ }
205
+ #reflect(element, value, beforeWrite) {
206
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
207
+ }
208
+ };
209
+
36
210
  function ownerIndex(candidates, node) {
37
211
  if (!(node instanceof Node)) return -1;
38
212
  return candidates.findIndex((candidate) => candidate.contains(node));
@@ -380,6 +554,8 @@ var FlashController = class _FlashController extends Controller {
380
554
  #dismiss = new PausableTimers();
381
555
  #fixedDismiss = new KeyedTimers();
382
556
  #reapply = new MorphRenderWatcher(() => this.#reapplyCap());
557
+ #role = new AttributeLease("role", this.identifier);
558
+ #writtenStates = /* @__PURE__ */ new WeakMap();
383
559
  #spared = [];
384
560
  #pointer = new HoverRecheck(
385
561
  this.element.ownerDocument,
@@ -405,7 +581,7 @@ var FlashController = class _FlashController extends Controller {
405
581
  this.#connected = true;
406
582
  const current = this.messageTargets;
407
583
  for (const item of this.#arrivals) {
408
- if (!current.includes(item)) this.#arrivals.delete(item);
584
+ if (!current.includes(item)) this.#returnMessage(item);
409
585
  }
410
586
  this.#discardInherited();
411
587
  this.#reapply.observe(this.element);
@@ -456,16 +632,23 @@ var FlashController = class _FlashController extends Controller {
456
632
  return this.hasRegionTarget && this.regionTarget.contains(message);
457
633
  }
458
634
  messageTargetDisconnected(message) {
459
- if (!this.#connected) return;
460
- const moved = this.#owns(message) && message.matches(this.#messageSelector);
461
- if (moved) {
462
- this.#afterMove(message);
635
+ if (this.#owns(message) && this.messageTargets.includes(message)) {
636
+ if (this.#connected) this.#afterMove(message);
463
637
  return;
464
638
  }
465
- this.#forget(message);
466
- this.#arrivals.delete(message);
639
+ if (!this.#connected && !message.isConnected) return;
640
+ this.#returnMessage(message);
641
+ if (this.#connected) this.#forget(message);
467
642
  this.#leaving.delete(message);
468
643
  }
644
+ #returnMessage(message) {
645
+ this.#role.return(message);
646
+ if (!this.#arrivals.delete(message) || !message.isConnected) return;
647
+ if (message.getAttribute("data-flash-state") === this.#writtenStates.get(message)) {
648
+ message.removeAttribute("data-flash-state");
649
+ }
650
+ message.removeAttribute(this.#shownAttribute);
651
+ }
469
652
  #resync() {
470
653
  if (!this.#connected) return;
471
654
  this.#syncObservation();
@@ -521,9 +704,9 @@ var FlashController = class _FlashController extends Controller {
521
704
  const type = message.getAttribute("data-flash-type") ?? "";
522
705
  const assertive = ASSERTIVE_TYPES.has(type);
523
706
  if (!message.hasAttribute("role")) {
524
- message.setAttribute("role", assertive ? "alert" : "status");
707
+ this.#role.write(message, assertive ? "alert" : "status");
525
708
  }
526
- message.setAttribute("data-flash-state", "visible");
709
+ this.#writeState(message, "visible");
527
710
  message.setAttribute(this.#shownAttribute, "");
528
711
  const known = this.#arrivals.has(message);
529
712
  this.#order.push(message);
@@ -568,14 +751,18 @@ var FlashController = class _FlashController extends Controller {
568
751
  if (spare) this.#spared.push(spare);
569
752
  this.#reapply.schedule();
570
753
  }
754
+ #writeState(message, state) {
755
+ this.#writtenStates.set(message, state);
756
+ message.setAttribute("data-flash-state", state);
757
+ }
571
758
  #reapplyCap() {
572
759
  for (const message of [...this.#order, ...this.#leaving]) {
573
760
  if (!this.#owns(message)) continue;
574
- message.setAttribute("data-flash-state", this.#leaving.has(message) ? "leaving" : "visible");
761
+ this.#writeState(message, this.#leaving.has(message) ? "leaving" : "visible");
575
762
  message.setAttribute(this.#shownAttribute, "");
576
763
  if (!message.hasAttribute("role")) {
577
764
  const assertive = ASSERTIVE_TYPES.has(message.getAttribute("data-flash-type") ?? "");
578
- message.setAttribute("role", assertive ? "alert" : "status");
765
+ this.#role.write(message, assertive ? "alert" : "status");
579
766
  }
580
767
  }
581
768
  const spared = this.#spared;
@@ -605,7 +792,7 @@ var FlashController = class _FlashController extends Controller {
605
792
  if (!this.#order.includes(message)) return;
606
793
  this.#forget(message);
607
794
  this.#leaving.add(message);
608
- message.setAttribute("data-flash-state", "leaving");
795
+ this.#writeState(message, "leaving");
609
796
  const finalize = () => {
610
797
  if (!this.#leaving.delete(message)) return;
611
798
  message.remove();
@@ -265,7 +265,8 @@ function ownedScope(element) {
265
265
  function orderScope(elements, walk) {
266
266
  const entries = [];
267
267
  const visit = (element) => {
268
- 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;
269
270
  const tabindex = authoredTabindex(element) ?? 0;
270
271
  let stop = null;
271
272
  if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
@@ -276,15 +277,23 @@ function orderScope(elements, walk) {
276
277
  if (owned) {
277
278
  if (tabindex >= 0) {
278
279
  entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
279
- } else if (walk.from && flatTreeContains(element, walk.from)) {
280
+ } else if (walk.from && flatTreeContains(element, walk.from) && (!isFrom || walk.side === "on")) {
280
281
  walk.fromAt = walk.tree.length;
281
282
  walk.excludedScope = element;
282
- orderScope(owned, { from: walk.from, tree: walk.tree, fromAt: -1, excludedScope: null });
283
+ orderScope(owned, {
284
+ from: walk.from,
285
+ side: walk.side,
286
+ tree: walk.tree,
287
+ fromAt: -1,
288
+ excludedScope: null
289
+ });
283
290
  }
291
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
284
292
  return;
285
293
  }
286
294
  if (stop) entries.push({ tabindex, stops: [stop] });
287
295
  for (const child of Array.from(element.children)) visit(child);
296
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
288
297
  };
289
298
  for (const element of elements) visit(element);
290
299
  const positive = entries.filter((entry) => entry.tabindex > 0);
@@ -292,26 +301,27 @@ function orderScope(elements, walk) {
292
301
  const rest = entries.filter((entry) => entry.tabindex === 0);
293
302
  return [...positive, ...rest].flatMap((entry) => entry.stops);
294
303
  }
295
- function passesRadioRule(element, tree, focusedRadio) {
304
+ function passesRadioRule(element, tree, focusedRadio, backward) {
296
305
  if (!isRadio(element) || element.checked) return true;
297
306
  const group = tree.filter(
298
307
  (stop) => stop === element || isRadio(stop) && sameRadioGroup(stop, element)
299
308
  );
300
309
  if (group.some((radio) => radio.checked)) return false;
301
- if (group[0] !== element) return false;
310
+ if (group[backward ? group.length - 1 : 0] !== element) return false;
302
311
  return focusedRadio === null || !sameRadioGroup(focusedRadio, element);
303
312
  }
304
313
  function startIndex(order, walk, backward, isStop) {
305
314
  if (walk.fromAt < 0) return backward ? order.length - 1 : 0;
306
- const at = order.indexOf(walk.from);
315
+ const at = walk.side === "on" ? order.indexOf(walk.from) : -1;
307
316
  if (at >= 0) return backward ? at - 1 : at + 1;
308
317
  const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
309
318
  if (neighbour) return order.indexOf(neighbour);
310
319
  return backward ? -1 : order.length;
311
320
  }
312
- function movesWithin(root, from, backward, wrap, radioContext) {
321
+ function movesWithin(root, from, backward, wrap, radioContext, side = "on", position = { holder: null }) {
313
322
  const walk = {
314
323
  from,
324
+ side,
315
325
  tree: [],
316
326
  fromAt: from === root ? 0 : -1,
317
327
  excludedScope: null
@@ -319,26 +329,111 @@ function movesWithin(root, from, backward, wrap, radioContext) {
319
329
  const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
320
330
  const radios = radioContext ?? {
321
331
  tree: walk.tree,
322
- focusedRadio: walk.fromAt >= 0 && isRadio(from) ? from : null
332
+ focusedRadio: side === "on" && walk.fromAt >= 0 && isRadio(from) ? from : null
323
333
  };
324
- const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio);
334
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio, backward);
335
+ const holds = (element) => position.holder !== null && flatTreeContains(element, position.holder);
325
336
  const start = startIndex(
326
337
  order,
327
338
  walk,
328
339
  backward,
329
- (element) => order.includes(element) && isStop(element)
340
+ (element) => order.includes(element) && isStop(element) && !holds(element)
330
341
  );
331
342
  const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
332
343
  const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
333
344
  const excluded = walk.excludedScope;
334
- const prefix = excluded && !(backward && from === excluded) ? movesWithin(excluded, from === excluded ? null : from, backward, false, radios) : [];
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
+ ) : [];
335
354
  return [
336
355
  ...prefix,
337
- ...[...remaining, ...wrapped].filter((element) => element !== from && isStop(element))
356
+ ...[...remaining, ...wrapped].filter(
357
+ (element) => (side !== "on" || element !== from) && isStop(element)
358
+ )
338
359
  ];
339
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
+ }
340
429
  function sequentialTabStops(root, from = null, backward = false) {
341
- return movesWithin(root, from, backward, true);
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
+ });
342
437
  }
343
438
  function deepActiveElement(ownerDocument) {
344
439
  let active = ownerDocument.activeElement;
@@ -509,7 +604,7 @@ var INERT_MARK = `data-${FOCUS_TRAP_OWNER}-inert`;
509
604
  var OVERFLOW_MARK = `data-${FOCUS_TRAP_OWNER}-overflow`;
510
605
  var FocusTrap = class _FocusTrap {
511
606
  static #registries = sharedRegistry(
512
- "stimeo-ui.focus-trap.registry.v2",
607
+ "stimeo-ui.focus-trap.registry.v3",
513
608
  () => /* @__PURE__ */ new WeakMap()
514
609
  );
515
610
  static #TEXT_ENTRY_TYPES = /* @__PURE__ */ new Set([
@@ -535,15 +630,17 @@ var FocusTrap = class _FocusTrap {
535
630
  this.#entry = this.#createEntry();
536
631
  }
537
632
  #createEntry() {
538
- return {
633
+ const entry = {
539
634
  container: () => this.#readContainer(),
540
635
  moveFocus: (backward) => this.#moveFocus(backward),
541
636
  refocus: () => this.#refocus(),
542
637
  isolated: false,
543
638
  lockedBody: null,
544
639
  openedFrom: null,
545
- handedBack: []
640
+ handedBack: [],
641
+ returnTargets: () => this.#activeReturnTargets(entry)
546
642
  };
643
+ return entry;
547
644
  }
548
645
  get active() {
549
646
  return this.#activeState;
@@ -569,6 +666,7 @@ var FocusTrap = class _FocusTrap {
569
666
  return;
570
667
  }
571
668
  const entry = this.#entry;
669
+ const parent = entry.openedFrom;
572
670
  const openers = this.#openers;
573
671
  this.#openers = [];
574
672
  this.#activeState = false;
@@ -578,7 +676,7 @@ var FocusTrap = class _FocusTrap {
578
676
  _FocusTrap.#reconcileBackground(document);
579
677
  try {
580
678
  const wayBack = this.#handOver(entry, openers, above);
581
- if (restoreFocus) this.#restore(wayBack, above);
679
+ if (restoreFocus) this.#restore(wayBack, above, parent);
582
680
  } finally {
583
681
  this.#returnTabindexes();
584
682
  }
@@ -617,6 +715,11 @@ var FocusTrap = class _FocusTrap {
617
715
  document.addEventListener("keydown", registry.onKeydown);
618
716
  document.addEventListener("turbo:morph", registry.onMorph);
619
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 });
620
723
  }
621
724
  registry.stack.push(this.#entry);
622
725
  }
@@ -629,6 +732,10 @@ var FocusTrap = class _FocusTrap {
629
732
  document.removeEventListener("keydown", registry.onKeydown);
630
733
  document.removeEventListener("turbo:morph", registry.onMorph);
631
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;
632
739
  }
633
740
  return above;
634
741
  }
@@ -665,7 +772,7 @@ var FocusTrap = class _FocusTrap {
665
772
  }
666
773
  return wayBack;
667
774
  }
668
- #restore(wayBack, above) {
775
+ #restore(wayBack, above, parent) {
669
776
  const active = deepActiveElement(document);
670
777
  const released = this.#readContainer();
671
778
  const holds = (trap) => {
@@ -676,7 +783,7 @@ var FocusTrap = class _FocusTrap {
676
783
  return !released || !flatTreeContains(released, active) || flatTreeContains(released, container);
677
784
  };
678
785
  if (above.some(holds)) return;
679
- for (const element of wayBack) {
786
+ for (const element of _FocusTrap.#returnCandidates(wayBack, parent)) {
680
787
  if (this.#activeState) return;
681
788
  if (!element.isConnected || !canTakeFocus(element) || !isRenderedForFocus(element)) continue;
682
789
  const before = deepActiveElement(document);
@@ -686,6 +793,19 @@ var FocusTrap = class _FocusTrap {
686
793
  }
687
794
  _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack)?.refocus();
688
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
+ }
689
809
  #refocus() {
690
810
  if (!this.#flag(this.#options.autoFocus, true)) return;
691
811
  if (flatTreeContains(this.#getContainer(), deepActiveElement(document))) return;
@@ -700,8 +820,8 @@ var FocusTrap = class _FocusTrap {
700
820
  const focused = deepActiveElement(document);
701
821
  for (const [element, loan] of [...this.#tabindexLoans]) {
702
822
  if (element === focused) continue;
703
- loan.returnAll();
704
823
  this.#tabindexLoans.delete(element);
824
+ loan.returnAll();
705
825
  }
706
826
  }
707
827
  #lockScroll() {
@@ -746,8 +866,12 @@ var FocusTrap = class _FocusTrap {
746
866
  stack: [],
747
867
  onKeydown: (event) => _FocusTrap.#resolveTab(registry.stack, event),
748
868
  onMorph: () => morphs.schedule(),
869
+ onPointerDown: (event) => _FocusTrap.#recordPress(registry, event),
870
+ onFocusIn: (event) => _FocusTrap.#recordFocus(registry, event),
749
871
  inerted: /* @__PURE__ */ new Set(),
750
- scrollLocks: /* @__PURE__ */ new Set()
872
+ scrollLocks: /* @__PURE__ */ new Set(),
873
+ start: null,
874
+ pressed: false
751
875
  };
752
876
  _FocusTrap.#registries.set(ownerDocument, registry);
753
877
  return registry;
@@ -845,7 +969,7 @@ var FocusTrap = class _FocusTrap {
845
969
  return _FocusTrap.#tabOwner(_FocusTrap.#registry(document).stack) === this.#entry;
846
970
  }
847
971
  #moveFocus(backward) {
848
- const from = deepActiveElement(document);
972
+ const from = _FocusTrap.#start();
849
973
  for (const stop of sequentialTabStops(this.#getContainer(), from, backward)) {
850
974
  if (!this.#ownsTab()) return;
851
975
  const before = deepActiveElement(document);
@@ -856,18 +980,66 @@ var FocusTrap = class _FocusTrap {
856
980
  return;
857
981
  }
858
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
+ }
859
1030
  static #selectOnEntry(stop) {
860
1031
  if (stop instanceof HTMLInputElement && _FocusTrap.#TEXT_ENTRY_TYPES.has(stop.type)) {
861
1032
  stop.select();
862
1033
  }
863
1034
  }
864
1035
  #focusInitial() {
1036
+ const container = this.#readContainer();
1037
+ if (!container) return;
865
1038
  const preferred = this.#options.initialFocus?.();
866
1039
  if (preferred) {
867
1040
  preferred.focus();
868
1041
  return;
869
1042
  }
870
- const container = this.#getContainer();
871
1043
  const [first] = sequentialTabStops(container);
872
1044
  if (first) {
873
1045
  first.focus();