@react-spectrum/menu 3.22.8 → 3.22.9

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 (71) hide show
  1. package/dist/Menu.main.js +1 -1
  2. package/dist/Menu.main.js.map +1 -1
  3. package/dist/Menu.mjs +2 -2
  4. package/dist/Menu.module.js +2 -2
  5. package/dist/Menu.module.js.map +1 -1
  6. package/dist/Popover.main.js +196 -0
  7. package/dist/Popover.main.js.map +1 -0
  8. package/dist/Popover.mjs +191 -0
  9. package/dist/Popover.module.js +191 -0
  10. package/dist/Popover.module.js.map +1 -0
  11. package/dist/SubmenuTrigger.main.js +2 -2
  12. package/dist/SubmenuTrigger.main.js.map +1 -1
  13. package/dist/SubmenuTrigger.mjs +2 -2
  14. package/dist/SubmenuTrigger.module.js +2 -2
  15. package/dist/SubmenuTrigger.module.js.map +1 -1
  16. package/dist/Underlay.main.js +54 -0
  17. package/dist/Underlay.main.js.map +1 -0
  18. package/dist/Underlay.mjs +49 -0
  19. package/dist/Underlay.module.js +49 -0
  20. package/dist/Underlay.module.js.map +1 -0
  21. package/dist/calculatePosition.main.js +392 -0
  22. package/dist/calculatePosition.main.js.map +1 -0
  23. package/dist/calculatePosition.mjs +386 -0
  24. package/dist/calculatePosition.module.js +386 -0
  25. package/dist/calculatePosition.module.js.map +1 -0
  26. package/dist/menu.2259a533.css +210 -0
  27. package/dist/menu.2259a533.css.map +1 -0
  28. package/dist/menu.359a0c0a.css +131 -0
  29. package/dist/menu.359a0c0a.css.map +1 -0
  30. package/dist/menu.57b3a408.css +12 -0
  31. package/dist/menu.57b3a408.css.map +1 -0
  32. package/dist/overlays_css.main.js +35 -0
  33. package/dist/overlays_css.main.js.map +1 -0
  34. package/dist/overlays_css.mjs +37 -0
  35. package/dist/overlays_css.module.js +37 -0
  36. package/dist/overlays_css.module.js.map +1 -0
  37. package/dist/popover_vars_css.main.js +83 -0
  38. package/dist/popover_vars_css.main.js.map +1 -0
  39. package/dist/popover_vars_css.mjs +85 -0
  40. package/dist/popover_vars_css.module.js +85 -0
  41. package/dist/popover_vars_css.module.js.map +1 -0
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/underlay_vars_css.main.js +50 -0
  44. package/dist/underlay_vars_css.main.js.map +1 -0
  45. package/dist/underlay_vars_css.mjs +52 -0
  46. package/dist/underlay_vars_css.module.js +52 -0
  47. package/dist/underlay_vars_css.module.js.map +1 -0
  48. package/dist/useCloseOnScroll.main.js +49 -0
  49. package/dist/useCloseOnScroll.main.js.map +1 -0
  50. package/dist/useCloseOnScroll.mjs +44 -0
  51. package/dist/useCloseOnScroll.module.js +44 -0
  52. package/dist/useCloseOnScroll.module.js.map +1 -0
  53. package/dist/useOverlayPosition.main.js +225 -0
  54. package/dist/useOverlayPosition.main.js.map +1 -0
  55. package/dist/useOverlayPosition.mjs +220 -0
  56. package/dist/useOverlayPosition.module.js +220 -0
  57. package/dist/useOverlayPosition.module.js.map +1 -0
  58. package/dist/usePopover.main.js +73 -0
  59. package/dist/usePopover.main.js.map +1 -0
  60. package/dist/usePopover.mjs +68 -0
  61. package/dist/usePopover.module.js +68 -0
  62. package/dist/usePopover.module.js.map +1 -0
  63. package/package.json +19 -19
  64. package/src/Menu.tsx +2 -1
  65. package/src/Popover.tsx +240 -0
  66. package/src/SubmenuTrigger.tsx +1 -1
  67. package/src/Underlay.tsx +43 -0
  68. package/src/calculatePosition.ts +627 -0
  69. package/src/useCloseOnScroll.ts +64 -0
  70. package/src/useOverlayPosition.ts +327 -0
  71. package/src/usePopover.ts +136 -0
@@ -0,0 +1,327 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {calculatePosition, getRect, PositionResult} from './calculatePosition';
14
+ import {DOMAttributes, RefObject} from '@react-types/shared';
15
+ import {Placement, PlacementAxis, PositionProps} from '@react-types/overlays';
16
+ import {useCallback, useEffect, useRef, useState} from 'react';
17
+ import {useCloseOnScroll} from './useCloseOnScroll';
18
+ import {useLayoutEffect, useResizeObserver} from '@react-aria/utils';
19
+ import {useLocale} from '@react-aria/i18n';
20
+
21
+ export interface AriaPositionProps extends PositionProps {
22
+ /**
23
+ * Cross size of the overlay arrow in pixels.
24
+ * @default 0
25
+ */
26
+ arrowSize?: number,
27
+ /**
28
+ * Element that that serves as the positioning boundary.
29
+ * @default document.body
30
+ */
31
+ boundaryElement?: Element,
32
+ /**
33
+ * The ref for the element which the overlay positions itself with respect to.
34
+ */
35
+ targetRef: RefObject<Element | null>,
36
+ /**
37
+ * The ref for the overlay element.
38
+ */
39
+ overlayRef: RefObject<Element | null>,
40
+ /**
41
+ * The ref for the arrow element.
42
+ */
43
+ arrowRef?: RefObject<Element | null>,
44
+ /**
45
+ * A ref for the scrollable region within the overlay.
46
+ * @default overlayRef
47
+ */
48
+ scrollRef?: RefObject<Element | null>,
49
+ /**
50
+ * Whether the overlay should update its position automatically.
51
+ * @default true
52
+ */
53
+ shouldUpdatePosition?: boolean,
54
+ /** Handler that is called when the overlay should close. */
55
+ onClose?: (() => void) | null,
56
+ /**
57
+ * The maxHeight specified for the overlay element.
58
+ * By default, it will take all space up to the current viewport height.
59
+ */
60
+ maxHeight?: number,
61
+ /**
62
+ * The minimum distance the arrow's edge should be from the edge of the overlay element.
63
+ * @default 0
64
+ */
65
+ arrowBoundaryOffset?: number
66
+ }
67
+
68
+ export interface PositionAria {
69
+ /** Props for the overlay container element. */
70
+ overlayProps: DOMAttributes,
71
+ /** Props for the overlay tip arrow if any. */
72
+ arrowProps: DOMAttributes,
73
+ /** Placement of the overlay with respect to the overlay trigger. */
74
+ placement: PlacementAxis | null,
75
+ /** The origin of the target in the overlay's coordinate system. Useful for animations. */
76
+ triggerAnchorPoint: {x: number, y: number} | null,
77
+ /** Updates the position of the overlay. */
78
+ updatePosition(): void
79
+ }
80
+
81
+ interface ScrollAnchor {
82
+ type: 'top' | 'bottom',
83
+ offset: number
84
+ }
85
+
86
+ let visualViewport = typeof document !== 'undefined' ? window.visualViewport : null;
87
+
88
+ /**
89
+ * Handles positioning overlays like popovers and menus relative to a trigger
90
+ * element, and updating the position when the window resizes.
91
+ */
92
+ export function useOverlayPosition(props: AriaPositionProps): PositionAria {
93
+ let {direction} = useLocale();
94
+ let {
95
+ arrowSize,
96
+ targetRef,
97
+ overlayRef,
98
+ arrowRef,
99
+ scrollRef = overlayRef,
100
+ placement = 'bottom' as Placement,
101
+ containerPadding = 12,
102
+ shouldFlip = true,
103
+ boundaryElement = typeof document !== 'undefined' ? document.body : null,
104
+ offset = 0,
105
+ crossOffset = 0,
106
+ shouldUpdatePosition = true,
107
+ isOpen = true,
108
+ onClose,
109
+ maxHeight,
110
+ arrowBoundaryOffset = 0
111
+ } = props;
112
+ let [position, setPosition] = useState<PositionResult | null>(null);
113
+
114
+ let deps = [
115
+ shouldUpdatePosition,
116
+ placement,
117
+ overlayRef.current,
118
+ targetRef.current,
119
+ arrowRef?.current,
120
+ scrollRef.current,
121
+ containerPadding,
122
+ shouldFlip,
123
+ boundaryElement,
124
+ offset,
125
+ crossOffset,
126
+ isOpen,
127
+ direction,
128
+ maxHeight,
129
+ arrowBoundaryOffset,
130
+ arrowSize
131
+ ];
132
+
133
+ // Note, the position freezing breaks if body sizes itself dynamicly with the visual viewport but that might
134
+ // just be a non-realistic use case
135
+ // Upon opening a overlay, record the current visual viewport scale so we can freeze the overlay styles
136
+ let lastScale = useRef(visualViewport?.scale);
137
+ useEffect(() => {
138
+ if (isOpen) {
139
+ lastScale.current = visualViewport?.scale;
140
+ }
141
+ }, [isOpen]);
142
+
143
+ let updatePosition = useCallback(() => {
144
+ if (shouldUpdatePosition === false || !isOpen || !overlayRef.current || !targetRef.current || !boundaryElement) {
145
+ return;
146
+ }
147
+
148
+ if (visualViewport?.scale !== lastScale.current) {
149
+ return;
150
+ }
151
+
152
+ // Determine a scroll anchor based on the focused element.
153
+ // This stores the offset of the anchor element from the scroll container
154
+ // so it can be restored after repositioning. This way if the overlay height
155
+ // changes, the focused element appears to stay in the same position.
156
+ let anchor: ScrollAnchor | null = null;
157
+ if (scrollRef.current && scrollRef.current.contains(document.activeElement)) {
158
+ let anchorRect = document.activeElement?.getBoundingClientRect();
159
+ let scrollRect = scrollRef.current.getBoundingClientRect();
160
+ // Anchor from the top if the offset is in the top half of the scrollable element,
161
+ // otherwise anchor from the bottom.
162
+ anchor = {
163
+ type: 'top',
164
+ offset: (anchorRect?.top ?? 0) - scrollRect.top
165
+ };
166
+ if (anchor.offset > scrollRect.height / 2) {
167
+ anchor.type = 'bottom';
168
+ anchor.offset = (anchorRect?.bottom ?? 0) - scrollRect.bottom;
169
+ }
170
+ }
171
+
172
+ // Always reset the overlay's previous max height if not defined by the user so that we can compensate for
173
+ // RAC collections populating after a second render and properly set a correct max height + positioning when it populates.
174
+ let overlay = (overlayRef.current as HTMLElement);
175
+ if (!maxHeight && overlayRef.current) {
176
+ overlay.style.top = '0px';
177
+ overlay.style.bottom = '';
178
+ overlay.style.maxHeight = (window.visualViewport?.height ?? window.innerHeight) + 'px';
179
+ }
180
+
181
+ let position = calculatePosition({
182
+ placement: translateRTL(placement, direction),
183
+ overlayNode: overlayRef.current,
184
+ targetNode: targetRef.current,
185
+ scrollNode: scrollRef.current || overlayRef.current,
186
+ padding: containerPadding,
187
+ shouldFlip,
188
+ boundaryElement,
189
+ offset,
190
+ crossOffset,
191
+ maxHeight,
192
+ arrowSize: arrowSize ?? (arrowRef?.current ? getRect(arrowRef.current, true).width : 0),
193
+ arrowBoundaryOffset
194
+ });
195
+
196
+ if (!position.position) {
197
+ return;
198
+ }
199
+
200
+ // Modify overlay styles directly so positioning happens immediately without the need of a second render
201
+ // This is so we don't have to delay autoFocus scrolling or delay applying preventScroll for popovers
202
+ overlay.style.top = '';
203
+ overlay.style.bottom = '';
204
+ overlay.style.left = '';
205
+ overlay.style.right = '';
206
+
207
+ Object.keys(position.position).forEach(key => overlay.style[key] = (position.position!)[key] + 'px');
208
+ overlay.style.maxHeight = position.maxHeight != null ? position.maxHeight + 'px' : '';
209
+
210
+ // Restore scroll position relative to anchor element.
211
+ if (anchor && document.activeElement && scrollRef.current) {
212
+ let anchorRect = document.activeElement.getBoundingClientRect();
213
+ let scrollRect = scrollRef.current.getBoundingClientRect();
214
+ let newOffset = anchorRect[anchor.type] - scrollRect[anchor.type];
215
+ scrollRef.current.scrollTop += newOffset - anchor.offset;
216
+ }
217
+
218
+ // Trigger a set state for a second render anyway for arrow positioning
219
+ setPosition(position);
220
+ // eslint-disable-next-line react-hooks/exhaustive-deps
221
+ }, deps);
222
+
223
+ // Update position when anything changes
224
+ // eslint-disable-next-line react-hooks/exhaustive-deps
225
+ useLayoutEffect(updatePosition, deps);
226
+
227
+ // Update position on window resize
228
+ useResize(updatePosition);
229
+
230
+ // Update position when the overlay changes size (might need to flip).
231
+ useResizeObserver({
232
+ ref: overlayRef,
233
+ onResize: updatePosition
234
+ });
235
+
236
+ // Update position when the target changes size (might need to flip).
237
+ useResizeObserver({
238
+ ref: targetRef,
239
+ onResize: updatePosition
240
+ });
241
+
242
+ // Reposition the overlay and do not close on scroll while the visual viewport is resizing.
243
+ // This will ensure that overlays adjust their positioning when the iOS virtual keyboard appears.
244
+ let isResizing = useRef(false);
245
+ useLayoutEffect(() => {
246
+ let timeout: ReturnType<typeof setTimeout>;
247
+ let onResize = () => {
248
+ isResizing.current = true;
249
+ clearTimeout(timeout);
250
+
251
+ timeout = setTimeout(() => {
252
+ isResizing.current = false;
253
+ }, 500);
254
+
255
+ updatePosition();
256
+ };
257
+
258
+ // Only reposition the overlay if a scroll event happens immediately as a result of resize (aka the virtual keyboard has appears)
259
+ // We don't want to reposition the overlay if the user has pinch zoomed in and is scrolling the viewport around.
260
+ let onScroll = () => {
261
+ if (isResizing.current) {
262
+ onResize();
263
+ }
264
+ };
265
+
266
+ visualViewport?.addEventListener('resize', onResize);
267
+ visualViewport?.addEventListener('scroll', onScroll);
268
+ return () => {
269
+ visualViewport?.removeEventListener('resize', onResize);
270
+ visualViewport?.removeEventListener('scroll', onScroll);
271
+ };
272
+ }, [updatePosition]);
273
+
274
+ let close = useCallback(() => {
275
+ if (!isResizing.current) {
276
+ onClose?.();
277
+ }
278
+ }, [onClose, isResizing]);
279
+
280
+ // When scrolling a parent scrollable region of the trigger (other than the body),
281
+ // we hide the popover. Otherwise, its position would be incorrect.
282
+ useCloseOnScroll({
283
+ triggerRef: targetRef,
284
+ isOpen,
285
+ onClose: onClose && close
286
+ });
287
+
288
+ return {
289
+ overlayProps: {
290
+ style: {
291
+ position: position ? 'absolute' : 'fixed',
292
+ top: !position ? 0 : undefined,
293
+ left: !position ? 0 : undefined,
294
+ zIndex: 100000, // should match the z-index in ModalTrigger
295
+ ...position?.position,
296
+ maxHeight: position?.maxHeight ?? '100vh'
297
+ }
298
+ },
299
+ placement: position?.placement ?? null,
300
+ triggerAnchorPoint: position?.triggerAnchorPoint ?? null,
301
+ arrowProps: {
302
+ 'aria-hidden': 'true',
303
+ role: 'presentation',
304
+ style: {
305
+ left: position?.arrowOffsetLeft,
306
+ top: position?.arrowOffsetTop
307
+ }
308
+ },
309
+ updatePosition
310
+ };
311
+ }
312
+
313
+ function useResize(onResize) {
314
+ useLayoutEffect(() => {
315
+ window.addEventListener('resize', onResize, false);
316
+ return () => {
317
+ window.removeEventListener('resize', onResize, false);
318
+ };
319
+ }, [onResize]);
320
+ }
321
+
322
+ function translateRTL(position, direction) {
323
+ if (direction === 'rtl') {
324
+ return position.replace('start', 'right').replace('end', 'left');
325
+ }
326
+ return position.replace('start', 'left').replace('end', 'right');
327
+ }
@@ -0,0 +1,136 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {ariaHideOutside, AriaPositionProps, useOverlay, usePreventScroll} from '@react-aria/overlays';
14
+ import {DOMAttributes, RefObject} from '@react-types/shared';
15
+ import {mergeProps} from '@react-aria/utils';
16
+ import {OverlayTriggerState} from '@react-stately/overlays';
17
+ import {PlacementAxis} from '@react-types/overlays';
18
+ import {useEffect} from 'react';
19
+ import {useOverlayPosition} from './useOverlayPosition';
20
+
21
+ export interface AriaPopoverProps extends Omit<AriaPositionProps, 'isOpen' | 'onClose' | 'targetRef' | 'overlayRef'> {
22
+ /**
23
+ * The ref for the element which the popover positions itself with respect to.
24
+ */
25
+ triggerRef: RefObject<Element | null>,
26
+ /**
27
+ * The ref for the popover element.
28
+ */
29
+ popoverRef: RefObject<Element | null>,
30
+ /** A ref for the popover arrow element. */
31
+ arrowRef?: RefObject<Element | null>,
32
+ /**
33
+ * An optional ref for a group of popovers, e.g. submenus.
34
+ * When provided, this element is used to detect outside interactions
35
+ * and hiding elements from assistive technologies instead of the popoverRef.
36
+ */
37
+ groupRef?: RefObject<Element | null>,
38
+ /**
39
+ * Whether the popover is non-modal, i.e. elements outside the popover may be
40
+ * interacted with by assistive technologies.
41
+ *
42
+ * Most popovers should not use this option as it may negatively impact the screen
43
+ * reader experience. Only use with components such as combobox, which are designed
44
+ * to handle this situation carefully.
45
+ */
46
+ isNonModal?: boolean,
47
+ /**
48
+ * Whether pressing the escape key to close the popover should be disabled.
49
+ *
50
+ * Most popovers should not use this option. When set to true, an alternative
51
+ * way to close the popover with a keyboard must be provided.
52
+ *
53
+ * @default false
54
+ */
55
+ isKeyboardDismissDisabled?: boolean,
56
+ /**
57
+ * When user interacts with the argument element outside of the popover ref,
58
+ * return true if onClose should be called. This gives you a chance to filter
59
+ * out interaction with elements that should not dismiss the popover.
60
+ * By default, onClose will always be called on interaction outside the popover ref.
61
+ */
62
+ shouldCloseOnInteractOutside?: (element: Element) => boolean
63
+ }
64
+
65
+ export interface PopoverAria {
66
+ /** Props for the popover element. */
67
+ popoverProps: DOMAttributes,
68
+ /** Props for the popover tip arrow if any. */
69
+ arrowProps: DOMAttributes,
70
+ /** Props to apply to the underlay element, if any. */
71
+ underlayProps: DOMAttributes,
72
+ /** Placement of the popover with respect to the trigger. */
73
+ placement: PlacementAxis | null,
74
+ /** The origin of the target in the overlay's coordinate system. Useful for animations. */
75
+ triggerAnchorPoint: {x: number, y: number} | null
76
+ }
77
+
78
+ /**
79
+ * Provides the behavior and accessibility implementation for a popover component.
80
+ * A popover is an overlay element positioned relative to a trigger.
81
+ */
82
+ export function usePopover(props: AriaPopoverProps, state: OverlayTriggerState): PopoverAria {
83
+ let {
84
+ triggerRef,
85
+ popoverRef,
86
+ groupRef,
87
+ isNonModal,
88
+ isKeyboardDismissDisabled,
89
+ shouldCloseOnInteractOutside,
90
+ ...otherProps
91
+ } = props;
92
+
93
+ let isSubmenu = otherProps['trigger'] === 'SubmenuTrigger';
94
+
95
+ let {overlayProps, underlayProps} = useOverlay(
96
+ {
97
+ isOpen: state.isOpen,
98
+ onClose: state.close,
99
+ shouldCloseOnBlur: true,
100
+ isDismissable: !isNonModal || isSubmenu,
101
+ isKeyboardDismissDisabled,
102
+ shouldCloseOnInteractOutside
103
+ },
104
+ groupRef ?? popoverRef
105
+ );
106
+
107
+ let {overlayProps: positionProps, arrowProps, placement, triggerAnchorPoint: origin} = useOverlayPosition({
108
+ ...otherProps,
109
+ targetRef: triggerRef,
110
+ overlayRef: popoverRef,
111
+ isOpen: state.isOpen,
112
+ onClose: isNonModal && !isSubmenu ? state.close : null
113
+ });
114
+
115
+ usePreventScroll({
116
+ isDisabled: isNonModal || !state.isOpen
117
+ });
118
+
119
+ useEffect(() => {
120
+ if (state.isOpen && popoverRef.current) {
121
+ if (isNonModal) {
122
+ return;
123
+ } else {
124
+ return ariaHideOutside([groupRef?.current ?? popoverRef.current], {shouldUseInert: true});
125
+ }
126
+ }
127
+ }, [isNonModal, state.isOpen, popoverRef, groupRef]);
128
+
129
+ return {
130
+ popoverProps: mergeProps(overlayProps, positionProps),
131
+ arrowProps,
132
+ underlayProps,
133
+ placement,
134
+ triggerAnchorPoint: origin
135
+ };
136
+ }