@atlaskit/top-layer 0.12.0 → 0.12.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 (79) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/cjs/entry-points/use-arrow-navigation.js +3 -2
  3. package/dist/cjs/internal/placement-to-position-area.js +43 -0
  4. package/dist/cjs/internal/placement-to-try-fallbacks.js +82 -0
  5. package/dist/cjs/internal/use-anchor-position.js +18 -131
  6. package/dist/cjs/popup/popup-content.js +2 -2
  7. package/dist/cjs/popup/popup-context.js +2 -38
  8. package/dist/cjs/popup/popup-provider.js +15 -0
  9. package/dist/cjs/popup/popup-trigger-function.js +2 -2
  10. package/dist/cjs/popup/popup-trigger.js +2 -2
  11. package/dist/cjs/popup/popup.js +2 -2
  12. package/dist/cjs/popup/use-maybe-popup-context.js +17 -0
  13. package/dist/cjs/popup/use-popup-context.js +20 -0
  14. package/dist/cjs/use-arrow-navigation/index.js +3 -2
  15. package/dist/cjs/use-arrow-navigation/is-at-current-menu-level.js +87 -0
  16. package/dist/cjs/use-arrow-navigation/use-arrow-navigation.js +0 -82
  17. package/dist/es2019/entry-points/use-arrow-navigation.js +2 -1
  18. package/dist/es2019/internal/placement-to-position-area.js +40 -0
  19. package/dist/es2019/internal/placement-to-try-fallbacks.js +77 -0
  20. package/dist/es2019/internal/use-anchor-position.js +2 -116
  21. package/dist/es2019/popup/popup-content.js +2 -1
  22. package/dist/es2019/popup/popup-context.js +2 -36
  23. package/dist/es2019/popup/popup-provider.js +10 -0
  24. package/dist/es2019/popup/popup-trigger-function.js +1 -1
  25. package/dist/es2019/popup/popup-trigger.js +2 -1
  26. package/dist/es2019/popup/popup.js +1 -1
  27. package/dist/es2019/popup/use-maybe-popup-context.js +12 -0
  28. package/dist/es2019/popup/use-popup-context.js +15 -0
  29. package/dist/es2019/use-arrow-navigation/index.js +2 -1
  30. package/dist/es2019/use-arrow-navigation/is-at-current-menu-level.js +81 -0
  31. package/dist/es2019/use-arrow-navigation/use-arrow-navigation.js +0 -81
  32. package/dist/esm/entry-points/use-arrow-navigation.js +2 -1
  33. package/dist/esm/internal/placement-to-position-area.js +38 -0
  34. package/dist/esm/internal/placement-to-try-fallbacks.js +76 -0
  35. package/dist/esm/internal/use-anchor-position.js +16 -127
  36. package/dist/esm/popup/popup-content.js +2 -1
  37. package/dist/esm/popup/popup-context.js +2 -36
  38. package/dist/esm/popup/popup-provider.js +10 -0
  39. package/dist/esm/popup/popup-trigger-function.js +1 -1
  40. package/dist/esm/popup/popup-trigger.js +2 -1
  41. package/dist/esm/popup/popup.js +1 -1
  42. package/dist/esm/popup/use-maybe-popup-context.js +12 -0
  43. package/dist/esm/popup/use-popup-context.js +15 -0
  44. package/dist/esm/use-arrow-navigation/index.js +2 -1
  45. package/dist/esm/use-arrow-navigation/is-at-current-menu-level.js +81 -0
  46. package/dist/esm/use-arrow-navigation/use-arrow-navigation.js +0 -81
  47. package/dist/types/entry-points/use-arrow-navigation.d.ts +2 -1
  48. package/dist/types/internal/placement-to-position-area.d.ts +21 -0
  49. package/dist/types/internal/placement-to-try-fallbacks.d.ts +36 -0
  50. package/dist/types/internal/use-anchor-position.d.ts +0 -55
  51. package/dist/types/popup/popup-context.d.ts +1 -21
  52. package/dist/types/popup/popup-provider.d.ts +10 -0
  53. package/dist/types/popup/use-maybe-popup-context.d.ts +8 -0
  54. package/dist/types/popup/use-popup-context.d.ts +7 -0
  55. package/dist/types/use-arrow-navigation/index.d.ts +2 -1
  56. package/dist/types/use-arrow-navigation/is-at-current-menu-level.d.ts +48 -0
  57. package/dist/types/use-arrow-navigation/use-arrow-navigation.d.ts +0 -48
  58. package/dist/types-ts4.5/entry-points/use-arrow-navigation.d.ts +2 -1
  59. package/dist/types-ts4.5/internal/placement-to-position-area.d.ts +21 -0
  60. package/dist/types-ts4.5/internal/placement-to-try-fallbacks.d.ts +36 -0
  61. package/dist/types-ts4.5/internal/use-anchor-position.d.ts +0 -55
  62. package/dist/types-ts4.5/popup/popup-context.d.ts +1 -21
  63. package/dist/types-ts4.5/popup/popup-provider.d.ts +10 -0
  64. package/dist/types-ts4.5/popup/use-maybe-popup-context.d.ts +8 -0
  65. package/dist/types-ts4.5/popup/use-popup-context.d.ts +7 -0
  66. package/dist/types-ts4.5/use-arrow-navigation/index.d.ts +2 -1
  67. package/dist/types-ts4.5/use-arrow-navigation/is-at-current-menu-level.d.ts +48 -0
  68. package/dist/types-ts4.5/use-arrow-navigation/use-arrow-navigation.d.ts +0 -48
  69. package/package.json +2 -2
  70. package/dist/cjs/arrow/index.js +0 -131
  71. package/dist/cjs/arrow/types.js +0 -5
  72. package/dist/es2019/arrow/index.js +0 -269
  73. package/dist/es2019/arrow/types.js +0 -1
  74. package/dist/esm/arrow/index.js +0 -124
  75. package/dist/esm/arrow/types.js +0 -1
  76. package/dist/types/arrow/index.d.ts +0 -24
  77. package/dist/types/arrow/types.d.ts +0 -29
  78. package/dist/types-ts4.5/arrow/index.d.ts +0 -24
  79. package/dist/types-ts4.5/arrow/types.d.ts +0 -29
@@ -3,7 +3,6 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.isAtCurrentMenuLevel = isAtCurrentMenuLevel;
7
6
  exports.useArrowNavigation = useArrowNavigation;
8
7
  var _react = require("react");
9
8
  var _bindEventListener = require("bind-event-listener");
@@ -12,86 +11,6 @@ var _keycodes = require("@atlaskit/ds-lib/keycodes");
12
11
  var _focus = require("../focus");
13
12
  var menuScope = '[role="menu"]';
14
13
 
15
- /**
16
- * Returns true if the element belongs to the current menu level of the
17
- * container, i.e. it is NOT inside a nested `role="menu"` sub-container.
18
- *
19
- * Uses `element.closest('[role="menu"]')` to determine the nearest menu
20
- * scope. If the nearest scope is the container's own `role="menu"` element,
21
- * the element belongs to this level. If it is a different (nested) menu,
22
- * the element belongs to a child level and should be excluded.
23
- *
24
- * Supports two container patterns:
25
- *
26
- * **1. Container has `role="menu"` directly** (preferred, used by avatar-group):
27
- * ```
28
- * <div ref={containerRef} role="menu">
29
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → true
30
- * <div role="menu"> ← nested sub-menu
31
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → false
32
- * </div>
33
- * </div>
34
- * ```
35
- * Uses the simple check: `element.closest('[role="menu"]') === container`
36
- *
37
- * **2. Container is a wrapper without `role="menu"`** (used by dropdown-menu,
38
- * where `MenuGroup` owns `role="menu"` but does not accept a ref):
39
- * ```
40
- * <div ref={containerRef}> ← wrapper (no role="menu")
41
- * <MenuGroup role="menu">
42
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → true
43
- * <div role="menu"> ← nested sub-menu
44
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → false
45
- * </div>
46
- * </MenuGroup>
47
- * </div>
48
- * ```
49
- * Falls back to walking the DOM to check that the element's closest
50
- * `role="menu"` is the container's own menu (not doubly nested).
51
- *
52
- * Ideally all consumers should use pattern 1 (place the ref directly on the
53
- * `role="menu"` element). Pattern 2 exists because `@atlaskit/menu`'s
54
- * `MenuGroup` does not currently accept a ref. If `MenuGroup` gains ref
55
- * support in the future, dropdown-menu should switch to pattern 1 and the
56
- * fallback path can be removed.
57
- *
58
- * Used both as a `TFocusableFilter` (to scope getNextFocusable etc.) and as
59
- * a guard to prevent parent menu handlers from acting on events that
60
- * belong to a nested menu.
61
- */
62
- function isAtCurrentMenuLevel(element, container) {
63
- var closestMenu = element.closest(menuScope);
64
-
65
- // Container itself has role="menu". Element belongs to this level
66
- // if its closest menu is the container.
67
- if (container.getAttribute('role') === 'menu') {
68
- return closestMenu === container;
69
- }
70
-
71
- // Container is a wrapper without role="menu" (e.g. dropdown-menu
72
- // wraps a MenuGroup). The element belongs to the top level if its
73
- // closest role="menu" ancestor is a direct child of the container,
74
- // i.e. there is no nested role="menu" between them.
75
- if (closestMenu === null) {
76
- return true;
77
- }
78
-
79
- // Walk up from the closest menu to see if it is "owned" by this
80
- // container without passing through another role="menu" first.
81
- var parent = closestMenu.parentElement;
82
- while (parent && parent !== container) {
83
- if (parent.getAttribute('role') === 'menu') {
84
- // There is a parent menu between the closest menu and the
85
- // container, so the element is in a nested sub-menu.
86
- return false;
87
- }
88
- parent = parent.parentElement;
89
- }
90
-
91
- // The closest menu is directly inside the container (no nesting).
92
- return parent === container;
93
- }
94
-
95
14
  /**
96
15
  * Args for `useArrowNavigation`.
97
16
  *
@@ -142,7 +61,6 @@ function isAtCurrentMenuLevel(element, container) {
142
61
  * });
143
62
  * ```
144
63
  */
145
- // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
146
64
  function useArrowNavigation(_ref) {
147
65
  var containerRef = _ref.containerRef,
148
66
  onClose = _ref.onClose,
@@ -1 +1,2 @@
1
- export { useArrowNavigation, isAtCurrentMenuLevel } from '../use-arrow-navigation';
1
+ export { useArrowNavigation } from '../use-arrow-navigation';
2
+ export { isAtCurrentMenuLevel } from '../use-arrow-navigation/is-at-current-menu-level';
@@ -0,0 +1,40 @@
1
+ import { getPlacement } from './resolve-placement';
2
+
3
+ /**
4
+ * Resolves placement options to CSS `position-area` syntax.
5
+ *
6
+ * The `align` field represents the *visual* alignment:
7
+ * - `align: 'start'` means "aligned to the trigger's start edge" (left in LTR for block axis)
8
+ * - `align: 'end'` means "aligned to the trigger's end edge"
9
+ * - `align: 'center'` means centered (single-axis position-area)
10
+ *
11
+ * CSS `position-area` uses `span-*` keywords which indicate the *expansion direction*.
12
+ * Visual start-alignment requires `span-{cross-axis}-end` (expand toward end = start-aligned).
13
+ * This function handles the inversion internally.
14
+ *
15
+ * @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'
19
+ */
20
+ export function placementToPositionArea({
21
+ placement
22
+ }) {
23
+ const {
24
+ axis,
25
+ edge,
26
+ align
27
+ } = getPlacement({
28
+ placement
29
+ });
30
+ const edgeValue = `${axis}-${edge}`;
31
+ if (align === 'center') {
32
+ return edgeValue;
33
+ }
34
+
35
+ // Cross-axis name: block ↔ inline
36
+ const crossAxis = axis === 'block' ? 'inline' : 'block';
37
+ // Invert align → CSS span direction: visual 'start' → span toward 'end'
38
+ const spanDir = align === 'start' ? 'end' : 'start';
39
+ return `${edgeValue} span-${crossAxis}-${spanDir}`;
40
+ }
@@ -0,0 +1,77 @@
1
+ import { getPlacement } from './resolve-placement';
2
+
3
+ /**
4
+ * Returns `position-try-fallbacks` value based on placement.
5
+ *
6
+ * For centered placements, includes cross-axis aligned position-area
7
+ * values so the browser can shift the popover along the cross-axis
8
+ * when the centered position would overflow the viewport edge.
9
+ *
10
+ * Fallback order for centered placements (e.g. `block-end`):
11
+ * 1. Same edge, start-aligned (shift for near-edge overflow)
12
+ * 2. Same edge, end-aligned (shift for far-edge overflow)
13
+ * 3. Flipped edge, centered
14
+ * 4. Flipped edge, start-aligned
15
+ * 5. Flipped edge, end-aligned
16
+ *
17
+ * For aligned placements (start/end), includes the same-edge centered
18
+ * position as fallbacks so the browser can try centering when the aligned
19
+ * position would overflow, before flipping to the opposite edge.
20
+ *
21
+ * **Why `flip-inline flip-block` (or `flip-block flip-inline`) is included:**
22
+ *
23
+ * When a popup is in a viewport corner (e.g. inline-end align-end at the
24
+ * top-right), flipping only the primary axis (`flip-inline`) moves the popup
25
+ * to the left but keeps the same cross-axis span direction (e.g. upward),
26
+ * which is still off-screen. The combined keyword `flip-inline flip-block`
27
+ * mirrors the popup diagonally. It flips BOTH the position-area AND both
28
+ * inline and block margins simultaneously, placing the popup at the
29
+ * diagonally opposite corner where there is always space.
30
+ *
31
+ * Named `position-area` entries (e.g. `inline-start span-block-end`) in
32
+ * `position-try-fallbacks` do NOT update margins, so only the `flip-*`
33
+ * tactic keywords correctly flip the gap margin along with the position.
34
+ */
35
+ export function placementToTryFallbacks({
36
+ placement
37
+ }) {
38
+ const {
39
+ axis,
40
+ edge,
41
+ align
42
+ } = getPlacement({
43
+ placement
44
+ });
45
+ const crossAxis = axis === 'block' ? 'inline' : 'block';
46
+ const flippedEdge = edge === 'start' ? 'end' : 'start';
47
+ const sameEdge = `${axis}-${edge}`;
48
+ const oppositeEdge = `${axis}-${flippedEdge}`;
49
+ const flipKeyword = axis === 'block' ? 'flip-block' : 'flip-inline';
50
+ const flipCrossKeyword = axis === 'block' ? 'flip-inline' : 'flip-block';
51
+
52
+ // Single algorithm: produce the shift list against any base alignment.
53
+ // For `align: 'start'` the "near" shift expands toward `end`; for
54
+ // `align: 'end'` it expands toward `start`; for `align: 'center'`
55
+ // either order is symmetric (we use end-then-start to match the
56
+ // historical fallback list).
57
+ function shifts(baseAlign, edgeKey) {
58
+ if (baseAlign === 'center') {
59
+ return [`${edgeKey} span-${crossAxis}-end`, `${edgeKey} span-${crossAxis}-start`];
60
+ }
61
+ const near = baseAlign === 'start' ? 'end' : 'start';
62
+ const far = baseAlign === 'start' ? 'start' : 'end';
63
+ return [`${edgeKey} span-${crossAxis}-${near}`, edgeKey, `${edgeKey} span-${crossAxis}-${far}`];
64
+ }
65
+ if (align !== 'center') {
66
+ // Aligned (start/end): try same-edge shifts, then single-axis flip,
67
+ // then diagonal flip for corner overflow, then opposite-edge shifts.
68
+ // Legacy Popper fallback ordering for e.g. bottom-start:
69
+ // [bottom, bottom-end, top-start, top, top-end, auto].
70
+ return [...shifts(align, sameEdge), flipKeyword, `${flipKeyword} ${flipCrossKeyword}`, ...shifts(align, oppositeEdge)].join(', ');
71
+ }
72
+
73
+ // Centered: same-edge shifts, single-axis flip, opposite-edge shifts.
74
+ // Diagonal flip is unnecessary because centered placements expand
75
+ // equally in both cross-axis directions.
76
+ return [...shifts('center', sameEdge), flipKeyword, ...shifts('center', oppositeEdge)].join(', ');
77
+ }
@@ -18,6 +18,8 @@ import rafSchedule from 'raf-schd';
18
18
  import once from '@atlaskit/ds-lib/once';
19
19
  import { computeFallbackPosition } from './anchor-positioning-fallback';
20
20
  import { combine } from './combine';
21
+ import { placementToPositionArea } from './placement-to-position-area';
22
+ import { placementToTryFallbacks } from './placement-to-try-fallbacks';
21
23
  import { resolveCssLengthToPixels } from './resolve-css-length-to-pixels';
22
24
  import { getPlacement } from './resolve-placement';
23
25
  import { setStyle } from './set-style';
@@ -36,122 +38,6 @@ const NoopResizeObserver = class {
36
38
  disconnect() {}
37
39
  };
38
40
 
39
- /**
40
- * Resolves placement options to CSS `position-area` syntax.
41
- *
42
- * The `align` field represents the *visual* alignment:
43
- * - `align: 'start'` means "aligned to the trigger's start edge" (left in LTR for block axis)
44
- * - `align: 'end'` means "aligned to the trigger's end edge"
45
- * - `align: 'center'` means centered (single-axis position-area)
46
- *
47
- * CSS `position-area` uses `span-*` keywords which indicate the *expansion direction*.
48
- * Visual start-alignment requires `span-{cross-axis}-end` (expand toward end = start-aligned).
49
- * This function handles the inversion internally.
50
- *
51
- * @example
52
- * placementToPositionArea({ placement: { axis: 'block', edge: 'end', align: 'center' } }) // → 'block-end'
53
- * placementToPositionArea({ placement: { axis: 'block', edge: 'end', align: 'start' } }) // → 'block-end span-inline-end'
54
- * placementToPositionArea({ placement: { axis: 'block', edge: 'end', align: 'end' } }) // → 'block-end span-inline-start'
55
- */
56
- export function placementToPositionArea({
57
- placement
58
- }) {
59
- const {
60
- axis,
61
- edge,
62
- align
63
- } = getPlacement({
64
- placement
65
- });
66
- const edgeValue = `${axis}-${edge}`;
67
- if (align === 'center') {
68
- return edgeValue;
69
- }
70
-
71
- // Cross-axis name: block ↔ inline
72
- const crossAxis = axis === 'block' ? 'inline' : 'block';
73
- // Invert align → CSS span direction: visual 'start' → span toward 'end'
74
- const spanDir = align === 'start' ? 'end' : 'start';
75
- return `${edgeValue} span-${crossAxis}-${spanDir}`;
76
- }
77
-
78
- /**
79
- * Returns `position-try-fallbacks` value based on placement.
80
- *
81
- * For centered placements, includes cross-axis aligned position-area
82
- * values so the browser can shift the popover along the cross-axis
83
- * when the centered position would overflow the viewport edge.
84
- *
85
- * Fallback order for centered placements (e.g. `block-end`):
86
- * 1. Same edge, start-aligned (shift for near-edge overflow)
87
- * 2. Same edge, end-aligned (shift for far-edge overflow)
88
- * 3. Flipped edge, centered
89
- * 4. Flipped edge, start-aligned
90
- * 5. Flipped edge, end-aligned
91
- *
92
- * For aligned placements (start/end), includes the same-edge centered
93
- * position as fallbacks so the browser can try centering when the aligned
94
- * position would overflow, before flipping to the opposite edge.
95
- *
96
- * **Why `flip-inline flip-block` (or `flip-block flip-inline`) is included:**
97
- *
98
- * When a popup is in a viewport corner (e.g. inline-end align-end at the
99
- * top-right), flipping only the primary axis (`flip-inline`) moves the popup
100
- * to the left but keeps the same cross-axis span direction (e.g. upward),
101
- * which is still off-screen. The combined keyword `flip-inline flip-block`
102
- * mirrors the popup diagonally. It flips BOTH the position-area AND both
103
- * inline and block margins simultaneously, placing the popup at the
104
- * diagonally opposite corner where there is always space.
105
- *
106
- * Named `position-area` entries (e.g. `inline-start span-block-end`) in
107
- * `position-try-fallbacks` do NOT update margins, so only the `flip-*`
108
- * tactic keywords correctly flip the gap margin along with the position.
109
- */
110
- // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
111
- export function placementToTryFallbacks({
112
- placement
113
- }) {
114
- const {
115
- axis,
116
- edge,
117
- align
118
- } = getPlacement({
119
- placement
120
- });
121
- const crossAxis = axis === 'block' ? 'inline' : 'block';
122
- const flippedEdge = edge === 'start' ? 'end' : 'start';
123
- const sameEdge = `${axis}-${edge}`;
124
- const oppositeEdge = `${axis}-${flippedEdge}`;
125
- const flipKeyword = axis === 'block' ? 'flip-block' : 'flip-inline';
126
- const flipCrossKeyword = axis === 'block' ? 'flip-inline' : 'flip-block';
127
-
128
- // Single algorithm: produce the shift list against any base alignment.
129
- // For `align: 'start'` the "near" shift expands toward `end`; for
130
- // `align: 'end'` it expands toward `start`; for `align: 'center'`
131
- // either order is symmetric (we use end-then-start to match the
132
- // historical fallback list).
133
- function shifts(baseAlign, edgeKey) {
134
- if (baseAlign === 'center') {
135
- return [`${edgeKey} span-${crossAxis}-end`, `${edgeKey} span-${crossAxis}-start`];
136
- }
137
- const near = baseAlign === 'start' ? 'end' : 'start';
138
- const far = baseAlign === 'start' ? 'start' : 'end';
139
- return [`${edgeKey} span-${crossAxis}-${near}`, edgeKey, `${edgeKey} span-${crossAxis}-${far}`];
140
- }
141
- if (align !== 'center') {
142
- // Aligned (start/end): try same-edge shifts, then single-axis flip,
143
- // then diagonal flip for corner overflow, then opposite-edge shifts.
144
- // Legacy Popper fallback ordering for e.g. bottom-start:
145
- // [bottom, bottom-end, top-start, top, top-end, auto].
146
- return [...shifts(align, sameEdge), flipKeyword, `${flipKeyword} ${flipCrossKeyword}`, ...shifts(align, oppositeEdge)].join(', ');
147
- }
148
-
149
- // Centered: same-edge shifts, single-axis flip, opposite-edge shifts.
150
- // Diagonal flip is unnecessary because centered placements expand
151
- // equally in both cross-axis directions.
152
- return [...shifts('center', sameEdge), flipKeyword, ...shifts('center', oppositeEdge)].join(', ');
153
- }
154
-
155
41
  /**
156
42
  * Returns true when the popover is currently open. Wrapped because the
157
43
  * `:popover-open` pseudo-class is not implemented in some test environments
@@ -8,7 +8,8 @@ import { roleToAriaHasPopup, shouldFocusIntoPopover } from '../internal/role-typ
8
8
  import { useAnchorPosition } from '../internal/use-anchor-position';
9
9
  import { useWidthFromAnchor } from '../internal/use-width-from-anchor';
10
10
  import { Popover } from '../popover/popover';
11
- import { useMaybePopupContext } from './popup-context';
11
+ import { useMaybePopupContext } from './use-maybe-popup-context';
12
+
12
13
  /**
13
14
  * Thin context wrapper that composes `Popover` + `useAnchorPosition`.
14
15
  *
@@ -1,4 +1,4 @@
1
- import { createContext, useContext } from 'react';
1
+ import { createContext } from 'react';
2
2
 
3
3
  /**
4
4
  * Represents the lifecycle state of the popup.
@@ -17,38 +17,4 @@ import { createContext, useContext } from 'react';
17
17
  * the trigger (and thus the popup anchor) mid-animation.
18
18
  */
19
19
 
20
- const PopupContext = /*#__PURE__*/createContext(null);
21
-
22
- /**
23
- * __Popup provider__
24
- *
25
- * Provides shared popup state (placement, popoverId, triggerRef,
26
- * isOpen, onClose) to compound child components such as `Popup.Trigger` and
27
- * `Popup.Content`.
28
- */
29
- export const PopupProvider = PopupContext.Provider;
30
-
31
- /**
32
- * Returns the nearest `Popup` context value.
33
- *
34
- * Throws if called outside of a `<Popup>` compound component.
35
- */
36
- // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
37
- export function usePopupContext() {
38
- const ctx = useContext(PopupContext);
39
- if (ctx === null) {
40
- throw new Error('@atlaskit/top-layer: Popup compound components must be used within <Popup>.');
41
- }
42
- return ctx;
43
- }
44
-
45
- /**
46
- * Returns the popup context if available, or `null` when used outside `<Popup>`.
47
- *
48
- * Used by `PopupContent` to support standalone usage (e.g. tooltip)
49
- * where values are passed as props instead of coming from a compound component.
50
- */
51
- // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
52
- export function useMaybePopupContext() {
53
- return useContext(PopupContext);
54
- }
20
+ export const PopupContext = /*#__PURE__*/createContext(null);
@@ -0,0 +1,10 @@
1
+ import { PopupContext } from './popup-context';
2
+
3
+ /**
4
+ * __Popup provider__
5
+ *
6
+ * Provides shared popup state (placement, popoverId, triggerRef,
7
+ * isOpen, onClose) to compound child components such as `Popup.Trigger` and
8
+ * `Popup.Content`.
9
+ */
10
+ export const PopupProvider = PopupContext.Provider;
@@ -1,5 +1,5 @@
1
1
  import { useCallback, useMemo } from 'react';
2
- import { usePopupContext } from './popup-context';
2
+ import { usePopupContext } from './use-popup-context';
3
3
 
4
4
  /**
5
5
  * Props passed to the `TriggerFunction` render-prop child.
@@ -1,6 +1,7 @@
1
1
  import React, { isValidElement, useCallback } from 'react';
2
2
  import { Slot } from '../internal/slot';
3
- import { usePopupContext } from './popup-context';
3
+ import { usePopupContext } from './use-popup-context';
4
+
4
5
  /**
5
6
  * Wraps a single trigger element via `cloneElement`. Attaches aria
6
7
  * attributes, the toggle handler, and the anchor-name. `aria-haspopup`
@@ -1,5 +1,5 @@
1
1
  import React, { useCallback, useId, useRef, useState } from 'react';
2
- import { PopupProvider } from './popup-context';
2
+ import { PopupProvider } from './popup-provider';
3
3
  /**
4
4
  * Popup compound: trigger + popover content.
5
5
  *
@@ -0,0 +1,12 @@
1
+ import { useContext } from 'react';
2
+ import { PopupContext } from './popup-context';
3
+
4
+ /**
5
+ * Returns the popup context if available, or `null` when used outside `<Popup>`.
6
+ *
7
+ * Used by `PopupContent` to support standalone usage (e.g. tooltip)
8
+ * where values are passed as props instead of coming from a compound component.
9
+ */
10
+ export function useMaybePopupContext() {
11
+ return useContext(PopupContext);
12
+ }
@@ -0,0 +1,15 @@
1
+ import { useContext } from 'react';
2
+ import { PopupContext } from './popup-context';
3
+
4
+ /**
5
+ * Returns the nearest `Popup` context value.
6
+ *
7
+ * Throws if called outside of a `<Popup>` compound component.
8
+ */
9
+ export function usePopupContext() {
10
+ const ctx = useContext(PopupContext);
11
+ if (ctx === null) {
12
+ throw new Error('@atlaskit/top-layer: Popup compound components must be used within <Popup>.');
13
+ }
14
+ return ctx;
15
+ }
@@ -1 +1,2 @@
1
- export { useArrowNavigation, isAtCurrentMenuLevel } from './use-arrow-navigation';
1
+ export { useArrowNavigation } from './use-arrow-navigation';
2
+ export { isAtCurrentMenuLevel } from './is-at-current-menu-level';
@@ -0,0 +1,81 @@
1
+ const menuScope = '[role="menu"]';
2
+
3
+ /**
4
+ * Returns true if the element belongs to the current menu level of the
5
+ * container, i.e. it is NOT inside a nested `role="menu"` sub-container.
6
+ *
7
+ * Uses `element.closest('[role="menu"]')` to determine the nearest menu
8
+ * scope. If the nearest scope is the container's own `role="menu"` element,
9
+ * the element belongs to this level. If it is a different (nested) menu,
10
+ * the element belongs to a child level and should be excluded.
11
+ *
12
+ * Supports two container patterns:
13
+ *
14
+ * **1. Container has `role="menu"` directly** (preferred, used by avatar-group):
15
+ * ```
16
+ * <div ref={containerRef} role="menu">
17
+ * <button role="menuitem" /> ← isAtCurrentMenuLevel → true
18
+ * <div role="menu"> ← nested sub-menu
19
+ * <button role="menuitem" /> ← isAtCurrentMenuLevel → false
20
+ * </div>
21
+ * </div>
22
+ * ```
23
+ * Uses the simple check: `element.closest('[role="menu"]') === container`
24
+ *
25
+ * **2. Container is a wrapper without `role="menu"`** (used by dropdown-menu,
26
+ * where `MenuGroup` owns `role="menu"` but does not accept a ref):
27
+ * ```
28
+ * <div ref={containerRef}> ← wrapper (no role="menu")
29
+ * <MenuGroup role="menu">
30
+ * <button role="menuitem" /> ← isAtCurrentMenuLevel → true
31
+ * <div role="menu"> ← nested sub-menu
32
+ * <button role="menuitem" /> ← isAtCurrentMenuLevel → false
33
+ * </div>
34
+ * </MenuGroup>
35
+ * </div>
36
+ * ```
37
+ * Falls back to walking the DOM to check that the element's closest
38
+ * `role="menu"` is the container's own menu (not doubly nested).
39
+ *
40
+ * Ideally all consumers should use pattern 1 (place the ref directly on the
41
+ * `role="menu"` element). Pattern 2 exists because `@atlaskit/menu`'s
42
+ * `MenuGroup` does not currently accept a ref. If `MenuGroup` gains ref
43
+ * support in the future, dropdown-menu should switch to pattern 1 and the
44
+ * fallback path can be removed.
45
+ *
46
+ * Used both as a `TFocusableFilter` (to scope getNextFocusable etc.) and as
47
+ * a guard to prevent parent menu handlers from acting on events that
48
+ * belong to a nested menu.
49
+ */
50
+ export function isAtCurrentMenuLevel(element, container) {
51
+ const closestMenu = element.closest(menuScope);
52
+
53
+ // Container itself has role="menu". Element belongs to this level
54
+ // if its closest menu is the container.
55
+ if (container.getAttribute('role') === 'menu') {
56
+ return closestMenu === container;
57
+ }
58
+
59
+ // Container is a wrapper without role="menu" (e.g. dropdown-menu
60
+ // wraps a MenuGroup). The element belongs to the top level if its
61
+ // closest role="menu" ancestor is a direct child of the container,
62
+ // i.e. there is no nested role="menu" between them.
63
+ if (closestMenu === null) {
64
+ return true;
65
+ }
66
+
67
+ // Walk up from the closest menu to see if it is "owned" by this
68
+ // container without passing through another role="menu" first.
69
+ let parent = closestMenu.parentElement;
70
+ while (parent && parent !== container) {
71
+ if (parent.getAttribute('role') === 'menu') {
72
+ // There is a parent menu between the closest menu and the
73
+ // container, so the element is in a nested sub-menu.
74
+ return false;
75
+ }
76
+ parent = parent.parentElement;
77
+ }
78
+
79
+ // The closest menu is directly inside the container (no nesting).
80
+ return parent === container;
81
+ }
@@ -5,86 +5,6 @@ import { KEY_DOWN, KEY_END, KEY_HOME, KEY_TAB, KEY_UP } from '@atlaskit/ds-lib/k
5
5
  import { getFirstFocusable, getLastFocusable, getNextFocusable } from '../focus';
6
6
  const menuScope = '[role="menu"]';
7
7
 
8
- /**
9
- * Returns true if the element belongs to the current menu level of the
10
- * container, i.e. it is NOT inside a nested `role="menu"` sub-container.
11
- *
12
- * Uses `element.closest('[role="menu"]')` to determine the nearest menu
13
- * scope. If the nearest scope is the container's own `role="menu"` element,
14
- * the element belongs to this level. If it is a different (nested) menu,
15
- * the element belongs to a child level and should be excluded.
16
- *
17
- * Supports two container patterns:
18
- *
19
- * **1. Container has `role="menu"` directly** (preferred, used by avatar-group):
20
- * ```
21
- * <div ref={containerRef} role="menu">
22
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → true
23
- * <div role="menu"> ← nested sub-menu
24
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → false
25
- * </div>
26
- * </div>
27
- * ```
28
- * Uses the simple check: `element.closest('[role="menu"]') === container`
29
- *
30
- * **2. Container is a wrapper without `role="menu"`** (used by dropdown-menu,
31
- * where `MenuGroup` owns `role="menu"` but does not accept a ref):
32
- * ```
33
- * <div ref={containerRef}> ← wrapper (no role="menu")
34
- * <MenuGroup role="menu">
35
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → true
36
- * <div role="menu"> ← nested sub-menu
37
- * <button role="menuitem" /> ← isAtCurrentMenuLevel → false
38
- * </div>
39
- * </MenuGroup>
40
- * </div>
41
- * ```
42
- * Falls back to walking the DOM to check that the element's closest
43
- * `role="menu"` is the container's own menu (not doubly nested).
44
- *
45
- * Ideally all consumers should use pattern 1 (place the ref directly on the
46
- * `role="menu"` element). Pattern 2 exists because `@atlaskit/menu`'s
47
- * `MenuGroup` does not currently accept a ref. If `MenuGroup` gains ref
48
- * support in the future, dropdown-menu should switch to pattern 1 and the
49
- * fallback path can be removed.
50
- *
51
- * Used both as a `TFocusableFilter` (to scope getNextFocusable etc.) and as
52
- * a guard to prevent parent menu handlers from acting on events that
53
- * belong to a nested menu.
54
- */
55
- export function isAtCurrentMenuLevel(element, container) {
56
- const closestMenu = element.closest(menuScope);
57
-
58
- // Container itself has role="menu". Element belongs to this level
59
- // if its closest menu is the container.
60
- if (container.getAttribute('role') === 'menu') {
61
- return closestMenu === container;
62
- }
63
-
64
- // Container is a wrapper without role="menu" (e.g. dropdown-menu
65
- // wraps a MenuGroup). The element belongs to the top level if its
66
- // closest role="menu" ancestor is a direct child of the container,
67
- // i.e. there is no nested role="menu" between them.
68
- if (closestMenu === null) {
69
- return true;
70
- }
71
-
72
- // Walk up from the closest menu to see if it is "owned" by this
73
- // container without passing through another role="menu" first.
74
- let parent = closestMenu.parentElement;
75
- while (parent && parent !== container) {
76
- if (parent.getAttribute('role') === 'menu') {
77
- // There is a parent menu between the closest menu and the
78
- // container, so the element is in a nested sub-menu.
79
- return false;
80
- }
81
- parent = parent.parentElement;
82
- }
83
-
84
- // The closest menu is directly inside the container (no nesting).
85
- return parent === container;
86
- }
87
-
88
8
  /**
89
9
  * Args for `useArrowNavigation`.
90
10
  *
@@ -135,7 +55,6 @@ export function isAtCurrentMenuLevel(element, container) {
135
55
  * });
136
56
  * ```
137
57
  */
138
- // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
139
58
  export function useArrowNavigation({
140
59
  containerRef,
141
60
  onClose,
@@ -1 +1,2 @@
1
- export { useArrowNavigation, isAtCurrentMenuLevel } from '../use-arrow-navigation';
1
+ export { useArrowNavigation } from '../use-arrow-navigation';
2
+ export { isAtCurrentMenuLevel } from '../use-arrow-navigation/is-at-current-menu-level';