@atlaskit/top-layer 4.4.5 → 5.0.1

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 (137) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +13 -4
  3. package/dist/cjs/dialog/dialog-content.js +2 -0
  4. package/dist/cjs/entry-points/use-popover-id.js +5 -10
  5. package/dist/cjs/internal/anchor-positioning/apply-anchor-positioning.js +86 -0
  6. package/dist/cjs/internal/anchor-positioning/cross-axis-shift-margins.js +34 -0
  7. package/dist/cjs/internal/anchor-positioning/edge-margin.js +21 -0
  8. package/dist/cjs/internal/anchor-positioning/fit-margins.js +77 -0
  9. package/dist/cjs/internal/anchor-positioning/get-existing-anchor-name.js +18 -0
  10. package/dist/cjs/internal/{placement-to-position-area.js → anchor-positioning/placement-to-position-area.js} +11 -11
  11. package/dist/cjs/internal/anchor-positioning/placement-to-try-fallbacks.js +95 -0
  12. package/dist/cjs/internal/anchored-popover-size.js +345 -0
  13. package/dist/cjs/internal/get-aria-for-trigger.js +7 -13
  14. package/dist/cjs/internal/is-native-element-open.js +19 -0
  15. package/dist/cjs/internal/{anchor-positioning-fallback.js → javascript-fallback/anchor-positioning-fallback.js} +12 -33
  16. package/dist/cjs/internal/javascript-fallback/apply-javascript-fallback-positioning.js +297 -0
  17. package/dist/cjs/internal/javascript-fallback/resolve-css-length-to-pixels.js +90 -0
  18. package/dist/cjs/internal/javascript-fallback/warn-fallback-failure.js +19 -0
  19. package/dist/cjs/internal/resolve-placement.js +14 -6
  20. package/dist/cjs/internal/supports-anchor-positioning.js +17 -0
  21. package/dist/cjs/internal/supports-anchor-size.js +17 -0
  22. package/dist/cjs/internal/supports-css-declaration.js +25 -0
  23. package/dist/cjs/internal/surface-reset.js +5 -0
  24. package/dist/cjs/internal/use-anchored-popover-at-point.js +124 -0
  25. package/dist/cjs/internal/use-anchored-popover.js +277 -0
  26. package/dist/cjs/internal/use-animated-visibility.js +11 -23
  27. package/dist/cjs/popover/popover.compiled.css +4 -0
  28. package/dist/cjs/popover/popover.js +63 -61
  29. package/dist/cjs/testing/polyfill.js +6 -7
  30. package/dist/es2019/dialog/dialog-content.js +2 -0
  31. package/dist/es2019/entry-points/use-popover-id.js +6 -10
  32. package/dist/es2019/internal/anchor-positioning/apply-anchor-positioning.js +83 -0
  33. package/dist/es2019/internal/anchor-positioning/cross-axis-shift-margins.js +29 -0
  34. package/dist/es2019/internal/anchor-positioning/edge-margin.js +18 -0
  35. package/dist/es2019/internal/anchor-positioning/fit-margins.js +69 -0
  36. package/dist/es2019/internal/anchor-positioning/get-existing-anchor-name.js +12 -0
  37. package/dist/es2019/internal/{placement-to-position-area.js → anchor-positioning/placement-to-position-area.js} +9 -9
  38. package/dist/es2019/internal/anchor-positioning/placement-to-try-fallbacks.js +94 -0
  39. package/dist/es2019/internal/anchored-popover-size.js +343 -0
  40. package/dist/es2019/internal/get-aria-for-trigger.js +7 -13
  41. package/dist/es2019/internal/is-native-element-open.js +14 -0
  42. package/dist/es2019/internal/{anchor-positioning-fallback.js → javascript-fallback/anchor-positioning-fallback.js} +12 -33
  43. package/dist/es2019/internal/javascript-fallback/apply-javascript-fallback-positioning.js +291 -0
  44. package/dist/es2019/internal/javascript-fallback/resolve-css-length-to-pixels.js +87 -0
  45. package/dist/es2019/internal/javascript-fallback/warn-fallback-failure.js +14 -0
  46. package/dist/es2019/internal/resolve-placement.js +14 -6
  47. package/dist/es2019/internal/supports-anchor-positioning.js +12 -0
  48. package/dist/es2019/internal/supports-anchor-size.js +12 -0
  49. package/dist/es2019/internal/supports-css-declaration.js +20 -0
  50. package/dist/es2019/internal/surface-reset.js +1 -0
  51. package/dist/es2019/internal/use-anchored-popover-at-point.js +116 -0
  52. package/dist/es2019/internal/use-anchored-popover.js +265 -0
  53. package/dist/es2019/internal/use-animated-visibility.js +1 -14
  54. package/dist/es2019/popover/popover.compiled.css +4 -0
  55. package/dist/es2019/popover/popover.js +69 -61
  56. package/dist/es2019/testing/polyfill.js +6 -7
  57. package/dist/esm/dialog/dialog-content.js +2 -0
  58. package/dist/esm/entry-points/use-popover-id.js +6 -10
  59. package/dist/esm/internal/anchor-positioning/apply-anchor-positioning.js +80 -0
  60. package/dist/esm/internal/anchor-positioning/cross-axis-shift-margins.js +28 -0
  61. package/dist/esm/internal/anchor-positioning/edge-margin.js +15 -0
  62. package/dist/esm/internal/anchor-positioning/fit-margins.js +71 -0
  63. package/dist/esm/internal/anchor-positioning/get-existing-anchor-name.js +12 -0
  64. package/dist/esm/internal/{placement-to-position-area.js → anchor-positioning/placement-to-position-area.js} +11 -12
  65. package/dist/esm/internal/anchor-positioning/placement-to-try-fallbacks.js +89 -0
  66. package/dist/esm/internal/anchored-popover-size.js +338 -0
  67. package/dist/esm/internal/get-aria-for-trigger.js +7 -13
  68. package/dist/esm/internal/is-native-element-open.js +13 -0
  69. package/dist/esm/internal/{anchor-positioning-fallback.js → javascript-fallback/anchor-positioning-fallback.js} +12 -33
  70. package/dist/esm/internal/javascript-fallback/apply-javascript-fallback-positioning.js +290 -0
  71. package/dist/esm/internal/javascript-fallback/resolve-css-length-to-pixels.js +85 -0
  72. package/dist/esm/internal/javascript-fallback/warn-fallback-failure.js +13 -0
  73. package/dist/esm/internal/resolve-placement.js +14 -6
  74. package/dist/esm/internal/supports-anchor-positioning.js +12 -0
  75. package/dist/esm/internal/supports-anchor-size.js +12 -0
  76. package/dist/esm/internal/supports-css-declaration.js +19 -0
  77. package/dist/esm/internal/surface-reset.js +1 -0
  78. package/dist/esm/internal/use-anchored-popover-at-point.js +118 -0
  79. package/dist/esm/internal/use-anchored-popover.js +271 -0
  80. package/dist/esm/internal/use-animated-visibility.js +10 -22
  81. package/dist/esm/popover/popover.compiled.css +4 -0
  82. package/dist/esm/popover/popover.js +63 -61
  83. package/dist/esm/testing/polyfill.js +6 -7
  84. package/dist/types/entry-points/placement-map.d.ts +2 -2
  85. package/dist/types/entry-points/use-popover-id.d.ts +3 -7
  86. package/dist/types/internal/anchor-positioning/apply-anchor-positioning.d.ts +26 -0
  87. package/dist/types/internal/anchor-positioning/cross-axis-shift-margins.d.ts +19 -0
  88. package/dist/types/internal/anchor-positioning/edge-margin.d.ts +10 -0
  89. package/dist/types/internal/anchor-positioning/fit-margins.d.ts +30 -0
  90. package/dist/types/internal/anchor-positioning/get-existing-anchor-name.d.ts +8 -0
  91. package/dist/types/internal/{placement-to-position-area.d.ts → anchor-positioning/placement-to-position-area.d.ts} +10 -6
  92. package/dist/types/internal/anchor-positioning/placement-to-try-fallbacks.d.ts +30 -0
  93. package/dist/types/internal/anchored-popover-size.d.ts +126 -0
  94. package/dist/types/internal/get-aria-for-trigger.d.ts +13 -25
  95. package/dist/types/internal/is-native-element-open.d.ts +3 -0
  96. package/dist/types/internal/javascript-fallback/anchor-positioning-fallback.d.ts +36 -0
  97. package/dist/types/internal/javascript-fallback/apply-javascript-fallback-positioning.d.ts +15 -0
  98. package/dist/types/internal/javascript-fallback/resolve-css-length-to-pixels.d.ts +20 -0
  99. package/dist/types/internal/javascript-fallback/warn-fallback-failure.d.ts +7 -0
  100. package/dist/types/internal/resolve-placement.d.ts +18 -2
  101. package/dist/types/internal/supports-anchor-positioning.d.ts +5 -0
  102. package/dist/types/internal/supports-anchor-size.d.ts +5 -0
  103. package/dist/types/internal/supports-css-declaration.d.ts +11 -0
  104. package/dist/types/internal/surface-reset.d.ts +25 -0
  105. package/dist/types/internal/use-anchored-popover-at-point.d.ts +48 -0
  106. package/dist/types/internal/use-anchored-popover.d.ts +143 -0
  107. package/dist/types/popover/index.d.ts +2 -1
  108. package/dist/types/popover/popover.d.ts +1 -1
  109. package/dist/types/popover/types.d.ts +15 -17
  110. package/dist/types/testing/polyfill.d.ts +3 -3
  111. package/package.json +2 -2
  112. package/use-anchored-popover/package.json +10 -0
  113. package/use-anchored-popover-at-point/package.json +10 -0
  114. package/dist/cjs/internal/placement-to-try-fallbacks.js +0 -108
  115. package/dist/cjs/internal/resolve-css-length-to-pixels.js +0 -104
  116. package/dist/cjs/internal/use-anchor-position-at-point.js +0 -129
  117. package/dist/cjs/internal/use-anchor-position.js +0 -501
  118. package/dist/cjs/internal/use-width-from-anchor.js +0 -124
  119. package/dist/es2019/internal/placement-to-try-fallbacks.js +0 -108
  120. package/dist/es2019/internal/resolve-css-length-to-pixels.js +0 -101
  121. package/dist/es2019/internal/use-anchor-position-at-point.js +0 -124
  122. package/dist/es2019/internal/use-anchor-position.js +0 -480
  123. package/dist/es2019/internal/use-width-from-anchor.js +0 -120
  124. package/dist/esm/internal/placement-to-try-fallbacks.js +0 -103
  125. package/dist/esm/internal/resolve-css-length-to-pixels.js +0 -98
  126. package/dist/esm/internal/use-anchor-position-at-point.js +0 -124
  127. package/dist/esm/internal/use-anchor-position.js +0 -494
  128. package/dist/esm/internal/use-width-from-anchor.js +0 -118
  129. package/dist/types/internal/anchor-positioning-fallback.d.ts +0 -51
  130. package/dist/types/internal/placement-to-try-fallbacks.d.ts +0 -13
  131. package/dist/types/internal/resolve-css-length-to-pixels.d.ts +0 -20
  132. package/dist/types/internal/use-anchor-position-at-point.d.ts +0 -67
  133. package/dist/types/internal/use-anchor-position.d.ts +0 -87
  134. package/dist/types/internal/use-width-from-anchor.d.ts +0 -19
  135. package/use-anchor-position/package.json +0 -10
  136. package/use-anchor-position-at-point/package.json +0 -10
  137. package/use-width-from-anchor/package.json +0 -10
@@ -0,0 +1,277 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useAnchoredPopover = useAnchoredPopover;
7
+ var _react = require("react");
8
+ var _useId = require("@atlaskit/ds-lib/use-id");
9
+ var _applyAnchorPositioning = require("./anchor-positioning/apply-anchor-positioning");
10
+ var _anchoredPopoverSize = require("./anchored-popover-size");
11
+ var _applyJavascriptFallbackPositioning = require("./javascript-fallback/apply-javascript-fallback-positioning");
12
+ var _resolvePlacement = require("./resolve-placement");
13
+ var _supportsAnchorPositioning = require("./supports-anchor-positioning");
14
+ var _supportsAnchorSize = require("./supports-anchor-size");
15
+ /**
16
+ * Positioning AND anchor-relative sizing for top-layer popovers. Anchors an
17
+ * ELEMENT; to anchor a viewport coordinate use `useAnchoredPopoverAtPoint`.
18
+ *
19
+ * Two runtime paths, each a function that applies its styles and returns the
20
+ * cleanup: CSS Anchor Positioning in `./anchor-positioning`, and the JavaScript
21
+ * fallback in `./javascript-fallback`. The path is resolved ONCE, at the top of
22
+ * the hook, and threaded through positioning, sizing and margins.
23
+ *
24
+ * See `notes/architecture/positioning.md` and
25
+ * `notes/decisions/fit-available-space.md`.
26
+ */
27
+
28
+ /**
29
+ * Which way the popover's inline axis runs. Maps every physical measurement below
30
+ * onto a logical axis: the viewport unit of each cap and the anchor's measured
31
+ * size. A style read, not a layout read, so it is cheap enough to take for every
32
+ * popover.
33
+ */
34
+ function getPopoverWritingMode(_ref) {
35
+ var popover = _ref.popover;
36
+ // `String()` because not every test environment implements `writing-mode`.
37
+ var writingMode = String(getComputedStyle(popover).writingMode);
38
+ var isVertical = writingMode.startsWith('vertical') || writingMode.startsWith('sideways');
39
+ return isVertical ? 'vertical' : 'horizontal';
40
+ }
41
+
42
+ /**
43
+ * Measured stand-in for `anchor-size(self-*)`, used wherever `anchor-size()` is
44
+ * not: the JavaScript fallback, and browsers with anchor positioning but no
45
+ * `anchor-size()`.
46
+ *
47
+ * `offsetWidth` / `offsetHeight` are physical, so the axis is picked from the
48
+ * POPOVER's `writing-mode`. Deliberately not memoized: a cached anchor size is a
49
+ * stale one. Runs once per effect, not per frame.
50
+ */
51
+ function measureAnchorSize(_ref2) {
52
+ var anchor = _ref2.anchor,
53
+ writingMode = _ref2.writingMode;
54
+ if (writingMode === 'vertical') {
55
+ return {
56
+ inline: "".concat(anchor.offsetHeight, "px"),
57
+ block: "".concat(anchor.offsetWidth, "px")
58
+ };
59
+ }
60
+ return {
61
+ inline: "".concat(anchor.offsetWidth, "px"),
62
+ block: "".concat(anchor.offsetHeight, "px")
63
+ };
64
+ }
65
+ var NO_ANCHOR_SIZE = {
66
+ inline: null,
67
+ block: null
68
+ };
69
+ var CSS_ANCHOR_SIZE = {
70
+ inline: 'anchor-size(self-inline)',
71
+ block: 'anchor-size(self-block)'
72
+ };
73
+ function isAnchorRelative(size) {
74
+ return size === 'match-anchor' || size === 'min-anchor';
75
+ }
76
+
77
+ /**
78
+ * The `anchor-name` written to an anchor that does not already carry one, as a CSS
79
+ * `<dashed-ident>`. `useId()` is the same source as every other id in the design
80
+ * system, including the popover's own `id` that `aria-controls` and `popovertarget`
81
+ * point at, so it is unique exactly where those are and collides exactly where
82
+ * they do. See `useAnchoredPopover` for the one case that needs the host's help.
83
+ *
84
+ * `@atlaskit/ds-lib`'s `useId` already swaps React's `:` and `«»` for `_`. The
85
+ * replace covers a host `identifierPrefix`, which may carry characters a
86
+ * `<dashed-ident>` cannot.
87
+ */
88
+ function useNewAnchorName() {
89
+ var id = (0, _useId.useId)();
90
+ return (0, _react.useMemo)(function () {
91
+ return "--anchor-".concat(id.replace(/[^A-Za-z0-9_-]/g, '_'));
92
+ }, [id]);
93
+ }
94
+
95
+ /**
96
+ * A referentially-stable, fully-resolved `TPlacement`, so a fresh inline
97
+ * `placement` object does not re-run the effect.
98
+ */
99
+ function useStablePlacement(placement) {
100
+ var resolved = (0, _resolvePlacement.resolvePlacement)({
101
+ placement: placement
102
+ });
103
+ var axis = resolved.axis;
104
+ var edge = resolved.edge;
105
+ var align = resolved.align;
106
+ var minSize = resolved.minSize;
107
+ var gap = resolved.offset.gap;
108
+ var shiftValue = resolved.offset.crossAxisShift.value;
109
+ var shiftDirection = resolved.offset.crossAxisShift.direction;
110
+ // Rebuilt from primitive deps, so the factory does not close over `resolved`
111
+ // and exhaustive-deps can verify it.
112
+ return (0, _react.useMemo)(function () {
113
+ return {
114
+ axis: axis,
115
+ edge: edge,
116
+ align: align,
117
+ minSize: minSize,
118
+ offset: {
119
+ gap: gap,
120
+ crossAxisShift: {
121
+ value: shiftValue,
122
+ direction: shiftDirection
123
+ }
124
+ }
125
+ };
126
+ }, [axis, edge, align, minSize, gap, shiftValue, shiftDirection]);
127
+ }
128
+
129
+ /**
130
+ * Exported so `useAnchoredPopoverAtPoint` can forward the options it does not own.
131
+ */
132
+
133
+ /**
134
+ * Positions and sizes a popover relative to an anchor. Owns every inline style on
135
+ * the popover host: where it goes, how big it is relative to its anchor, and the
136
+ * margins that keep it clear of the anchor and the viewport edge. It knows nothing
137
+ * about visibility or animation - compose it with `Popover`.
138
+ *
139
+ * On modern browsers the browser handles flipping via CSS Anchor Positioning;
140
+ * otherwise it falls back to measured `position: fixed` coordinates, re-run on
141
+ * scroll and resize.
142
+ *
143
+ * `inlineSize` and `blockSize` both default to `'content'`. Two rules are
144
+ * surprising: fitting one axis also fits an axis left on `'content'`, and fitting
145
+ * either axis floors the placement axis. See `TPopoverAxisSize`.
146
+ *
147
+ * **Several React roots on one page.** The `anchor-name` this hook writes to an
148
+ * anchor that has none is minted from `useId()`, like every other id in the design
149
+ * system. `useId()` is unique within a root; across roots it is unique for
150
+ * client-rendered roots but NOT for roots that hydrate the same markup, where it
151
+ * is derived from tree position. Two anchors carrying one name resolve to the last
152
+ * in tree order, so a popover can attach to another root's trigger. A host that
153
+ * hydrates more than one root must give each a distinct `identifierPrefix` (on
154
+ * `hydrateRoot` and the matching server render). That is already required for the
155
+ * popover's own `id`, which `aria-controls` and `popovertarget` point at, and for
156
+ * every `aria-*` id the design system mints. A bundle with two copies of
157
+ * `react-dom` has the same exposure for client-rendered roots.
158
+ *
159
+ * @example Position a popover below its trigger
160
+ * ```tsx
161
+ * const triggerRef = useRef<HTMLButtonElement>(null);
162
+ * const popoverRef = useRef<HTMLDivElement>(null);
163
+ *
164
+ * useAnchoredPopover({
165
+ * anchorRef: triggerRef,
166
+ * popoverRef,
167
+ * placement: { axis: 'block', edge: 'end' },
168
+ * isOpen,
169
+ * });
170
+ * ```
171
+ *
172
+ * @example Match the trigger's width and keep the popover on screen
173
+ * ```tsx
174
+ * // What `<Popup shouldFitContainer shouldFitViewport>` maps to.
175
+ * useAnchoredPopover({
176
+ * anchorRef: triggerRef,
177
+ * popoverRef,
178
+ * placement: { axis: 'block', edge: 'end' },
179
+ * isOpen,
180
+ * inlineSize: 'match-anchor',
181
+ * blockSize: 'max-available',
182
+ * });
183
+ * ```
184
+ *
185
+ * @example Switch to a cursor position mid-mount
186
+ * ```tsx
187
+ * // Complementary `isEnabled`, so exactly one hook owns the popover.
188
+ * const shared = { popoverRef, placement, isOpen };
189
+ *
190
+ * useAnchoredPopover({ ...shared, anchorRef: triggerRef, isEnabled: !mousePos });
191
+ * useAnchoredPopoverAtPoint({
192
+ * ...shared,
193
+ * isEnabled: Boolean(mousePos),
194
+ * getPoint: () => mousePosRef.current,
195
+ * });
196
+ * ```
197
+ */
198
+ function useAnchoredPopover(_ref3) {
199
+ var anchorRef = _ref3.anchorRef,
200
+ popoverRef = _ref3.popoverRef,
201
+ placement = _ref3.placement,
202
+ isOpen = _ref3.isOpen,
203
+ _ref3$isEnabled = _ref3.isEnabled,
204
+ isEnabled = _ref3$isEnabled === void 0 ? true : _ref3$isEnabled,
205
+ _ref3$inlineSize = _ref3.inlineSize,
206
+ inlineSize = _ref3$inlineSize === void 0 ? 'content' : _ref3$inlineSize,
207
+ _ref3$blockSize = _ref3.blockSize,
208
+ blockSize = _ref3$blockSize === void 0 ? 'content' : _ref3$blockSize,
209
+ _ref3$shouldPreserveI = _ref3.shouldPreserveInlineSize,
210
+ shouldPreserveInlineSize = _ref3$shouldPreserveI === void 0 ? false : _ref3$shouldPreserveI,
211
+ _ref3$forceFallbackPo = _ref3.forceFallbackPositioning,
212
+ forceFallbackPositioning = _ref3$forceFallbackPo === void 0 ? false : _ref3$forceFallbackPo;
213
+ // Stable for the life of the hook instance, so it follows a replacement anchor
214
+ // element (the point hook remounts its `<div>`).
215
+ var fallbackAnchorName = useNewAnchorName();
216
+ var stablePlacement = useStablePlacement(placement);
217
+ (0, _react.useLayoutEffect)(function () {
218
+ if (!isEnabled) {
219
+ return;
220
+ }
221
+
222
+ // Resolved once, so positioning, sizing and margins cannot disagree about it.
223
+ var isUsingCssAnchorPositioning = (0, _supportsAnchorPositioning.supportsAnchorPositioning)() && !forceFallbackPositioning;
224
+
225
+ // BOTH probes: a browser can have positioning without sizing, and
226
+ // `anchor-size()` needs the `position-anchor` only the CSS path writes.
227
+ var canUseAnchorSize = isUsingCssAnchorPositioning && (0, _supportsAnchorSize.supportsAnchorSize)();
228
+ var trigger = anchorRef.current;
229
+ var popover = popoverRef.current;
230
+ if (!trigger || !popover) {
231
+ return;
232
+ }
233
+
234
+ // Fitting also reserves padding from the viewport edge as a margin.
235
+ var isFitting = inlineSize === 'max-available' || blockSize === 'max-available';
236
+
237
+ // Always read: the viewport caps are always written, and their unit follows
238
+ // the popover's writing mode.
239
+ var writingMode = getPopoverWritingMode({
240
+ popover: popover
241
+ });
242
+
243
+ // Skipped where possible, so a position-only consumer pays no layout read.
244
+ // Fitting on the CSS path needs it to clamp the default flip floor; see
245
+ // `getDefaultPlacementAxisFloor`.
246
+ var needsAnchorSize = isAnchorRelative(inlineSize) || isAnchorRelative(blockSize) || isFitting && isUsingCssAnchorPositioning;
247
+ var anchorSize = needsAnchorSize ? canUseAnchorSize ? CSS_ANCHOR_SIZE : measureAnchorSize({
248
+ anchor: trigger,
249
+ writingMode: writingMode
250
+ }) : NO_ANCHOR_SIZE;
251
+ var sizeStyles = (0, _anchoredPopoverSize.getAnchoredPopoverSizeDeclarations)({
252
+ placement: stablePlacement,
253
+ inlineSize: inlineSize,
254
+ blockSize: blockSize,
255
+ isUsingCssAnchorPositioning: isUsingCssAnchorPositioning,
256
+ anchorSize: anchorSize,
257
+ writingMode: writingMode,
258
+ shouldPreserveInlineSize: shouldPreserveInlineSize
259
+ });
260
+ if (isUsingCssAnchorPositioning) {
261
+ return (0, _applyAnchorPositioning.applyAnchorPositioning)({
262
+ anchor: trigger,
263
+ popover: popover,
264
+ placement: stablePlacement,
265
+ sizeStyles: sizeStyles,
266
+ isFitting: isFitting,
267
+ fallbackAnchorName: fallbackAnchorName
268
+ });
269
+ }
270
+ return (0, _applyJavascriptFallbackPositioning.applyJavaScriptFallbackPositioning)({
271
+ anchor: trigger,
272
+ popover: popover,
273
+ placement: stablePlacement,
274
+ sizeStyles: sizeStyles
275
+ });
276
+ }, [isEnabled, anchorRef, popoverRef, stablePlacement, inlineSize, blockSize, shouldPreserveInlineSize, fallbackAnchorName, forceFallbackPositioning, isOpen]);
277
+ }
@@ -8,6 +8,7 @@ exports.useAnimatedVisibility = useAnimatedVisibility;
8
8
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
10
  var _react = require("react");
11
+ var _isNativeElementOpen = require("./is-native-element-open");
11
12
  var _reducedMotion = require("./reduced-motion");
12
13
  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; }
13
14
  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) { (0, _defineProperty2.default)(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; }
@@ -45,19 +46,6 @@ function runOnAnimationsSettled(_ref2) {
45
46
  }
46
47
  });
47
48
  }
48
- function isNativeElementOpen(_ref3) {
49
- var element = _ref3.element;
50
- if (element instanceof HTMLDialogElement) {
51
- return element.open;
52
- }
53
-
54
- // Some DOM implementations throw for unsupported pseudo-classes.
55
- try {
56
- return element.matches(':popover-open');
57
- } catch (_unused) {
58
- return false;
59
- }
60
- }
61
49
 
62
50
  /**
63
51
  * Visibility lifecycle phase shared between `useAnimatedVisibility`,
@@ -86,9 +74,9 @@ function isNativeElementOpen(_ref3) {
86
74
  * ```
87
75
  */
88
76
 
89
- function getInitialVisibilityState(_ref4) {
90
- var isOpen = _ref4.isOpen,
91
- willAnimate = _ref4.willAnimate;
77
+ function getInitialVisibilityState(_ref3) {
78
+ var isOpen = _ref3.isOpen,
79
+ willAnimate = _ref3.willAnimate;
92
80
  if (!isOpen) {
93
81
  return {
94
82
  phase: 'closed',
@@ -184,12 +172,12 @@ function getNextVisibilityState(state, action) {
184
172
  * phase: open ──────────────── exiting ─── (exit settle) ─── closed
185
173
  * ```
186
174
  */
187
- function useAnimatedVisibility(_ref5) {
188
- var isOpen = _ref5.isOpen,
189
- shouldAnimate = _ref5.shouldAnimate,
190
- elementRef = _ref5.elementRef,
191
- onEnterFinish = _ref5.onEnterFinish,
192
- onExitFinish = _ref5.onExitFinish;
175
+ function useAnimatedVisibility(_ref4) {
176
+ var isOpen = _ref4.isOpen,
177
+ shouldAnimate = _ref4.shouldAnimate,
178
+ elementRef = _ref4.elementRef,
179
+ onEnterFinish = _ref4.onEnterFinish,
180
+ onExitFinish = _ref4.onExitFinish;
193
181
  var willAnimate = shouldAnimate && !(0, _reducedMotion.prefersReducedMotion)();
194
182
  var _useReducer = (0, _react.useReducer)(getNextVisibilityState, {
195
183
  isOpen: isOpen,
@@ -274,7 +262,7 @@ function useAnimatedVisibility(_ref5) {
274
262
  queueMicrotask(function () {
275
263
  // A controlled close can be interrupted by isOpen changing back to true,
276
264
  // which reopens the element before this microtask runs.
277
- if (isNativeElementOpen({
265
+ if ((0, _isNativeElementOpen.isNativeElementOpen)({
278
266
  element: element
279
267
  })) {
280
268
  return;
@@ -22,6 +22,10 @@
22
22
  ._n3tdidpf{padding-bottom:0}
23
23
  ._o5724jg8{white-space:normal}
24
24
  ._otyridpf{margin-bottom:0}
25
+ ._p6861txw:popover-open{display:flex}
25
26
  ._u5f3idpf{padding-right:0}
26
27
  ._y3gnv2br{text-align:start}
28
+ :where(._1yp3idpf)>*{min-inline-size:0}
29
+ :where(._51hckb7n)>*{flex-grow:1}
30
+ :where(._f5r5idpf)>*{min-block-size:0}
27
31
  @media (prefers-reduced-motion:reduce){._bl8aglyw{animation-name:none}._156y147i{transition-duration:var(--ds-duration-instant,0ms)}}
@@ -15,8 +15,8 @@ var _bindEventListener = require("bind-event-listener");
15
15
  var _mergeRefs = _interopRequireDefault(require("@atlaskit/ds-lib/merge-refs"));
16
16
  var _once = _interopRequireDefault(require("@atlaskit/ds-lib/once"));
17
17
  var _useNotifyOpenLayerObserver = require("@atlaskit/layering/use-notify-open-layer-observer");
18
+ var _isNativeElementOpen = require("../internal/is-native-element-open");
18
19
  var _resolvePlacement2 = require("../internal/resolve-placement");
19
- var _shouldFocusIntoPopover = require("../internal/should-focus-into-popover");
20
20
  var _useAnimatedVisibility = require("../internal/use-animated-visibility");
21
21
  var _useFocusWrap = require("../internal/use-focus-wrap");
22
22
  var _useInitialFocus = require("../internal/use-initial-focus");
@@ -42,15 +42,22 @@ var supportsPopoverHint = (0, _once.default)(function () {
42
42
  // avoided this by rendering at `<body>`). Excludes `color`/`font` (theming) and
43
43
  // `direction`/`unicode-bidi` (RTL must inherit).
44
44
  //
45
+ // Not `all: initial`: the target is what `<body>` gave the portal path, not the
46
+ // spec initial values (UA serif, black, `color-scheme: normal`), and as an author
47
+ // declaration it would also override the UA `[popover]` rules (`position: fixed`,
48
+ // `display: none` when closed).
49
+ //
45
50
  // KEEP IN SYNC with the identical `surfaceResetStyles` in `dialog/dialog-content.tsx`.
46
51
  // ADS forbids sharing styles across files (`no-exported-styles` /
47
52
  // `no-imported-style-values` — Compiled styles are null at runtime), so the reset is
48
- // co-located and duplicated deliberately.
53
+ // co-located and duplicated deliberately. The `satisfies` check below fails the
54
+ // build if either copy drifts.
49
55
  var surfaceResetStyles = {
50
56
  root: "_lcxv1wug _o5724jg8 _1nmz4jg8 _1i4q4jg8 _y3gnv2br _azhwidpf _1p1dglyw"
51
57
  };
58
+ true;
52
59
  var styles = {
53
- root: "_19itglyw _ca0qidpf _u5f3idpf _n3tdidpf _19bvidpf _19pkidpf _2hwxidpf _otyridpf _18u0idpf _1r041wug _1reoewfl _18m9ewfl _bfhk1j28 _4t3i1wug",
60
+ root: "_19itglyw _ca0qidpf _u5f3idpf _n3tdidpf _19bvidpf _19pkidpf _2hwxidpf _otyridpf _18u0idpf _1r041wug _1reoewfl _18m9ewfl _bfhk1j28 _4t3i1wug _p6861txw _51hckb7n _1yp3idpf _f5r5idpf",
54
61
  motion: "_k8m011k7 _1oecdn4k _1s9z1a5r _1o511hrf _bl8aglyw _156y147i",
55
62
  enter: "_y44v179n",
56
63
  exit: "_y44vof1f"
@@ -88,7 +95,7 @@ var POPUP_ROLES = new Set(['menu', 'listbox', 'dialog', 'alertdialog', 'tree', '
88
95
 
89
96
  /**
90
97
  * Unopinionated top-layer primitive. Owns visibility and animation only;
91
- * compose with `useAnchorPosition` / `useWidthFromAnchor` for positioning.
98
+ * compose with `useAnchoredPopover` for positioning and sizing.
92
99
  *
93
100
  * ### 🔌 Visibility
94
101
  *
@@ -140,14 +147,12 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
140
147
  // `useId()` colons are invalid in CSS selectors and popover target attributes.
141
148
  var popoverId = idProp !== null && idProp !== void 0 ? idProp : "popover-".concat(autoId.replace(/:/g, ''));
142
149
 
143
- // Keep semantic callbacks current without rebinding native lifecycle
150
+ // Keep the semantic callback current without rebinding native lifecycle
144
151
  // listeners during the same commit that calls showPopover()/hidePopover().
145
152
  var onCloseRef = (0, _react.useRef)(onClose);
146
- var roleRef = (0, _react.useRef)(role);
147
153
  (0, _react.useLayoutEffect)(function () {
148
154
  onCloseRef.current = onClose;
149
- roleRef.current = role;
150
- }, [onClose, role]);
155
+ }, [onClose]);
151
156
 
152
157
  // Register with open layer observer so `closeLayers()` and open-count
153
158
  // subscriptions work. Only popup-like roles register as `popup`; passive
@@ -181,65 +186,60 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
181
186
  return popupMotionStyles[axis][edge];
182
187
  }, [placement, shouldAnimate]);
183
188
 
184
- // Prevents toggle handler from calling onClose for our own hidePopover() calls.
185
- var programmaticCloseRef = (0, _react.useRef)(false);
186
-
187
- // Set in capture-phase keydown so the toggle handler knows close reason.
189
+ // Tracks the close reason, including our own hidePopover() calls.
188
190
  var closeReasonRef = (0, _react.useRef)('light-dismiss');
189
191
 
190
192
  // Snapshot of pre-open focus, used to restore focus for nested popovers
191
- // with focus-capturing roles (browser only restores the outermost).
192
- var previouslyFocusedElementRef = (0, _react.useRef)(null);
193
+ // because the browser only restores the outermost.
194
+ var focusRestorationTargetRef = (0, _react.useRef)(null);
193
195
  var handleBeforeToggle = (0, _react.useCallback)(function (event) {
194
- if (event.newState !== 'open') {
196
+ var popover = event.currentTarget;
197
+ if (!(popover instanceof HTMLElement)) {
195
198
  return;
196
199
  }
197
- // eslint-disable-next-line @atlaskit/platform/no-direct-document-usage -- need active element snapshot
198
- var active = document.activeElement;
199
- previouslyFocusedElementRef.current = active instanceof HTMLElement ? active : null;
200
- }, []);
201
- var restorePreviouslyFocusedElement = (0, _react.useCallback)(function () {
202
- var previouslyFocused = previouslyFocusedElementRef.current;
203
- previouslyFocusedElementRef.current = null;
204
- if (previouslyFocused !== null && previouslyFocused.isConnected && (0, _shouldFocusIntoPopover.shouldFocusIntoPopover)({
205
- role: roleRef.current
206
- })) {
207
- previouslyFocused.focus({
208
- preventScroll: true
209
- });
200
+ var ownerDocument = popover.ownerDocument;
201
+ var activeElement = ownerDocument.activeElement;
202
+ if (event.newState === 'open') {
203
+ focusRestorationTargetRef.current = activeElement instanceof HTMLElement ? activeElement : null;
204
+ return;
205
+ }
206
+
207
+ // Check before closing so focus already on body is not mistaken for focus lost during close.
208
+ if (closeReasonRef.current === 'light-dismiss' || !popover.contains(activeElement)) {
209
+ focusRestorationTargetRef.current = null;
210
210
  }
211
211
  }, []);
212
- var handleToggleClosed = (0, _react.useCallback)(function (_event) {
213
- var _onCloseRef$current2;
214
- // Nested-popover focus restoration fallback (browser only
215
- // restores outermost; Firefox skips nested entirely).
216
- // Restore only when the role moved focus in on open AND the
217
- // close was Escape/programmatic. For light dismiss, the
218
- // click target keeps focus per HTML spec
219
- // (`focusPreviousElement=false`).
220
- var reason = closeReasonRef.current;
221
- var isProgrammatic = programmaticCloseRef.current;
222
- if (reason === 'escape' || isProgrammatic) {
223
- restorePreviouslyFocusedElement();
224
- } else {
225
- previouslyFocusedElementRef.current = null;
212
+ var handleToggleClosed = (0, _react.useCallback)(function (event) {
213
+ var popover = event.currentTarget;
214
+ if (!(popover instanceof HTMLElement)) {
215
+ return;
226
216
  }
227
-
217
+ var focusRestorationTarget = focusRestorationTargetRef.current;
218
+ focusRestorationTargetRef.current = null;
219
+ var reason = closeReasonRef.current;
228
220
  // Reset reason so a stale 'escape' from a race with Escape keydown
229
221
  // does not corrupt the next browser-dismiss cycle.
230
222
  closeReasonRef.current = 'light-dismiss';
231
-
232
- // Programmatic close: consumer already knows.
233
- if (isProgrammatic) {
234
- return;
223
+ if (focusRestorationTarget) {
224
+ var ownerDocument = popover.ownerDocument;
225
+ // Native restoration has finished. Preserve focus moved outside the popover.
226
+ var activeElementAfterClose = ownerDocument.activeElement;
227
+ if (activeElementAfterClose === ownerDocument.body || popover.contains(activeElementAfterClose)) {
228
+ // Match native popover restoration, which does not scroll the viewport.
229
+ focusRestorationTarget.focus({
230
+ preventScroll: true
231
+ });
232
+ }
235
233
  }
236
234
 
237
- // Browser dismiss (Escape/click-outside).
238
- // Optional: `manual` mode has no `onClose` in the forwarded type.
239
- (_onCloseRef$current2 = onCloseRef.current) === null || _onCloseRef$current2 === void 0 || _onCloseRef$current2.call(onCloseRef, {
240
- reason: reason
241
- });
242
- }, [restorePreviouslyFocusedElement]);
235
+ // Programmatic closes are already known to the consumer.
236
+ if (reason !== 'programmatic') {
237
+ var _onCloseRef$current2;
238
+ (_onCloseRef$current2 = onCloseRef.current) === null || _onCloseRef$current2 === void 0 || _onCloseRef$current2.call(onCloseRef, {
239
+ reason: reason
240
+ });
241
+ }
242
+ }, []);
243
243
  var _useAnimatedVisibilit = (0, _useAnimatedVisibility.useAnimatedVisibility)({
244
244
  isOpen: isOpen,
245
245
  shouldAnimate: shouldAnimate,
@@ -254,9 +254,9 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
254
254
 
255
255
  // Focus management: initial focus on entry (role-dependent), Tab cycling
256
256
  // for dialog roles. Restoration is native for outermost popovers; nested
257
- // focus-capturing roles are handled above via the `beforetoggle` snapshot.
257
+ // popovers are handled above via the `beforetoggle` snapshot.
258
258
  // Passing `phase` keeps `useFocusWrap` attached through the animated-exit
259
- // window (WCAG 2.4.3 guard). See notes/architecture/focus-restoration.md.
259
+ // window (WCAG 2.4.3 guard). See notes/architecture/focus.md.
260
260
  (0, _useFocusWrap.useFocusWrap)({
261
261
  elementRef: ownRef,
262
262
  role: role,
@@ -271,7 +271,7 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
271
271
  // Bind via `useLayoutEffect`, and BEFORE the show/hide layout effect
272
272
  // below, so listeners are attached when `showPopover()` synchronously
273
273
  // dispatches `beforetoggle`. A regular `useEffect` would miss that
274
- // first dispatch, dropping the `previouslyFocusedElementRef` snapshot
274
+ // first dispatch, dropping the `focusRestorationTargetRef` snapshot
275
275
  // and breaking nested-popover focus restoration on close. React runs
276
276
  // layout effects in source order on the same commit.
277
277
  (0, _react.useLayoutEffect)(function () {
@@ -284,7 +284,10 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
284
284
  var unbindEscape = (0, _bindEventListener.bind)(element, {
285
285
  type: 'keydown',
286
286
  listener: function listener(event) {
287
- if (event.key === 'Escape') {
287
+ // Ignore Escape events dispatched after native close.
288
+ if (event.key === 'Escape' && (0, _isNativeElementOpen.isNativeElementOpen)({
289
+ element: element
290
+ })) {
288
291
  closeReasonRef.current = 'escape';
289
292
  }
290
293
  },
@@ -301,7 +304,7 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
301
304
  onToggle(event);
302
305
  }
303
306
  });
304
- // Snapshot `document.activeElement` before `useInitialFocus` moves it,
307
+ // Snapshot the active element before `useInitialFocus` moves it,
305
308
  // then advance the shared visibility lifecycle.
306
309
  var unbindBeforeToggle = (0, _bindEventListener.bind)(element, {
307
310
  type: 'beforetoggle',
@@ -326,7 +329,6 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
326
329
  return;
327
330
  }
328
331
  if (isOpen) {
329
- programmaticCloseRef.current = false;
330
332
  // Clear stale 'escape' from a prior cycle: the ref outlives
331
333
  // the host element, and interrupted close lifecycles may not reach
332
334
  // `handleToggleClosed` to reset it.
@@ -335,13 +337,13 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
335
337
  element.showPopover();
336
338
  } catch (_unused) {}
337
339
  return function () {
338
- programmaticCloseRef.current = true;
340
+ closeReasonRef.current = 'programmatic';
339
341
  try {
340
342
  element.hidePopover();
341
343
  } catch (_unused2) {}
342
344
  };
343
345
  }
344
- programmaticCloseRef.current = true;
346
+ closeReasonRef.current = 'programmatic';
345
347
  try {
346
348
  element.hidePopover();
347
349
  } catch (_unused3) {}
@@ -16,9 +16,9 @@ var _bindEventListener = require("bind-event-listener");
16
16
  * - Popover open state tracked via `data-popover-open` (no `:popover-open` in jsdom).
17
17
  * - Auto popovers: maintain an open stack, perform light-dismiss on document mousedown
18
18
  * outside the open chain, and dismiss the topmost on Escape.
19
- * - On open, removes any inline `opacity: 0` from the popover element so callers that
20
- * rely on a `ResizeObserver` reveal callback (e.g. `use-anchor-position`) become
21
- * visible in jsdom, where layout-driven RO callbacks never fire.
19
+ * - On open, removes any inline `opacity: 0` from the popover element, so the
20
+ * JavaScript positioning fallback's `ResizeObserver` reveal is not needed in
21
+ * jsdom, where its non-zero-layout guard can never pass.
22
22
  *
23
23
  * Companion `toBeVisible` patch lives at `./to-be-visible`.
24
24
  *
@@ -249,10 +249,9 @@ function stackForMode(_ref9) {
249
249
  return null;
250
250
  }
251
251
 
252
- // Polyfill-only. Real browsers reveal the popover once `ResizeObserver`
253
- // callbacks fire after the first layout pass. jsdom has no layout, so we clear
254
- // any inline `opacity: 0` (set by callers like `use-anchor-position`) once
255
- // the open toggle has fired.
252
+ // Polyfill-only. Real browsers reveal the popover on the first non-zero layout
253
+ // their `ResizeObserver` reports; jsdom always reports 0, so that guard never
254
+ // passes and the inline `opacity: 0` is cleared on the open toggle instead.
256
255
  function clearOpacityOnOpenToggle(_ref0) {
257
256
  var element = _ref0.element;
258
257
  var unbind = (0, _bindEventListener.bind)(element, {
@@ -19,9 +19,11 @@ import { useSafariEscapeFix } from '../internal/use-safari-escape-fix';
19
19
  //
20
20
  // KEEP IN SYNC with the identical `surfaceResetStyles` in `popover/popover.tsx`
21
21
  // (ADS forbids sharing styles across files, so it is co-located and duplicated).
22
+ // The `satisfies` check below fails the build if either copy drifts.
22
23
  const surfaceResetStyles = {
23
24
  root: "_lcxv1wug _o5724jg8 _1nmz4jg8 _1i4q4jg8 _y3gnv2br _azhwidpf _1p1dglyw"
24
25
  };
26
+ true;
25
27
  const dialogStyles = {
26
28
  root: "_19itglyw _19pk1wug _2hwx1wug _otyr1wug _18u01wug _1q51ze3t _y4tize3t _85i5ze3t _bozgze3t _p12fglyw _c71lglyw _bfhk1j28",
27
29
  motion: "_k8m011k7 _1oeci9qs _1s9z1a5r _1o511hrf _bl8aglyw _156y147i",
@@ -1,17 +1,13 @@
1
1
  import { useId } from '@atlaskit/ds-lib/use-id';
2
+
2
3
  /**
3
- * Returns a stable HTML `id` used to wire a trigger element to its popover
4
- * via ARIA (`aria-controls`) and the Popover API (`popovertarget`).
4
+ * A stable `id` for wiring a trigger to its popover: the popover's `id`, and the
5
+ * trigger's matching `aria-controls` / `popovertarget`.
5
6
  *
6
- * The returned value is intended for the `id` attribute on the popover and
7
- * for the matching `aria-controls` / `popovertarget` references on the
8
- * trigger. It is NOT a CSS `anchor-name` value: CSS anchor positioning is
9
- * handled internally by `useAnchorPosition`, which mints and applies its
10
- * own `anchor-name` on the trigger element.
7
+ * NOT a CSS `anchor-name` - `useAnchoredPopover` mints and writes its own.
11
8
  */
12
9
  export function usePopoverId() {
13
- // `@atlaskit/ds-lib/use-id` already strips characters that React's
14
- // `useId` produces (`:`, `«`, `»`) which are invalid in HTML ID
15
- // tokens. The `popover-` prefix is purely for DOM debug clarity.
10
+ // `@atlaskit/ds-lib/use-id`, not React's `useId`: React's output contains
11
+ // `:`, `«` and `»`, which are invalid in an HTML id token.
16
12
  return `popover-${useId()}`;
17
13
  }