@lostgradient/cinder 0.24.1 → 0.24.3

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 (30) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/components.json +1 -1
  3. package/dist/components/alert-dialog/index.js +31 -4
  4. package/dist/components/confirm-dialog/index.js +31 -4
  5. package/dist/components/drawer/index.js +31 -4
  6. package/dist/components/focus-trap/focus-trap.schema.js +16 -1
  7. package/dist/components/focus-trap/focus-trap.types.d.ts +6 -0
  8. package/dist/components/focus-trap/focus-trap.utilities.svelte.d.ts +37 -0
  9. package/dist/components/focus-trap/index.js +31 -4
  10. package/dist/components/modal/index.js +31 -4
  11. package/dist/components/selection-popover/index.js +4 -2
  12. package/dist/components/sidebar/index.js +31 -4
  13. package/dist/index.js +34 -5
  14. package/dist/server/components/alert-dialog/index.js +1 -1
  15. package/dist/server/components/confirm-dialog/index.js +1 -1
  16. package/dist/server/components/drawer/index.js +1 -1
  17. package/dist/server/components/focus-trap/index.js +1 -1
  18. package/dist/server/components/modal/index.js +1 -1
  19. package/dist/server/components/selection-popover/index.js +1 -1
  20. package/dist/server/components/sidebar/index.js +1 -1
  21. package/dist/server/index.js +2 -2
  22. package/dist/server/{index.server-c71m0a88.js → index.server-0yrz8ryn.js} +3 -2
  23. package/dist/server/{index.server-r5emexd4.js → index.server-es919z55.js} +27 -2
  24. package/package.json +1 -1
  25. package/src/components/focus-trap/focus-trap.schema.json +15 -0
  26. package/src/components/focus-trap/focus-trap.schema.ts +17 -0
  27. package/src/components/focus-trap/focus-trap.svelte +3 -1
  28. package/src/components/focus-trap/focus-trap.types.ts +6 -0
  29. package/src/components/focus-trap/focus-trap.utilities.svelte.ts +41 -1
  30. package/src/components/selection-popover/selection-popover.svelte +24 -4
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # @lostgradient/cinder
2
2
 
3
+ ## 0.24.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1293](https://github.com/stevekinney/cinder/pull/1293) [`5bd476a`](https://github.com/stevekinney/cinder/commit/5bd476a74c1f04bf76e4689268d2256ee490dc58) Thanks [@stevekinney](https://github.com/stevekinney)! - Give `createFocusTrap` a `restoreFallback`, so closing an overlay whose opener has been removed no longer drops focus on `<body>`.
8
+
9
+ The trap captured the focused element on activation and handed it to `restoreFocusTo` on deactivation. That helper correctly refuses to focus a disconnected node — and its return value was discarded, so when the control that opened the overlay had been removed in the meantime, nothing else happened and focus landed nowhere. A screen reader says nothing; a keyboard user's next Tab restarts at the top of the document.
10
+
11
+ The reachable instance is `ReviewEditor`'s thread popover: deleting a thread from inside its own popover removes the sidebar item the popover was opened from, so the restore target is gone by the time the trap runs. It only bites a consumer that actually applies `onthreaddelete`, which is why notification-only demos never showed it — their sidebar item survives the delete.
12
+
13
+ `restoreFallback` is consulted only when restoring to the captured element fails, so supplying it can never override a restore that would have worked, and it is resolved against the document rather than the trap root because a restore target lives outside the trap by definition and the trap's own node is usually already detached by then. A restore now also counts as successful only when focus actually lands: `restoreFocusTo` reports success whenever `.focus()` did not throw, which is equally true of a still-connected element that has since become `disabled` or `inert`.
14
+
15
+ A companion `preferRestoreFallback` covers the asynchronous case. A consumer whose delete handler waits on a server keeps the opener mounted while the request is in flight, so restoration finds it perfectly focusable, hands focus back, and then watches it unmount — landing on `<body>` after all. `ThreadPopover` sets it once a delete has been requested. It reorders the candidates rather than discarding one, so a missing fallback still falls through to the captured element.
16
+
17
+ `ReviewEditor` points the popover at that editor instance's comments-sidebar toggle — always mounted, always focusable, adjacent to the work, and its label announces the changed comment count. The toggle gained an `id` so the target can be resolved with `getElementById` rather than an attribute selector: the editor `id` is consumer-supplied and only has to be a valid HTML id, so it may contain `"` or `\`, which would make an interpolated selector invalid and fail silently — back to `<body>`.
18
+
19
+ Fixes [#1291](https://github.com/stevekinney/cinder/issues/1291).
20
+
21
+ ## 0.24.2
22
+
23
+ ### Patch Changes
24
+
25
+ - [`8c3065a`](https://github.com/stevekinney/cinder/commit/8c3065a5889811bc3315e3a7fcbfa1bfc816b62c) Thanks [@stevekinney](https://github.com/stevekinney)! - Keep SelectionPopover's pre-open focus owner for the popover's whole open lifetime so an Escape that follows a cancel or submit still returns focus to it instead of dropping it on `<body>`.
26
+
3
27
  ## 0.24.1
4
28
 
5
29
  ### Patch Changes
package/components.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "manifestVersion": 1,
4
4
  "package": {
5
5
  "name": "@lostgradient/cinder",
6
- "version": "0.24.1",
6
+ "version": "0.24.3",
7
7
  "framework": "svelte",
8
8
  "frameworkVersionRange": ">=5.56.0 <6",
9
9
  "classPrefix": "cinder-",
@@ -439,6 +439,23 @@ function getFallbackTarget(root, fallbackFocus) {
439
439
  const resolved = resolveScopedTarget(root, fallbackFocus);
440
440
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
441
441
  }
442
+ function getRestoreFallbackTarget(restoreFallback) {
443
+ const resolved = readOption(restoreFallback);
444
+ if (!resolved)
445
+ return null;
446
+ if (typeof resolved === "string") {
447
+ if (typeof document === "undefined")
448
+ return null;
449
+ let match;
450
+ try {
451
+ match = document.querySelector(resolved);
452
+ } catch {
453
+ return null;
454
+ }
455
+ return match && isProgrammaticallyFocusable(match) ? match : null;
456
+ }
457
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
458
+ }
442
459
  function tryFocus(element) {
443
460
  element.focus();
444
461
  return document.activeElement === element;
@@ -475,6 +492,8 @@ function createFocusTrap(options = {}) {
475
492
  const restoreFocus = options.restoreFocus ?? true;
476
493
  const initialFocus = options.initialFocus ?? null;
477
494
  const fallbackFocus = options.fallbackFocus ?? null;
495
+ const restoreFallback = options.restoreFallback ?? null;
496
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
478
497
  const manageInitialFocus = options.manageInitialFocus ?? true;
479
498
  let activated = false;
480
499
  let activationGeneration = 0;
@@ -519,7 +538,11 @@ function createFocusTrap(options = {}) {
519
538
  restoreRootFocusability = noop;
520
539
  removeTrap(trapId);
521
540
  if (restoreFocus) {
522
- restoreFocusTo(capturedFocus);
541
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
542
+ for (const candidate of candidates) {
543
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
544
+ break;
545
+ }
523
546
  }
524
547
  capturedFocus = null;
525
548
  }
@@ -572,13 +595,15 @@ var rest_excludes = new Set([
572
595
  "restoreFocus",
573
596
  "initialFocus",
574
597
  "fallbackFocus",
598
+ "restoreFallback",
599
+ "preferRestoreFallback",
575
600
  "class",
576
601
  "children"
577
602
  ]);
578
603
  var root = $2.from_html(`<div><!></div>`);
579
604
  function Focus_trap($$anchor, $$props) {
580
605
  $2.push($$props, true);
581
- let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), rest = $2.rest_props($$props, rest_excludes);
606
+ let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), restoreFallback = $2.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $2.prop($$props, "preferRestoreFallback", 3, false), rest = $2.rest_props($$props, rest_excludes);
582
607
  var div = root();
583
608
  $2.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
584
609
  var node = $2.child(div);
@@ -588,7 +613,9 @@ function Focus_trap($$anchor, $$props) {
588
613
  active: () => active(),
589
614
  restoreFocus: restoreFocus(),
590
615
  initialFocus: () => initialFocus(),
591
- fallbackFocus: () => fallbackFocus()
616
+ fallbackFocus: () => fallbackFocus(),
617
+ restoreFallback: () => restoreFallback(),
618
+ preferRestoreFallback: () => preferRestoreFallback()
592
619
  }));
593
620
  $2.append($$anchor, div);
594
621
  $2.pop();
@@ -1641,4 +1668,4 @@ export {
1641
1668
  Alert_dialog as AlertDialog
1642
1669
  };
1643
1670
 
1644
- //# debugId=6F7CAFE8B47C632264756E2164756E21
1671
+ //# debugId=465BE598F4728E6264756E2164756E21
@@ -439,6 +439,23 @@ function getFallbackTarget(root, fallbackFocus) {
439
439
  const resolved = resolveScopedTarget(root, fallbackFocus);
440
440
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
441
441
  }
442
+ function getRestoreFallbackTarget(restoreFallback) {
443
+ const resolved = readOption(restoreFallback);
444
+ if (!resolved)
445
+ return null;
446
+ if (typeof resolved === "string") {
447
+ if (typeof document === "undefined")
448
+ return null;
449
+ let match;
450
+ try {
451
+ match = document.querySelector(resolved);
452
+ } catch {
453
+ return null;
454
+ }
455
+ return match && isProgrammaticallyFocusable(match) ? match : null;
456
+ }
457
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
458
+ }
442
459
  function tryFocus(element) {
443
460
  element.focus();
444
461
  return document.activeElement === element;
@@ -475,6 +492,8 @@ function createFocusTrap(options = {}) {
475
492
  const restoreFocus = options.restoreFocus ?? true;
476
493
  const initialFocus = options.initialFocus ?? null;
477
494
  const fallbackFocus = options.fallbackFocus ?? null;
495
+ const restoreFallback = options.restoreFallback ?? null;
496
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
478
497
  const manageInitialFocus = options.manageInitialFocus ?? true;
479
498
  let activated = false;
480
499
  let activationGeneration = 0;
@@ -519,7 +538,11 @@ function createFocusTrap(options = {}) {
519
538
  restoreRootFocusability = noop;
520
539
  removeTrap(trapId);
521
540
  if (restoreFocus) {
522
- restoreFocusTo(capturedFocus);
541
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
542
+ for (const candidate of candidates) {
543
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
544
+ break;
545
+ }
523
546
  }
524
547
  capturedFocus = null;
525
548
  }
@@ -572,13 +595,15 @@ var rest_excludes = new Set([
572
595
  "restoreFocus",
573
596
  "initialFocus",
574
597
  "fallbackFocus",
598
+ "restoreFallback",
599
+ "preferRestoreFallback",
575
600
  "class",
576
601
  "children"
577
602
  ]);
578
603
  var root = $2.from_html(`<div><!></div>`);
579
604
  function Focus_trap($$anchor, $$props) {
580
605
  $2.push($$props, true);
581
- let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), rest = $2.rest_props($$props, rest_excludes);
606
+ let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), restoreFallback = $2.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $2.prop($$props, "preferRestoreFallback", 3, false), rest = $2.rest_props($$props, rest_excludes);
582
607
  var div = root();
583
608
  $2.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
584
609
  var node = $2.child(div);
@@ -588,7 +613,9 @@ function Focus_trap($$anchor, $$props) {
588
613
  active: () => active(),
589
614
  restoreFocus: restoreFocus(),
590
615
  initialFocus: () => initialFocus(),
591
- fallbackFocus: () => fallbackFocus()
616
+ fallbackFocus: () => fallbackFocus(),
617
+ restoreFallback: () => restoreFallback(),
618
+ preferRestoreFallback: () => preferRestoreFallback()
592
619
  }));
593
620
  $2.append($$anchor, div);
594
621
  $2.pop();
@@ -2459,4 +2486,4 @@ export {
2459
2486
  Confirm_dialog as ConfirmDialog
2460
2487
  };
2461
2488
 
2462
- //# debugId=0DD105BDC1C07CFD64756E2164756E21
2489
+ //# debugId=EDDA979B9903E24464756E2164756E21
@@ -439,6 +439,23 @@ function getFallbackTarget(root, fallbackFocus) {
439
439
  const resolved = resolveScopedTarget(root, fallbackFocus);
440
440
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
441
441
  }
442
+ function getRestoreFallbackTarget(restoreFallback) {
443
+ const resolved = readOption(restoreFallback);
444
+ if (!resolved)
445
+ return null;
446
+ if (typeof resolved === "string") {
447
+ if (typeof document === "undefined")
448
+ return null;
449
+ let match;
450
+ try {
451
+ match = document.querySelector(resolved);
452
+ } catch {
453
+ return null;
454
+ }
455
+ return match && isProgrammaticallyFocusable(match) ? match : null;
456
+ }
457
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
458
+ }
442
459
  function tryFocus(element) {
443
460
  element.focus();
444
461
  return document.activeElement === element;
@@ -475,6 +492,8 @@ function createFocusTrap(options = {}) {
475
492
  const restoreFocus = options.restoreFocus ?? true;
476
493
  const initialFocus = options.initialFocus ?? null;
477
494
  const fallbackFocus = options.fallbackFocus ?? null;
495
+ const restoreFallback = options.restoreFallback ?? null;
496
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
478
497
  const manageInitialFocus = options.manageInitialFocus ?? true;
479
498
  let activated = false;
480
499
  let activationGeneration = 0;
@@ -519,7 +538,11 @@ function createFocusTrap(options = {}) {
519
538
  restoreRootFocusability = noop;
520
539
  removeTrap(trapId);
521
540
  if (restoreFocus) {
522
- restoreFocusTo(capturedFocus);
541
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
542
+ for (const candidate of candidates) {
543
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
544
+ break;
545
+ }
523
546
  }
524
547
  capturedFocus = null;
525
548
  }
@@ -572,13 +595,15 @@ var rest_excludes = new Set([
572
595
  "restoreFocus",
573
596
  "initialFocus",
574
597
  "fallbackFocus",
598
+ "restoreFallback",
599
+ "preferRestoreFallback",
575
600
  "class",
576
601
  "children"
577
602
  ]);
578
603
  var root = $2.from_html(`<div><!></div>`);
579
604
  function Focus_trap($$anchor, $$props) {
580
605
  $2.push($$props, true);
581
- let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), rest = $2.rest_props($$props, rest_excludes);
606
+ let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), restoreFallback = $2.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $2.prop($$props, "preferRestoreFallback", 3, false), rest = $2.rest_props($$props, rest_excludes);
582
607
  var div = root();
583
608
  $2.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
584
609
  var node = $2.child(div);
@@ -588,7 +613,9 @@ function Focus_trap($$anchor, $$props) {
588
613
  active: () => active(),
589
614
  restoreFocus: restoreFocus(),
590
615
  initialFocus: () => initialFocus(),
591
- fallbackFocus: () => fallbackFocus()
616
+ fallbackFocus: () => fallbackFocus(),
617
+ restoreFallback: () => restoreFallback(),
618
+ preferRestoreFallback: () => preferRestoreFallback()
592
619
  }));
593
620
  $2.append($$anchor, div);
594
621
  $2.pop();
@@ -1355,4 +1382,4 @@ export {
1355
1382
  Drawer
1356
1383
  };
1357
1384
 
1358
- //# debugId=6CA31983CA90FD9964756E2164756E21
1385
+ //# debugId=04305779D28BD7B664756E2164756E21
@@ -86,6 +86,21 @@ var schema = {
86
86
  ],
87
87
  description: "CSS selector for the element that receives focus when `initialFocus` is unset or unresolvable. Defaults to the trap container itself."
88
88
  },
89
+ restoreFallback: {
90
+ anyOf: [
91
+ {
92
+ type: "string"
93
+ },
94
+ {
95
+ type: "null"
96
+ }
97
+ ],
98
+ description: "CSS selector, resolved against the document, for the element that receives focus when the previously focused element cannot take it back — typically because it was removed from the DOM while the trap was open. Without it, focus falls to `<body>`."
99
+ },
100
+ preferRestoreFallback: {
101
+ type: "boolean",
102
+ description: "When true, `restoreFallback` is tried before the previously focused element rather than only after it. For hosts that know the captured element is about to be removed but cannot prove it yet, such as a delete that awaits a server round trip."
103
+ },
89
104
  class: {
90
105
  type: "string",
91
106
  description: "Additional class applied to the focus-trap wrapper element."
@@ -107,4 +122,4 @@ export {
107
122
  focus_trap_schema_default as default
108
123
  };
109
124
 
110
- //# debugId=33E27AEF6C7236BF64756E2164756E21
125
+ //# debugId=A5E192B047DA2F2964756E2164756E21
@@ -7,6 +7,8 @@ export type FocusTrapProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | '
7
7
  restoreFocus?: boolean;
8
8
  initialFocus?: Exclude<FocusTargetInput, Function>;
9
9
  fallbackFocus?: Exclude<FocusTargetInput, Function>;
10
+ restoreFallback?: Exclude<FocusTargetInput, Function>;
11
+ preferRestoreFallback?: boolean;
10
12
  class?: string;
11
13
  };
12
14
  export interface FocusTrapSchemaProps {
@@ -18,6 +20,10 @@ export interface FocusTrapSchemaProps {
18
20
  initialFocus?: string | null;
19
21
  /** CSS selector for the element that receives focus when `initialFocus` is unset or unresolvable. Defaults to the trap container itself. */
20
22
  fallbackFocus?: string | null;
23
+ /** CSS selector, resolved against the document, for the element that receives focus when the previously focused element cannot take it back — typically because it was removed from the DOM while the trap was open. Without it, focus falls to `<body>`. */
24
+ restoreFallback?: string | null;
25
+ /** When true, `restoreFallback` is tried before the previously focused element rather than only after it. For hosts that know the captured element is about to be removed but cannot prove it yet, such as a delete that awaits a server round trip. */
26
+ preferRestoreFallback?: boolean;
21
27
  /** Additional class applied to the focus-trap wrapper element. */
22
28
  class?: string;
23
29
  }
@@ -19,5 +19,42 @@ export type FocusTrapOptions = {
19
19
  * `restoreFocus: false`.
20
20
  */
21
21
  manageInitialFocus?: boolean;
22
+ /**
23
+ * Where focus goes when the element captured at activation cannot take it
24
+ * back — most often because the control that opened the overlay was removed
25
+ * from the DOM while the overlay was open. Deleting the item whose popover
26
+ * you are standing in is the canonical case: the popover closes, its opener
27
+ * no longer exists, and without this focus lands on `<body>`, which reads as
28
+ * "nowhere" to a screen reader and restarts a keyboard user at the top of the
29
+ * document.
30
+ *
31
+ * Resolved against the DOCUMENT, unlike {@link initialFocus} and
32
+ * {@link fallbackFocus}, which are scoped to the trap root. That difference is
33
+ * deliberate and not an inconsistency: a restore target lives outside the trap
34
+ * by definition, and by the time restoration runs on unmount the trap's own
35
+ * node is usually already detached, so a root-scoped lookup would find
36
+ * nothing.
37
+ *
38
+ * Consulted only when restoring to the captured element fails, so supplying it
39
+ * can never override a restore that would have worked. Ignored entirely when
40
+ * `restoreFocus` is `false`.
41
+ */
42
+ restoreFallback?: FocusTargetInput;
43
+ /**
44
+ * Go to {@link restoreFallback} FIRST on the next deactivation, rather than
45
+ * only when the captured element fails.
46
+ *
47
+ * For hosts that know the captured element is on its way out but cannot prove
48
+ * it yet. A popover whose delete action is server-backed is the case: the
49
+ * popover closes as soon as the request is made, while the list item that
50
+ * opened it is still on screen waiting for the response. Restoration would
51
+ * find that item perfectly focusable, hand focus back to it, and then watch it
52
+ * unmount a moment later — landing on `<body>` after all, with the fallback
53
+ * never consulted.
54
+ *
55
+ * Still falls through to the captured element if the fallback cannot take
56
+ * focus, so this changes the order rather than discarding a candidate.
57
+ */
58
+ preferRestoreFallback?: boolean | (() => boolean);
22
59
  };
23
60
  export declare function createFocusTrap(options?: FocusTrapOptions): Attachment<HTMLElement>;
@@ -439,6 +439,23 @@ function getFallbackTarget(root, fallbackFocus) {
439
439
  const resolved = resolveScopedTarget(root, fallbackFocus);
440
440
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
441
441
  }
442
+ function getRestoreFallbackTarget(restoreFallback) {
443
+ const resolved = readOption(restoreFallback);
444
+ if (!resolved)
445
+ return null;
446
+ if (typeof resolved === "string") {
447
+ if (typeof document === "undefined")
448
+ return null;
449
+ let match;
450
+ try {
451
+ match = document.querySelector(resolved);
452
+ } catch {
453
+ return null;
454
+ }
455
+ return match && isProgrammaticallyFocusable(match) ? match : null;
456
+ }
457
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
458
+ }
442
459
  function tryFocus(element) {
443
460
  element.focus();
444
461
  return document.activeElement === element;
@@ -475,6 +492,8 @@ function createFocusTrap(options = {}) {
475
492
  const restoreFocus = options.restoreFocus ?? true;
476
493
  const initialFocus = options.initialFocus ?? null;
477
494
  const fallbackFocus = options.fallbackFocus ?? null;
495
+ const restoreFallback = options.restoreFallback ?? null;
496
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
478
497
  const manageInitialFocus = options.manageInitialFocus ?? true;
479
498
  let activated = false;
480
499
  let activationGeneration = 0;
@@ -519,7 +538,11 @@ function createFocusTrap(options = {}) {
519
538
  restoreRootFocusability = noop;
520
539
  removeTrap(trapId);
521
540
  if (restoreFocus) {
522
- restoreFocusTo(capturedFocus);
541
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
542
+ for (const candidate of candidates) {
543
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
544
+ break;
545
+ }
523
546
  }
524
547
  capturedFocus = null;
525
548
  }
@@ -572,13 +595,15 @@ var rest_excludes = new Set([
572
595
  "restoreFocus",
573
596
  "initialFocus",
574
597
  "fallbackFocus",
598
+ "restoreFallback",
599
+ "preferRestoreFallback",
575
600
  "class",
576
601
  "children"
577
602
  ]);
578
603
  var root = $2.from_html(`<div><!></div>`);
579
604
  function Focus_trap($$anchor, $$props) {
580
605
  $2.push($$props, true);
581
- let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), rest = $2.rest_props($$props, rest_excludes);
606
+ let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), restoreFallback = $2.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $2.prop($$props, "preferRestoreFallback", 3, false), rest = $2.rest_props($$props, rest_excludes);
582
607
  var div = root();
583
608
  $2.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
584
609
  var node = $2.child(div);
@@ -588,7 +613,9 @@ function Focus_trap($$anchor, $$props) {
588
613
  active: () => active(),
589
614
  restoreFocus: restoreFocus(),
590
615
  initialFocus: () => initialFocus(),
591
- fallbackFocus: () => fallbackFocus()
616
+ fallbackFocus: () => fallbackFocus(),
617
+ restoreFallback: () => restoreFallback(),
618
+ preferRestoreFallback: () => preferRestoreFallback()
592
619
  }));
593
620
  $2.append($$anchor, div);
594
621
  $2.pop();
@@ -853,4 +880,4 @@ export {
853
880
  Focus_trap as FocusTrap
854
881
  };
855
882
 
856
- //# debugId=ACF9DEC1DBBFA2B264756E2164756E21
883
+ //# debugId=716097E866FBB7BF64756E2164756E21
@@ -439,6 +439,23 @@ function getFallbackTarget(root, fallbackFocus) {
439
439
  const resolved = resolveScopedTarget(root, fallbackFocus);
440
440
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
441
441
  }
442
+ function getRestoreFallbackTarget(restoreFallback) {
443
+ const resolved = readOption(restoreFallback);
444
+ if (!resolved)
445
+ return null;
446
+ if (typeof resolved === "string") {
447
+ if (typeof document === "undefined")
448
+ return null;
449
+ let match;
450
+ try {
451
+ match = document.querySelector(resolved);
452
+ } catch {
453
+ return null;
454
+ }
455
+ return match && isProgrammaticallyFocusable(match) ? match : null;
456
+ }
457
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
458
+ }
442
459
  function tryFocus(element) {
443
460
  element.focus();
444
461
  return document.activeElement === element;
@@ -475,6 +492,8 @@ function createFocusTrap(options = {}) {
475
492
  const restoreFocus = options.restoreFocus ?? true;
476
493
  const initialFocus = options.initialFocus ?? null;
477
494
  const fallbackFocus = options.fallbackFocus ?? null;
495
+ const restoreFallback = options.restoreFallback ?? null;
496
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
478
497
  const manageInitialFocus = options.manageInitialFocus ?? true;
479
498
  let activated = false;
480
499
  let activationGeneration = 0;
@@ -519,7 +538,11 @@ function createFocusTrap(options = {}) {
519
538
  restoreRootFocusability = noop;
520
539
  removeTrap(trapId);
521
540
  if (restoreFocus) {
522
- restoreFocusTo(capturedFocus);
541
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
542
+ for (const candidate of candidates) {
543
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
544
+ break;
545
+ }
523
546
  }
524
547
  capturedFocus = null;
525
548
  }
@@ -572,13 +595,15 @@ var rest_excludes = new Set([
572
595
  "restoreFocus",
573
596
  "initialFocus",
574
597
  "fallbackFocus",
598
+ "restoreFallback",
599
+ "preferRestoreFallback",
575
600
  "class",
576
601
  "children"
577
602
  ]);
578
603
  var root = $2.from_html(`<div><!></div>`);
579
604
  function Focus_trap($$anchor, $$props) {
580
605
  $2.push($$props, true);
581
- let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), rest = $2.rest_props($$props, rest_excludes);
606
+ let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), restoreFallback = $2.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $2.prop($$props, "preferRestoreFallback", 3, false), rest = $2.rest_props($$props, rest_excludes);
582
607
  var div = root();
583
608
  $2.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
584
609
  var node = $2.child(div);
@@ -588,7 +613,9 @@ function Focus_trap($$anchor, $$props) {
588
613
  active: () => active(),
589
614
  restoreFocus: restoreFocus(),
590
615
  initialFocus: () => initialFocus(),
591
- fallbackFocus: () => fallbackFocus()
616
+ fallbackFocus: () => fallbackFocus(),
617
+ restoreFallback: () => restoreFallback(),
618
+ preferRestoreFallback: () => preferRestoreFallback()
592
619
  }));
593
620
  $2.append($$anchor, div);
594
621
  $2.pop();
@@ -1308,4 +1335,4 @@ export {
1308
1335
  Modal
1309
1336
  };
1310
1337
 
1311
- //# debugId=DA06FABC90A9625E64756E2164756E21
1338
+ //# debugId=C298FBD9899945CA64756E2164756E21
@@ -3720,9 +3720,10 @@ function Selection_popover($$anchor, $$props) {
3720
3720
  }
3721
3721
  function restoreFocus(preventScroll = true) {
3722
3722
  const target = restoreFocusElement;
3723
- restoreFocusElement = null;
3724
3723
  if (!target)
3725
3724
  return;
3725
+ if (document.activeElement === target)
3726
+ return;
3726
3727
  isRestoringFocus = true;
3727
3728
  target.focus({ preventScroll });
3728
3729
  isRestoringFocus = false;
@@ -3807,6 +3808,7 @@ function Selection_popover($$anchor, $$props) {
3807
3808
  $6.set(expanded, false);
3808
3809
  $6.set(commentBody, "");
3809
3810
  restoreFocus();
3811
+ restoreFocusElement = null;
3810
3812
  }
3811
3813
  return;
3812
3814
  }
@@ -3878,4 +3880,4 @@ export {
3878
3880
  Selection_popover as SelectionPopover
3879
3881
  };
3880
3882
 
3881
- //# debugId=C5F1ABDDAB0A709664756E2164756E21
3883
+ //# debugId=0D22F90112A8785E64756E2164756E21
@@ -439,6 +439,23 @@ function getFallbackTarget(root, fallbackFocus) {
439
439
  const resolved = resolveScopedTarget(root, fallbackFocus);
440
440
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
441
441
  }
442
+ function getRestoreFallbackTarget(restoreFallback) {
443
+ const resolved = readOption(restoreFallback);
444
+ if (!resolved)
445
+ return null;
446
+ if (typeof resolved === "string") {
447
+ if (typeof document === "undefined")
448
+ return null;
449
+ let match;
450
+ try {
451
+ match = document.querySelector(resolved);
452
+ } catch {
453
+ return null;
454
+ }
455
+ return match && isProgrammaticallyFocusable(match) ? match : null;
456
+ }
457
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
458
+ }
442
459
  function tryFocus(element) {
443
460
  element.focus();
444
461
  return document.activeElement === element;
@@ -475,6 +492,8 @@ function createFocusTrap(options = {}) {
475
492
  const restoreFocus = options.restoreFocus ?? true;
476
493
  const initialFocus = options.initialFocus ?? null;
477
494
  const fallbackFocus = options.fallbackFocus ?? null;
495
+ const restoreFallback = options.restoreFallback ?? null;
496
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
478
497
  const manageInitialFocus = options.manageInitialFocus ?? true;
479
498
  let activated = false;
480
499
  let activationGeneration = 0;
@@ -519,7 +538,11 @@ function createFocusTrap(options = {}) {
519
538
  restoreRootFocusability = noop;
520
539
  removeTrap(trapId);
521
540
  if (restoreFocus) {
522
- restoreFocusTo(capturedFocus);
541
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
542
+ for (const candidate of candidates) {
543
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
544
+ break;
545
+ }
523
546
  }
524
547
  capturedFocus = null;
525
548
  }
@@ -572,13 +595,15 @@ var rest_excludes = new Set([
572
595
  "restoreFocus",
573
596
  "initialFocus",
574
597
  "fallbackFocus",
598
+ "restoreFallback",
599
+ "preferRestoreFallback",
575
600
  "class",
576
601
  "children"
577
602
  ]);
578
603
  var root = $2.from_html(`<div><!></div>`);
579
604
  function Focus_trap($$anchor, $$props) {
580
605
  $2.push($$props, true);
581
- let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), rest = $2.rest_props($$props, rest_excludes);
606
+ let active = $2.prop($$props, "active", 3, true), restoreFocus = $2.prop($$props, "restoreFocus", 3, true), initialFocus = $2.prop($$props, "initialFocus", 3, null), fallbackFocus = $2.prop($$props, "fallbackFocus", 3, null), restoreFallback = $2.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $2.prop($$props, "preferRestoreFallback", 3, false), rest = $2.rest_props($$props, rest_excludes);
582
607
  var div = root();
583
608
  $2.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
584
609
  var node = $2.child(div);
@@ -588,7 +613,9 @@ function Focus_trap($$anchor, $$props) {
588
613
  active: () => active(),
589
614
  restoreFocus: restoreFocus(),
590
615
  initialFocus: () => initialFocus(),
591
- fallbackFocus: () => fallbackFocus()
616
+ fallbackFocus: () => fallbackFocus(),
617
+ restoreFallback: () => restoreFallback(),
618
+ preferRestoreFallback: () => preferRestoreFallback()
592
619
  }));
593
620
  $2.append($$anchor, div);
594
621
  $2.pop();
@@ -1602,4 +1629,4 @@ export {
1602
1629
  SIDEBAR_MOBILE_BREAKPOINT
1603
1630
  };
1604
1631
 
1605
- //# debugId=DEAFB83540901A8E64756E2164756E21
1632
+ //# debugId=D68604E5B21F442D64756E2164756E21
package/dist/index.js CHANGED
@@ -7289,6 +7289,23 @@ function getFallbackTarget(root8, fallbackFocus) {
7289
7289
  const resolved = resolveScopedTarget(root8, fallbackFocus);
7290
7290
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
7291
7291
  }
7292
+ function getRestoreFallbackTarget(restoreFallback) {
7293
+ const resolved = readOption(restoreFallback);
7294
+ if (!resolved)
7295
+ return null;
7296
+ if (typeof resolved === "string") {
7297
+ if (typeof document === "undefined")
7298
+ return null;
7299
+ let match;
7300
+ try {
7301
+ match = document.querySelector(resolved);
7302
+ } catch {
7303
+ return null;
7304
+ }
7305
+ return match && isProgrammaticallyFocusable(match) ? match : null;
7306
+ }
7307
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
7308
+ }
7292
7309
  function tryFocus(element2) {
7293
7310
  element2.focus();
7294
7311
  return document.activeElement === element2;
@@ -7325,6 +7342,8 @@ function createFocusTrap(options = {}) {
7325
7342
  const restoreFocus = options.restoreFocus ?? true;
7326
7343
  const initialFocus = options.initialFocus ?? null;
7327
7344
  const fallbackFocus = options.fallbackFocus ?? null;
7345
+ const restoreFallback = options.restoreFallback ?? null;
7346
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
7328
7347
  const manageInitialFocus = options.manageInitialFocus ?? true;
7329
7348
  let activated = false;
7330
7349
  let activationGeneration = 0;
@@ -7369,7 +7388,11 @@ function createFocusTrap(options = {}) {
7369
7388
  restoreRootFocusability = noop3;
7370
7389
  removeTrap(trapId);
7371
7390
  if (restoreFocus) {
7372
- restoreFocusTo(capturedFocus);
7391
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
7392
+ for (const candidate of candidates) {
7393
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
7394
+ break;
7395
+ }
7373
7396
  }
7374
7397
  capturedFocus = null;
7375
7398
  }
@@ -7422,13 +7445,15 @@ var rest_excludes5 = new Set([
7422
7445
  "restoreFocus",
7423
7446
  "initialFocus",
7424
7447
  "fallbackFocus",
7448
+ "restoreFallback",
7449
+ "preferRestoreFallback",
7425
7450
  "class",
7426
7451
  "children"
7427
7452
  ]);
7428
7453
  var root8 = $73.from_html(`<div><!></div>`);
7429
7454
  function Focus_trap($$anchor, $$props) {
7430
7455
  $73.push($$props, true);
7431
- let active = $73.prop($$props, "active", 3, true), restoreFocus = $73.prop($$props, "restoreFocus", 3, true), initialFocus = $73.prop($$props, "initialFocus", 3, null), fallbackFocus = $73.prop($$props, "fallbackFocus", 3, null), rest = $73.rest_props($$props, rest_excludes5);
7456
+ let active = $73.prop($$props, "active", 3, true), restoreFocus = $73.prop($$props, "restoreFocus", 3, true), initialFocus = $73.prop($$props, "initialFocus", 3, null), fallbackFocus = $73.prop($$props, "fallbackFocus", 3, null), restoreFallback = $73.prop($$props, "restoreFallback", 3, null), preferRestoreFallback = $73.prop($$props, "preferRestoreFallback", 3, false), rest = $73.rest_props($$props, rest_excludes5);
7432
7457
  var div = root8();
7433
7458
  $73.attribute_effect(div, ($0) => ({ class: $0, ...rest }), [() => classNames($$props.class)]);
7434
7459
  var node = $73.child(div);
@@ -7438,7 +7463,9 @@ function Focus_trap($$anchor, $$props) {
7438
7463
  active: () => active(),
7439
7464
  restoreFocus: restoreFocus(),
7440
7465
  initialFocus: () => initialFocus(),
7441
- fallbackFocus: () => fallbackFocus()
7466
+ fallbackFocus: () => fallbackFocus(),
7467
+ restoreFallback: () => restoreFallback(),
7468
+ preferRestoreFallback: () => preferRestoreFallback()
7442
7469
  }));
7443
7470
  $73.append($$anchor, div);
7444
7471
  $73.pop();
@@ -61140,9 +61167,10 @@ function Selection_popover($$anchor, $$props) {
61140
61167
  }
61141
61168
  function restoreFocus(preventScroll = true) {
61142
61169
  const target = restoreFocusElement;
61143
- restoreFocusElement = null;
61144
61170
  if (!target)
61145
61171
  return;
61172
+ if (document.activeElement === target)
61173
+ return;
61146
61174
  isRestoringFocus = true;
61147
61175
  target.focus({ preventScroll });
61148
61176
  isRestoringFocus = false;
@@ -61227,6 +61255,7 @@ function Selection_popover($$anchor, $$props) {
61227
61255
  $266.set(expanded, false);
61228
61256
  $266.set(commentBody, "");
61229
61257
  restoreFocus();
61258
+ restoreFocusElement = null;
61230
61259
  }
61231
61260
  return;
61232
61261
  }
@@ -72875,4 +72904,4 @@ export {
72875
72904
  AccessGateExport as AccessGate
72876
72905
  };
72877
72906
 
72878
- //# debugId=98FDF13840394EF064756E2164756E21
72907
+ //# debugId=80C8E1090D7D97D764756E2164756E21
@@ -5,7 +5,7 @@ import {
5
5
  import"../../index.server-tkt7wm9w.js";
6
6
  import"../../index.server-x0sbmhn7.js";
7
7
  import"../../index.server-78d33trm.js";
8
- import"../../index.server-r5emexd4.js";
8
+ import"../../index.server-es919z55.js";
9
9
  import"../../index.server-gdtg1xg7.js";
10
10
  import"../../index.server-fdpe3vky.js";
11
11
  import"../../index.server-th1tsad4.js";
@@ -10,7 +10,7 @@ import"../../index.server-493m9fm5.js";
10
10
  import"../../index.server-tkt7wm9w.js";
11
11
  import"../../index.server-x0sbmhn7.js";
12
12
  import"../../index.server-78d33trm.js";
13
- import"../../index.server-r5emexd4.js";
13
+ import"../../index.server-es919z55.js";
14
14
  import"../../index.server-gdtg1xg7.js";
15
15
  import"../../index.server-abm21gz1.js";
16
16
  import"../../index.server-fdpe3vky.js";
@@ -6,7 +6,7 @@ import {
6
6
  } from "../../index.server-9m7bqtev.js";
7
7
  import"../../index.server-x0sbmhn7.js";
8
8
  import"../../index.server-78d33trm.js";
9
- import"../../index.server-r5emexd4.js";
9
+ import"../../index.server-es919z55.js";
10
10
  import"../../index.server-gdtg1xg7.js";
11
11
  import"../../index.server-th1tsad4.js";
12
12
  import"../../index.server-6pdmym90.js";
@@ -2,7 +2,7 @@ import {
2
2
  Focus_trap,
3
3
  createFocusTrap,
4
4
  focus_trap_default
5
- } from "../../index.server-r5emexd4.js";
5
+ } from "../../index.server-es919z55.js";
6
6
  import"../../index.server-gdtg1xg7.js";
7
7
  import {
8
8
  createBodyScrollLock
@@ -6,7 +6,7 @@ import {
6
6
  } from "../../index.server-tkt7wm9w.js";
7
7
  import"../../index.server-x0sbmhn7.js";
8
8
  import"../../index.server-78d33trm.js";
9
- import"../../index.server-r5emexd4.js";
9
+ import"../../index.server-es919z55.js";
10
10
  import"../../index.server-gdtg1xg7.js";
11
11
  import"../../index.server-th1tsad4.js";
12
12
  import"../../index.server-6pdmym90.js";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  Selection_popover,
3
3
  selection_popover_default
4
- } from "../../index.server-c71m0a88.js";
4
+ } from "../../index.server-0yrz8ryn.js";
5
5
  import"../../index.server-grq89cv5.js";
6
6
  import"../../index.server-s4pfz6pb.js";
7
7
  import"../../index.server-mf4dr1kp.js";
@@ -8,7 +8,7 @@ import"../../index.server-9m7bqtev.js";
8
8
  import"../../index.server-493m9fm5.js";
9
9
  import"../../index.server-x0sbmhn7.js";
10
10
  import"../../index.server-78d33trm.js";
11
- import"../../index.server-r5emexd4.js";
11
+ import"../../index.server-es919z55.js";
12
12
  import"../../index.server-gdtg1xg7.js";
13
13
  import"../../index.server-th1tsad4.js";
14
14
  import"../../index.server-6pdmym90.js";
@@ -165,7 +165,7 @@ import {
165
165
  } from "./index.server-1gzbrhhh.js";
166
166
  import {
167
167
  selection_popover_default
168
- } from "./index.server-c71m0a88.js";
168
+ } from "./index.server-0yrz8ryn.js";
169
169
  import {
170
170
  share_card_default
171
171
  } from "./index.server-bs7zqkt0.js";
@@ -677,7 +677,7 @@ import"./index.server-78d33trm.js";
677
677
  import {
678
678
  createFocusTrap,
679
679
  focus_trap_default
680
- } from "./index.server-r5emexd4.js";
680
+ } from "./index.server-es919z55.js";
681
681
  import"./index.server-gdtg1xg7.js";
682
682
  import {
683
683
  approval_card_default
@@ -84,9 +84,10 @@ function Selection_popover($$renderer, $$props) {
84
84
  }
85
85
  function restoreFocus(preventScroll = true) {
86
86
  const target = restoreFocusElement;
87
- restoreFocusElement = null;
88
87
  if (!target)
89
88
  return;
89
+ if (document.activeElement === target)
90
+ return;
90
91
  isRestoringFocus = true;
91
92
  target.focus({ preventScroll });
92
93
  isRestoringFocus = false;
@@ -195,4 +196,4 @@ function Selection_popover($$renderer, $$props) {
195
196
  var selection_popover_default = Selection_popover;
196
197
  export { Selection_popover, selection_popover_default };
197
198
 
198
- //# debugId=B81BA07C4FE94D8F64756E2164756E21
199
+ //# debugId=A16C2C54A73CE48364756E2164756E21
@@ -81,6 +81,23 @@ function getFallbackTarget(root, fallbackFocus) {
81
81
  const resolved = resolveScopedTarget(root, fallbackFocus);
82
82
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
83
83
  }
84
+ function getRestoreFallbackTarget(restoreFallback) {
85
+ const resolved = readOption(restoreFallback);
86
+ if (!resolved)
87
+ return null;
88
+ if (typeof resolved === "string") {
89
+ if (typeof document === "undefined")
90
+ return null;
91
+ let match;
92
+ try {
93
+ match = document.querySelector(resolved);
94
+ } catch {
95
+ return null;
96
+ }
97
+ return match && isProgrammaticallyFocusable(match) ? match : null;
98
+ }
99
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
100
+ }
84
101
  function tryFocus(element) {
85
102
  element.focus();
86
103
  return document.activeElement === element;
@@ -117,6 +134,8 @@ function createFocusTrap(options = {}) {
117
134
  const restoreFocus = options.restoreFocus ?? true;
118
135
  const initialFocus = options.initialFocus ?? null;
119
136
  const fallbackFocus = options.fallbackFocus ?? null;
137
+ const restoreFallback = options.restoreFallback ?? null;
138
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
120
139
  const manageInitialFocus = options.manageInitialFocus ?? true;
121
140
  let activated = false;
122
141
  let activationGeneration = 0;
@@ -161,7 +180,11 @@ function createFocusTrap(options = {}) {
161
180
  restoreRootFocusability = noop;
162
181
  removeTrap(trapId);
163
182
  if (restoreFocus) {
164
- restoreFocusTo(capturedFocus);
183
+ const candidates = readOption(preferRestoreFallback ?? false) ? [getRestoreFallbackTarget(restoreFallback), capturedFocus] : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
184
+ for (const candidate of candidates) {
185
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
186
+ break;
187
+ }
165
188
  }
166
189
  capturedFocus = null;
167
190
  }
@@ -205,6 +228,8 @@ function Focus_trap($$renderer, $$props) {
205
228
  restoreFocus = true,
206
229
  initialFocus = null,
207
230
  fallbackFocus = null,
231
+ restoreFallback = null,
232
+ preferRestoreFallback = false,
208
233
  class: className,
209
234
  children,
210
235
  $$slots,
@@ -221,4 +246,4 @@ function Focus_trap($$renderer, $$props) {
221
246
  var focus_trap_default = Focus_trap;
222
247
  export { createFocusTrap, Focus_trap, focus_trap_default };
223
248
 
224
- //# debugId=B0616E7B8F56A4FE64756E2164756E21
249
+ //# debugId=2A658975792E379464756E2164756E21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lostgradient/cinder",
3
- "version": "0.24.1",
3
+ "version": "0.24.3",
4
4
  "description": "A Svelte 5 component library and design system focused on accessibility, theming, and per-component tree-shaking.",
5
5
  "keywords": [
6
6
  "svelte",
@@ -32,6 +32,21 @@
32
32
  ],
33
33
  "description": "CSS selector for the element that receives focus when `initialFocus` is unset or unresolvable. Defaults to the trap container itself."
34
34
  },
35
+ "restoreFallback": {
36
+ "anyOf": [
37
+ {
38
+ "type": "string"
39
+ },
40
+ {
41
+ "type": "null"
42
+ }
43
+ ],
44
+ "description": "CSS selector, resolved against the document, for the element that receives focus when the previously focused element cannot take it back — typically because it was removed from the DOM while the trap was open. Without it, focus falls to `<body>`."
45
+ },
46
+ "preferRestoreFallback": {
47
+ "type": "boolean",
48
+ "description": "When true, `restoreFallback` is tried before the previously focused element rather than only after it. For hosts that know the captured element is about to be removed but cannot prove it yet, such as a delete that awaits a server round trip."
49
+ },
35
50
  "class": {
36
51
  "type": "string",
37
52
  "description": "Additional class applied to the focus-trap wrapper element."
@@ -38,6 +38,23 @@ const schema = {
38
38
  description:
39
39
  'CSS selector for the element that receives focus when `initialFocus` is unset or unresolvable. Defaults to the trap container itself.',
40
40
  },
41
+ restoreFallback: {
42
+ anyOf: [
43
+ {
44
+ type: 'string',
45
+ },
46
+ {
47
+ type: 'null',
48
+ },
49
+ ],
50
+ description:
51
+ 'CSS selector, resolved against the document, for the element that receives focus when the previously focused element cannot take it back — typically because it was removed from the DOM while the trap was open. Without it, focus falls to `<body>`.',
52
+ },
53
+ preferRestoreFallback: {
54
+ type: 'boolean',
55
+ description:
56
+ 'When true, `restoreFallback` is tried before the previously focused element rather than only after it. For hosts that know the captured element is about to be removed but cannot prove it yet, such as a delete that awaits a server round trip.',
57
+ },
41
58
  class: {
42
59
  type: 'string',
43
60
  description: 'Additional class applied to the focus-trap wrapper element.',
@@ -5,7 +5,7 @@ export {};
5
5
  <script lang="ts">
6
6
  import { classNames } from '../../utilities/class-names.ts';
7
7
  import { createFocusTrap } from './focus-trap.utilities.svelte.ts';
8
- let { active = true, restoreFocus = true, initialFocus = null, fallbackFocus = null, class: className, children, ...rest } = $props();
8
+ let { active = true, restoreFocus = true, initialFocus = null, fallbackFocus = null, restoreFallback = null, preferRestoreFallback = false, class: className, children, ...rest } = $props();
9
9
  </script>
10
10
 
11
11
  <div
@@ -14,6 +14,8 @@ let { active = true, restoreFocus = true, initialFocus = null, fallbackFocus = n
14
14
  restoreFocus,
15
15
  initialFocus: () => initialFocus,
16
16
  fallbackFocus: () => fallbackFocus,
17
+ restoreFallback: () => restoreFallback,
18
+ preferRestoreFallback: () => preferRestoreFallback,
17
19
  })}
18
20
  class={classNames(className)}
19
21
  {...rest}
@@ -9,6 +9,8 @@ export type FocusTrapProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | '
9
9
  restoreFocus?: boolean;
10
10
  initialFocus?: Exclude<FocusTargetInput, Function>;
11
11
  fallbackFocus?: Exclude<FocusTargetInput, Function>;
12
+ restoreFallback?: Exclude<FocusTargetInput, Function>;
13
+ preferRestoreFallback?: boolean;
12
14
  class?: string;
13
15
  };
14
16
 
@@ -21,6 +23,10 @@ export interface FocusTrapSchemaProps {
21
23
  initialFocus?: string | null;
22
24
  /** CSS selector for the element that receives focus when `initialFocus` is unset or unresolvable. Defaults to the trap container itself. */
23
25
  fallbackFocus?: string | null;
26
+ /** CSS selector, resolved against the document, for the element that receives focus when the previously focused element cannot take it back — typically because it was removed from the DOM while the trap was open. Without it, focus falls to `<body>`. */
27
+ restoreFallback?: string | null;
28
+ /** When true, `restoreFallback` is tried before the previously focused element rather than only after it. For hosts that know the captured element is about to be removed but cannot prove it yet, such as a delete that awaits a server round trip. */
29
+ preferRestoreFallback?: boolean;
24
30
  /** Additional class applied to the focus-trap wrapper element. */
25
31
  class?: string;
26
32
  }
@@ -90,6 +90,32 @@ function getFallbackTarget(root, fallbackFocus) {
90
90
  const resolved = resolveScopedTarget(root, fallbackFocus);
91
91
  return resolved && isProgrammaticallyFocusable(resolved) ? resolved : null;
92
92
  }
93
+ /**
94
+ * Resolve a focus target against the document rather than the trap root.
95
+ *
96
+ * Used only for {@link FocusTrapOptions.restoreFallback} — see that option for
97
+ * why restoration cannot use the root-scoped `resolveScopedTarget`.
98
+ */
99
+ function getRestoreFallbackTarget(restoreFallback) {
100
+ const resolved = readOption(restoreFallback);
101
+ if (!resolved)
102
+ return null;
103
+ if (typeof resolved === 'string') {
104
+ if (typeof document === 'undefined')
105
+ return null;
106
+ let match;
107
+ try {
108
+ match = document.querySelector(resolved);
109
+ }
110
+ catch {
111
+ // An invalid selector is a caller mistake, not a reason to throw out of an
112
+ // unmount path and leave the trap stack inconsistent.
113
+ return null;
114
+ }
115
+ return match && isProgrammaticallyFocusable(match) ? match : null;
116
+ }
117
+ return resolved instanceof HTMLElement && isProgrammaticallyFocusable(resolved) ? resolved : null;
118
+ }
93
119
  /**
94
120
  * Calls `.focus()` and returns whether the element actually became `document.activeElement`.
95
121
  * `isProgrammaticallyFocusable` cannot tell whether a plain `<div>` or `<h2>` (no `tabindex`,
@@ -133,6 +159,8 @@ export function createFocusTrap(options = {}) {
133
159
  const restoreFocus = options.restoreFocus ?? true;
134
160
  const initialFocus = options.initialFocus ?? null;
135
161
  const fallbackFocus = options.fallbackFocus ?? null;
162
+ const restoreFallback = options.restoreFallback ?? null;
163
+ const preferRestoreFallback = options.preferRestoreFallback ?? false;
136
164
  const manageInitialFocus = options.manageInitialFocus ?? true;
137
165
  let activated = false;
138
166
  // Bumped on every `activate()`. The deferred `focusTrapTarget` microtask snapshots this value
@@ -193,7 +221,19 @@ export function createFocusTrap(options = {}) {
193
221
  restoreRootFocusability = noop;
194
222
  removeTrap(trapId);
195
223
  if (restoreFocus) {
196
- restoreFocusTo(capturedFocus);
224
+ // Both candidates, in preference order, taking the first that actually
225
+ // lands. `restoreFocusTo`'s own boolean is necessary but not sufficient:
226
+ // it reports success whenever `.focus()` did not throw, which is also
227
+ // true for a still-connected element that has since become `disabled` or
228
+ // `inert` and cannot take focus. Confirming `document.activeElement`
229
+ // is what makes "restored" mean restored.
230
+ const candidates = readOption(preferRestoreFallback ?? false)
231
+ ? [getRestoreFallbackTarget(restoreFallback), capturedFocus]
232
+ : [capturedFocus, getRestoreFallbackTarget(restoreFallback)];
233
+ for (const candidate of candidates) {
234
+ if (restoreFocusTo(candidate) && document.activeElement === candidate)
235
+ break;
236
+ }
197
237
  }
198
238
  capturedFocus = null;
199
239
  }
@@ -94,15 +94,31 @@ function rememberFocus() {
94
94
  const activeElement = document.activeElement;
95
95
  restoreFocusElement = activeElement instanceof HTMLElement ? activeElement : null;
96
96
  }
97
+ // The remembered element describes "what had focus before this popover
98
+ // opened", which stays true for the popover's whole open lifetime — not just
99
+ // until the first restore. Keeping the reference here (rather than spending
100
+ // it) is what lets a second restore work: expand -> cancel restores once
101
+ // while the popover stays open, and a later Escape must restore again
102
+ // instead of dropping focus on <body> (issue #1269). The reference is
103
+ // released where the `wasOpen` latch resets, so the next open re-arms it,
104
+ // and by onFocusMovedOutside once the user genuinely moves on.
97
105
  function restoreFocus(preventScroll = true) {
98
106
  const target = restoreFocusElement;
99
- restoreFocusElement = null;
100
107
  if (!target)
101
108
  return;
109
+ // Focus is already where it belongs, so there is nothing to restore. Now
110
+ // that the reference outlives the first restore, this is what keeps an
111
+ // external close following an internal cancel/submit from driving focus
112
+ // onto the same element a second time.
113
+ if (document.activeElement === target)
114
+ return;
102
115
  // .focus() dispatches `focusin` synchronously, and the window-level
103
116
  // ownership-tracking listener below can't otherwise tell this
104
117
  // component-driven restoration apart from the user genuinely moving on
105
118
  // to something outside the popover. Flag it so that listener exempts it.
119
+ // Load-bearing now that the reference outlives the restore: without the
120
+ // exemption, restoring to the (outside-the-popover) pre-open owner would
121
+ // trip onFocusMovedOutside and clear the very reference we are keeping.
106
122
  isRestoringFocus = true;
107
123
  target.focus({ preventScroll });
108
124
  isRestoringFocus = false;
@@ -206,10 +222,14 @@ $effect(() => {
206
222
  wasOpen = false;
207
223
  expanded = false;
208
224
  commentBody = '';
209
- // Return focus to wherever it was before the popover opened.
210
- // restoreFocus() null-guards and clears the ref, so an internal close
211
- // that already restored leaves this a safe no-op (no double-restore).
225
+ // Return focus to wherever it was before the popover opened, then
226
+ // release the reference: this open session is over, so the next open
227
+ // must re-arm from whatever has focus then rather than reuse a stale
228
+ // element. restoreFocus() null-guards and no-ops when the target
229
+ // already has focus, so an internal close that already restored does
230
+ // not drive focus a second time.
212
231
  restoreFocus();
232
+ restoreFocusElement = null;
213
233
  }
214
234
  return;
215
235
  }