@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
@@ -13,9 +13,9 @@
13
13
  * - Popover open state tracked via `data-popover-open` (no `:popover-open` in jsdom).
14
14
  * - Auto popovers: maintain an open stack, perform light-dismiss on document mousedown
15
15
  * outside the open chain, and dismiss the topmost on Escape.
16
- * - On open, removes any inline `opacity: 0` from the popover element so callers that
17
- * rely on a `ResizeObserver` reveal callback (e.g. `use-anchor-position`) become
18
- * visible in jsdom, where layout-driven RO callbacks never fire.
16
+ * - On open, removes any inline `opacity: 0` from the popover element, so the
17
+ * JavaScript positioning fallback's `ResizeObserver` reveal is not needed in
18
+ * jsdom, where its non-zero-layout guard can never pass.
19
19
  *
20
20
  * Companion `toBeVisible` patch lives at `./to-be-visible`.
21
21
  *
@@ -256,10 +256,9 @@ function stackForMode({
256
256
  return null;
257
257
  }
258
258
 
259
- // Polyfill-only. Real browsers reveal the popover once `ResizeObserver`
260
- // callbacks fire after the first layout pass. jsdom has no layout, so we clear
261
- // any inline `opacity: 0` (set by callers like `use-anchor-position`) once
262
- // the open toggle has fired.
259
+ // Polyfill-only. Real browsers reveal the popover on the first non-zero layout
260
+ // their `ResizeObserver` reports; jsdom always reports 0, so that guard never
261
+ // passes and the inline `opacity: 0` is cleared on the open toggle instead.
263
262
  function clearOpacityOnOpenToggle({
264
263
  element
265
264
  }) {
@@ -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
  var surfaceResetStyles = {
23
24
  root: "_lcxv1wug _o5724jg8 _1nmz4jg8 _1i4q4jg8 _y3gnv2br _azhwidpf _1p1dglyw"
24
25
  };
26
+ true;
25
27
  var 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-".concat(useId());
17
13
  }
@@ -0,0 +1,80 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ /**
3
+ * The CSS Anchor Positioning path: `position-anchor`, `position-area`,
4
+ * `position-try-fallbacks` and the gap / shift margins, all declared on the
5
+ * popover so the browser owns placement and flipping. The twin of
6
+ * `../javascript-fallback/apply-javascript-fallback-positioning`.
7
+ */
8
+
9
+ import { setStyle } from '../set-style';
10
+ import { crossAxisShiftMargins } from './cross-axis-shift-margins';
11
+ import { edgeMargin } from './edge-margin';
12
+ import { getFitMarginDeclarations } from './fit-margins';
13
+ import { getExistingAnchorName } from './get-existing-anchor-name';
14
+ import { placementToPositionArea } from './placement-to-position-area';
15
+ import { placementToTryFallbacks } from './placement-to-try-fallbacks';
16
+
17
+ /**
18
+ * Returns the cleanup.
19
+ */
20
+ export function applyAnchorPositioning(_ref) {
21
+ var anchor = _ref.anchor,
22
+ popover = _ref.popover,
23
+ placement = _ref.placement,
24
+ sizeStyles = _ref.sizeStyles,
25
+ isFitting = _ref.isFitting,
26
+ fallbackAnchorName = _ref.fallbackAnchorName;
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
+ var existingAnchorName = getExistingAnchorName(anchor);
30
+ var anchorName = existingAnchorName !== null && existingAnchorName !== void 0 ? existingAnchorName : fallbackAnchorName;
31
+ var gap = edgeMargin({
32
+ placement: placement,
33
+ offset: placement.offset.gap
34
+ });
35
+ var crossAxisShift = crossAxisShiftMargins({
36
+ placement: placement,
37
+ crossAxisShiftCssValue: placement.offset.crossAxisShift.value,
38
+ direction: placement.offset.crossAxisShift.direction
39
+ });
40
+ var popoverStyles = [{
41
+ property: 'position-anchor',
42
+ value: anchorName
43
+ }, {
44
+ property: 'position-area',
45
+ value: placementToPositionArea({
46
+ placement: placement
47
+ })
48
+ }, {
49
+ property: 'position-try-fallbacks',
50
+ value: placementToTryFallbacks({
51
+ placement: 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].concat(_toConsumableArray(isFitting ? getFitMarginDeclarations({
63
+ placement: placement,
64
+ crossAxisShiftMargins: crossAxisShift
65
+ }) : crossAxisShift), _toConsumableArray(sizeStyles));
66
+
67
+ // Anchor names are never cleaned up: multiple popovers can share an anchor,
68
+ // and even with reference counting an async React update could remove the name
69
+ // while something else still uses it. See
70
+ // `notes/decisions/anchor-name-lifetime.md`. Only written when minted: a name
71
+ // from a stylesheet may be a list, which an inline copy of its first entry
72
+ // would collapse.
73
+ if (existingAnchorName === null) {
74
+ anchor.style.setProperty('anchor-name', anchorName);
75
+ }
76
+ return setStyle({
77
+ element: popover,
78
+ styles: popoverStyles
79
+ });
80
+ }
@@ -0,0 +1,28 @@
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(_ref) {
13
+ var placement = _ref.placement,
14
+ crossAxisShiftCssValue = _ref.crossAxisShiftCssValue,
15
+ direction = _ref.direction;
16
+ var crossAxis = placement.axis === 'block' ? 'inline' : 'block';
17
+ // `calc()` rather than JavaScript math, so opaque values such as design tokens
18
+ // negate correctly.
19
+ var negated = "calc(-1 * ".concat(crossAxisShiftCssValue, ")");
20
+ var isForwards = direction === 'forwards';
21
+ return [{
22
+ property: "margin-".concat(crossAxis, "-start"),
23
+ value: isForwards ? crossAxisShiftCssValue : negated
24
+ }, {
25
+ property: "margin-".concat(crossAxis, "-end"),
26
+ value: isForwards ? negated : crossAxisShiftCssValue
27
+ }];
28
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The margin that creates the gap between the popover and its anchor.
3
+ */
4
+ export function edgeMargin(_ref) {
5
+ var placement = _ref.placement,
6
+ offset = _ref.offset;
7
+ var axis = placement.axis,
8
+ edge = placement.edge;
9
+ // The side FACING the anchor is always opposite the placement edge.
10
+ var anchorFacingEdge = edge === 'end' ? 'start' : 'end';
11
+ return {
12
+ property: "margin-".concat(axis, "-").concat(anchorFacingEdge),
13
+ value: offset
14
+ };
15
+ }
@@ -0,0 +1,71 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import { VIEWPORT_PADDING } from '../anchored-popover-size';
3
+
4
+ /**
5
+ * Every logical margin property the fit recipe can write. Declared here and
6
+ * imported by `edge-margin.tsx` and `cross-axis-shift-margins.tsx`: two
7
+ * structurally identical declarations are interchangeable to the compiler, so a
8
+ * divergence would surface only as a silently-ignored property name at
9
+ * `setStyle`.
10
+ */
11
+
12
+ /**
13
+ * The cross-axis sides that face the viewport rather than the anchor, and so take
14
+ * the reserved padding.
15
+ *
16
+ * A `span-*` cell aligns the popover's MARGIN box to the anchor's edge, so padding
17
+ * on the anchor-facing side does not reserve space at the viewport: it insets the
18
+ * border box from the edge the consumer asked it to line up with. A centred
19
+ * popover has no anchor-facing side, and padding both keeps the shift pair
20
+ * antisymmetric so `anchor-center` still centres.
21
+ *
22
+ * Known cost: the slide fallbacks do not swap the inline margins the way
23
+ * `flip-inline` does, so a slid popover keeps its padding on the anchor side (or,
24
+ * re-centred, sits 2.5px off centre). Alignment in the requested position wins.
25
+ */
26
+ function viewportFacingCrossAxisSides(_ref) {
27
+ var align = _ref.align;
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(_ref2) {
49
+ var placement = _ref2.placement,
50
+ crossAxisShiftMargins = _ref2.crossAxisShiftMargins;
51
+ var crossAxis = placement.axis === 'block' ? 'inline' : 'block';
52
+ var paddedProperties = new Set(viewportFacingCrossAxisSides({
53
+ align: placement.align
54
+ }).map(function (side) {
55
+ return "margin-".concat(crossAxis, "-").concat(side);
56
+ }));
57
+ return [{
58
+ property: "margin-".concat(placement.axis, "-").concat(placement.edge),
59
+ value: VIEWPORT_PADDING
60
+ }].concat(_toConsumableArray(crossAxisShiftMargins.map(function (_ref3) {
61
+ var property = _ref3.property,
62
+ value = _ref3.value;
63
+ return paddedProperties.has(property) ? {
64
+ property: property,
65
+ value: "calc(".concat(value, " + ").concat(VIEWPORT_PADDING, ")")
66
+ } : {
67
+ property: property,
68
+ value: value
69
+ };
70
+ })));
71
+ }
@@ -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
+ var declared = element.style.getPropertyValue('anchor-name') || getComputedStyle(element).getPropertyValue('anchor-name');
10
+ var 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,19 +10,20 @@ 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(_ref) {
21
23
  var placement = _ref.placement;
22
- var _resolvePlacement = resolvePlacement({
23
- placement: placement
24
- }),
25
- axis = _resolvePlacement.axis,
26
- edge = _resolvePlacement.edge,
27
- align = _resolvePlacement.align;
24
+ var axis = placement.axis,
25
+ edge = placement.edge,
26
+ align = placement.align;
28
27
  var edgeValue = "".concat(axis, "-").concat(edge);
29
28
  if (align === 'center') {
30
29
  return edgeValue;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * The placement axis joined to its edge, e.g. `block-end`.
3
+ */
4
+
5
+ /**
6
+ * The cells to slide to along one edge, on the cross axis.
7
+ *
8
+ * A try option is only worth listing if its margin box can fit where an earlier
9
+ * one did not, and two options over the SAME cell have the same margin box and
10
+ * the same fit result. That rules out three cells for an aligned placement: the
11
+ * near span on the requested edge IS the base position-area, and on the opposite
12
+ * edge the near and far spans are where `flip-{A}` and the diagonal flip land.
13
+ *
14
+ * A centred placement has no near or far, so it slides to both spans on both
15
+ * edges.
16
+ */
17
+ function slideOnEdge(_ref) {
18
+ var align = _ref.align,
19
+ edgeKey = _ref.edgeKey,
20
+ crossAxis = _ref.crossAxis,
21
+ isOppositeEdge = _ref.isOppositeEdge;
22
+ if (align === 'center') {
23
+ return ["".concat(edgeKey, " span-").concat(crossAxis, "-end"), "".concat(edgeKey, " span-").concat(crossAxis, "-start")];
24
+ }
25
+ if (isOppositeEdge) {
26
+ return [edgeKey];
27
+ }
28
+ // For `align: 'start'` the popover expands toward `end`, so the far span is
29
+ // `start`; `align: 'end'` mirrors this.
30
+ var far = align === 'start' ? 'start' : 'end';
31
+ return [edgeKey, "".concat(edgeKey, " span-").concat(crossAxis, "-").concat(far)];
32
+ }
33
+ function flipDiagonally(_ref2) {
34
+ var axis = _ref2.axis;
35
+ var flipOnMainAxis = axis === 'block' ? 'flip-block' : 'flip-inline';
36
+ var flipToCrossAxis = axis === 'block' ? 'flip-inline' : 'flip-block';
37
+ return "".concat(flipOnMainAxis, " ").concat(flipToCrossAxis);
38
+ }
39
+
40
+ /**
41
+ * Returns the `position-try-fallbacks` value for a RESOLVED placement. The
42
+ * browser tries the entries in order and uses the first one whose margin box
43
+ * fits, so an entry over a cell already tried can never be chosen; `slideOnEdge`
44
+ * explains which ones those are.
45
+ *
46
+ * Centred: slide along the requested edge to either span, flip to the opposite
47
+ * edge, then slide along that edge to either span.
48
+ *
49
+ * Start / end aligned: slide along the requested edge (re-centred, then the far
50
+ * span), flip to the opposite edge keeping the alignment, flip on the diagonal
51
+ * into the opposite corner, then re-centre on the opposite edge. The diagonal is
52
+ * the clean escape when the popover would land off screen on both axes: a flip
53
+ * keyword carries the gap and shift margins across, where a position-area slide
54
+ * leaves them on the original sides.
55
+ *
56
+ * @example
57
+ * // Centred on the block-end edge (the default placement):
58
+ * placementToTryFallbacks({ placement: resolvePlacement({ placement: {} }) });
59
+ * // => 'block-end span-inline-end, block-end span-inline-start, flip-block,
60
+ * // block-start span-inline-end, block-start span-inline-start'
61
+ *
62
+ * // Start-aligned on the block-end edge:
63
+ * placementToTryFallbacks({ placement: resolvePlacement({ placement: { align: 'start' } }) });
64
+ * // => 'block-end, block-end span-inline-start, flip-block, flip-block flip-inline, block-start'
65
+ */
66
+ export function placementToTryFallbacks(_ref3) {
67
+ var placement = _ref3.placement;
68
+ var axis = placement.axis,
69
+ edge = placement.edge,
70
+ align = placement.align;
71
+ var crossAxis = axis === 'block' ? 'inline' : 'block';
72
+ var flippedEdge = edge === 'start' ? 'end' : 'start';
73
+ var sameEdgeArea = "".concat(axis, "-").concat(edge);
74
+ var oppositeEdgeArea = "".concat(axis, "-").concat(flippedEdge);
75
+ var flipOnMainAxis = axis === 'block' ? 'flip-block' : 'flip-inline';
76
+ return [slideOnEdge({
77
+ align: align,
78
+ edgeKey: sameEdgeArea,
79
+ crossAxis: crossAxis,
80
+ isOppositeEdge: false
81
+ }), flipOnMainAxis, align !== 'center' ? flipDiagonally({
82
+ axis: axis
83
+ }) : null, slideOnEdge({
84
+ align: align,
85
+ edgeKey: oppositeEdgeArea,
86
+ crossAxis: crossAxis,
87
+ isOppositeEdge: true
88
+ })].filter(Boolean).flat().join(', ');
89
+ }