@atlaskit/top-layer 0.15.0 → 0.16.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 (55) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/cjs/dialog/dialog-content.js +33 -4
  3. package/dist/cjs/dialog-scroll-lock/index.js +17 -7
  4. package/dist/cjs/entry-points/use-popover-id.js +3 -1
  5. package/dist/cjs/internal/get-aria-for-trigger.js +8 -3
  6. package/dist/cjs/internal/use-anchor-position-at-point.js +4 -2
  7. package/dist/cjs/internal/use-anchor-position.js +3 -2
  8. package/dist/cjs/internal/use-animated-visibility.js +233 -155
  9. package/dist/cjs/internal/use-focus-wrap.js +5 -3
  10. package/dist/cjs/internal/use-initial-focus.js +196 -50
  11. package/dist/cjs/internal/use-width-from-anchor.js +5 -4
  12. package/dist/cjs/popover/popover.js +58 -9
  13. package/dist/es2019/dialog/dialog-content.js +33 -4
  14. package/dist/es2019/dialog-scroll-lock/index.js +18 -8
  15. package/dist/es2019/entry-points/use-popover-id.js +3 -1
  16. package/dist/es2019/internal/get-aria-for-trigger.js +8 -3
  17. package/dist/es2019/internal/use-anchor-position-at-point.js +4 -2
  18. package/dist/es2019/internal/use-anchor-position.js +3 -2
  19. package/dist/es2019/internal/use-animated-visibility.js +222 -149
  20. package/dist/es2019/internal/use-focus-wrap.js +5 -4
  21. package/dist/es2019/internal/use-initial-focus.js +189 -45
  22. package/dist/es2019/internal/use-width-from-anchor.js +5 -4
  23. package/dist/es2019/popover/popover.js +59 -10
  24. package/dist/esm/dialog/dialog-content.js +33 -4
  25. package/dist/esm/dialog-scroll-lock/index.js +17 -8
  26. package/dist/esm/entry-points/use-popover-id.js +3 -1
  27. package/dist/esm/internal/get-aria-for-trigger.js +8 -3
  28. package/dist/esm/internal/use-anchor-position-at-point.js +4 -2
  29. package/dist/esm/internal/use-anchor-position.js +3 -2
  30. package/dist/esm/internal/use-animated-visibility.js +235 -157
  31. package/dist/esm/internal/use-focus-wrap.js +5 -4
  32. package/dist/esm/internal/use-initial-focus.js +196 -51
  33. package/dist/esm/internal/use-width-from-anchor.js +5 -4
  34. package/dist/esm/popover/popover.js +59 -10
  35. package/dist/types/dialog/types.d.ts +9 -1
  36. package/dist/types/dialog-scroll-lock/index.d.ts +22 -6
  37. package/dist/types/internal/get-aria-for-trigger.d.ts +8 -1
  38. package/dist/types/internal/use-anchor-position-at-point.d.ts +6 -1
  39. package/dist/types/internal/use-anchor-position.d.ts +14 -1
  40. package/dist/types/internal/use-animated-visibility.d.ts +48 -19
  41. package/dist/types/internal/use-focus-wrap.d.ts +6 -1
  42. package/dist/types/internal/use-initial-focus.d.ts +55 -4
  43. package/dist/types/internal/use-width-from-anchor.d.ts +3 -2
  44. package/dist/types/popover/types.d.ts +13 -2
  45. package/dist/types-ts4.5/dialog/types.d.ts +9 -1
  46. package/dist/types-ts4.5/dialog-scroll-lock/index.d.ts +22 -6
  47. package/dist/types-ts4.5/internal/get-aria-for-trigger.d.ts +8 -1
  48. package/dist/types-ts4.5/internal/use-anchor-position-at-point.d.ts +6 -1
  49. package/dist/types-ts4.5/internal/use-anchor-position.d.ts +14 -1
  50. package/dist/types-ts4.5/internal/use-animated-visibility.d.ts +48 -19
  51. package/dist/types-ts4.5/internal/use-focus-wrap.d.ts +6 -1
  52. package/dist/types-ts4.5/internal/use-initial-focus.d.ts +55 -4
  53. package/dist/types-ts4.5/internal/use-width-from-anchor.d.ts +3 -2
  54. package/dist/types-ts4.5/popover/types.d.ts +13 -2
  55. package/package.json +1 -1
@@ -1,7 +1,71 @@
1
- import { useEffect, useRef, useState } from 'react';
2
- import { bind } from 'bind-event-listener';
1
+ import { useEffect, useLayoutEffect, useRef, useState } from 'react';
2
+ import { bind, bindAll } from 'bind-event-listener';
3
3
  import { prefersReducedMotion } from './reduced-motion';
4
4
  import { usePresetStyles } from './use-preset-styles';
5
+ /**
6
+ * Fires `onSettled` once when `transitionend` fires on `element`, or after
7
+ * `safetyNetMs` if the event is swallowed (mid-transition unmount, CSS
8
+ * mismatch, browser quirks). Returns a cleanup that cancels both paths.
9
+ */
10
+ function runOnTransitionEndOrTimeout({
11
+ element,
12
+ safetyNetMs,
13
+ onSettled
14
+ }) {
15
+ let isSettled = false;
16
+ function teardown() {
17
+ isSettled = true;
18
+ clearTimeout(safetyNetId);
19
+ unbind();
20
+ }
21
+ function settle() {
22
+ if (isSettled) {
23
+ return;
24
+ }
25
+ teardown();
26
+ onSettled();
27
+ }
28
+ const unbind = bind(element, {
29
+ type: 'transitionend',
30
+ listener: settle,
31
+ options: {
32
+ once: true
33
+ }
34
+ });
35
+ const safetyNetId = setTimeout(settle, safetyNetMs);
36
+ return teardown;
37
+ }
38
+
39
+ /**
40
+ * Visibility lifecycle phase shared between `useAnimatedVisibility`,
41
+ * `useFocusWrap`, `useInitialFocus`, and the `Popover` / `Dialog`
42
+ * components.
43
+ *
44
+ * - `closed`: host element is unmounted. No DOM presence.
45
+ * - `entering`: host element is mounted and the CSS entry transition
46
+ * is playing. **Only emitted when animation is enabled.** With
47
+ * animation disabled the lifecycle goes directly `closed → open`.
48
+ * - `open`: host element is mounted and settled. No transition is
49
+ * playing.
50
+ * - `exiting`: `isOpen` has flipped to `false` but the host element
51
+ * is still mounted. Emitted while the CSS exit transition is playing
52
+ * (animation enabled) OR while we wait for the browser's
53
+ * `toggle`/`close` event to fire on the still-attached element so
54
+ * focus restoration can run (animation disabled).
55
+ *
56
+ * State machine:
57
+ * ```
58
+ * animation ON: closed → entering → open → exiting → closed
59
+ * animation OFF: closed → open → exiting → closed
60
+ * interrupts: entering → exiting (close mid-entry)
61
+ * exiting → entering (reopen mid-exit, animated)
62
+ * exiting → open (reopen mid-handshake, non-animated)
63
+ * ```
64
+ *
65
+ * `phase !== 'closed'` is the canonical "host is currently in the DOM
66
+ * and on screen" predicate.
67
+ */
68
+
5
69
  /**
6
70
  * Manages the children mount/unmount lifecycle around CSS exit transitions.
7
71
  *
@@ -18,26 +82,20 @@ import { usePresetStyles } from './use-preset-styles';
18
82
  *
19
83
  * ## How it works
20
84
  *
21
- * `showChildren` tracks whether the consumer's children should be in the DOM.
22
- * It does not mirror `isOpen` exactly - it intentionally lags behind during
23
- * exit animations:
85
+ * The lifecycle is modeled as a `phase` discriminated union (`closed`,
86
+ * `entering`, `open`, `exiting`). The host element is rendered while
87
+ * `phase !== 'closed'`, so the `exiting` phase keeps the host mounted
88
+ * while the CSS exit transition plays (or, with animation disabled,
89
+ * while we wait for the browser `toggle` / `close` event to fire on the
90
+ * still-attached element):
24
91
  *
25
92
  * ```
26
- * isOpen: true ──────────────── false
27
- * showChildren: true ──────────────── true ─── (exit animation) ─── false
93
+ * isOpen: true ──────────────── false
94
+ * phase: open ──────────────── exiting ─── (exit settle) ─── closed
28
95
  * ```
29
96
  *
30
- * Two close paths exist:
31
- *
32
- * 1. **Animated close** (`willAnimate === true`):
33
- * `isOpen` goes `false` → `showChildren` stays `true` → the CSS exit
34
- * transition plays → `transitionend` fires → `showChildren` → `false`
35
- * → `onExitFinish` fires. A timeout fallback ensures we still unmount
36
- * if `transitionend` never fires (e.g. browser quirks, zero-duration).
37
- *
38
- * 2. **Non-animated close** (`willAnimate === false`):
39
- * `isOpen` goes `false` → `showChildren` → `false` synchronously during
40
- * the same render → `onExitFinish` fires in a follow-up effect.
97
+ * Every effect dispatches on the single `phase` value instead of a
98
+ * cross-product of booleans.
41
99
  */
42
100
  export function useAnimatedVisibility({
43
101
  isOpen,
@@ -56,26 +114,34 @@ export function useAnimatedVisibility({
56
114
  // or the user has enabled the "prefers reduced motion" OS setting.
57
115
  const willAnimate = Boolean(animate) && !prefersReducedMotion();
58
116
 
59
- // showChildren state
60
- //
61
- // This is the core of the hook. `showChildren` controls whether the
62
- // consumer's children are in the DOM. The two `if` blocks below are
63
- // intentionally written as synchronous state updates during render
64
- // (React supports this pattern - setState during render is treated as
65
- // a synchronous re-render before the browser paints).
66
- //
67
- // Mount path: isOpen went true but showChildren is still false → mount.
68
- // Unmount path: isOpen went false, showChildren is still true, and we
69
- // are NOT animating → unmount immediately (no transition
70
- // to wait for). If we ARE animating, showChildren stays
71
- // true here and the effect below will set it to false
72
- // after `transitionend`.
73
- const [showChildren, setShowChildren] = useState(isOpen);
74
- if (isOpen && !showChildren) {
75
- setShowChildren(true);
117
+ // `phase` is the single source of truth for the visibility lifecycle.
118
+ // We promote `closed → entering` (animated) or `closed → open`
119
+ // (non-animated) synchronously during render so the host element mounts
120
+ // in the same commit as the open intent (React supports a single
121
+ // setState during render as a synchronous re-render before paint).
122
+ // All other transitions are driven by phase-specific effects below.
123
+ const [phase, setPhase] = useState(() => {
124
+ if (!isOpen) {
125
+ return 'closed';
126
+ }
127
+ return willAnimate ? 'entering' : 'open';
128
+ });
129
+ if (isOpen && phase === 'closed') {
130
+ setPhase(willAnimate ? 'entering' : 'open');
131
+ }
132
+ if (!isOpen && (phase === 'open' || phase === 'entering')) {
133
+ // Close intent. Move to `exiting` to gate the unmount on either
134
+ // the exit transition (animated) or the toggle/close event
135
+ // (non-animated). Includes interrupted entry (`entering → exiting`).
136
+ setPhase('exiting');
76
137
  }
77
- if (!isOpen && showChildren && !willAnimate) {
78
- setShowChildren(false);
138
+ if (isOpen && phase === 'exiting') {
139
+ // Reopen mid-exit. With animation, jump back to `entering` and let
140
+ // the entry transition restart from current visual state. Without
141
+ // animation, jump straight to `open` (the handshake we were waiting
142
+ // for is no longer relevant; the consumer wants the host visible
143
+ // again synchronously).
144
+ setPhase(willAnimate ? 'entering' : 'open');
79
145
  }
80
146
 
81
147
  // Keep stable refs to the latest callbacks so effects and timeout closures
@@ -85,152 +151,159 @@ export function useAnimatedVisibility({
85
151
  const onExitFinishRef = useRef(onExitFinish);
86
152
  onExitFinishRef.current = onExitFinish;
87
153
 
88
- // Animated entry effect
89
- //
90
- // Fires `onEnterFinish` after the entry transition completes.
91
- // When there is no animation, fires it immediately via a separate effect.
92
- //
93
- // A fallback timeout (enterDurationMs + 50ms buffer) ensures it always
94
- // fires even if `transitionend` is never emitted (e.g. zero-duration,
95
- // browser quirk, or reduced motion).
154
+ // `entering` (animation enabled):
155
+ // Wait for the entry transition to settle, then move to `open` and fire
156
+ // `onEnterFinish`. A fallback timeout (enterDurationMs + 50ms buffer)
157
+ // guarantees the settle path runs even if `transitionend` is swallowed
158
+ // (zero-duration, browser quirk, mid-transition unmount).
96
159
  useEffect(() => {
97
160
  var _preset$enterDuration;
98
- if (!isOpen || !showChildren || !willAnimate) {
161
+ if (phase !== 'entering') {
99
162
  return;
100
163
  }
101
164
  const element = elementRef.current;
102
165
  if (!element) {
103
166
  return;
104
167
  }
105
-
106
- // Both the transitionend listener and the fallback timeout are guarded by
107
- // a shared `hasFired` flag to prevent double-firing. This handles the race
108
- // where the 350ms transition finishes after the 400ms fallback fires
109
- // (or vice versa): whichever fires first sets `hasFired = true`, and the
110
- // second path becomes a no-op.
111
- let hasFired = false;
112
- function handleTransitionEnd() {
113
- var _onEnterFinishRef$cur;
114
- if (hasFired) {
115
- return;
116
- }
117
- hasFired = true;
118
- clearTimeout(fallbackId);
119
- unbind();
120
- (_onEnterFinishRef$cur = onEnterFinishRef.current) === null || _onEnterFinishRef$cur === void 0 ? void 0 : _onEnterFinishRef$cur.call(onEnterFinishRef);
121
- }
122
- const fallbackId = setTimeout(handleTransitionEnd, ((_preset$enterDuration = preset === null || preset === void 0 ? void 0 : preset.enterDurationMs) !== null && _preset$enterDuration !== void 0 ? _preset$enterDuration : 0) + 50);
123
- const unbind = bind(element, {
124
- type: 'transitionend',
125
- listener: handleTransitionEnd,
126
- options: {
127
- once: true
168
+ return runOnTransitionEndOrTimeout({
169
+ element,
170
+ safetyNetMs: ((_preset$enterDuration = preset === null || preset === void 0 ? void 0 : preset.enterDurationMs) !== null && _preset$enterDuration !== void 0 ? _preset$enterDuration : 0) + 50,
171
+ onSettled: () => {
172
+ var _onEnterFinishRef$cur;
173
+ // Fire the consumer callback before transitioning phase so any
174
+ // state the consumer reads (refs, DOM nodes) is still attached
175
+ // in the same synchronous tick, regardless of React batching
176
+ // semantics.
177
+ (_onEnterFinishRef$cur = onEnterFinishRef.current) === null || _onEnterFinishRef$cur === void 0 ? void 0 : _onEnterFinishRef$cur.call(onEnterFinishRef);
178
+ setPhase(current => current === 'entering' ? 'open' : current);
128
179
  }
129
180
  });
130
- return () => {
131
- hasFired = true;
132
- clearTimeout(fallbackId);
133
- unbind();
134
- };
135
- }, [isOpen, showChildren, willAnimate, preset === null || preset === void 0 ? void 0 : preset.enterDurationMs, elementRef]);
181
+ }, [phase, preset === null || preset === void 0 ? void 0 : preset.enterDurationMs, elementRef]);
136
182
 
137
- // Non-animated entry callback
138
- //
139
- // When there is no animation, fire `onEnterFinish` immediately on mount and after `isOpen` goes false->true.
140
- // The initial value is false to make sure it fires on mount if the current `isOpen` is true.
141
- const prevIsOpenForEntryRef = useRef(false);
183
+ // Non-animated entry callback:
184
+ // When animation is disabled, the lifecycle goes `closed → open`
185
+ // directly with no `entering` phase. Fire `onEnterFinish` on the
186
+ // transition into `open` from the non-animated path. We detect this
187
+ // via a previous-phase ref to avoid firing on the animated path's
188
+ // `entering → open` transition (which has its own callback above).
189
+ const prevPhaseForEntryRef = useRef('closed');
142
190
  useEffect(() => {
143
- const prevIsOpen = prevIsOpenForEntryRef.current;
144
- prevIsOpenForEntryRef.current = isOpen;
145
- if (!prevIsOpen && isOpen && !willAnimate) {
191
+ const prevPhase = prevPhaseForEntryRef.current;
192
+ prevPhaseForEntryRef.current = phase;
193
+
194
+ // Animated entry settle goes through `entering` and already fired
195
+ // onEnterFinish above; only handle the non-animated direct jumps.
196
+ if (prevPhase !== 'open' && phase === 'open' && prevPhase !== 'entering') {
146
197
  var _onEnterFinishRef$cur2;
147
198
  (_onEnterFinishRef$cur2 = onEnterFinishRef.current) === null || _onEnterFinishRef$cur2 === void 0 ? void 0 : _onEnterFinishRef$cur2.call(onEnterFinishRef);
148
199
  }
149
- }, [isOpen, willAnimate]);
200
+ }, [phase]);
150
201
 
151
- // Animated exit effect
152
- //
153
- // Runs when all three conditions are true:
154
- // • `isOpen` is false (consumer wants to close)
155
- // • `showChildren` is true (children are still mounted for exit anim)
156
- // • `willAnimate` is true (we have a preset and reduced motion is off)
157
- //
158
- // Listens for `transitionend` on the element to know when the CSS exit
159
- // transition has finished, then unmounts children and fires onExitFinish.
160
- //
161
- // Fallback timeout: `transitionend` can fail to fire in edge cases
162
- // (e.g. element removed from top layer by browser, zero-duration
163
- // transition, interrupted animation). The timeout is set to the
164
- // preset's exit duration + 50ms buffer to guarantee we always unmount.
202
+ // `exiting` (animation enabled):
203
+ // Wait for the exit transition to settle, then unmount the host element
204
+ // (set phase back to `closed`) and fire `onExitFinish`. The fallback
205
+ // timeout (exitDurationMs + 50ms buffer) guarantees we always unmount
206
+ // even if `transitionend` is never emitted (element removed from the
207
+ // top layer, zero-duration transition, interrupted animation).
165
208
  useEffect(() => {
166
209
  var _preset$exitDurationM;
167
- // Guard: only run during the "exiting" window
168
- if (isOpen || !showChildren || !willAnimate) {
210
+ if (phase !== 'exiting' || !willAnimate) {
169
211
  return;
170
212
  }
171
213
  const element = elementRef.current;
172
214
  if (!element) {
173
215
  return;
174
216
  }
175
-
176
- // Both the transitionend listener and the fallback timeout are guarded by
177
- // a shared `hasFired` flag to prevent double-firing. This handles the race
178
- // where the 350ms transition finishes after the 400ms fallback fires
179
- // (or vice versa): whichever fires first sets `hasFired = true`, and the
180
- // second path becomes a no-op.
181
- let hasFired = false;
182
- function handleTransitionEnd() {
183
- var _onExitFinishRef$curr;
184
- if (hasFired) {
185
- return;
186
- }
187
- hasFired = true;
188
- clearTimeout(fallbackId);
189
- unbind();
190
- setShowChildren(false);
191
- (_onExitFinishRef$curr = onExitFinishRef.current) === null || _onExitFinishRef$curr === void 0 ? void 0 : _onExitFinishRef$curr.call(onExitFinishRef);
192
- }
193
-
194
- // Fallback: unmount even if transitionend never fires
195
- const fallbackId = setTimeout(handleTransitionEnd, ((_preset$exitDurationM = preset === null || preset === void 0 ? void 0 : preset.exitDurationMs) !== null && _preset$exitDurationM !== void 0 ? _preset$exitDurationM : 0) + 50);
196
-
197
- // Primary: listen for the CSS transition to complete
198
- const unbind = bind(element, {
199
- type: 'transitionend',
200
- listener: handleTransitionEnd,
201
- options: {
202
- once: true
217
+ return runOnTransitionEndOrTimeout({
218
+ element,
219
+ safetyNetMs: ((_preset$exitDurationM = preset === null || preset === void 0 ? void 0 : preset.exitDurationMs) !== null && _preset$exitDurationM !== void 0 ? _preset$exitDurationM : 0) + 50,
220
+ onSettled: () => {
221
+ var _onExitFinishRef$curr;
222
+ // Fire the consumer callback before transitioning phase to
223
+ // `closed`. The phase change drives the host element unmount,
224
+ // and consumers expect to be able to read the element (or a
225
+ // forwarded ref to it) inside `onExitFinish`. Doing this in
226
+ // the safe order avoids relying on React's automatic batching
227
+ // to keep the element attached across the callback.
228
+ (_onExitFinishRef$curr = onExitFinishRef.current) === null || _onExitFinishRef$curr === void 0 ? void 0 : _onExitFinishRef$curr.call(onExitFinishRef);
229
+ setPhase(current => current === 'exiting' ? 'closed' : current);
203
230
  }
204
231
  });
232
+ }, [phase, willAnimate, preset === null || preset === void 0 ? void 0 : preset.exitDurationMs, elementRef]);
205
233
 
206
- // Cleanup on re-render or unmount: cancel pending timeout and listener
207
- return () => {
208
- hasFired = true;
209
- clearTimeout(fallbackId);
210
- unbind();
211
- };
212
- }, [isOpen, showChildren, willAnimate, preset === null || preset === void 0 ? void 0 : preset.exitDurationMs, elementRef]);
213
-
214
- // Non-animated exit callback
234
+ // `exiting` (animation disabled):
235
+ // Wait for the browser's `toggle` (closed) event (for `<div popover>`)
236
+ // or `close` event (for `<dialog>`) to fire before unmounting. This
237
+ // guarantees:
238
+ // - the consumer's toggle/close listener (close-reason capture,
239
+ // nested focus restoration, `onClose` notification) runs against
240
+ // the still-attached element,
241
+ // - the browser's native focus restoration triggered by
242
+ // `hidePopover()` / `<dialog>.close()` has fired,
243
+ // before React unmounts the node. A naive `setTimeout(0)` race
244
+ // occasionally beats the `toggle` task in Chromium, causing focus
245
+ // restoration to drop (the toggle event arrives at a detached element).
215
246
  //
216
- // When there is no animation, children unmount synchronously (handled
217
- // by the `if` block above). But `onExitFinish` still needs to fire -
218
- // we do that in a follow-up effect so the consumer can react to it.
247
+ // `useLayoutEffect` (not `useEffect`) so the listener is bound in the
248
+ // same commit as the consumer's show/hide layout effect that calls
249
+ // `hidePopover()` / `dialog.close()`. The browser queues the resulting
250
+ // `toggle`/`close` event as a task, which cannot run until the JS
251
+ // frame (including all effect flushes) completes, so the listener is
252
+ // always attached before the event fires. Promotion to layout timing
253
+ // removes any theoretical gap and future-proofs the ordering against
254
+ // effect reordering. Safe under SSR: `phase` starts at `'closed'`, so
255
+ // the body short-circuits and no DOM access happens on the server.
219
256
  //
220
- // Uses a previous-value ref (`wasOpenRef`) to detect the exact
221
- // true→false transition of `isOpen`, avoiding false positives on
222
- // mount or re-renders where `isOpen` was already false.
223
- const wasOpenRef = useRef(isOpen);
257
+ // `onExitFinish` is NOT called here. It is decoupled from the unmount
258
+ // handshake and fired by the dedicated non-animated exit-callback
259
+ // effect below, so consumers observe a synchronous "exit finished"
260
+ // signal that matches `isOpen` flipping to `false`.
261
+ useLayoutEffect(() => {
262
+ if (phase !== 'exiting' || willAnimate) {
263
+ return;
264
+ }
265
+ const element = elementRef.current;
266
+ if (!element) {
267
+ return;
268
+ }
269
+ function unmount() {
270
+ setPhase(current => current === 'exiting' ? 'closed' : current);
271
+ }
272
+ return bindAll(element, [{
273
+ type: 'toggle',
274
+ listener: event => {
275
+ if (event.newState === 'closed') {
276
+ unmount();
277
+ }
278
+ }
279
+ }, {
280
+ type: 'close',
281
+ listener: unmount
282
+ }]);
283
+ }, [phase, willAnimate, elementRef]);
284
+
285
+ // Non-animated exit callback:
286
+ // When animation is disabled there is no `transitionend` to wait for,
287
+ // so fire `onExitFinish` on the transition into `exiting`. The actual
288
+ // host-element unmount is gated on the browser `toggle`/`close` event
289
+ // by the effect above; `onExitFinish` is observably decoupled from
290
+ // that handshake so consumers see the callback fire at the same time
291
+ // `isOpen` flipped to `false`.
292
+ const prevPhaseForExitRef = useRef('closed');
224
293
  useEffect(() => {
225
- const wasOpen = wasOpenRef.current;
226
- wasOpenRef.current = isOpen;
227
- if (wasOpen && !isOpen && !willAnimate) {
294
+ const prevPhase = prevPhaseForExitRef.current;
295
+ prevPhaseForExitRef.current = phase;
296
+
297
+ // Only fire on the non-animated transition into `exiting`. The
298
+ // animated path's `exiting → closed` settle already fires
299
+ // `onExitFinish` from `runOnTransitionEndOrTimeout`.
300
+ if (prevPhase !== 'exiting' && phase === 'exiting' && !willAnimate) {
228
301
  var _onExitFinishRef$curr2;
229
302
  (_onExitFinishRef$curr2 = onExitFinishRef.current) === null || _onExitFinishRef$curr2 === void 0 ? void 0 : _onExitFinishRef$curr2.call(onExitFinishRef);
230
303
  }
231
- }, [isOpen, willAnimate]);
304
+ }, [phase, willAnimate]);
232
305
  return {
233
- showChildren,
306
+ phase,
234
307
  preset
235
308
  };
236
309
  }
@@ -4,7 +4,6 @@ import { getFirstFocusable } from '../focus/get-first-focusable';
4
4
  import { getLastFocusable } from '../focus/get-last-focusable';
5
5
  import { getNextFocusable } from '../focus/get-next-focusable';
6
6
  import { isNestedLayerFocused } from '../focus/is-nested-layer-focused';
7
-
8
7
  /**
9
8
  * Roles that require focus wrapping per WAI-ARIA APG.
10
9
  *
@@ -45,11 +44,13 @@ function roleRequiresFocusWrap(role) {
45
44
  */
46
45
  export function useFocusWrap({
47
46
  elementRef,
48
- role
47
+ role,
48
+ phase
49
49
  }) {
50
+ const isVisible = phase !== 'closed';
50
51
  useEffect(() => {
51
52
  const element = elementRef.current;
52
- if (!element || !roleRequiresFocusWrap(role)) {
53
+ if (!element || !roleRequiresFocusWrap(role) || !isVisible) {
53
54
  return;
54
55
  }
55
56
  const unbind = bind(element, {
@@ -100,5 +101,5 @@ export function useFocusWrap({
100
101
  }
101
102
  });
102
103
  return unbind;
103
- }, [elementRef, role]);
104
+ }, [elementRef, role, isVisible]);
104
105
  }