@atlaskit/top-layer 4.4.5 → 5.0.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 (130) hide show
  1. package/CHANGELOG.md +41 -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/{anchor-positioning-fallback.js → javascript-fallback/anchor-positioning-fallback.js} +12 -33
  15. package/dist/cjs/internal/javascript-fallback/apply-javascript-fallback-positioning.js +297 -0
  16. package/dist/cjs/internal/javascript-fallback/resolve-css-length-to-pixels.js +90 -0
  17. package/dist/cjs/internal/javascript-fallback/warn-fallback-failure.js +19 -0
  18. package/dist/cjs/internal/resolve-placement.js +14 -6
  19. package/dist/cjs/internal/supports-anchor-positioning.js +17 -0
  20. package/dist/cjs/internal/supports-anchor-size.js +17 -0
  21. package/dist/cjs/internal/supports-css-declaration.js +25 -0
  22. package/dist/cjs/internal/surface-reset.js +5 -0
  23. package/dist/cjs/internal/use-anchored-popover-at-point.js +124 -0
  24. package/dist/cjs/internal/use-anchored-popover.js +277 -0
  25. package/dist/cjs/popover/popover.compiled.css +4 -0
  26. package/dist/cjs/popover/popover.js +11 -4
  27. package/dist/cjs/testing/polyfill.js +6 -7
  28. package/dist/es2019/dialog/dialog-content.js +2 -0
  29. package/dist/es2019/entry-points/use-popover-id.js +6 -10
  30. package/dist/es2019/internal/anchor-positioning/apply-anchor-positioning.js +83 -0
  31. package/dist/es2019/internal/anchor-positioning/cross-axis-shift-margins.js +29 -0
  32. package/dist/es2019/internal/anchor-positioning/edge-margin.js +18 -0
  33. package/dist/es2019/internal/anchor-positioning/fit-margins.js +69 -0
  34. package/dist/es2019/internal/anchor-positioning/get-existing-anchor-name.js +12 -0
  35. package/dist/es2019/internal/{placement-to-position-area.js → anchor-positioning/placement-to-position-area.js} +9 -9
  36. package/dist/es2019/internal/anchor-positioning/placement-to-try-fallbacks.js +94 -0
  37. package/dist/es2019/internal/anchored-popover-size.js +343 -0
  38. package/dist/es2019/internal/get-aria-for-trigger.js +7 -13
  39. package/dist/es2019/internal/{anchor-positioning-fallback.js → javascript-fallback/anchor-positioning-fallback.js} +12 -33
  40. package/dist/es2019/internal/javascript-fallback/apply-javascript-fallback-positioning.js +291 -0
  41. package/dist/es2019/internal/javascript-fallback/resolve-css-length-to-pixels.js +87 -0
  42. package/dist/es2019/internal/javascript-fallback/warn-fallback-failure.js +14 -0
  43. package/dist/es2019/internal/resolve-placement.js +14 -6
  44. package/dist/es2019/internal/supports-anchor-positioning.js +12 -0
  45. package/dist/es2019/internal/supports-anchor-size.js +12 -0
  46. package/dist/es2019/internal/supports-css-declaration.js +20 -0
  47. package/dist/es2019/internal/surface-reset.js +1 -0
  48. package/dist/es2019/internal/use-anchored-popover-at-point.js +116 -0
  49. package/dist/es2019/internal/use-anchored-popover.js +265 -0
  50. package/dist/es2019/popover/popover.compiled.css +4 -0
  51. package/dist/es2019/popover/popover.js +11 -4
  52. package/dist/es2019/testing/polyfill.js +6 -7
  53. package/dist/esm/dialog/dialog-content.js +2 -0
  54. package/dist/esm/entry-points/use-popover-id.js +6 -10
  55. package/dist/esm/internal/anchor-positioning/apply-anchor-positioning.js +80 -0
  56. package/dist/esm/internal/anchor-positioning/cross-axis-shift-margins.js +28 -0
  57. package/dist/esm/internal/anchor-positioning/edge-margin.js +15 -0
  58. package/dist/esm/internal/anchor-positioning/fit-margins.js +71 -0
  59. package/dist/esm/internal/anchor-positioning/get-existing-anchor-name.js +12 -0
  60. package/dist/esm/internal/{placement-to-position-area.js → anchor-positioning/placement-to-position-area.js} +11 -12
  61. package/dist/esm/internal/anchor-positioning/placement-to-try-fallbacks.js +89 -0
  62. package/dist/esm/internal/anchored-popover-size.js +338 -0
  63. package/dist/esm/internal/get-aria-for-trigger.js +7 -13
  64. package/dist/esm/internal/{anchor-positioning-fallback.js → javascript-fallback/anchor-positioning-fallback.js} +12 -33
  65. package/dist/esm/internal/javascript-fallback/apply-javascript-fallback-positioning.js +290 -0
  66. package/dist/esm/internal/javascript-fallback/resolve-css-length-to-pixels.js +85 -0
  67. package/dist/esm/internal/javascript-fallback/warn-fallback-failure.js +13 -0
  68. package/dist/esm/internal/resolve-placement.js +14 -6
  69. package/dist/esm/internal/supports-anchor-positioning.js +12 -0
  70. package/dist/esm/internal/supports-anchor-size.js +12 -0
  71. package/dist/esm/internal/supports-css-declaration.js +19 -0
  72. package/dist/esm/internal/surface-reset.js +1 -0
  73. package/dist/esm/internal/use-anchored-popover-at-point.js +118 -0
  74. package/dist/esm/internal/use-anchored-popover.js +271 -0
  75. package/dist/esm/popover/popover.compiled.css +4 -0
  76. package/dist/esm/popover/popover.js +11 -4
  77. package/dist/esm/testing/polyfill.js +6 -7
  78. package/dist/types/entry-points/placement-map.d.ts +2 -2
  79. package/dist/types/entry-points/use-popover-id.d.ts +3 -7
  80. package/dist/types/internal/anchor-positioning/apply-anchor-positioning.d.ts +26 -0
  81. package/dist/types/internal/anchor-positioning/cross-axis-shift-margins.d.ts +19 -0
  82. package/dist/types/internal/anchor-positioning/edge-margin.d.ts +10 -0
  83. package/dist/types/internal/anchor-positioning/fit-margins.d.ts +30 -0
  84. package/dist/types/internal/anchor-positioning/get-existing-anchor-name.d.ts +8 -0
  85. package/dist/types/internal/{placement-to-position-area.d.ts → anchor-positioning/placement-to-position-area.d.ts} +10 -6
  86. package/dist/types/internal/anchor-positioning/placement-to-try-fallbacks.d.ts +30 -0
  87. package/dist/types/internal/anchored-popover-size.d.ts +126 -0
  88. package/dist/types/internal/get-aria-for-trigger.d.ts +13 -25
  89. package/dist/types/internal/javascript-fallback/anchor-positioning-fallback.d.ts +36 -0
  90. package/dist/types/internal/javascript-fallback/apply-javascript-fallback-positioning.d.ts +15 -0
  91. package/dist/types/internal/javascript-fallback/resolve-css-length-to-pixels.d.ts +20 -0
  92. package/dist/types/internal/javascript-fallback/warn-fallback-failure.d.ts +7 -0
  93. package/dist/types/internal/resolve-placement.d.ts +18 -2
  94. package/dist/types/internal/supports-anchor-positioning.d.ts +5 -0
  95. package/dist/types/internal/supports-anchor-size.d.ts +5 -0
  96. package/dist/types/internal/supports-css-declaration.d.ts +11 -0
  97. package/dist/types/internal/surface-reset.d.ts +25 -0
  98. package/dist/types/internal/use-anchored-popover-at-point.d.ts +48 -0
  99. package/dist/types/internal/use-anchored-popover.d.ts +143 -0
  100. package/dist/types/popover/index.d.ts +2 -1
  101. package/dist/types/popover/popover.d.ts +1 -1
  102. package/dist/types/popover/types.d.ts +15 -17
  103. package/dist/types/testing/polyfill.d.ts +3 -3
  104. package/package.json +2 -2
  105. package/use-anchored-popover/package.json +10 -0
  106. package/use-anchored-popover-at-point/package.json +10 -0
  107. package/dist/cjs/internal/placement-to-try-fallbacks.js +0 -108
  108. package/dist/cjs/internal/resolve-css-length-to-pixels.js +0 -104
  109. package/dist/cjs/internal/use-anchor-position-at-point.js +0 -129
  110. package/dist/cjs/internal/use-anchor-position.js +0 -501
  111. package/dist/cjs/internal/use-width-from-anchor.js +0 -124
  112. package/dist/es2019/internal/placement-to-try-fallbacks.js +0 -108
  113. package/dist/es2019/internal/resolve-css-length-to-pixels.js +0 -101
  114. package/dist/es2019/internal/use-anchor-position-at-point.js +0 -124
  115. package/dist/es2019/internal/use-anchor-position.js +0 -480
  116. package/dist/es2019/internal/use-width-from-anchor.js +0 -120
  117. package/dist/esm/internal/placement-to-try-fallbacks.js +0 -103
  118. package/dist/esm/internal/resolve-css-length-to-pixels.js +0 -98
  119. package/dist/esm/internal/use-anchor-position-at-point.js +0 -124
  120. package/dist/esm/internal/use-anchor-position.js +0 -494
  121. package/dist/esm/internal/use-width-from-anchor.js +0 -118
  122. package/dist/types/internal/anchor-positioning-fallback.d.ts +0 -51
  123. package/dist/types/internal/placement-to-try-fallbacks.d.ts +0 -13
  124. package/dist/types/internal/resolve-css-length-to-pixels.d.ts +0 -20
  125. package/dist/types/internal/use-anchor-position-at-point.d.ts +0 -67
  126. package/dist/types/internal/use-anchor-position.d.ts +0 -87
  127. package/dist/types/internal/use-width-from-anchor.d.ts +0 -19
  128. package/use-anchor-position/package.json +0 -10
  129. package/use-anchor-position-at-point/package.json +0 -10
  130. 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
+ }
@@ -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)}}
@@ -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
  *
@@ -256,7 +263,7 @@ var Popover = exports.Popover = /*#__PURE__*/(0, _react.forwardRef)(function Pop
256
263
  // for dialog roles. Restoration is native for outermost popovers; nested
257
264
  // focus-capturing roles are handled above via the `beforetoggle` snapshot.
258
265
  // Passing `phase` keeps `useFocusWrap` attached through the animated-exit
259
- // window (WCAG 2.4.3 guard). See notes/architecture/focus-restoration.md.
266
+ // window (WCAG 2.4.3 guard). See notes/architecture/focus.md.
260
267
  (0, _useFocusWrap.useFocusWrap)({
261
268
  elementRef: ownRef,
262
269
  role: role,
@@ -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
  }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The CSS Anchor Positioning path: `position-anchor`, `position-area`,
3
+ * `position-try-fallbacks` and the gap / shift margins, all declared on the
4
+ * popover so the browser owns placement and flipping. The twin of
5
+ * `../javascript-fallback/apply-javascript-fallback-positioning`.
6
+ */
7
+
8
+ import { setStyle } from '../set-style';
9
+ import { crossAxisShiftMargins } from './cross-axis-shift-margins';
10
+ import { edgeMargin } from './edge-margin';
11
+ import { getFitMarginDeclarations } from './fit-margins';
12
+ import { getExistingAnchorName } from './get-existing-anchor-name';
13
+ import { placementToPositionArea } from './placement-to-position-area';
14
+ import { placementToTryFallbacks } from './placement-to-try-fallbacks';
15
+
16
+ /**
17
+ * Returns the cleanup.
18
+ */
19
+ export function applyAnchorPositioning({
20
+ anchor,
21
+ popover,
22
+ placement,
23
+ sizeStyles,
24
+ isFitting,
25
+ fallbackAnchorName
26
+ }) {
27
+ // Reuse rather than mint: another popover may already be anchored to it, or a
28
+ // CONSUMER may have set it on their own element, as `post-office` does.
29
+ const existingAnchorName = getExistingAnchorName(anchor);
30
+ const anchorName = existingAnchorName !== null && existingAnchorName !== void 0 ? existingAnchorName : fallbackAnchorName;
31
+ const gap = edgeMargin({
32
+ placement,
33
+ offset: placement.offset.gap
34
+ });
35
+ const crossAxisShift = crossAxisShiftMargins({
36
+ placement,
37
+ crossAxisShiftCssValue: placement.offset.crossAxisShift.value,
38
+ direction: placement.offset.crossAxisShift.direction
39
+ });
40
+ const popoverStyles = [{
41
+ property: 'position-anchor',
42
+ value: anchorName
43
+ }, {
44
+ property: 'position-area',
45
+ value: placementToPositionArea({
46
+ placement
47
+ })
48
+ }, {
49
+ property: 'position-try-fallbacks',
50
+ value: placementToTryFallbacks({
51
+ placement
52
+ })
53
+ },
54
+ // Reset browser default popover positioning that conflicts
55
+ // with anchor positioning (UA: `inset: 0; margin: auto;`)
56
+ {
57
+ property: 'margin',
58
+ value: '0'
59
+ }, {
60
+ property: 'inset',
61
+ value: 'auto'
62
+ }, gap,
63
+ // When fitting, the shift margins come back COMPOSED with the reserved
64
+ // viewport padding, rather than being written twice.
65
+ ...(isFitting ? getFitMarginDeclarations({
66
+ placement,
67
+ crossAxisShiftMargins: crossAxisShift
68
+ }) : crossAxisShift), ...sizeStyles];
69
+
70
+ // Anchor names are never cleaned up: multiple popovers can share an anchor,
71
+ // and even with reference counting an async React update could remove the name
72
+ // while something else still uses it. See
73
+ // `notes/decisions/anchor-name-lifetime.md`. Only written when minted: a name
74
+ // from a stylesheet may be a list, which an inline copy of its first entry
75
+ // would collapse.
76
+ if (existingAnchorName === null) {
77
+ anchor.style.setProperty('anchor-name', anchorName);
78
+ }
79
+ return setStyle({
80
+ element: popover,
81
+ styles: popoverStyles
82
+ });
83
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The cross-axis shift, written antisymmetrically on BOTH cross-axis sides rather
3
+ * than as one margin. Do not collapse it to one side: a single margin widens the
4
+ * margin box, so `anchor-center` displaces the border box by only half the value,
5
+ * a `<position-area>` fallback sliding across the cross axis drops it entirely,
6
+ * and the widened box changes where the popover is judged to overflow.
7
+ *
8
+ * A positive value always moves the popover toward the cross-axis END, so
9
+ * `direction: 'forwards'` means the same thing for every `align` value. See
10
+ * `notes/decisions/placement-offset.md`.
11
+ */
12
+ export function crossAxisShiftMargins({
13
+ placement,
14
+ crossAxisShiftCssValue,
15
+ direction
16
+ }) {
17
+ const crossAxis = placement.axis === 'block' ? 'inline' : 'block';
18
+ // `calc()` rather than JavaScript math, so opaque values such as design tokens
19
+ // negate correctly.
20
+ const negated = `calc(-1 * ${crossAxisShiftCssValue})`;
21
+ const isForwards = direction === 'forwards';
22
+ return [{
23
+ property: `margin-${crossAxis}-start`,
24
+ value: isForwards ? crossAxisShiftCssValue : negated
25
+ }, {
26
+ property: `margin-${crossAxis}-end`,
27
+ value: isForwards ? negated : crossAxisShiftCssValue
28
+ }];
29
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The margin that creates the gap between the popover and its anchor.
3
+ */
4
+ export function edgeMargin({
5
+ placement,
6
+ offset
7
+ }) {
8
+ const {
9
+ axis,
10
+ edge
11
+ } = placement;
12
+ // The side FACING the anchor is always opposite the placement edge.
13
+ const anchorFacingEdge = edge === 'end' ? 'start' : 'end';
14
+ return {
15
+ property: `margin-${axis}-${anchorFacingEdge}`,
16
+ value: offset
17
+ };
18
+ }
@@ -0,0 +1,69 @@
1
+ import { VIEWPORT_PADDING } from '../anchored-popover-size';
2
+
3
+ /**
4
+ * Every logical margin property the fit recipe can write. Declared here and
5
+ * imported by `edge-margin.tsx` and `cross-axis-shift-margins.tsx`: two
6
+ * structurally identical declarations are interchangeable to the compiler, so a
7
+ * divergence would surface only as a silently-ignored property name at
8
+ * `setStyle`.
9
+ */
10
+
11
+ /**
12
+ * The cross-axis sides that face the viewport rather than the anchor, and so take
13
+ * the reserved padding.
14
+ *
15
+ * A `span-*` cell aligns the popover's MARGIN box to the anchor's edge, so padding
16
+ * on the anchor-facing side does not reserve space at the viewport: it insets the
17
+ * border box from the edge the consumer asked it to line up with. A centred
18
+ * popover has no anchor-facing side, and padding both keeps the shift pair
19
+ * antisymmetric so `anchor-center` still centres.
20
+ *
21
+ * Known cost: the slide fallbacks do not swap the inline margins the way
22
+ * `flip-inline` does, so a slid popover keeps its padding on the anchor side (or,
23
+ * re-centred, sits 2.5px off centre). Alignment in the requested position wins.
24
+ */
25
+ function viewportFacingCrossAxisSides({
26
+ align
27
+ }) {
28
+ if (align === 'center') {
29
+ return ['start', 'end'];
30
+ }
31
+ // `align: 'start'` sits on the anchor's start edge and expands toward end.
32
+ return [align === 'start' ? 'end' : 'start'];
33
+ }
34
+
35
+ /**
36
+ * The margin half of the fit recipe. The caps reserve the viewport padding by
37
+ * subtracting it, but nothing makes the reservation land on the viewport side, so
38
+ * a capped popover packs flush against the edge it slid or flipped to.
39
+ *
40
+ * The padding buys visual clearance and nothing else: at the cap the margin box
41
+ * is exactly `100%` of the cell, so these margins never make
42
+ * `position-try-fallbacks` fire. Only the unclamped placement-axis floor does
43
+ * that; see `getPlacementAxisFloor`.
44
+ *
45
+ * The cross-axis sides are composed, not overwritten: they already carry the
46
+ * antisymmetric `crossAxisShift` pair.
47
+ */
48
+ export function getFitMarginDeclarations({
49
+ placement,
50
+ crossAxisShiftMargins
51
+ }) {
52
+ const crossAxis = placement.axis === 'block' ? 'inline' : 'block';
53
+ const paddedProperties = new Set(viewportFacingCrossAxisSides({
54
+ align: placement.align
55
+ }).map(side => `margin-${crossAxis}-${side}`));
56
+ return [{
57
+ property: `margin-${placement.axis}-${placement.edge}`,
58
+ value: VIEWPORT_PADDING
59
+ }, ...crossAxisShiftMargins.map(({
60
+ property,
61
+ value
62
+ }) => paddedProperties.has(property) ? {
63
+ property,
64
+ value: `calc(${value} + ${VIEWPORT_PADDING})`
65
+ } : {
66
+ property,
67
+ value
68
+ })];
69
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The `anchor-name` an element already carries, or `null`. Computed as well as
3
+ * inline, so a name set from a STYLESHEET is found: a minted inline name would
4
+ * otherwise override it for every other rule anchored to that element.
5
+ * `anchor-name` is a list and `position-anchor` takes one ident, so the first
6
+ * name is the one to anchor to.
7
+ */
8
+ export function getExistingAnchorName(element) {
9
+ const declared = element.style.getPropertyValue('anchor-name') || getComputedStyle(element).getPropertyValue('anchor-name');
10
+ const first = declared.split(',')[0].trim();
11
+ return first !== '' && first !== 'none' ? first : null;
12
+ }
@@ -1,7 +1,5 @@
1
- import { resolvePlacement } from './resolve-placement';
2
-
3
1
  /**
4
- * Resolves placement options to CSS `position-area` syntax.
2
+ * Resolves a placement to CSS `position-area` syntax.
5
3
  *
6
4
  * The `align` field represents the *visual* alignment:
7
5
  * - `align: 'start'` means "aligned to the trigger's start edge" (left in LTR for block axis)
@@ -12,10 +10,14 @@ import { resolvePlacement } from './resolve-placement';
12
10
  * Visual start-alignment requires `span-{cross-axis}-end` (expand toward end = start-aligned).
13
11
  * This function handles the inversion internally.
14
12
  *
13
+ * Takes the RESOLVED placement, which the hook threads through positioning,
14
+ * sizing and margins alike, so nothing here can disagree with them.
15
+ *
15
16
  * @example
16
- * placementToPositionArea({ placement: { axis: 'block', edge: 'end', align: 'center' } }) // → 'block-end'
17
- * placementToPositionArea({ placement: { axis: 'block', edge: 'end', align: 'start' } }) // → 'block-end span-inline-end'
18
- * placementToPositionArea({ placement: { axis: 'block', edge: 'end', align: 'end' } }) // → 'block-end span-inline-start'
17
+ * // For a `block-end` placement, by `align`:
18
+ * // 'center' → 'block-end'
19
+ * // 'start' → 'block-end span-inline-end'
20
+ * // 'end' → 'block-end span-inline-start'
19
21
  */
20
22
  export function placementToPositionArea({
21
23
  placement
@@ -24,9 +26,7 @@ export function placementToPositionArea({
24
26
  axis,
25
27
  edge,
26
28
  align
27
- } = resolvePlacement({
28
- placement
29
- });
29
+ } = placement;
30
30
  const edgeValue = `${axis}-${edge}`;
31
31
  if (align === 'center') {
32
32
  return edgeValue;