@atlaskit/top-layer 3.2.0 → 4.1.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 (50) hide show
  1. package/AGENTS.md +3 -1
  2. package/CHANGELOG.md +18 -0
  3. package/README.md +0 -14
  4. package/dist/cjs/dialog/dialog-content.js +47 -20
  5. package/dist/cjs/focus/is-in-same-layer.js +1 -7
  6. package/dist/cjs/focus/selectors.js +1 -1
  7. package/dist/cjs/internal/get-aria-for-trigger.js +23 -9
  8. package/dist/cjs/internal/role-types.js +1 -64
  9. package/dist/cjs/internal/use-anchor-position.js +2 -3
  10. package/dist/cjs/internal/use-animated-visibility.js +36 -39
  11. package/dist/cjs/popover/popover.js +18 -20
  12. package/dist/es2019/dialog/dialog-content.js +48 -22
  13. package/dist/es2019/focus/is-in-same-layer.js +1 -7
  14. package/dist/es2019/focus/selectors.js +1 -1
  15. package/dist/es2019/internal/get-aria-for-trigger.js +23 -10
  16. package/dist/es2019/internal/role-types.js +0 -59
  17. package/dist/es2019/internal/use-anchor-position.js +2 -3
  18. package/dist/es2019/internal/use-animated-visibility.js +38 -39
  19. package/dist/es2019/popover/popover.js +15 -17
  20. package/dist/esm/dialog/dialog-content.js +49 -22
  21. package/dist/esm/focus/is-in-same-layer.js +1 -7
  22. package/dist/esm/focus/selectors.js +1 -1
  23. package/dist/esm/internal/get-aria-for-trigger.js +23 -10
  24. package/dist/esm/internal/role-types.js +0 -58
  25. package/dist/esm/internal/use-anchor-position.js +2 -3
  26. package/dist/esm/internal/use-animated-visibility.js +38 -41
  27. package/dist/esm/popover/popover.js +17 -19
  28. package/dist/types/dialog/dialog-content.d.ts +2 -2
  29. package/dist/types/dialog/index.d.ts +1 -1
  30. package/dist/types/dialog/types.d.ts +17 -3
  31. package/dist/types/entry-points/dialog.d.ts +1 -1
  32. package/dist/types/internal/get-aria-for-trigger.d.ts +21 -7
  33. package/dist/types/internal/role-types.d.ts +0 -32
  34. package/dist/types/internal/use-animated-visibility.d.ts +7 -1
  35. package/dist/types/popover/popover.d.ts +1 -5
  36. package/dist/types/popover/types.d.ts +1 -1
  37. package/package.json +3 -30
  38. package/animations/package.json +0 -10
  39. package/dist/cjs/entry-points/animations.js +0 -1
  40. package/dist/cjs/internal/role-to-aria-has-popup.js +0 -37
  41. package/dist/cjs/internal/slot.js +0 -65
  42. package/dist/es2019/entry-points/animations.js +0 -0
  43. package/dist/es2019/internal/role-to-aria-has-popup.js +0 -32
  44. package/dist/es2019/internal/slot.js +0 -57
  45. package/dist/esm/entry-points/animations.js +0 -0
  46. package/dist/esm/internal/role-to-aria-has-popup.js +0 -31
  47. package/dist/esm/internal/slot.js +0 -59
  48. package/dist/types/entry-points/animations.d.ts +0 -0
  49. package/dist/types/internal/role-to-aria-has-popup.d.ts +0 -13
  50. package/dist/types/internal/slot.d.ts +0 -29
@@ -1,9 +1,9 @@
1
1
  /* dialog-content.tsx generated by @compiled/babel-plugin v2.0.0 */
2
2
  import "./dialog-content.compiled.css";
3
3
  import { ax, ix } from "@compiled/react/runtime";
4
- import React, { forwardRef, useCallback, useEffect, useId, useLayoutEffect, useRef } from 'react';
4
+ import React, { forwardRef, useCallback, useId, useLayoutEffect, useRef } from 'react';
5
5
  import { cx } from '@compiled/react';
6
- import { bind } from 'bind-event-listener';
6
+ import { bindAll } from 'bind-event-listener';
7
7
  import mergeRefs from '@atlaskit/ds-lib/merge-refs';
8
8
  import noop from '@atlaskit/ds-lib/noop';
9
9
  import { useNotifyOpenLayerObserver } from '@atlaskit/layering/use-notify-open-layer-observer';
@@ -45,8 +45,8 @@ const backdropStyles = {
45
45
  *
46
46
  * Handles native `cancel` event (Escape) and backdrop click detection.
47
47
  *
48
- * Close flow: we never call `dialog.close()` from event handlers. We always call
49
- * `onClose`; the consumer decides whether to set `isOpen={false}`.
48
+ * Close flow: allowed user dismissal closes the native dialog before `onClose`
49
+ * notifies the consumer. The consumer must synchronize `isOpen` in response.
50
50
  *
51
51
  * Accessibility: render at least one focusable element (typically a close
52
52
  * button) when `isOpen` becomes `true`. Tab is always trapped inside the
@@ -56,6 +56,7 @@ export const Dialog = /*#__PURE__*/forwardRef(function Dialog({
56
56
  children,
57
57
  isOpen,
58
58
  onClose,
59
+ dismissedBy = 'escape-and-outside-click',
59
60
  onEnterFinish,
60
61
  onExitFinish,
61
62
  shouldAnimate = false,
@@ -73,8 +74,10 @@ export const Dialog = /*#__PURE__*/forwardRef(function Dialog({
73
74
  const dialogId = providedId !== null && providedId !== void 0 ? providedId : generatedId;
74
75
  const ownRef = useRef(null);
75
76
  const combinedRef = mergeRefs([ownRef, ref]);
77
+ const closeReasonRef = useRef(null);
76
78
  const {
77
- phase
79
+ phase,
80
+ onToggle
78
81
  } = useAnimatedVisibility({
79
82
  isOpen,
80
83
  animationKind: 'dialog',
@@ -113,6 +116,7 @@ export const Dialog = /*#__PURE__*/forwardRef(function Dialog({
113
116
  return;
114
117
  }
115
118
  if (isOpen) {
119
+ closeReasonRef.current = null;
116
120
  if (!dialog.open) {
117
121
  dialog.showModal();
118
122
  }
@@ -146,45 +150,67 @@ export const Dialog = /*#__PURE__*/forwardRef(function Dialog({
146
150
  if (event.target !== event.currentTarget) {
147
151
  return;
148
152
  }
149
- event.preventDefault();
153
+
150
154
  // Spurious Safari `cancel`: the keydown snapshot saw an open child
151
155
  // popover, so this Escape belongs to that popover (light-dismissed
152
156
  // natively). Keep the dialog open instead of forwarding to `onClose`.
153
157
  if (shouldIgnoreEscape()) {
158
+ event.preventDefault();
159
+ return;
160
+ }
161
+
162
+ // Without native `closedby`, cancel Escape only when dismissal is disabled.
163
+ // Allowed dismissal follows Popover behavior: the browser closes the element
164
+ // and `onClose` notifies the controlled owner of the reason.
165
+ if (dismissedBy === 'none') {
166
+ event.preventDefault();
154
167
  return;
155
168
  }
156
- onClose({
157
- reason: 'escape'
158
- });
159
- }, [onClose, shouldIgnoreEscape]);
169
+ closeReasonRef.current = 'escape';
170
+ }, [dismissedBy, shouldIgnoreEscape]);
160
171
 
161
- // Handle backdrop click
172
+ // Handle backdrop interaction
162
173
  // Attached via bind-event-listener rather than a React prop so we avoid
163
174
  // a11y lint suppressions on the <dialog> element.
164
- // Keyboard dismiss is already handled natively (Escape → onCancel above).
175
+ // The click listener provides light dismiss consistently across browsers.
165
176
  // `isVisible` is in deps so the listener re-binds to the new <dialog>
166
177
  // element after a host unmount / remount cycle.
167
- useEffect(() => {
178
+ useLayoutEffect(() => {
168
179
  const dialog = ownRef.current;
169
180
  if (!dialog) {
170
181
  return;
171
182
  }
172
- return bind(dialog, {
183
+ return bindAll(dialog, [{
173
184
  type: 'click',
174
185
  listener(event) {
175
- if (event.target === event.currentTarget) {
176
- onClose({
177
- reason: 'overlay-click'
178
- });
186
+ if (event.target === event.currentTarget && dismissedBy === 'escape-and-outside-click') {
187
+ closeReasonRef.current = 'overlay-click';
188
+ // Calls native close because this is emulating the native light-dismiss behavior
189
+ dialog.close();
179
190
  }
180
191
  }
181
- });
182
- }, [onClose, isVisible]);
192
+ }, {
193
+ type: 'toggle',
194
+ listener(event) {
195
+ if (event.newState === 'closed') {
196
+ const reason = closeReasonRef.current;
197
+ closeReasonRef.current = null;
198
+ if (reason) {
199
+ onClose({
200
+ reason
201
+ });
202
+ }
203
+ }
204
+
205
+ // Visibility cleanup runs after close-reason handling.
206
+ onToggle(event);
207
+ }
208
+ }]);
209
+ }, [dismissedBy, onClose, onToggle, isVisible]);
183
210
 
184
211
  // Unmount the `<dialog>` once exit completes so it does not leave an
185
212
  // empty `role="dialog"` element in the accessibility tree. On the next
186
- // open the element remounts and the `[isOpen]` and `[onClose, isVisible]`
187
- // effects re-run against the fresh element.
213
+ // open the element remounts and its effects run against the fresh element.
188
214
  if (!isVisible) {
189
215
  return null;
190
216
  }
@@ -8,11 +8,5 @@ export function isInSameLayer({
8
8
  element,
9
9
  container
10
10
  }) {
11
- const elementLayer = element.closest(topLayerSelector);
12
- const containerLayer = container.closest(topLayerSelector);
13
- // Both elements are in the "document" layer
14
- if (!elementLayer && !containerLayer) {
15
- return true;
16
- }
17
- return elementLayer === containerLayer;
11
+ return element.closest(topLayerSelector) === container.closest(topLayerSelector);
18
12
  }
@@ -2,7 +2,7 @@ export const selectors = (() => {
2
2
  // Common exclusion filter applied to every selector.
3
3
  const not = ':not([tabindex="-1"]):not([aria-disabled="true"]):not([aria-hidden="true"])';
4
4
  const notDisabled = `:not([disabled])${not}`;
5
- const focusable = [`a[href]${not}`, `area[href]${not}`, `input${notDisabled}:not([type="hidden"])`, `select${notDisabled}`, `textarea${notDisabled}`, `button${notDisabled}`, `iframe${not}`, `audio[controls]${not}`, `video[controls]${not}`, `[contenteditable]:not([contenteditable="false"])${not}`, `[tabindex]${notDisabled}${not}`];
5
+ const focusable = [`a[href]${not}`, `area[href]${not}`, `input${notDisabled}:not([type="hidden"])`, `select${notDisabled}`, `textarea${notDisabled}`, `button${notDisabled}`, `iframe${not}`, `audio[controls]${not}`, `video[controls]${not}`, `[contenteditable]:not([contenteditable="false"])${not}`, `[tabindex]${notDisabled}`];
6
6
  return {
7
7
  focusable: focusable.join(','),
8
8
  // Build the `:focus` selector
@@ -1,5 +1,3 @@
1
- import { roleToAriaHasPopup } from './role-to-aria-has-popup';
2
-
3
1
  /**
4
2
  * Roles accepted by `getAriaForTrigger`.
5
3
  *
@@ -9,12 +7,34 @@ import { roleToAriaHasPopup } from './role-to-aria-has-popup';
9
7
  * manually for tooltip triggers.
10
8
  */
11
9
 
10
+ /**
11
+ * Valid values for the `aria-haspopup` attribute.
12
+ *
13
+ * Derived from the HTML spec - maps popover roles to what the trigger
14
+ * announces. `undefined` means the attribute is omitted entirely (used for
15
+ * non-popup roles like `tooltip`/`status`/`alert`/`note`/`log` where
16
+ * `aria-haspopup` would be misleading).
17
+ *
18
+ * `true` is intentionally NOT in the union - the runtime only ever produces
19
+ * the explicit string forms, so widening the type would invite consumers to
20
+ * pass `true` and get an `aria-haspopup="true"` serialisation that the
21
+ * runtime no longer emits.
22
+ */
23
+
12
24
  /**
13
25
  * Non-undefined subset of `TAriaHasPopupValue`. Every role accepted by
14
26
  * `getAriaForTrigger` maps to a concrete `aria-haspopup` string, so the
15
27
  * trigger always receives a defined attribute value.
16
28
  */
17
29
 
30
+ const roleToAriaHasPopup = {
31
+ dialog: 'dialog',
32
+ alertdialog: 'dialog',
33
+ menu: 'menu',
34
+ listbox: 'listbox',
35
+ tree: 'tree',
36
+ grid: 'grid'
37
+ };
18
38
  /**
19
39
  * Returns the ARIA attributes to spread onto a click-activated popover trigger.
20
40
  *
@@ -67,13 +87,6 @@ export function getAriaForTrigger({
67
87
  isOpen,
68
88
  popoverId
69
89
  }) {
70
- // `roleToAriaHasPopup` accepts a wider role union (including tooltip-family
71
- // roles that map to `undefined`). `TAriaForTriggerRole` excludes those, so
72
- // the returned value is guaranteed to be a defined string. The cast narrows
73
- // the wider helper signature to match.
74
- const ariaHasPopup = roleToAriaHasPopup({
75
- role
76
- });
77
90
  // `aria-controls` is set to `undefined` while closed. The `Popover` /
78
91
  // `Dialog` primitives unmount their host element after the exit
79
92
  // animation finishes, so a closed-state `aria-controls` would point
@@ -82,7 +95,7 @@ export function getAriaForTrigger({
82
95
  // until the target exists, avoiding the dangling reference while
83
96
  // keeping the relationship live whenever it is meaningful.
84
97
  return {
85
- 'aria-haspopup': ariaHasPopup,
98
+ 'aria-haspopup': roleToAriaHasPopup[role],
86
99
  'aria-expanded': isOpen,
87
100
  'aria-controls': isOpen ? popoverId : undefined
88
101
  };
@@ -1,59 +0,0 @@
1
- /**
2
- * Valid values for the `aria-haspopup` attribute.
3
- *
4
- * Derived from the HTML spec - maps popover roles to what the trigger
5
- * announces. `undefined` means the attribute is omitted entirely (used for
6
- * non-popup roles like `tooltip`/`status`/`alert`/`note`/`log` where
7
- * `aria-haspopup` would be misleading).
8
- *
9
- * `true` is intentionally NOT in the union - the runtime only ever produces
10
- * the explicit string forms, so widening the type would invite consumers to
11
- * pass `true` and get an `aria-haspopup="true"` serialisation that the
12
- * runtime no longer emits.
13
- */
14
-
15
- /**
16
- * Roles that require an accessible name (`label` or `labelledBy`) per WCAG 4.1.2.
17
- * Without a name, assistive technology cannot identify these landmarks.
18
- */
19
-
20
- /**
21
- * Roles where the accessible name is derived from content or provided externally
22
- * (e.g. a combobox labels its listbox, tooltip content IS the accessible name).
23
- */
24
-
25
- // Shared ARIA role discrimination types
26
- //
27
- // These encode the WCAG 4.1.2 constraint: roles like `dialog`, `alertdialog`,
28
- // and `menu` MUST have an accessible name (either `label` or `labelledBy`).
29
- // TypeScript enforces this at compile time via discriminated unions.
30
-
31
- /**
32
- * ARIA role props where a role is always required.
33
- *
34
- * Used by `TPopupContentProps`. Popup content must always declare
35
- * its role so assistive technology can identify the layer.
36
- */
37
-
38
- /**
39
- * ARIA role props where a role is optional.
40
- *
41
- * Used by `TPopoverProps`. The low-level primitive allows omitting
42
- * the role for cases where it is set externally or not needed.
43
- */
44
-
45
- const rolesThatMoveFocus = new Set(['dialog', 'alertdialog', 'menu', 'listbox', 'tree', 'grid']);
46
-
47
- /**
48
- * Returns `true` when the given popover role moves focus into the popover
49
- * on open, and `false` otherwise (including when `role` is `undefined`).
50
- */
51
- // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
52
- export function shouldFocusIntoPopover({
53
- role
54
- }) {
55
- if (!role) {
56
- return false;
57
- }
58
- return rolesThatMoveFocus.has(role);
59
- }
@@ -307,11 +307,10 @@ export function useAnchorPosition({
307
307
  * See `notes/decisions/anchor-name-lifetime.md`
308
308
  */
309
309
  trigger.style.setProperty('anchor-name', anchorName);
310
- const undoPositioning = combine(setStyle({
310
+ return setStyle({
311
311
  element: popover,
312
312
  styles: popoverStyles
313
- }));
314
- return undoPositioning;
313
+ });
315
314
  }
316
315
 
317
316
  // JS fallback. The popover is already in the top layer via
@@ -1,5 +1,5 @@
1
- import { useEffect, useLayoutEffect, useRef, useState } from 'react';
2
- import { bind, bindAll } from 'bind-event-listener';
1
+ import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
2
+ import { bind } from 'bind-event-listener';
3
3
  import { prefersReducedMotion } from './reduced-motion';
4
4
  /**
5
5
  * Using the recommended motion timings for safety net.
@@ -110,7 +110,7 @@ function runOnTransitionEndOrTimeout({
110
110
  * `entering`, `open`, `exiting`). The host element is rendered while
111
111
  * `phase !== 'closed'`, so the `exiting` phase keeps the host mounted
112
112
  * while the CSS exit transition plays (or, with animation disabled,
113
- * while we wait for the browser `toggle` / `close` event to fire on the
113
+ * while we wait for the browser's closing `toggle` event to fire on the
114
114
  * still-attached element):
115
115
  *
116
116
  * ```
@@ -130,6 +130,7 @@ export function useAnimatedVisibility({
130
130
  onExitFinish
131
131
  }) {
132
132
  const willAnimate = shouldAnimate && !prefersReducedMotion();
133
+ const isNativeClosedRef = useRef(false);
133
134
 
134
135
  // Promote `closed → entering`/`closed → open` synchronously during
135
136
  // render so the host element mounts in the same commit as the open
@@ -140,16 +141,23 @@ export function useAnimatedVisibility({
140
141
  }
141
142
  return willAnimate ? 'entering' : 'open';
142
143
  });
143
- if (isOpen && phase === 'closed') {
144
+
145
+ // Rearm only after controlled state and the native lifecycle have both
146
+ // reached closed. Resetting as soon as `isOpen` becomes false can erase a
147
+ // closing toggle that was handled in the same React batch.
148
+ if (!isOpen && phase === 'closed') {
149
+ isNativeClosedRef.current = false;
150
+ }
151
+ if (isOpen && phase === 'closed' && !isNativeClosedRef.current) {
144
152
  setPhase(willAnimate ? 'entering' : 'open');
145
153
  }
146
154
  if (!isOpen && (phase === 'open' || phase === 'entering')) {
147
155
  // Close intent. Move to `exiting` to gate the unmount on either
148
- // the exit transition (animated) or the toggle/close event
156
+ // the exit transition (animated) or the closing toggle event
149
157
  // (non-animated). Includes interrupted entry (`entering → exiting`).
150
158
  setPhase('exiting');
151
159
  }
152
- if (isOpen && phase === 'exiting') {
160
+ if (isOpen && phase === 'exiting' && !isNativeClosedRef.current) {
153
161
  // Reopen mid-exit. With animation, jump back to `entering` and let
154
162
  // the entry transition restart from current visual state. Without
155
163
  // animation, jump straight to `open` (the handshake we were waiting
@@ -234,47 +242,37 @@ export function useAnimatedVisibility({
234
242
  });
235
243
  }, [phase, willAnimate, animationKind, elementRef]);
236
244
 
237
- // Non-animated exit: wait for the browser's `toggle` (closed) or
238
- // `close` event before unmounting, so the consumer's listener (close
239
- // reason, nested focus restoration, `onClose`) and the browser's
240
- // native focus restoration both run against the still-attached
241
- // element. A naive `setTimeout(0)` occasionally beats the `toggle`
242
- // task in Chromium and drops focus restoration.
243
- //
244
- // `useLayoutEffect` so the listener binds in the same commit as the
245
- // show/hide layout effect that calls `hidePopover()` / `dialog.close()`.
246
- // The toggle/close event is queued as a task and cannot run until the
247
- // frame completes. SSR-safe: `phase` starts at `'closed'`.
248
- //
249
- // `onExitFinish` is fired separately (see effect below) so consumers
250
- // observe it the moment `isOpen` flips to `false`.
245
+ // Native dismissal is authoritative. Its toggle can arrive while controlled
246
+ // state still says open, so remember that native state and do not
247
+ // reopen until `isOpen` has first moved to false.
251
248
  useLayoutEffect(() => {
252
- if (phase !== 'exiting' || willAnimate) {
253
- return;
249
+ if (phase === 'exiting' && !willAnimate && isNativeClosedRef.current) {
250
+ setPhase('closed');
254
251
  }
255
- const element = elementRef.current;
256
- if (!element) {
252
+ }, [phase, willAnimate]);
253
+ const onToggle = useCallback(event => {
254
+ if (event.newState === 'open') {
255
+ isNativeClosedRef.current = false;
257
256
  return;
258
257
  }
259
- function unmount() {
260
- setPhase(current => current === 'exiting' ? 'closed' : current);
258
+ if (event.newState !== 'closed') {
259
+ return;
261
260
  }
262
- return bindAll(element, [{
263
- type: 'toggle',
264
- listener: event => {
265
- if (event.newState === 'closed') {
266
- unmount();
267
- }
261
+ isNativeClosedRef.current = true;
262
+ setPhase(current => {
263
+ if (current === 'open' || current === 'entering') {
264
+ return 'exiting';
268
265
  }
269
- }, {
270
- type: 'close',
271
- listener: unmount
272
- }]);
273
- }, [phase, willAnimate, elementRef]);
266
+ if (current === 'exiting' && !willAnimate) {
267
+ return 'closed';
268
+ }
269
+ return current;
270
+ });
271
+ }, [willAnimate]);
274
272
 
275
273
  // Non-animated exit callback. No `transitionend` to wait for, so fire
276
274
  // `onExitFinish` on the transition into `exiting` (the unmount itself
277
- // is gated by the toggle/close handshake above).
275
+ // is gated by the closing toggle handshake above).
278
276
  const prevPhaseForExitRef = useRef('closed');
279
277
  useEffect(() => {
280
278
  const prevPhase = prevPhaseForExitRef.current;
@@ -289,6 +287,7 @@ export function useAnimatedVisibility({
289
287
  }
290
288
  }, [phase, willAnimate]);
291
289
  return {
292
- phase
290
+ phase,
291
+ onToggle
293
292
  };
294
293
  }
@@ -8,7 +8,7 @@ import mergeRefs from '@atlaskit/ds-lib/merge-refs';
8
8
  import once from '@atlaskit/ds-lib/once';
9
9
  import { useNotifyOpenLayerObserver } from '@atlaskit/layering/use-notify-open-layer-observer';
10
10
  import { resolvePlacement } from '../internal/resolve-placement';
11
- import { shouldFocusIntoPopover } from '../internal/role-types';
11
+ import { shouldFocusIntoPopover } from '../internal/should-focus-into-popover';
12
12
  import { useAnimatedVisibility } from '../internal/use-animated-visibility';
13
13
  import { useFocusWrap } from '../internal/use-focus-wrap';
14
14
  import { useInitialFocus } from '../internal/use-initial-focus';
@@ -88,11 +88,7 @@ const POPUP_ROLES = new Set(['menu', 'listbox', 'dialog', 'alertdialog', 'tree',
88
88
  * ### 📜 Browser dismiss is non-cancellable
89
89
  *
90
90
  * Per the popover spec, `beforetoggle` is cancellable on open transitions
91
- * only, not on close. Consumers that need to ignore a specific dismiss
92
- * (e.g. a click on a logically-associated external trigger) must reconcile
93
- * inside their own `onClose` handler by calling `showPopover()` again.
94
- * `Popover` does not self-heal so consumers do not pay a reconcile cost
95
- * by default.
91
+ * only, not on close.
96
92
  *
97
93
  * - Spec: <https://html.spec.whatwg.org/multipage/popover.html#the-popover-attribute>
98
94
  * - <https://github.com/whatwg/html/issues/8973>
@@ -132,7 +128,8 @@ export const Popover = /*#__PURE__*/forwardRef(function Popover({
132
128
  // `useId()` colons are invalid in CSS selectors and popover target attributes.
133
129
  const popoverId = idProp !== null && idProp !== void 0 ? idProp : `popover-${autoId.replace(/:/g, '')}`;
134
130
  const {
135
- phase
131
+ phase,
132
+ onToggle
136
133
  } = useAnimatedVisibility({
137
134
  isOpen,
138
135
  animationKind: 'popover',
@@ -214,6 +211,7 @@ export const Popover = /*#__PURE__*/forwardRef(function Popover({
214
211
  const handleToggle = useCallback(event => {
215
212
  const element = ownRef.current;
216
213
  if (event.newState === 'open') {
214
+ onToggle(event);
217
215
  return;
218
216
  }
219
217
  if (event.newState === 'closed') {
@@ -243,18 +241,18 @@ export const Popover = /*#__PURE__*/forwardRef(function Popover({
243
241
  // Reset reason so a stale 'escape' from a race with Escape keydown
244
242
  // does not corrupt the next browser-dismiss cycle.
245
243
  closeReasonRef.current = 'light-dismiss';
246
- return;
244
+ } else {
245
+ // Browser dismiss (Escape/click-outside).
246
+ const reason = closeReasonRef.current;
247
+ closeReasonRef.current = 'light-dismiss';
248
+ // Optional: `manual` mode has no `onClose` in the forwarded type.
249
+ onClose === null || onClose === void 0 ? void 0 : onClose({
250
+ reason
251
+ });
247
252
  }
248
-
249
- // Browser dismiss (Escape/click-outside).
250
- const reason = closeReasonRef.current;
251
- closeReasonRef.current = 'light-dismiss';
252
- // Optional: `manual` mode has no `onClose` in the forwarded type.
253
- onClose === null || onClose === void 0 ? void 0 : onClose({
254
- reason
255
- });
256
253
  }
257
- }, [onClose, role]);
254
+ onToggle(event);
255
+ }, [onClose, onToggle, role]);
258
256
 
259
257
  // Bind via `useLayoutEffect`, and BEFORE the show/hide layout effect
260
258
  // below, so listeners are attached when `showPopover()` synchronously
@@ -1,9 +1,9 @@
1
1
  /* dialog-content.tsx generated by @compiled/babel-plugin v2.0.0 */
2
2
  import "./dialog-content.compiled.css";
3
3
  import { ax, ix } from "@compiled/react/runtime";
4
- import React, { forwardRef, useCallback, useEffect, useId, useLayoutEffect, useRef } from 'react';
4
+ import React, { forwardRef, useCallback, useId, useLayoutEffect, useRef } from 'react';
5
5
  import { cx } from '@compiled/react';
6
- import { bind } from 'bind-event-listener';
6
+ import { bindAll } from 'bind-event-listener';
7
7
  import mergeRefs from '@atlaskit/ds-lib/merge-refs';
8
8
  import noop from '@atlaskit/ds-lib/noop';
9
9
  import { useNotifyOpenLayerObserver } from '@atlaskit/layering/use-notify-open-layer-observer';
@@ -45,8 +45,8 @@ var backdropStyles = {
45
45
  *
46
46
  * Handles native `cancel` event (Escape) and backdrop click detection.
47
47
  *
48
- * Close flow: we never call `dialog.close()` from event handlers. We always call
49
- * `onClose`; the consumer decides whether to set `isOpen={false}`.
48
+ * Close flow: allowed user dismissal closes the native dialog before `onClose`
49
+ * notifies the consumer. The consumer must synchronize `isOpen` in response.
50
50
  *
51
51
  * Accessibility: render at least one focusable element (typically a close
52
52
  * button) when `isOpen` becomes `true`. Tab is always trapped inside the
@@ -56,6 +56,8 @@ export var Dialog = /*#__PURE__*/forwardRef(function Dialog(_ref, ref) {
56
56
  var children = _ref.children,
57
57
  isOpen = _ref.isOpen,
58
58
  onClose = _ref.onClose,
59
+ _ref$dismissedBy = _ref.dismissedBy,
60
+ dismissedBy = _ref$dismissedBy === void 0 ? 'escape-and-outside-click' : _ref$dismissedBy,
59
61
  onEnterFinish = _ref.onEnterFinish,
60
62
  onExitFinish = _ref.onExitFinish,
61
63
  _ref$shouldAnimate = _ref.shouldAnimate,
@@ -73,6 +75,7 @@ export var Dialog = /*#__PURE__*/forwardRef(function Dialog(_ref, ref) {
73
75
  var dialogId = providedId !== null && providedId !== void 0 ? providedId : generatedId;
74
76
  var ownRef = useRef(null);
75
77
  var combinedRef = mergeRefs([ownRef, ref]);
78
+ var closeReasonRef = useRef(null);
76
79
  var _useAnimatedVisibilit = useAnimatedVisibility({
77
80
  isOpen: isOpen,
78
81
  animationKind: 'dialog',
@@ -81,7 +84,8 @@ export var Dialog = /*#__PURE__*/forwardRef(function Dialog(_ref, ref) {
81
84
  onEnterFinish: onEnterFinish,
82
85
  onExitFinish: onExitFinish
83
86
  }),
84
- phase = _useAnimatedVisibilit.phase;
87
+ phase = _useAnimatedVisibilit.phase,
88
+ onToggle = _useAnimatedVisibilit.onToggle;
85
89
 
86
90
  // True while the host element is mounted (any phase except `closed`).
87
91
  // Used as a dep so listener-rebind effects re-attach after a remount.
@@ -112,6 +116,7 @@ export var Dialog = /*#__PURE__*/forwardRef(function Dialog(_ref, ref) {
112
116
  return;
113
117
  }
114
118
  if (isOpen) {
119
+ closeReasonRef.current = null;
115
120
  if (!dialog.open) {
116
121
  dialog.showModal();
117
122
  }
@@ -144,45 +149,67 @@ export var Dialog = /*#__PURE__*/forwardRef(function Dialog(_ref, ref) {
144
149
  if (event.target !== event.currentTarget) {
145
150
  return;
146
151
  }
147
- event.preventDefault();
152
+
148
153
  // Spurious Safari `cancel`: the keydown snapshot saw an open child
149
154
  // popover, so this Escape belongs to that popover (light-dismissed
150
155
  // natively). Keep the dialog open instead of forwarding to `onClose`.
151
156
  if (shouldIgnoreEscape()) {
157
+ event.preventDefault();
152
158
  return;
153
159
  }
154
- onClose({
155
- reason: 'escape'
156
- });
157
- }, [onClose, shouldIgnoreEscape]);
158
160
 
159
- // Handle backdrop click
161
+ // Without native `closedby`, cancel Escape only when dismissal is disabled.
162
+ // Allowed dismissal follows Popover behavior: the browser closes the element
163
+ // and `onClose` notifies the controlled owner of the reason.
164
+ if (dismissedBy === 'none') {
165
+ event.preventDefault();
166
+ return;
167
+ }
168
+ closeReasonRef.current = 'escape';
169
+ }, [dismissedBy, shouldIgnoreEscape]);
170
+
171
+ // Handle backdrop interaction
160
172
  // Attached via bind-event-listener rather than a React prop so we avoid
161
173
  // a11y lint suppressions on the <dialog> element.
162
- // Keyboard dismiss is already handled natively (Escape → onCancel above).
174
+ // The click listener provides light dismiss consistently across browsers.
163
175
  // `isVisible` is in deps so the listener re-binds to the new <dialog>
164
176
  // element after a host unmount / remount cycle.
165
- useEffect(function () {
177
+ useLayoutEffect(function () {
166
178
  var dialog = ownRef.current;
167
179
  if (!dialog) {
168
180
  return;
169
181
  }
170
- return bind(dialog, {
182
+ return bindAll(dialog, [{
171
183
  type: 'click',
172
184
  listener: function listener(event) {
173
- if (event.target === event.currentTarget) {
174
- onClose({
175
- reason: 'overlay-click'
176
- });
185
+ if (event.target === event.currentTarget && dismissedBy === 'escape-and-outside-click') {
186
+ closeReasonRef.current = 'overlay-click';
187
+ // Calls native close because this is emulating the native light-dismiss behavior
188
+ dialog.close();
177
189
  }
178
190
  }
179
- });
180
- }, [onClose, isVisible]);
191
+ }, {
192
+ type: 'toggle',
193
+ listener: function listener(event) {
194
+ if (event.newState === 'closed') {
195
+ var reason = closeReasonRef.current;
196
+ closeReasonRef.current = null;
197
+ if (reason) {
198
+ onClose({
199
+ reason: reason
200
+ });
201
+ }
202
+ }
203
+
204
+ // Visibility cleanup runs after close-reason handling.
205
+ onToggle(event);
206
+ }
207
+ }]);
208
+ }, [dismissedBy, onClose, onToggle, isVisible]);
181
209
 
182
210
  // Unmount the `<dialog>` once exit completes so it does not leave an
183
211
  // empty `role="dialog"` element in the accessibility tree. On the next
184
- // open the element remounts and the `[isOpen]` and `[onClose, isVisible]`
185
- // effects re-run against the fresh element.
212
+ // open the element remounts and its effects run against the fresh element.
186
213
  if (!isVisible) {
187
214
  return null;
188
215
  }
@@ -7,11 +7,5 @@ var topLayerSelector = '[popover], dialog, [role="dialog"], [role="alertdialog"]
7
7
  export function isInSameLayer(_ref) {
8
8
  var element = _ref.element,
9
9
  container = _ref.container;
10
- var elementLayer = element.closest(topLayerSelector);
11
- var containerLayer = container.closest(topLayerSelector);
12
- // Both elements are in the "document" layer
13
- if (!elementLayer && !containerLayer) {
14
- return true;
15
- }
16
- return elementLayer === containerLayer;
10
+ return element.closest(topLayerSelector) === container.closest(topLayerSelector);
17
11
  }
@@ -2,7 +2,7 @@ export var selectors = function () {
2
2
  // Common exclusion filter applied to every selector.
3
3
  var not = ':not([tabindex="-1"]):not([aria-disabled="true"]):not([aria-hidden="true"])';
4
4
  var notDisabled = ":not([disabled])".concat(not);
5
- var focusable = ["a[href]".concat(not), "area[href]".concat(not), "input".concat(notDisabled, ":not([type=\"hidden\"])"), "select".concat(notDisabled), "textarea".concat(notDisabled), "button".concat(notDisabled), "iframe".concat(not), "audio[controls]".concat(not), "video[controls]".concat(not), "[contenteditable]:not([contenteditable=\"false\"])".concat(not), "[tabindex]".concat(notDisabled).concat(not)];
5
+ var focusable = ["a[href]".concat(not), "area[href]".concat(not), "input".concat(notDisabled, ":not([type=\"hidden\"])"), "select".concat(notDisabled), "textarea".concat(notDisabled), "button".concat(notDisabled), "iframe".concat(not), "audio[controls]".concat(not), "video[controls]".concat(not), "[contenteditable]:not([contenteditable=\"false\"])".concat(not), "[tabindex]".concat(notDisabled)];
6
6
  return {
7
7
  focusable: focusable.join(','),
8
8
  // Build the `:focus` selector