@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,290 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ /**
3
+ * The JavaScript positioning fallback: measured `position: fixed` coordinates for
4
+ * browsers without CSS Anchor Positioning.
5
+ *
6
+ * Best effort, degrading in that order: measured anchored position, then anchored
7
+ * with unresolvable offsets treated as `0`, then centred in the viewport, and
8
+ * always visible. The two hazards differ in kind: `new ResizeObserver` runs
9
+ * synchronously in the layout effect, where a throw unmounts the React tree,
10
+ * while `update` runs asynchronously, where the danger is an uncleared
11
+ * `opacity: 0`.
12
+ */
13
+ import { bind } from 'bind-event-listener';
14
+ import rafSchedule from 'raf-schd';
15
+ import { combine } from '../combine';
16
+ import { setStyle } from '../set-style';
17
+ import { computeFallbackPosition } from './anchor-positioning-fallback';
18
+ import { resolveCssLengthToPixels } from './resolve-css-length-to-pixels';
19
+ import { warnFallbackFailure } from './warn-fallback-failure';
20
+
21
+ /**
22
+ * Wrapped because jsdom throws on the unknown `:popover-open` selector.
23
+ */
24
+ function isPopoverOpen(popover) {
25
+ try {
26
+ return popover.matches(':popover-open');
27
+ } catch (_unused) {
28
+ return false;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Observes the popover until its first non-zero layout, then self-disconnects;
34
+ * ongoing movement is handled by the scroll and resize listeners. The popover is
35
+ * `display: none` until `showPopover()`, and some browsers fire `toggle` before
36
+ * layout, so RAF-after-toggle is unreliable.
37
+ *
38
+ * `null` where `ResizeObserver` is missing (non-DOM jest environments). There is
39
+ * then no measurement to wait for, so the caller positions immediately and must
40
+ * not hide the popover, because no reveal would follow.
41
+ */
42
+ function createFirstLayoutObserver(_ref) {
43
+ var popover = _ref.popover,
44
+ onFirstLayout = _ref.onFirstLayout;
45
+ if (typeof ResizeObserver === 'undefined') {
46
+ return null;
47
+ }
48
+ try {
49
+ var observer = new ResizeObserver(function () {
50
+ if (popover.offsetWidth > 0 && popover.offsetHeight > 0) {
51
+ onFirstLayout();
52
+ observer.disconnect();
53
+ }
54
+ });
55
+ return observer;
56
+ } catch (error) {
57
+ warnFallbackFailure({
58
+ error: error
59
+ });
60
+ return null;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * The consumer's offsets in pixels. See `resolvedOffsets` below.
66
+ */
67
+
68
+ /**
69
+ * Positions `popover` against `anchor` from measurements, and keeps it there
70
+ * across opens, scroll and resize. Returns the cleanup.
71
+ *
72
+ * No fit margins on this path: the gap and the cross-axis shift are coordinate
73
+ * deltas here, so writing them as margins too would double-apply them.
74
+ */
75
+ export function applyJavaScriptFallbackPositioning(_ref2) {
76
+ var anchor = _ref2.anchor,
77
+ popover = _ref2.popover,
78
+ placement = _ref2.placement,
79
+ sizeStyles = _ref2.sizeStyles;
80
+ // Already in the top layer via the `popover` attribute, so this only resets the
81
+ // UA defaults and writes measured `top` / `left`.
82
+ var cleanupBaseStyles = setStyle({
83
+ element: popover,
84
+ styles: [{
85
+ property: 'margin',
86
+ value: '0'
87
+ }, {
88
+ property: 'inset',
89
+ value: 'auto'
90
+ }].concat(_toConsumableArray(sizeStyles))
91
+ });
92
+
93
+ // The popover can be consumer-owned (`@atlaskit/popper`'s `createPopper`),
94
+ // so restore prior inline values rather than removing ours.
95
+ var undoPosition;
96
+ var undoHide;
97
+
98
+ // Resolved once per open and per resize, reused across scrolls: resolving is a
99
+ // full style and layout flush, and a scroll cannot change what a CSS length
100
+ // resolves to. A resize can (viewport units), and so can a re-open (a token
101
+ // retargeted while closed).
102
+ var resolvedOffsets = null;
103
+ function invalidateResolvedOffsets() {
104
+ resolvedOffsets = null;
105
+ }
106
+ function getResolvedOffsets() {
107
+ if (resolvedOffsets) {
108
+ return resolvedOffsets;
109
+ }
110
+ // Probed INSIDE the popover: it is in the top layer, not the anchor's DOM
111
+ // tree, so anywhere else resolves a token against a different scope.
112
+ resolvedOffsets = {
113
+ gap: resolveCssLengthToPixels({
114
+ value: placement.offset.gap,
115
+ container: popover
116
+ }),
117
+ crossAxisShift: resolveCssLengthToPixels({
118
+ value: placement.offset.crossAxisShift.value,
119
+ container: popover
120
+ })
121
+ };
122
+ return resolvedOffsets;
123
+ }
124
+ function reveal() {
125
+ var _undoHide;
126
+ (_undoHide = undoHide) === null || _undoHide === void 0 || _undoHide();
127
+ undoHide = undefined;
128
+ }
129
+ function restorePosition() {
130
+ var _undoPosition;
131
+ (_undoPosition = undoPosition) === null || _undoPosition === void 0 || _undoPosition();
132
+ undoPosition = undefined;
133
+ }
134
+
135
+ // `opacity: 0` not `visibility: hidden`: Firefox skips visibility-hidden
136
+ // elements during `<dialog>` initial-focus traversal.
137
+ function hideUntilPositioned() {
138
+ // Keep the first snapshot, else we capture our own `opacity: 0`.
139
+ if (undoHide) {
140
+ return;
141
+ }
142
+ undoHide = setStyle({
143
+ element: popover,
144
+ styles: [{
145
+ property: 'opacity',
146
+ value: '0'
147
+ }]
148
+ });
149
+ }
150
+
151
+ /**
152
+ * Last resort. The base styles above reset the UA
153
+ * `[popover] { inset: 0; margin: auto }`, so a popover with no `top` / `left`
154
+ * sits in the viewport corner rather than centred.
155
+ */
156
+ function centerInViewport() {
157
+ restorePosition();
158
+ undoPosition = setStyle({
159
+ element: popover,
160
+ styles: [{
161
+ property: 'inset',
162
+ value: '0'
163
+ }, {
164
+ property: 'margin',
165
+ value: 'auto'
166
+ }]
167
+ });
168
+ }
169
+ function update() {
170
+ try {
171
+ var triggerRect = anchor.getBoundingClientRect();
172
+ var viewport = {
173
+ width: window.innerWidth,
174
+ height: window.innerHeight
175
+ };
176
+ var _getResolvedOffsets = getResolvedOffsets(),
177
+ gap = _getResolvedOffsets.gap,
178
+ crossAxisShift = _getResolvedOffsets.crossAxisShift;
179
+ var _computeFallbackPosit = computeFallbackPosition({
180
+ triggerRect: triggerRect,
181
+ popoverEl: popover,
182
+ placement: placement,
183
+ viewport: viewport,
184
+ gap: gap,
185
+ crossAxisShift: {
186
+ value: crossAxisShift,
187
+ direction: placement.offset.crossAxisShift.direction
188
+ }
189
+ }),
190
+ top = _computeFallbackPosit.top,
191
+ left = _computeFallbackPosit.left;
192
+
193
+ // A non-finite coordinate serialises to a declaration the browser drops,
194
+ // landing the popover in the corner just as silently as a throw would.
195
+ if (!Number.isFinite(top) || !Number.isFinite(left)) {
196
+ centerInViewport();
197
+ return;
198
+ }
199
+
200
+ // Restore first so the snapshot stays the consumer's value.
201
+ restorePosition();
202
+ undoPosition = setStyle({
203
+ element: popover,
204
+ styles: [{
205
+ property: 'top',
206
+ value: "".concat(top, "px")
207
+ }, {
208
+ property: 'left',
209
+ value: "".concat(left, "px")
210
+ }]
211
+ });
212
+ } catch (error) {
213
+ warnFallbackFailure({
214
+ error: error
215
+ });
216
+ // Anchoring needs the trigger geometry we just failed to get, so the
217
+ // centred position is the only one left to degrade to.
218
+ try {
219
+ centerInViewport();
220
+ } catch (_unused2) {}
221
+ } finally {
222
+ // Unconditional. A popover in the wrong place is recoverable on the
223
+ // next scroll or resize; one left at `opacity: 0` is invisible forever.
224
+ reveal();
225
+ }
226
+ }
227
+ function invalidateAndUpdate() {
228
+ invalidateResolvedOffsets();
229
+ update();
230
+ }
231
+
232
+ // One update per animation frame. Scroll reuses the resolved offsets; resize
233
+ // re-resolves them.
234
+ var scheduledScrollUpdate = rafSchedule(update);
235
+ var scheduledResizeUpdate = rafSchedule(invalidateAndUpdate);
236
+ var firstLayoutObserver = createFirstLayoutObserver({
237
+ popover: popover,
238
+ onFirstLayout: update
239
+ });
240
+ function positionOnNextLayout() {
241
+ // A token can have been retargeted while the popover was closed, so each
242
+ // open re-resolves the offsets.
243
+ invalidateResolvedOffsets();
244
+ // No observer means no measurement is coming: position now, and do not
245
+ // hide, because the reveal would never arrive.
246
+ if (!firstLayoutObserver) {
247
+ update();
248
+ return;
249
+ }
250
+ hideUntilPositioned();
251
+ firstLayoutObserver.observe(popover);
252
+ }
253
+
254
+ // The popover can already be open when this runs, in which case the initial
255
+ // `toggle` has fired and the listener below would miss it.
256
+ if (isPopoverOpen(popover)) {
257
+ positionOnNextLayout();
258
+ }
259
+ return combine(cleanupBaseStyles,
260
+ // Each open needs a fresh measurement, so hide and re-observe.
261
+ bind(popover, {
262
+ type: 'toggle',
263
+ listener: function listener(event) {
264
+ var toggleEvent = event;
265
+ if (toggleEvent.newState === 'open') {
266
+ positionOnNextLayout();
267
+ }
268
+ }
269
+ }), function () {
270
+ return firstLayoutObserver === null || firstLayoutObserver === void 0 ? void 0 : firstLayoutObserver.disconnect();
271
+ }, bind(window, {
272
+ type: 'scroll',
273
+ listener: scheduledScrollUpdate,
274
+ options: {
275
+ capture: true,
276
+ passive: true
277
+ }
278
+ }), bind(window, {
279
+ type: 'resize',
280
+ listener: scheduledResizeUpdate,
281
+ options: {
282
+ passive: true
283
+ }
284
+ }), function () {
285
+ scheduledScrollUpdate.cancel();
286
+ scheduledResizeUpdate.cancel();
287
+ restorePosition();
288
+ reveal();
289
+ });
290
+ }
@@ -0,0 +1,85 @@
1
+ import { warnFallbackFailure } from './warn-fallback-failure';
2
+
3
+ /**
4
+ * `null` when the environment cannot measure at all.
5
+ */
6
+ function measureWithProbe(_ref) {
7
+ var value = _ref.value,
8
+ container = _ref.container;
9
+ // Reached through the container rather than the global, so a popover in
10
+ // another document still measures and one with no view degrades gracefully.
11
+ var view = container.ownerDocument.defaultView;
12
+ if (!view) {
13
+ return null;
14
+ }
15
+ var probe = container.ownerDocument.createElement('div');
16
+ probe.style.position = 'absolute';
17
+ probe.style.visibility = 'hidden';
18
+ probe.style.pointerEvents = 'none';
19
+ probe.style.height = '0';
20
+ probe.style.padding = '0';
21
+ probe.style.border = '0';
22
+ // `width` clamps negatives to 0; use `margin-left` instead so signs are
23
+ // preserved. `getComputedStyle` returns the resolved pixel value for
24
+ // margin-* properties even when the source was a token / calc / var.
25
+ probe.style.marginLeft = value;
26
+ // Hidden from assistive tech and event targeting; defensive against
27
+ // `getElementsByTagName('div')` walks in consumer code.
28
+ probe.setAttribute('aria-hidden', 'true');
29
+ probe.setAttribute('inert', '');
30
+ try {
31
+ container.appendChild(probe);
32
+ var resolved = parseFloat(view.getComputedStyle(probe).marginLeft);
33
+ return Number.isFinite(resolved) ? resolved : 0;
34
+ } catch (error) {
35
+ warnFallbackFailure({
36
+ error: error
37
+ });
38
+ return null;
39
+ } finally {
40
+ // `remove()` rather than `removeChild()`: consumer code may already have
41
+ // detached the probe, which must not throw out of a layout effect.
42
+ probe.remove();
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Resolves a CSS length value to a pixel number using a hidden DOM probe.
48
+ *
49
+ * Used by the JS positioning fallback, where a consumer offset can be a number, a
50
+ * `${n}px` string, a token, a `calc()` expression, a viewport unit, and so on.
51
+ * Parsing arbitrary CSS in JS is fragile, and impossible for `calc(var(...))`
52
+ * without a layout context, so the browser does the math instead. The probe is
53
+ * appended to `container` so it inherits the popover's containing block, font
54
+ * size and custom-property scope. Anything unmeasurable resolves to `0`.
55
+ *
56
+ * Costs one synchronous style and layout flush per call that reaches the probe.
57
+ * Deliberately NOT memoised: a token can be retargeted by an override on the
58
+ * consumer's wrapper, a viewport unit changes on resize and an `em` on a font
59
+ * change, so no (container, value) cache can be proven fresh. The caller decides
60
+ * when to re-resolve; see `applyJavaScriptFallbackPositioning`.
61
+ */
62
+ export function resolveCssLengthToPixels(_ref2) {
63
+ var _measureWithProbe;
64
+ var value = _ref2.value,
65
+ container = _ref2.container;
66
+ if (typeof value === 'number') {
67
+ return value;
68
+ }
69
+
70
+ // Fast path: consumers occasionally pass `'0'` instead of the number `0`.
71
+ if (value === '0' || value === '-0') {
72
+ return 0;
73
+ }
74
+
75
+ // Fast path: bare `${n}px` strings come from `resolvePlacement`, so this avoids
76
+ // a reflow for the common case.
77
+ var pxMatch = value.match(/^(-?\d+(?:\.\d+)?)px$/);
78
+ if (pxMatch) {
79
+ return Number(pxMatch[1]);
80
+ }
81
+ return (_measureWithProbe = measureWithProbe({
82
+ value: value,
83
+ container: container
84
+ })) !== null && _measureWithProbe !== void 0 ? _measureWithProbe : 0;
85
+ }
@@ -0,0 +1,13 @@
1
+ import warnOnce from '@atlaskit/ds-lib/warn-once';
2
+
3
+ /**
4
+ * Callers on the JavaScript fallback path degrade rather than throw, so this is
5
+ * the only signal that a popover is not where it should be.
6
+ */
7
+ export function warnFallbackFailure(_ref) {
8
+ var error = _ref.error;
9
+ if (process.env.NODE_ENV === 'production') {
10
+ return;
11
+ }
12
+ warnOnce("@atlaskit/top-layer: the JavaScript positioning fallback hit an error, so this popover may be positioned incorrectly. ".concat(String(error)));
13
+ }
@@ -20,8 +20,9 @@ var DEFAULT_GAP = "var(--ds-space-100, 8px)";
20
20
 
21
21
  /**
22
22
  * Fully-resolved placement (internal). Use `resolvePlacement` for partial input.
23
- * `offset.gap` and `offset.crossAxisShift.value` are always CSS
24
- * length strings.
23
+ * `offset.gap`, `offset.crossAxisShift.value` and `minSize` are always CSS length
24
+ * strings. `minSize` stays `undefined` when unset, so "not specified" and an
25
+ * explicit `0` remain distinguishable.
25
26
  */
26
27
 
27
28
  /**
@@ -41,18 +42,25 @@ var DEFAULT_GAP = "var(--ds-space-100, 8px)";
41
42
  * Defaults: `axis: 'block'`, `edge: 'end'`, `align: 'center'`,
42
43
  * `offset.gap: token('space.100', '8px')`,
43
44
  * `offset.crossAxisShift: { value: '0px', direction: 'forwards' }`.
45
+ *
46
+ * `minSize` has no default here: whether it needs one depends on how the popover
47
+ * is sized, which only `useAnchoredPopover` knows.
44
48
  */
45
49
  export function resolvePlacement(_ref) {
46
- var _placement$offset, _placement$axis, _placement$edge, _placement$align, _placement$offset$cro, _placement$offset2, _placement$offset$cro2, _placement$offset3;
50
+ var _placement$axis, _placement$edge, _placement$align, _placement$offset$gap, _placement$offset, _placement$offset$cro, _placement$offset2, _placement$offset$cro2, _placement$offset3;
47
51
  var placement = _ref.placement;
48
- var consumerGap = (_placement$offset = placement.offset) === null || _placement$offset === void 0 ? void 0 : _placement$offset.gap;
49
52
  return {
50
53
  axis: (_placement$axis = placement.axis) !== null && _placement$axis !== void 0 ? _placement$axis : 'block',
51
54
  edge: (_placement$edge = placement.edge) !== null && _placement$edge !== void 0 ? _placement$edge : 'end',
52
55
  align: (_placement$align = placement.align) !== null && _placement$align !== void 0 ? _placement$align : 'center',
56
+ // Not `??`: `undefined` must survive, so "not specified" stays
57
+ // distinguishable from an explicit `0`.
58
+ minSize: placement.minSize === undefined ? undefined : toCssLengthString({
59
+ value: placement.minSize
60
+ }),
53
61
  offset: {
54
- gap: consumerGap === undefined ? DEFAULT_GAP : toCssLengthString({
55
- value: consumerGap
62
+ gap: toCssLengthString({
63
+ value: (_placement$offset$gap = (_placement$offset = placement.offset) === null || _placement$offset === void 0 ? void 0 : _placement$offset.gap) !== null && _placement$offset$gap !== void 0 ? _placement$offset$gap : DEFAULT_GAP
56
64
  }),
57
65
  crossAxisShift: {
58
66
  value: toCssLengthString({
@@ -0,0 +1,12 @@
1
+ import { createCssDeclarationProbe } from './supports-css-declaration';
2
+
3
+ /**
4
+ * Probes `anchor-name` and NOT `anchor-size()`: some browsers have positioning
5
+ * without sizing, which is what `supportsAnchorSize` is for.
6
+ */
7
+ // Annotated rather than inferred: `--isolatedDeclarations` cannot emit a type
8
+ // for a `const` initialised from a call.
9
+ export var supportsAnchorPositioning = createCssDeclarationProbe({
10
+ property: 'anchor-name',
11
+ value: '--a'
12
+ });
@@ -0,0 +1,12 @@
1
+ import { createCssDeclarationProbe } from './supports-css-declaration';
2
+
3
+ /**
4
+ * Separate from `supportsAnchorPositioning`: some browsers have anchor
5
+ * positioning without anchor sizing.
6
+ */
7
+ // Annotated rather than inferred: `--isolatedDeclarations` cannot emit a type
8
+ // for a `const` initialised from a call.
9
+ export var supportsAnchorSize = createCssDeclarationProbe({
10
+ property: 'min-inline-size',
11
+ value: 'anchor-size(self-inline)'
12
+ });
@@ -0,0 +1,19 @@
1
+ import once from '@atlaskit/ds-lib/once';
2
+
3
+ /**
4
+ * A lazy, cached `CSS.supports(property, value)` probe. Lazy so it is SSR safe
5
+ * and cannot cause a hydration mismatch: nothing is read until the first call,
6
+ * which happens in an effect. Cached because the answer cannot change within a
7
+ * page. `false` wherever `CSS.supports` is missing (SSR, jsdom), so a caller
8
+ * falls back rather than throws.
9
+ */
10
+ export function createCssDeclarationProbe(_ref) {
11
+ var property = _ref.property,
12
+ value = _ref.value;
13
+ return once(function supportsCssDeclaration() {
14
+ if (typeof window === 'undefined' || typeof CSS === 'undefined' || typeof CSS.supports !== 'function') {
15
+ return false;
16
+ }
17
+ return CSS.supports(property, value);
18
+ });
19
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,118 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
+ var _excluded = ["getPoint", "isEnabled"];
4
+ 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; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
+ /**
7
+ * Positioning and sizing for a popover anchored to a viewport COORDINATE. No
8
+ * positioning logic here: CSS Anchor Positioning needs a real element to carry
9
+ * `anchor-name`, so this hook creates a hidden, zero-size one at the requested
10
+ * point and delegates everything else to `useAnchoredPopover`.
11
+ *
12
+ * See `notes/decisions/anchored-popover-at-point.md` for why this is a sibling
13
+ * hook instead of an anchor union inside `useAnchoredPopover`.
14
+ */
15
+ import { useLayoutEffect, useRef } from 'react';
16
+ import { useAnchoredPopover } from './use-anchored-popover';
17
+
18
+ /**
19
+ * A point in viewport coordinates (CSS pixels). Axis-named rather than
20
+ * edge-named, because viewport coordinates are always physical.
21
+ */
22
+
23
+ /**
24
+ * Appended to `document.body` to escape any transformed ancestor, which would
25
+ * break viewport-relative resolution of `position: fixed`. `Object.assign`
26
+ * rather than `setStyle`: we own the element, so there is nothing to restore.
27
+ */
28
+ function mountAnchorElementAtPoint(_ref) {
29
+ var point = _ref.point;
30
+ var element = document.createElement('div');
31
+ element.setAttribute('aria-hidden', 'true');
32
+ Object.assign(element.style, {
33
+ position: 'fixed',
34
+ // Physical insets: `inset-inline-start` would need `viewportWidth - point.x`
35
+ // in RTL.
36
+ top: "".concat(point.y, "px"),
37
+ left: "".concat(point.x, "px"),
38
+ // Zero size, so placement alone decides which side the popover sits on.
39
+ width: '0',
40
+ height: '0',
41
+ pointerEvents: 'none'
42
+ });
43
+ document.body.appendChild(element);
44
+ return {
45
+ element: element,
46
+ cleanup: function cleanup() {
47
+ element.remove();
48
+ }
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Positions and sizes a popover at a viewport coordinate. Takes every option
54
+ * `useAnchoredPopover` does except `anchorRef`, with the same meaning. One caveat
55
+ * is specific to a point: it has no size, so `'match-anchor'` and `'min-anchor'`
56
+ * resolve to zero against it.
57
+ *
58
+ * A consumer that anchors to an element sometimes and to a point at other times
59
+ * calls BOTH hooks with complementary `isEnabled` values, since a hook cannot be
60
+ * called conditionally. Nothing enforces that; see `isEnabled` on
61
+ * `useAnchoredPopover` for why two enabled hooks on one popover is a bug.
62
+ *
63
+ * @example Anchor at the cursor, falling back to the trigger element
64
+ * ```tsx
65
+ * const shared = { popoverRef, placement, isOpen };
66
+ *
67
+ * useAnchoredPopover({ ...shared, anchorRef: triggerRef, isEnabled: !mousePos });
68
+ * useAnchoredPopoverAtPoint({
69
+ * ...shared,
70
+ * isEnabled: Boolean(mousePos),
71
+ * // Read through a ref: `getPoint` is latched, see below.
72
+ * getPoint: () => mousePosRef.current,
73
+ * });
74
+ * ```
75
+ */
76
+ export function useAnchoredPopoverAtPoint(_ref2) {
77
+ var getPoint = _ref2.getPoint,
78
+ _ref2$isEnabled = _ref2.isEnabled,
79
+ isEnabled = _ref2$isEnabled === void 0 ? true : _ref2$isEnabled,
80
+ rest = _objectWithoutProperties(_ref2, _excluded);
81
+ var anchorRef = useRef(null);
82
+
83
+ // Latched, keyed on `isEnabled`: consumers read mutable refs inside `getPoint`,
84
+ // so re-reading it would move an already-open popover, and keying on
85
+ // `getPoint`'s identity would re-latch on every parent render.
86
+ var getPointRef = useRef(getPoint);
87
+ getPointRef.current = getPoint;
88
+
89
+ // Declared BEFORE `useAnchoredPopover` below, so the element exists when that
90
+ // hook's effect reads the ref (React runs layout effects in source order). This
91
+ // effect must not depend on anything the delegated effect does not: a dependency
92
+ // here alone re-creates the element without that hook writing an `anchor-name`
93
+ // onto it, and positioning silently dies.
94
+ useLayoutEffect(function () {
95
+ if (!isEnabled) {
96
+ return;
97
+ }
98
+ var point = getPointRef.current();
99
+ if (!point) {
100
+ return;
101
+ }
102
+ var _mountAnchorElementAt = mountAnchorElementAtPoint({
103
+ point: point
104
+ }),
105
+ element = _mountAnchorElementAt.element,
106
+ unmountAnchorElement = _mountAnchorElementAt.cleanup;
107
+ anchorRef.current = element;
108
+ return function cleanup() {
109
+ unmountAnchorElement();
110
+ anchorRef.current = null;
111
+ };
112
+ // The latch. See `getPointRef` above: `getPoint` is deliberately absent.
113
+ }, [isEnabled]);
114
+ useAnchoredPopover(_objectSpread(_objectSpread({}, rest), {}, {
115
+ anchorRef: anchorRef,
116
+ isEnabled: isEnabled
117
+ }));
118
+ }