@atlaskit/top-layer 4.4.3 → 4.4.4

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.
@@ -1,5 +1,8 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
- import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
5
+ import { useCallback, useEffect, useLayoutEffect, useReducer, useRef } from 'react';
3
6
  import { prefersReducedMotion } from './reduced-motion';
4
7
  function runCancellableSettlement(_ref) {
5
8
  var schedule = _ref.schedule,
@@ -12,6 +15,7 @@ function runCancellableSettlement(_ref) {
12
15
  onSettled();
13
16
  });
14
17
  return function () {
18
+ // Prevent an interrupted or unmounted lifecycle from firing a stale callback.
15
19
  isCancelled = true;
16
20
  };
17
21
  }
@@ -34,6 +38,13 @@ function runOnAnimationsSettled(_ref2) {
34
38
  }
35
39
  });
36
40
  }
41
+ function isNativeElementOpen(_ref3) {
42
+ var element = _ref3.element;
43
+ if (element instanceof HTMLDialogElement) {
44
+ return element.open;
45
+ }
46
+ return element.matches(':popover-open');
47
+ }
37
48
 
38
49
  /**
39
50
  * Visibility lifecycle phase shared between `useAnimatedVisibility`,
@@ -46,11 +57,11 @@ function runOnAnimationsSettled(_ref2) {
46
57
  * animation disabled the lifecycle goes directly `closed → open`.
47
58
  * - `open`: host element is mounted and settled. No transition is
48
59
  * playing.
49
- * - `exiting`: `isOpen` has flipped to `false` but the host element
50
- * is still mounted. Emitted while the CSS exit transition is playing
51
- * (animation enabled) OR while we wait for the browser's
52
- * `toggle`/`close` event to fire on the still-attached element so
53
- * focus restoration can run (animation disabled).
60
+ * - `exiting`: a native close has begun but the host element remains
61
+ * mounted. Usually this follows `isOpen` changing to `false`, but it can
62
+ * also begin with a browser-initiated dismissal while controlled intent
63
+ * is still open. With animation disabled, this phase lasts until the
64
+ * browser's closed `toggle` fires so native focus restoration can finish.
54
65
  *
55
66
  * State machine:
56
67
  * ```
@@ -60,140 +71,253 @@ function runOnAnimationsSettled(_ref2) {
60
71
  * exiting → entering (reopen mid-exit, animated)
61
72
  * exiting → open (reopen mid-handshake, non-animated)
62
73
  * ```
63
- *
64
- * `phase !== 'closed'` is the canonical "host is currently in the DOM
65
- * and on screen" predicate.
66
74
  */
67
75
 
76
+ function getInitialVisibilityState(_ref4) {
77
+ var isOpen = _ref4.isOpen,
78
+ willAnimate = _ref4.willAnimate;
79
+ if (!isOpen) {
80
+ return {
81
+ phase: 'closed',
82
+ controlledIntent: 'closed'
83
+ };
84
+ }
85
+ if (willAnimate) {
86
+ return {
87
+ phase: 'entering',
88
+ controlledIntent: 'open'
89
+ };
90
+ }
91
+ return {
92
+ phase: 'open',
93
+ controlledIntent: 'open'
94
+ };
95
+ }
96
+
68
97
  /**
69
- * Manages the children mount/unmount lifecycle around CSS exit transitions.
98
+ * Applies one lifecycle event to the current visibility phase.
70
99
  *
71
- * Used by both `Popover` and `Dialog` to share the same animation lifecycle
72
- * logic. Those components own their own show/hide mechanisms (showPopover /
73
- * hidePopover vs showModal / close) and event handling - this hook only
74
- * manages the relationship between `isOpen` and when children are rendered.
75
- *
76
- * ## Problem
77
- *
78
- * We want to delay the unmount of children until a CSS exit transition has
79
- * finished playing. If we unmount children the moment `isOpen` becomes
80
- * `false`, the exit animation is never visible - the content just disappears.
100
+ * Events that are stale for the current phase are ignored. This allows
101
+ * animation callbacks and native close events to settle safely after an
102
+ * interrupted lifecycle.
103
+ */
104
+ function getNextVisibilityState(state, action) {
105
+ if (action.type === 'open-requested') {
106
+ var shouldEnter = state.phase === 'closed' || state.phase === 'exiting' && state.controlledIntent === 'closed';
107
+ if (shouldEnter && action.willAnimate) {
108
+ return {
109
+ phase: 'entering',
110
+ controlledIntent: 'open'
111
+ };
112
+ }
113
+ if (shouldEnter) {
114
+ return {
115
+ phase: 'open',
116
+ controlledIntent: 'open'
117
+ };
118
+ }
119
+ return _objectSpread(_objectSpread({}, state), {}, {
120
+ controlledIntent: 'open'
121
+ });
122
+ }
123
+ if (action.type === 'close-requested') {
124
+ return _objectSpread(_objectSpread({}, state), {}, {
125
+ controlledIntent: 'closed'
126
+ });
127
+ }
128
+ if (action.type === 'exit-started') {
129
+ if (state.phase !== 'open' && state.phase !== 'entering') {
130
+ return state;
131
+ }
132
+ return _objectSpread(_objectSpread({}, state), {}, {
133
+ phase: 'exiting'
134
+ });
135
+ }
136
+ if (action.type === 'entry-settled') {
137
+ if (state.phase !== 'entering') {
138
+ return state;
139
+ }
140
+ return _objectSpread(_objectSpread({}, state), {}, {
141
+ phase: 'open'
142
+ });
143
+ }
144
+ if (action.type === 'exit-settled') {
145
+ if (state.phase !== 'exiting') {
146
+ return state;
147
+ }
148
+ return _objectSpread(_objectSpread({}, state), {}, {
149
+ phase: 'closed'
150
+ });
151
+ }
152
+ return state;
153
+ }
154
+
155
+ /**
156
+ * Coordinates controlled intent, native visibility, lifecycle phase, and host
157
+ * mounting around CSS exit transitions. The canonical contract is documented in
158
+ * `notes/architecture/animations.md#canonical-visibility-lifecycle-contract`.
81
159
  *
82
- * ## How it works
160
+ * Used by both `Popover` and `Dialog` to share the same lifecycle logic. Those
161
+ * components own native event binding, show and hide commands, and
162
+ * component-specific event handling. This hook returns stable `onBeforeToggle`
163
+ * and `onToggle` callbacks that advance lifecycle phase and host mounting.
83
164
  *
84
- * The lifecycle is modeled as a `phase` discriminated union (`closed`,
85
- * `entering`, `open`, `exiting`). The host element is rendered while
86
- * `phase !== 'closed'`, so the `exiting` phase keeps the host mounted
87
- * while the CSS exit transition plays (or, with animation disabled,
88
- * while we wait for the browser's closing `toggle` event to fire on the
89
- * still-attached element):
165
+ * The host element is rendered while `phase !== 'closed'`. The `exiting`
166
+ * phase keeps it mounted until its CSS animation settles or, with animation
167
+ * disabled, until the browser reports that the native element has closed.
90
168
  *
91
169
  * ```
92
170
  * isOpen: true ──────────────── false
93
171
  * phase: open ──────────────── exiting ─── (exit settle) ─── closed
94
172
  * ```
95
- *
96
- * Every effect dispatches on the single `phase` value instead of a
97
- * cross-product of booleans.
98
173
  */
99
- export function useAnimatedVisibility(_ref3) {
100
- var isOpen = _ref3.isOpen,
101
- shouldAnimate = _ref3.shouldAnimate,
102
- elementRef = _ref3.elementRef,
103
- onEnterFinish = _ref3.onEnterFinish,
104
- onExitFinish = _ref3.onExitFinish;
174
+ export function useAnimatedVisibility(_ref5) {
175
+ var isOpen = _ref5.isOpen,
176
+ shouldAnimate = _ref5.shouldAnimate,
177
+ elementRef = _ref5.elementRef,
178
+ onEnterFinish = _ref5.onEnterFinish,
179
+ onExitFinish = _ref5.onExitFinish;
105
180
  var willAnimate = shouldAnimate && !prefersReducedMotion();
106
- var isNativeClosedRef = useRef(false);
107
-
108
- // Promote `closed → entering`/`closed → open` synchronously during
109
- // render so the host element mounts in the same commit as the open
110
- // intent. All other transitions are driven by phase-specific effects.
111
- var _useState = useState(function () {
112
- if (!isOpen) {
113
- return 'closed';
114
- }
115
- return willAnimate ? 'entering' : 'open';
116
- }),
117
- _useState2 = _slicedToArray(_useState, 2),
118
- phase = _useState2[0],
119
- setPhase = _useState2[1];
120
-
121
- // Rearm only after controlled state and the native lifecycle have both
122
- // reached closed. Resetting as soon as `isOpen` becomes false can erase a
123
- // closing toggle that was handled in the same React batch.
124
- if (!isOpen && phase === 'closed') {
125
- isNativeClosedRef.current = false;
126
- }
127
- if (isOpen && phase === 'closed' && !isNativeClosedRef.current) {
128
- setPhase(willAnimate ? 'entering' : 'open');
129
- }
130
- if (!isOpen && (phase === 'open' || phase === 'entering')) {
131
- // Close intent. Move to `exiting` to gate the unmount on either
132
- // the exit transition (animated) or the closing toggle event
133
- // (non-animated). Includes interrupted entry (`entering → exiting`).
134
- setPhase('exiting');
135
- }
136
- if (isOpen && phase === 'exiting' && !isNativeClosedRef.current) {
137
- // Reopen mid-exit. With animation, jump back to `entering` and let
138
- // the entry transition restart from current visual state. Without
139
- // animation, jump straight to `open` (the handshake we were waiting
140
- // for is no longer relevant; the consumer wants the host visible
141
- // again synchronously).
142
- setPhase(willAnimate ? 'entering' : 'open');
143
- }
181
+ var _useReducer = useReducer(getNextVisibilityState, {
182
+ isOpen: isOpen,
183
+ willAnimate: willAnimate
184
+ }, getInitialVisibilityState),
185
+ _useReducer2 = _slicedToArray(_useReducer, 2),
186
+ visibilityState = _useReducer2[0],
187
+ dispatch = _useReducer2[1];
188
+ var phase = visibilityState.phase,
189
+ controlledIntent = visibilityState.controlledIntent;
144
190
 
145
- // Keep stable refs to the latest callbacks so effects and promise callbacks
191
+ // Keep stable refs to the latest callbacks so effects and animation settlement closures
146
192
  // always call the current callback without needing them in dependency arrays.
147
193
  var onEnterFinishRef = useRef(onEnterFinish);
148
- onEnterFinishRef.current = onEnterFinish;
149
194
  var onExitFinishRef = useRef(onExitFinish);
150
- onExitFinishRef.current = onExitFinish;
195
+ var willAnimateRef = useRef(willAnimate);
196
+ useLayoutEffect(function () {
197
+ onEnterFinishRef.current = onEnterFinish;
198
+ onExitFinishRef.current = onExitFinish;
199
+ willAnimateRef.current = willAnimate;
200
+ }, [onEnterFinish, onExitFinish, willAnimate]);
151
201
 
152
- // Animated entry: wait for all active animations and move to `open`.
202
+ // Synchronize controlled intent during render so opening mounts the host in
203
+ // the same commit. Keeping the processed intent in reducer state prevents a
204
+ // browser-driven exit from being mistaken for a new controlled open request.
205
+ if (isOpen && controlledIntent === 'closed') {
206
+ dispatch({
207
+ type: 'open-requested',
208
+ willAnimate: willAnimate
209
+ });
210
+ }
211
+ if (!isOpen && controlledIntent === 'open') {
212
+ dispatch({
213
+ type: 'close-requested'
214
+ });
215
+ }
216
+
217
+ // Entry can commit `open` before its callback because the host stays mounted.
218
+ // This ensures consumers observe the settled DOM rather than entering styles.
219
+ // Treat the phase before the initial commit as closed so an initially open host
220
+ // notifies consumers.
221
+ var prevPhaseRef = useRef('closed');
153
222
  useEffect(function () {
154
- if (phase !== 'entering') {
223
+ var prevPhase = prevPhaseRef.current;
224
+ prevPhaseRef.current = phase;
225
+ if (prevPhase === phase) {
226
+ return;
227
+ }
228
+ if (phase === 'open') {
229
+ var _onEnterFinishRef$cur;
230
+ (_onEnterFinishRef$cur = onEnterFinishRef.current) === null || _onEnterFinishRef$cur === void 0 || _onEnterFinishRef$cur.call(onEnterFinishRef);
231
+ }
232
+ }, [phase]);
233
+ var isMounted = phase !== 'closed';
234
+ var cancelExitSettlementRef = useRef(function () {});
235
+
236
+ // Exit must notify consumers before `closed` unmounts the host.
237
+ var settleExit = useCallback(function () {
238
+ var _onExitFinishRef$curr;
239
+ (_onExitFinishRef$curr = onExitFinishRef.current) === null || _onExitFinishRef$curr === void 0 || _onExitFinishRef$curr.call(onExitFinishRef);
240
+ dispatch({
241
+ type: 'exit-settled'
242
+ });
243
+ }, []);
244
+
245
+ // Primitives bind these stable callbacks to their native lifecycle events.
246
+ // This keeps event ownership at the primitive while the hook owns state transitions.
247
+ var onBeforeToggle = useCallback(function (event) {
248
+ // Any native transition supersedes a previous exit snapshot. This prevents
249
+ // a stale settlement from closing a host that is reopening or closing again.
250
+ cancelExitSettlementRef.current();
251
+ // Snapshot currentTarget because the DOM clears it after event dispatch.
252
+ var element = event.currentTarget;
253
+ if (element instanceof HTMLElement) {
254
+ if (event.newState === 'open') {
255
+ element.removeAttribute('inert');
256
+ element.removeAttribute('aria-hidden');
257
+ } else {
258
+ // We use a microtask to let native focus restoration happen before making the element inert.
259
+ // The browser restores focus after beforetoggle returns, during its close steps.
260
+ queueMicrotask(function () {
261
+ // A controlled close can be interrupted by isOpen changing back to true,
262
+ // which reopens the element before this microtask runs.
263
+ if (isNativeElementOpen({
264
+ element: element
265
+ })) {
266
+ return;
267
+ }
268
+ element.setAttribute('inert', '');
269
+ // Tooling does not consistently treat inert content as hidden, even though the browser does.
270
+ element.setAttribute('aria-hidden', 'true');
271
+ });
272
+ }
273
+ }
274
+ if (event.newState === 'closed') {
275
+ dispatch({
276
+ type: 'exit-started'
277
+ });
278
+ }
279
+ }, []);
280
+ var onToggle = useCallback(function (event) {
281
+ if (event.newState !== 'closed') {
282
+ return;
283
+ }
284
+ if (!willAnimateRef.current) {
285
+ cancelExitSettlementRef.current = runCancellableSettlement({
286
+ schedule: queueMicrotask,
287
+ onSettled: settleExit
288
+ });
155
289
  return;
156
290
  }
157
291
  var element = elementRef.current;
158
292
  if (!element) {
159
293
  return;
160
294
  }
161
- return runOnAnimationsSettled({
295
+
296
+ // The closed `toggle` is task-queued after synchronous `beforetoggle`.
297
+ // Snapshot here so the exiting phase styles have committed and the list
298
+ // represents the animations that actually govern exit settlement.
299
+ cancelExitSettlementRef.current = runOnAnimationsSettled({
162
300
  element: element,
163
- onSettled: function onSettled() {
164
- var _onEnterFinishRef$cur;
165
- // Fire the consumer callback before transitioning phase so any
166
- // state the consumer reads (refs, DOM nodes) is still attached
167
- // in the same synchronous tick, regardless of React batching
168
- // semantics.
169
- (_onEnterFinishRef$cur = onEnterFinishRef.current) === null || _onEnterFinishRef$cur === void 0 || _onEnterFinishRef$cur.call(onEnterFinishRef);
170
- setPhase(function (current) {
171
- return current === 'entering' ? 'open' : current;
172
- });
173
- }
301
+ onSettled: settleExit
174
302
  });
175
- }, [phase, elementRef]);
176
-
177
- // Non-animated entry callback. The lifecycle goes `closed → open`
178
- // directly with no `entering` phase; fire `onEnterFinish` on that jump.
179
- // A previous-phase ref avoids double-firing on the animated path.
180
- var prevPhaseForEntryRef = useRef('closed');
181
- useEffect(function () {
182
- var prevPhase = prevPhaseForEntryRef.current;
183
- prevPhaseForEntryRef.current = phase;
303
+ }, [elementRef, settleExit]);
304
+ useLayoutEffect(function () {
305
+ return function () {
306
+ // Prevent a pending animation or microtask from notifying after unmount.
307
+ cancelExitSettlementRef.current();
308
+ };
309
+ }, []);
184
310
 
185
- // Animated entry settle goes through `entering` and already fired
186
- // onEnterFinish above; only handle the non-animated direct jumps.
187
- if (prevPhase !== 'open' && phase === 'open' && prevPhase !== 'entering') {
188
- var _onEnterFinishRef$cur2;
189
- (_onEnterFinishRef$cur2 = onEnterFinishRef.current) === null || _onEnterFinishRef$cur2 === void 0 || _onEnterFinishRef$cur2.call(onEnterFinishRef);
311
+ // Entry settlement only removes entering styles. Its callback runs after the
312
+ // resulting `open` commit in the effect above.
313
+ useEffect(function settleEntry() {
314
+ if (phase !== 'entering') {
315
+ return;
190
316
  }
191
- }, [phase]);
192
-
193
- // Animated exit: wait for all active animations, fire `onExitFinish`,
194
- // then move to `closed` to unmount the host.
195
- useEffect(function () {
196
- if (phase !== 'exiting' || !willAnimate) {
317
+ if (!willAnimate) {
318
+ dispatch({
319
+ type: 'entry-settled'
320
+ });
197
321
  return;
198
322
  }
199
323
  var element = elementRef.current;
@@ -203,67 +327,16 @@ export function useAnimatedVisibility(_ref3) {
203
327
  return runOnAnimationsSettled({
204
328
  element: element,
205
329
  onSettled: function onSettled() {
206
- var _onExitFinishRef$curr;
207
- // Fire the consumer callback before transitioning phase to
208
- // `closed`. The phase change drives the host element unmount,
209
- // and consumers expect to be able to read the element (or a
210
- // forwarded ref to it) inside `onExitFinish`. Doing this in
211
- // the safe order avoids relying on React's automatic batching
212
- // to keep the element attached across the callback.
213
- (_onExitFinishRef$curr = onExitFinishRef.current) === null || _onExitFinishRef$curr === void 0 || _onExitFinishRef$curr.call(onExitFinishRef);
214
- setPhase(function (current) {
215
- return current === 'exiting' ? 'closed' : current;
330
+ dispatch({
331
+ type: 'entry-settled'
216
332
  });
217
333
  }
218
334
  });
219
335
  }, [phase, willAnimate, elementRef]);
220
-
221
- // Native dismissal is authoritative. Its toggle can arrive while controlled
222
- // state still says open, so remember that native state and do not
223
- // reopen until `isOpen` has first moved to false.
224
- useLayoutEffect(function () {
225
- if (phase === 'exiting' && !willAnimate && isNativeClosedRef.current) {
226
- setPhase('closed');
227
- }
228
- }, [phase, willAnimate]);
229
- var onToggle = useCallback(function (event) {
230
- if (event.newState === 'open') {
231
- isNativeClosedRef.current = false;
232
- return;
233
- }
234
- if (event.newState !== 'closed') {
235
- return;
236
- }
237
- isNativeClosedRef.current = true;
238
- setPhase(function (current) {
239
- if (current === 'open' || current === 'entering') {
240
- return 'exiting';
241
- }
242
- if (current === 'exiting' && !willAnimate) {
243
- return 'closed';
244
- }
245
- return current;
246
- });
247
- }, [willAnimate]);
248
-
249
- // Non-animated exit callback. There are no animations to wait for, so fire
250
- // `onExitFinish` on the transition into `exiting` (the unmount itself
251
- // is gated by the closing toggle handshake above).
252
- var prevPhaseForExitRef = useRef('closed');
253
- useEffect(function () {
254
- var prevPhase = prevPhaseForExitRef.current;
255
- prevPhaseForExitRef.current = phase;
256
-
257
- // Only fire on the non-animated transition into `exiting`. The
258
- // animated path's `exiting → closed` settle already fires
259
- // `onExitFinish` from `runOnAnimationsSettled`.
260
- if (prevPhase !== 'exiting' && phase === 'exiting' && !willAnimate) {
261
- var _onExitFinishRef$curr2;
262
- (_onExitFinishRef$curr2 = onExitFinishRef.current) === null || _onExitFinishRef$curr2 === void 0 || _onExitFinishRef$curr2.call(onExitFinishRef);
263
- }
264
- }, [phase, willAnimate]);
265
336
  return {
266
337
  phase: phase,
338
+ isMounted: isMounted,
339
+ onBeforeToggle: onBeforeToggle,
267
340
  onToggle: onToggle
268
341
  };
269
342
  }