@lostgradient/cinder 0.24.0 → 0.24.2

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.
@@ -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
  }