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
 
@@ -245,6 +266,12 @@ function flatTreeParent(element) {
245
266
  if (parent instanceof ShadowRoot) return parent.host;
246
267
  return parent instanceof Element ? parent : null;
247
268
  }
269
+ function flatTreeContains(ancestor, element) {
270
+ for (let node = element; node; node = flatTreeParent(node)) {
271
+ if (node === ancestor) return true;
272
+ }
273
+ return false;
274
+ }
248
275
  function canTakeFocus(element) {
249
276
  for (let node = element; node; node = flatTreeParent(node)) {
250
277
  if (unslotted(node) || node.matches("[hidden], [inert]")) return false;
@@ -304,12 +331,239 @@ function isTabStop(element) {
304
331
  if (tabindex !== null) return tabindex >= 0;
305
332
  return hasNativeTabStop(element) || hasEditableTabStop(element);
306
333
  }
307
- function firstTabStop(root) {
308
- for (const candidate of root.querySelectorAll(TAB_STOP_CANDIDATE_SELECTOR)) {
309
- if (isTabStop(candidate)) return candidate;
334
+ function isRadio(element) {
335
+ return element instanceof HTMLInputElement && element.type === "radio";
336
+ }
337
+ function sameRadioGroup(a, b) {
338
+ return a.name !== "" && a.name === b.name && a.form === b.form && a.getRootNode() === b.getRootNode();
339
+ }
340
+ function isInEditableContent(element) {
341
+ for (let node = element.parentElement; node; node = node.parentElement) {
342
+ if (node.getAttribute("contenteditable")?.toLowerCase() === "false") return false;
343
+ if (hasEditableTabStop(node)) return true;
344
+ }
345
+ return false;
346
+ }
347
+ var XLINK_NAMESPACE = "http://www.w3.org/1999/xlink";
348
+ function isSvgLink(element) {
349
+ return element instanceof SVGElement && element.localName === "a" && (element.hasAttribute("href") || element.hasAttributeNS(XLINK_NAMESPACE, "href"));
350
+ }
351
+ function hasRenderedImage(area) {
352
+ const map = area.closest("map");
353
+ const key = map?.name || map?.id;
354
+ if (!map || !key) return false;
355
+ const root = map.getRootNode();
356
+ return Array.from(root.querySelectorAll("img[usemap]")).some(
357
+ (image) => image.getAttribute("usemap") === `#${key}` && isRenderedForFocus(image)
358
+ );
359
+ }
360
+ function isFocusableInOrder(element) {
361
+ const tabindex = authoredTabindex(element);
362
+ return (tabindex === null || tabindex >= 0) && canTakeFocus(element);
363
+ }
364
+ function extraStop(element) {
365
+ if (element instanceof HTMLAreaElement) {
366
+ const focusable = element.hasAttribute("href") || authoredTabindex(element) !== null;
367
+ return focusable && isFocusableInOrder(element) && hasRenderedImage(element);
368
+ }
369
+ if (element instanceof HTMLObjectElement) {
370
+ return isFocusableInOrder(element) && isRenderedForFocus(element) && element.contentWindow !== null;
371
+ }
372
+ if (isSvgLink(element))
373
+ return isFocusableInOrder(element) && isRenderedForFocus(element);
374
+ return null;
375
+ }
376
+ function isSequentialStop(element) {
377
+ const stop = extraStop(element) ?? (element.matches(TAB_STOP_CANDIDATE_SELECTOR) && isTabStop(element));
378
+ if (!stop) return false;
379
+ const link = element instanceof HTMLAnchorElement || element instanceof HTMLAreaElement || isSvgLink(element);
380
+ return !(link && authoredTabindex(element) === null && isInEditableContent(element));
381
+ }
382
+ function ownedScope(element) {
383
+ if (element.shadowRoot) return Array.from(element.shadowRoot.children);
384
+ if (element instanceof HTMLSlotElement) {
385
+ return element.assignedNodes().length > 0 ? element.assignedElements() : Array.from(element.children);
310
386
  }
311
387
  return null;
312
388
  }
389
+ function orderScope(elements, walk) {
390
+ const entries = [];
391
+ const visit = (element) => {
392
+ const isFrom = element === walk.from;
393
+ if (isFrom && walk.side !== "after") walk.fromAt = walk.tree.length;
394
+ const tabindex = authoredTabindex(element) ?? 0;
395
+ let stop = null;
396
+ if ((element instanceof HTMLElement || element instanceof SVGElement) && isSequentialStop(element)) {
397
+ stop = element;
398
+ walk.tree.push(element);
399
+ }
400
+ const owned = ownedScope(element);
401
+ if (owned) {
402
+ if (tabindex >= 0) {
403
+ entries.push({ tabindex, stops: [...stop ? [stop] : [], ...orderScope(owned, walk)] });
404
+ } else if (walk.from && flatTreeContains(element, walk.from) && (!isFrom || walk.side === "on")) {
405
+ walk.fromAt = walk.tree.length;
406
+ walk.excludedScope = element;
407
+ orderScope(owned, {
408
+ from: walk.from,
409
+ side: walk.side,
410
+ tree: walk.tree,
411
+ fromAt: -1,
412
+ excludedScope: null
413
+ });
414
+ }
415
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
416
+ return;
417
+ }
418
+ if (stop) entries.push({ tabindex, stops: [stop] });
419
+ for (const child of Array.from(element.children)) visit(child);
420
+ if (isFrom && walk.side === "after") walk.fromAt = walk.tree.length;
421
+ };
422
+ for (const element of elements) visit(element);
423
+ const positive = entries.filter((entry) => entry.tabindex > 0);
424
+ positive.sort((a, b) => a.tabindex - b.tabindex);
425
+ const rest = entries.filter((entry) => entry.tabindex === 0);
426
+ return [...positive, ...rest].flatMap((entry) => entry.stops);
427
+ }
428
+ function passesRadioRule(element, tree, focusedRadio, backward) {
429
+ if (!isRadio(element) || element.checked) return true;
430
+ const group = tree.filter(
431
+ (stop) => stop === element || isRadio(stop) && sameRadioGroup(stop, element)
432
+ );
433
+ if (group.some((radio) => radio.checked)) return false;
434
+ if (group[backward ? group.length - 1 : 0] !== element) return false;
435
+ return focusedRadio === null || !sameRadioGroup(focusedRadio, element);
436
+ }
437
+ function startIndex(order, walk, backward, isStop) {
438
+ if (walk.fromAt < 0) return backward ? order.length - 1 : 0;
439
+ const at = walk.side === "on" ? order.indexOf(walk.from) : -1;
440
+ if (at >= 0) return backward ? at - 1 : at + 1;
441
+ const neighbour = backward ? walk.tree.slice(0, walk.fromAt).reverse().find(isStop) : walk.tree.slice(walk.fromAt).find(isStop);
442
+ if (neighbour) return order.indexOf(neighbour);
443
+ return backward ? -1 : order.length;
444
+ }
445
+ function movesWithin(root, from, backward, wrap, radioContext, side = "on", position = { holder: null }) {
446
+ const walk = {
447
+ from,
448
+ side,
449
+ tree: [],
450
+ fromAt: from === root ? 0 : -1,
451
+ excludedScope: null
452
+ };
453
+ const order = orderScope(ownedScope(root) ?? Array.from(root.children), walk);
454
+ const radios = radioContext ?? {
455
+ tree: walk.tree,
456
+ focusedRadio: side === "on" && walk.fromAt >= 0 && isRadio(from) ? from : null
457
+ };
458
+ const isStop = (element) => passesRadioRule(element, radios.tree, radios.focusedRadio, backward);
459
+ const holds = (element) => position.holder !== null && flatTreeContains(element, position.holder);
460
+ const start = startIndex(
461
+ order,
462
+ walk,
463
+ backward,
464
+ (element) => order.includes(element) && isStop(element) && !holds(element)
465
+ );
466
+ const remaining = backward ? order.slice(0, start + 1).reverse() : order.slice(start);
467
+ const wrapped = wrap ? backward ? order.slice(start + 1).reverse() : order.slice(0, start) : [];
468
+ const excluded = walk.excludedScope;
469
+ const prefix = excluded && !(backward && from === excluded) ? movesWithin(
470
+ excluded,
471
+ from === excluded ? null : from,
472
+ backward,
473
+ false,
474
+ radios,
475
+ side,
476
+ position
477
+ ) : [];
478
+ return [
479
+ ...prefix,
480
+ ...[...remaining, ...wrapped].filter(
481
+ (element) => (side !== "on" || element !== from) && isStop(element)
482
+ )
483
+ ];
484
+ }
485
+ function flatParentNode(node) {
486
+ const parent = node.parentNode;
487
+ if (parent instanceof Element && parent.shadowRoot) {
488
+ for (const slot of parent.shadowRoot.querySelectorAll("slot")) {
489
+ if (slot.assignedNodes().includes(node)) return slot;
490
+ }
491
+ }
492
+ return parent instanceof ShadowRoot ? parent.host : parent;
493
+ }
494
+ function flatChildNodes(node) {
495
+ if (node instanceof Element && node.shadowRoot) return Array.from(node.shadowRoot.childNodes);
496
+ if (node instanceof HTMLSlotElement) {
497
+ const assigned = node.assignedNodes();
498
+ return assigned.length > 0 ? assigned : Array.from(node.childNodes);
499
+ }
500
+ return Array.from(node.childNodes);
501
+ }
502
+ function flatNodeAfter(node, root) {
503
+ for (let current = node; current && current !== root; ) {
504
+ const parent = flatParentNode(current);
505
+ if (!parent) return null;
506
+ const siblings = flatChildNodes(parent);
507
+ const index = siblings.indexOf(current);
508
+ const next = index >= 0 ? siblings[index + 1] : void 0;
509
+ if (next) return next;
510
+ current = parent;
511
+ }
512
+ return null;
513
+ }
514
+ function flatElementBefore(node, root) {
515
+ for (let current = node; current && current !== root; ) {
516
+ const parent = flatParentNode(current);
517
+ if (!parent) return null;
518
+ const siblings = flatChildNodes(parent);
519
+ for (let index = siblings.indexOf(current) - 1; index >= 0; index--) {
520
+ const sibling = siblings[index];
521
+ if (sibling instanceof Element) return sibling;
522
+ }
523
+ current = parent;
524
+ }
525
+ return null;
526
+ }
527
+ function elementAfterPosition(container, offset, root) {
528
+ const owner = container instanceof ShadowRoot ? container.host : container;
529
+ const child = container.childNodes[offset];
530
+ const previous = container.childNodes[offset - 1];
531
+ let node = child ?? (previous ? flatNodeAfter(previous, root) : flatChildNodes(owner)[0] ?? flatNodeAfter(owner, root));
532
+ while (node && !(node instanceof Element)) {
533
+ node = flatChildNodes(node)[0] ?? flatNodeAfter(node, root);
534
+ }
535
+ return node;
536
+ }
537
+ function elementBeforePosition(container, offset, root) {
538
+ const child = container.childNodes[offset];
539
+ if (child) return flatElementBefore(child, root);
540
+ const previous = container.childNodes[offset - 1];
541
+ if (previous) return previous instanceof Element ? previous : flatElementBefore(previous, root);
542
+ return flatElementBefore(container instanceof ShadowRoot ? container.host : container, root);
543
+ }
544
+ function elementHoldingPosition(container, offset) {
545
+ if (container instanceof Element && container.shadowRoot) {
546
+ const near = container.childNodes[offset] ?? container.childNodes[offset - 1];
547
+ return near ? flatParentNode(near) : container;
548
+ }
549
+ if (container instanceof Element) return container;
550
+ if (container instanceof ShadowRoot) return container.host;
551
+ return flatParentNode(container);
552
+ }
553
+ function sequentialTabStops(root, from = null, backward = false) {
554
+ if (!(from instanceof Range)) return movesWithin(root, from, backward, true);
555
+ const container = backward ? from.startContainer : from.endContainer;
556
+ const offset = backward ? from.startOffset : from.endOffset;
557
+ const anchor = backward ? elementBeforePosition(container, offset, root) : elementAfterPosition(container, offset, root);
558
+ return movesWithin(root, anchor, backward, true, void 0, backward ? "after" : "before", {
559
+ holder: elementHoldingPosition(container, offset)
560
+ });
561
+ }
562
+ function deepActiveElement(ownerDocument) {
563
+ let active = ownerDocument.activeElement;
564
+ while (active?.shadowRoot?.activeElement) active = active.shadowRoot.activeElement;
565
+ return active;
566
+ }
313
567
 
314
568
  var ListenerSet = class {
315
569
  #abort = new AbortController();
@@ -355,6 +609,126 @@ function stateReasonFor(event) {
355
609
  return "user";
356
610
  }
357
611
 
612
+ function queryOpenDescendants(root, selector) {
613
+ const matches = [];
614
+ const roots = [root];
615
+ for (const scope of roots) {
616
+ matches.push(...scope.querySelectorAll(selector));
617
+ if (scope.nodeType === Node.ELEMENT_NODE) {
618
+ const shadow = scope.shadowRoot;
619
+ if (shadow) roots.push(shadow);
620
+ }
621
+ for (const element of scope.querySelectorAll("*")) {
622
+ if (element.shadowRoot) roots.push(element.shadowRoot);
623
+ }
624
+ }
625
+ return matches;
626
+ }
627
+
628
+ function holdsFocus(element) {
629
+ return element.matches(":focus") || deepActiveElement(element.ownerDocument) === element;
630
+ }
631
+ var TabindexLoan = class _TabindexLoan {
632
+ static #registry = sharedRegistry(
633
+ "stimeo-ui.tabindex-loan.registry.v1",
634
+ () => ({ stacks: /* @__PURE__ */ new WeakMap(), names: /* @__PURE__ */ new Set() })
635
+ );
636
+ #value;
637
+ #record;
638
+ #preserveFocus;
639
+ #lent = /* @__PURE__ */ new Set();
640
+ constructor(value, owner, options = {}) {
641
+ this.#value = value;
642
+ this.#record = `data-${owner}-tabindex-loan`;
643
+ this.#preserveFocus = options.preserveFocus === true;
644
+ _TabindexLoan.#registry.names.add(this.#record);
645
+ }
646
+ lend(element, options = {}) {
647
+ const stack = this.#stack(element);
648
+ const current = element.getAttribute("tabindex");
649
+ const held = stack.find((hold) => hold.loan === this) ?? stack.find(
650
+ (hold) => hold.loan === null && hold.record === this.#record && hold.value === this.#value
651
+ );
652
+ const linked = stack.at(-1)?.value === current;
653
+ if (!held && options.onlyIfUnfocusable && (current !== null || element.tabIndex >= 0)) return;
654
+ if (!held && current !== null && !linked) return;
655
+ this.#lent.add(element);
656
+ if (held) held.loan = this;
657
+ else stack.push({ loan: this, record: this.#record, value: this.#value, linked });
658
+ this.#mark(element, stack);
659
+ if (current === null || !held && current !== this.#value) {
660
+ element.setAttribute("tabindex", this.#value);
661
+ }
662
+ }
663
+ return(element) {
664
+ const stack = this.#stack(element);
665
+ const hold = stack.find((held) => held.loan === this) ?? stack.find(
666
+ (held) => held.loan === null && held.record === this.#record && held.value === this.#value
667
+ );
668
+ if (!hold) return false;
669
+ if (!this.#release(element, stack, hold, this.#preserveFocus)) {
670
+ hold.loan = this;
671
+ this.#lent.add(element);
672
+ return false;
673
+ }
674
+ this.#lent.delete(element);
675
+ return true;
676
+ }
677
+ returnAll() {
678
+ for (const element of Array.from(this.#lent)) this.return(element);
679
+ }
680
+ reclaim(element) {
681
+ if (element.getAttribute(this.#record) !== this.#value) return;
682
+ const stack = this.#stack(element);
683
+ const hold = stack.find((held) => held.loan === null && held.record === this.#record);
684
+ if (hold && !holdsFocus(element)) this.#release(element, stack, hold);
685
+ }
686
+ reclaimWithin(root) {
687
+ for (const element of queryOpenDescendants(root, `[${this.#record}]`)) {
688
+ this.reclaim(element);
689
+ }
690
+ }
691
+ #stack(element) {
692
+ const stacks = _TabindexLoan.#registry.stacks;
693
+ const stack = stacks.get(element) ?? this.#seed(element);
694
+ stacks.set(element, stack);
695
+ return stack;
696
+ }
697
+ #seed(element) {
698
+ const current = element.getAttribute("tabindex");
699
+ const holds = [];
700
+ for (const record of _TabindexLoan.#registry.names) {
701
+ const value = element.getAttribute(record);
702
+ if (value !== null) holds.push({ loan: null, record, value, linked: true });
703
+ }
704
+ holds.sort((one, other) => Number(one.value === current) - Number(other.value === current));
705
+ for (const [index, hold] of holds.entries()) hold.linked = index > 0;
706
+ return holds;
707
+ }
708
+ #release(element, stack, hold, preserveFocus = false) {
709
+ const current = element.getAttribute("tabindex");
710
+ const index = stack.indexOf(hold);
711
+ const owns = current === hold.value && !stack.slice(index + 1).some((above2) => above2.value === current);
712
+ if (owns && preserveFocus && holdsFocus(element)) return false;
713
+ stack.splice(index, 1);
714
+ const above = stack[index];
715
+ if (above) above.linked &&= hold.linked;
716
+ this.#mark(element, stack);
717
+ if (!owns) return true;
718
+ const below = hold.linked ? stack[index - 1] : void 0;
719
+ if (!below) element.removeAttribute("tabindex");
720
+ else if (below.value !== current) element.setAttribute("tabindex", below.value);
721
+ return true;
722
+ }
723
+ #mark(element, stack) {
724
+ const records = /* @__PURE__ */ new Map([[this.#record, null]]);
725
+ for (const hold of stack) records.set(hold.record, hold.value);
726
+ for (const [name, recorded] of records) {
727
+ setAttributeIfChanged(element, name, recorded);
728
+ }
729
+ }
730
+ };
731
+
358
732
  var PopoverController = class extends Controller {
359
733
  static targets = ["trigger", "panel"];
360
734
  static values = {
@@ -365,11 +739,18 @@ var PopoverController = class extends Controller {
365
739
  #stopScrollDismiss = null;
366
740
  #escapeLayer = new EscapeLayer();
367
741
  #expanded = new AttributeLease("aria-expanded", this.identifier);
368
- #shownPanel = null;
742
+ #openState = false;
743
+ #currentPanel = null;
744
+ #openToken = null;
745
+ #hidden = new AttributeLease("hidden", this.identifier);
746
+ #tabindex = new TabindexLoan("-1", this.identifier);
369
747
  #connected = false;
370
748
  #reporting = false;
371
749
  #listeners = new ListenerSet();
372
750
  connect() {
751
+ this.#reporting = false;
752
+ this.#openState = false;
753
+ this.#currentPanel = null;
373
754
  this.#close("api");
374
755
  this.#listeners.add(document, "click", this.#onOutsideClick, { capture: true });
375
756
  this.#listeners.add(this.element, "focusout", this.#onFocusOut);
@@ -382,16 +763,20 @@ var PopoverController = class extends Controller {
382
763
  this.#reporting = false;
383
764
  this.#release();
384
765
  this.#listeners.dispose();
766
+ this.#openState = false;
767
+ this.#currentPanel = null;
385
768
  }
386
769
  closeOnScrollValueChanged() {
387
- if (this.#shownPanel) this.#syncScrollDismiss();
770
+ if (this.#openState) this.#syncScrollDismiss();
771
+ }
772
+ panelTargetConnected() {
773
+ if (this.#connected) this.#syncPanel();
388
774
  }
389
775
  panelTargetDisconnected(panel) {
390
- if (panel !== this.#shownPanel) return;
391
- if (this.panelTargets.includes(panel)) return;
392
- this.#release();
393
- panel.hidden = true;
394
- this.#reflectClosed();
776
+ if (this.#connected) this.#syncPanel();
777
+ if (!this.scope.containsElement(panel) || !this.panelTargets.includes(panel)) {
778
+ this.#returnPanel(panel);
779
+ }
395
780
  }
396
781
  triggerTargetConnected() {
397
782
  if (this.#connected) this.#reflectTrigger();
@@ -408,19 +793,23 @@ var PopoverController = class extends Controller {
408
793
  }
409
794
  }
410
795
  open(event) {
411
- this.#open(stateReasonFor(event));
796
+ this.#reveal(stateReasonFor(event));
412
797
  }
413
798
  close(event) {
414
799
  this.#close(stateReasonFor(event));
415
800
  }
416
- #open(reason) {
417
- if (!this.hasPanelTarget || this.#isOpen) return;
418
- const panel = this.panelTarget;
419
- panel.hidden = false;
420
- if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, "true");
421
- this.#shownPanel = panel;
801
+ #reveal(reason) {
802
+ this.#syncPanel();
803
+ if (this.#isOpen) return;
804
+ this.#openState = true;
805
+ const token = {};
806
+ this.#openToken = token;
807
+ this.#reflectPanel(this.#currentPanel, true);
808
+ this.#reflectTrigger();
422
809
  if (this.#reporting) this.dispatch("open", { detail: { reason }, cancelable: false });
423
- if (this.#shownPanel !== panel || !this.#isOpen) return;
810
+ if (!this.#openState || !this.#reporting) return;
811
+ this.#syncPanel();
812
+ if (this.#openToken !== token || !this.#isOpen) return;
424
813
  this.#escapeLayer.activate(document, {
425
814
  onDismiss: () => this.#closeAndRestore(),
426
815
  claims: claimsWhileFocusWithin(this.element)
@@ -429,58 +818,89 @@ var PopoverController = class extends Controller {
429
818
  this.#focusFirst();
430
819
  }
431
820
  #close(reason) {
821
+ this.#syncPanel();
432
822
  const was = this.#isOpen;
823
+ this.#openState = false;
433
824
  this.#release();
434
- this.#reflectClosed();
825
+ this.#reflectPanel(this.#currentPanel, false);
826
+ this.#reflectTrigger();
435
827
  if (was && this.#reporting) this.dispatch("close", { detail: { reason }, cancelable: false });
436
828
  }
437
- #reflectClosed() {
438
- if (this.hasPanelTarget) this.panelTarget.hidden = true;
439
- if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, "false");
829
+ #syncPanel() {
830
+ const next = this.hasPanelTarget ? this.panelTarget : null;
831
+ const current = this.#currentPanel;
832
+ if (current === next) return;
833
+ if (current) {
834
+ this.#openState = !current.hidden;
835
+ if (!this.scope.containsElement(current) || !this.panelTargets.includes(current)) {
836
+ this.#returnPanel(current);
837
+ } else this.#reflectPanel(current, false);
838
+ }
839
+ this.#currentPanel = next;
840
+ this.#reflectPanel(next, this.#openState);
841
+ this.#reflectTrigger();
842
+ if (!this.#openState) this.#release();
843
+ else this.#syncScrollDismiss();
844
+ }
845
+ #reflectPanel(panel, open) {
846
+ if (!panel) return;
847
+ this.#hidden.write(panel, open ? null : "");
848
+ if (!open) this.#tabindex.return(panel);
849
+ }
850
+ #returnPanel(panel) {
851
+ this.#hidden.return(panel);
852
+ this.#tabindex.return(panel);
440
853
  }
441
854
  #reflectTrigger() {
442
855
  if (this.hasTriggerTarget) this.#expanded.write(this.triggerTarget, String(this.#isOpen));
443
856
  }
444
857
  #release() {
445
- this.#shownPanel = null;
858
+ this.#openToken = null;
446
859
  this.#escapeLayer.deactivate();
447
860
  this.#stopScrollDismiss?.();
448
861
  this.#stopScrollDismiss = null;
449
862
  }
450
863
  #syncScrollDismiss() {
451
- if (this.closeOnScrollValue) {
452
- this.#stopScrollDismiss ??= observeScrollDismiss(this.element, () => this.#close("scroll"));
864
+ if (this.closeOnScrollValue && this.#currentPanel && this.#openState) {
865
+ this.#stopScrollDismiss ??= observeScrollDismiss(this.element, () => {
866
+ if (this.hasPanelTarget) this.#close("scroll");
867
+ });
453
868
  return;
454
869
  }
455
870
  this.#stopScrollDismiss?.();
456
871
  this.#stopScrollDismiss = null;
457
872
  }
458
873
  #focusFirst() {
459
- const first = firstTabStop(this.panelTarget);
874
+ const panel = this.#currentPanel;
875
+ if (!panel) return;
876
+ const [first] = sequentialTabStops(panel);
460
877
  if (first) {
461
878
  first.focus();
462
879
  return;
463
880
  }
464
- if (!this.panelTarget.hasAttribute("tabindex")) this.panelTarget.tabIndex = -1;
465
- this.panelTarget.focus();
881
+ this.#tabindex.lend(panel);
882
+ panel.focus();
466
883
  }
467
884
  #closeAndRestore() {
468
885
  this.#close("escape");
469
886
  if (this.hasTriggerTarget) this.triggerTarget.focus();
470
887
  }
471
888
  #onOutsideClick = (event) => {
889
+ this.#syncPanel();
472
890
  const target = event.target;
473
891
  if (this.#closable && target instanceof Node && !this.element.contains(target)) {
474
892
  this.#close("outside");
475
893
  }
476
894
  };
477
895
  #onFocusOut = (event) => {
896
+ this.#syncPanel();
478
897
  if (!this.#closable) return;
479
898
  const next = event.relatedTarget;
480
899
  if (!(next instanceof Node) || this.element.contains(next)) return;
481
900
  this.#close("focus");
482
901
  };
483
902
  #onDocumentFocusIn = (event) => {
903
+ this.#syncPanel();
484
904
  if (!this.#closable) return;
485
905
  const target = event.target;
486
906
  if (!(target instanceof Node) || target === document.body || this.element.contains(target)) {
@@ -489,10 +909,10 @@ var PopoverController = class extends Controller {
489
909
  this.#close("focus");
490
910
  };
491
911
  get #isOpen() {
492
- return this.hasPanelTarget && !this.panelTarget.hidden;
912
+ return this.#currentPanel ? !this.#currentPanel.hidden : this.#openState;
493
913
  }
494
914
  get #closable() {
495
- return this.#isOpen || this.#shownPanel !== null;
915
+ return this.#isOpen || this.#openState;
496
916
  }
497
917
  };
498
918
 
@@ -161,6 +161,7 @@ function clampRange(value, min, max) {
161
161
 
162
162
  function setAttributeIfChanged(element, name, value, beforeWrite) {
163
163
  if (element.getAttribute(name) === value) return false;
164
+ beforeWrite?.();
164
165
  if (value === null) element.removeAttribute(name);
165
166
  else element.setAttribute(name, value);
166
167
  return true;
@@ -246,14 +247,34 @@ var AttributeLease = class {
246
247
  const names = registry.names;
247
248
  names.set(attribute, (names.get(attribute) ?? /* @__PURE__ */ new Set()).add(this.#record));
248
249
  }
249
- write(element, value) {
250
+ write(element, value, beforeWrite) {
250
251
  const stack = attributeStack(element, this.#attribute);
251
252
  const hold = this.#take(stack) ?? this.#acquire(element, stack);
252
253
  this.#held.add(element);
253
254
  const shows = !this.#covered(element, stack, hold);
254
255
  hold.written = value;
255
256
  this.#mark(element, stack);
256
- if (shows) this.#reflect(element, value);
257
+ if (shows) this.#reflect(element, value, beforeWrite);
258
+ }
259
+ adopt(element, options = {}) {
260
+ const known = registry.stacks.get(element)?.has(this.#attribute);
261
+ if (!this.#held.has(element) && !known && !element.hasAttribute(this.#record)) return false;
262
+ const stack = attributeStack(element, this.#attribute);
263
+ const hold = this.#take(stack);
264
+ if (!hold) return false;
265
+ const current = element.getAttribute(this.#attribute);
266
+ if (options.ifChanged && (current === hold.written || this.#covered(element, stack, hold))) {
267
+ return false;
268
+ }
269
+ this.#held.add(element);
270
+ hold.original = current;
271
+ hold.linked = false;
272
+ for (const above of stack.slice(stack.indexOf(hold) + 1)) {
273
+ if (!above.linked) break;
274
+ above.original = current;
275
+ }
276
+ this.#mark(element, stack);
277
+ return true;
257
278
  }
258
279
  return(element) {
259
280
  const held = this.#held.delete(element);
@@ -319,8 +340,8 @@ var AttributeLease = class {
319
340
  setAttributeIfChanged(element, name, recorded);
320
341
  }
321
342
  }
322
- #reflect(element, value) {
323
- setAttributeIfChanged(element, this.#attribute, value);
343
+ #reflect(element, value, beforeWrite) {
344
+ setAttributeIfChanged(element, this.#attribute, value, beforeWrite);
324
345
  }
325
346
  };
326
347