diff --git a/packages/@react-spectrum/menu/src/Popover.tsx b/packages/@react-spectrum/menu/src/Popover.tsx new file mode 100644 index 00000000000..51f43a769dd --- /dev/null +++ b/packages/@react-spectrum/menu/src/Popover.tsx @@ -0,0 +1,240 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import {AriaPopoverProps, DismissButton, PopoverAria} from '@react-aria/overlays'; +import {classNames, useDOMRef, useStyleProps} from '@react-spectrum/utils'; +import {DOMRef, RefObject, StyleProps} from '@react-types/shared'; +import {FocusWithinProps, useFocusWithin} from '@react-aria/interactions'; +import {mergeProps, useLayoutEffect, useObjectRef} from '@react-aria/utils'; +import {Overlay} from '@react-spectrum/overlays'; +import {OverlayTriggerState} from '@react-stately/overlays'; +import overrideStyles from './overlays.css'; +import React, {ForwardedRef, forwardRef, ReactNode, useRef, useState} from 'react'; +import styles from '@adobe/spectrum-css-temp/components/popover/vars.css'; +import {Underlay} from './Underlay'; +import {usePopover} from './usePopover'; + +interface PopoverProps extends Omit, FocusWithinProps, StyleProps { + children: ReactNode, + hideArrow?: boolean, + state: OverlayTriggerState, + shouldContainFocus?: boolean, + onEntering?: () => void, + onEnter?: () => void, + onEntered?: () => void, + onExiting?: () => void, + onExited?: () => void, + onExit?: () => void, + container?: HTMLElement, + disableFocusManagement?: boolean, + enableBothDismissButtons?: boolean, + onDismissButtonPress?: () => void +} + +interface PopoverWrapperProps extends PopoverProps, FocusWithinProps { + isOpen?: boolean, + wrapperRef: RefObject +} + +interface ArrowProps { + arrowProps: PopoverAria['arrowProps'], + isLandscape: boolean, + arrowRef?: RefObject, + primary: number, + secondary: number, + borderDiagonal: number +} + +/** + * Arrow placement can be done pointing right or down because those paths start at 0, x or y. Because the + * other two don't, they start at a fractional pixel value, it introduces rounding differences between browsers and + * between display types (retina with subpixels vs not retina). By flipping them with CSS we can ensure that + * the path always starts at 0 so that it perfectly overlaps the popover's border. + * See bottom of file for more explanation. + */ +let arrowPlacement = { + left: 'right', + right: 'right', + top: 'bottom', + bottom: 'bottom' +}; + +export const Popover = forwardRef(function Popover(props: PopoverProps, ref: DOMRef) { + let { + children, + state, + ...otherProps + } = props; + let domRef = useDOMRef(ref); + let wrapperRef = useRef(null); + + return ( + + + {children} + + + ); +}); + +const PopoverWrapper = forwardRef((props: PopoverWrapperProps, ref: ForwardedRef) => { + let { + children, + isOpen, + hideArrow, + isNonModal, + enableBothDismissButtons, + state, + wrapperRef, + onDismissButtonPress = () => state.close() + } = props; + let {styleProps} = useStyleProps(props); + let objRef = useObjectRef(ref); + + let {size, borderWidth, arrowRef} = useArrowSize(); + const borderRadius = usePopoverBorderRadius(objRef); + let borderDiagonal = borderWidth * Math.SQRT2; + let primary = size + borderDiagonal; + let secondary = primary * 2; + let { + popoverProps, + arrowProps, + underlayProps, + placement + } = usePopover({ + ...props, + popoverRef: objRef, + maxHeight: undefined, + arrowSize: hideArrow ? 0 : secondary, + arrowBoundaryOffset: borderRadius + }, state); + let {focusWithinProps} = useFocusWithin(props); + + // Attach Transition's nodeRef to outermost wrapper for node.reflow: https://github.com/reactjs/react-transition-group/blob/c89f807067b32eea6f68fd6c622190d88ced82e2/src/Transition.js#L231 + return ( +
+ {!isNonModal && } +
+ {(!isNonModal || enableBothDismissButtons) && } + {children} + {hideArrow ? null : ( + + )} + +
+
+ ); +}); + +function usePopoverBorderRadius(popoverRef: RefObject) { + let [borderRadius, setBorderRadius] = useState(0); + useLayoutEffect(() => { + if (popoverRef.current) { + let spectrumBorderRadius = window.getComputedStyle(popoverRef.current).borderRadius; + if (spectrumBorderRadius !== '') { + setBorderRadius(parseInt(spectrumBorderRadius, 10)); + } + } + }, [popoverRef]); + return borderRadius; +} + +function useArrowSize() { + let [size, setSize] = useState(20); + let [borderWidth, setBorderWidth] = useState(1); + let arrowRef = useRef(null); + // get the css value for the tip size and divide it by 2 for this arrow implementation + useLayoutEffect(() => { + if (arrowRef.current) { + let spectrumTipWidth = window.getComputedStyle(arrowRef.current) + .getPropertyValue('--spectrum-popover-tip-size'); + if (spectrumTipWidth !== '') { + setSize(parseInt(spectrumTipWidth, 10) / 2); + } + + let spectrumBorderWidth = window.getComputedStyle(arrowRef.current) + .getPropertyValue('--spectrum-popover-tip-borderWidth'); + if (spectrumBorderWidth !== '') { + setBorderWidth(parseInt(spectrumBorderWidth, 10)); + } + } + }, []); + return {size, borderWidth, arrowRef}; +} + +function Arrow(props: ArrowProps) { + let {primary, secondary, isLandscape, arrowProps, borderDiagonal, arrowRef} = props; + let halfBorderDiagonal = borderDiagonal / 2; + + let primaryStart = 0; + let primaryEnd = primary - halfBorderDiagonal; + + let secondaryStart = halfBorderDiagonal; + let secondaryMiddle = secondary / 2; + let secondaryEnd = secondary - halfBorderDiagonal; + + let pathData = isLandscape ? [ + 'M', secondaryStart, primaryStart, + 'L', secondaryMiddle, primaryEnd, + 'L', secondaryEnd, primaryStart + ] : [ + 'M', primaryStart, secondaryStart, + 'L', primaryEnd, secondaryMiddle, + 'L', primaryStart, secondaryEnd + ]; + + /* use ceil because the svg needs to always accommodate the path inside it */ + return ( + + + + ); +} diff --git a/packages/@react-spectrum/menu/src/SubmenuTrigger.tsx b/packages/@react-spectrum/menu/src/SubmenuTrigger.tsx index 902870bc922..186189c7d9e 100644 --- a/packages/@react-spectrum/menu/src/SubmenuTrigger.tsx +++ b/packages/@react-spectrum/menu/src/SubmenuTrigger.tsx @@ -14,7 +14,7 @@ import {classNames, useIsMobileDevice} from '@react-spectrum/utils'; import {Key} from '@react-types/shared'; import {MenuContext, SubmenuTriggerContext, useMenuStateContext} from './context'; import {mergeProps} from '@react-aria/utils'; -import {Popover} from '@react-spectrum/overlays'; +import {Popover} from './Popover'; import React, {type JSX, ReactElement, useRef} from 'react'; import ReactDOM from 'react-dom'; import styles from '@adobe/spectrum-css-temp/components/menu/vars.css'; diff --git a/packages/@react-spectrum/menu/src/Underlay.tsx b/packages/@react-spectrum/menu/src/Underlay.tsx new file mode 100644 index 00000000000..da9911834d8 --- /dev/null +++ b/packages/@react-spectrum/menu/src/Underlay.tsx @@ -0,0 +1,43 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import {classNames} from '@react-spectrum/utils'; +import {isScrollable} from '@react-aria/utils'; +import React, {JSX} from 'react'; +import underlayStyles from '@adobe/spectrum-css-temp/components/underlay/vars.css'; + +interface UnderlayProps { + isOpen?: boolean, + isTransparent?: boolean +} + +export function Underlay({isOpen, isTransparent, ...otherProps}: UnderlayProps): JSX.Element { + let pageHeight: number | undefined = undefined; + if (typeof document !== 'undefined') { + let scrollingElement = isScrollable(document.body) ? document.body : document.scrollingElement || document.documentElement; + // Prevent Firefox from adding scrollbars when the page has a fractional height. + let fractionalHeightDifference = scrollingElement.getBoundingClientRect().height % 1; + pageHeight = scrollingElement.scrollHeight - fractionalHeightDifference; + } + + return ( +
+ ); +} diff --git a/packages/@react-spectrum/menu/src/calculatePosition.ts b/packages/@react-spectrum/menu/src/calculatePosition.ts new file mode 100644 index 00000000000..ad872c7d7ea --- /dev/null +++ b/packages/@react-spectrum/menu/src/calculatePosition.ts @@ -0,0 +1,627 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import {Axis, Placement, PlacementAxis, SizeAxis} from '@react-types/overlays'; +import {clamp, isWebKit} from '@react-aria/utils'; + +interface Position { + top?: number, + left?: number, + bottom?: number, + right?: number +} + +interface Dimensions { + width: number, + height: number, + totalWidth: number, + totalHeight: number, + top: number, + left: number, + scroll: Position +} + +interface ParsedPlacement { + placement: PlacementAxis, + crossPlacement: PlacementAxis, + axis: Axis, + crossAxis: Axis, + size: SizeAxis, + crossSize: SizeAxis +} + +interface Offset { + top: number, + left: number, + width: number, + height: number +} + +interface PositionOpts { + arrowSize: number, + placement: Placement, + targetNode: Element, + overlayNode: Element, + scrollNode: Element, + padding: number, + shouldFlip: boolean, + boundaryElement: Element, + offset: number, + crossOffset: number, + maxHeight?: number, + arrowBoundaryOffset?: number +} + +type HeightGrowthDirection = 'top' | 'bottom'; + +export interface PositionResult { + position: Position, + arrowOffsetLeft?: number, + arrowOffsetTop?: number, + triggerAnchorPoint: {x: number, y: number}, + maxHeight: number, + placement: PlacementAxis +} + +const AXIS = { + top: 'top', + bottom: 'top', + left: 'left', + right: 'left' +}; + +const FLIPPED_DIRECTION = { + top: 'bottom', + bottom: 'top', + left: 'right', + right: 'left' +}; + +const CROSS_AXIS = { + top: 'left', + left: 'top' +}; + +const AXIS_SIZE = { + top: 'height', + left: 'width' +}; + +const TOTAL_SIZE = { + width: 'totalWidth', + height: 'totalHeight' +}; + +const PARSED_PLACEMENT_CACHE = {}; + +let visualViewport = typeof document !== 'undefined' ? window.visualViewport : null; + +function getContainerDimensions(containerNode: Element): Dimensions { + let width = 0, height = 0, totalWidth = 0, totalHeight = 0, top = 0, left = 0; + let scroll: Position = {}; + let isPinchZoomedIn = (visualViewport?.scale ?? 1) > 1; + + if (containerNode.tagName === 'BODY') { + let documentElement = document.documentElement; + totalWidth = documentElement.clientWidth; + totalHeight = documentElement.clientHeight; + width = visualViewport?.width ?? totalWidth; + height = visualViewport?.height ?? totalHeight; + scroll.top = documentElement.scrollTop || containerNode.scrollTop; + scroll.left = documentElement.scrollLeft || containerNode.scrollLeft; + + // The goal of the below is to get a top/left value that represents the top/left of the visual viewport with + // respect to the layout viewport origin. This combined with the scrollTop/scrollLeft will allow us to calculate + // coordinates/values with respect to the visual viewport or with respect to the layout viewport. + if (visualViewport) { + top = visualViewport.offsetTop; + left = visualViewport.offsetLeft; + } + } else { + ({width, height, top, left} = getOffset(containerNode, false)); + scroll.top = containerNode.scrollTop; + scroll.left = containerNode.scrollLeft; + totalWidth = width; + totalHeight = height; + } + + if (isWebKit() && (containerNode.tagName === 'BODY' || containerNode.tagName === 'HTML') && isPinchZoomedIn) { + // Safari will report a non-zero scrollTop/Left for the non-scrolling body/HTML element when pinch zoomed in unlike other browsers. + // Set to zero for parity calculations so we get consistent positioning of overlays across all browsers. + // Also switch to visualViewport.pageTop/pageLeft so that we still accomodate for scroll positioning for body/HTML elements that are actually scrollable + // before pinch zoom happens + scroll.top = 0; + scroll.left = 0; + top = visualViewport?.pageTop ?? 0; + left = visualViewport?.pageLeft ?? 0; + } + + return {width, height, totalWidth, totalHeight, scroll, top, left}; +} + +function getScroll(node: Element): Offset { + return { + top: node.scrollTop, + left: node.scrollLeft, + width: node.scrollWidth, + height: node.scrollHeight + }; +} + +// Determines the amount of space required when moving the overlay to ensure it remains in the boundary +function getDelta( + axis: Axis, + offset: number, + size: number, + // The dimensions of the boundary element that the popover is + // positioned within (most of the time this is the ). + boundaryDimensions: Dimensions, + // The dimensions of the containing block element that the popover is + // positioned relative to (e.g. parent with position: relative). + // Usually this is the same as the boundary element, but if the popover + // is portaled somewhere other than the body and has an ancestor with + // position: relative/absolute, it will be different. + containerDimensions: Dimensions, + padding: number, + containerOffsetWithBoundary: Offset +) { + let containerScroll = containerDimensions.scroll[axis] ?? 0; + // The height/width of the boundary. Matches the axis along which we are adjusting the overlay position + let boundarySize = boundaryDimensions[AXIS_SIZE[axis]]; + // Calculate the edges of the boundary (accomodating for the boundary padding) and the edges of the overlay. + // Note that these values are with respect to the visual viewport (aka 0,0 is the top left of the viewport) + let boundaryStartEdge = boundaryDimensions.scroll[AXIS[axis]] + padding; + let boundaryEndEdge = boundarySize + boundaryDimensions.scroll[AXIS[axis]] - padding; + let startEdgeOffset = offset - containerScroll + containerOffsetWithBoundary[axis] - boundaryDimensions[AXIS[axis]]; + let endEdgeOffset = offset - containerScroll + size + containerOffsetWithBoundary[axis] - boundaryDimensions[AXIS[axis]]; + + // If any of the overlay edges falls outside of the boundary, shift the overlay the required amount to align one of the overlay's + // edges with the closest boundary edge. + if (startEdgeOffset < boundaryStartEdge) { + return boundaryStartEdge - startEdgeOffset; + } else if (endEdgeOffset > boundaryEndEdge) { + return Math.max(boundaryEndEdge - endEdgeOffset, boundaryStartEdge - startEdgeOffset); + } else { + return 0; + } +} + +function getMargins(node: Element): Position { + let style = window.getComputedStyle(node); + return { + top: parseInt(style.marginTop, 10) || 0, + bottom: parseInt(style.marginBottom, 10) || 0, + left: parseInt(style.marginLeft, 10) || 0, + right: parseInt(style.marginRight, 10) || 0 + }; +} + +function parsePlacement(input: Placement): ParsedPlacement { + if (PARSED_PLACEMENT_CACHE[input]) { + return PARSED_PLACEMENT_CACHE[input]; + } + + let [placement, crossPlacement] = input.split(' '); + let axis: Axis = AXIS[placement] || 'right'; + let crossAxis: Axis = CROSS_AXIS[axis]; + + if (!AXIS[crossPlacement]) { + crossPlacement = 'center'; + } + + let size = AXIS_SIZE[axis]; + let crossSize = AXIS_SIZE[crossAxis]; + PARSED_PLACEMENT_CACHE[input] = {placement, crossPlacement, axis, crossAxis, size, crossSize}; + return PARSED_PLACEMENT_CACHE[input]; +} + +function computePosition( + childOffset: Offset, + boundaryDimensions: Dimensions, + overlaySize: Offset, + placementInfo: ParsedPlacement, + offset: number, + crossOffset: number, + containerOffsetWithBoundary: Offset, + isContainerPositioned: boolean, + arrowSize: number, + arrowBoundaryOffset: number +) { + let {placement, crossPlacement, axis, crossAxis, size, crossSize} = placementInfo; + let position: Position = {}; + + // button position + position[crossAxis] = childOffset[crossAxis] ?? 0; + if (crossPlacement === 'center') { + // + (button size / 2) - (overlay size / 2) + // at this point the overlay center should match the button center + position[crossAxis]! += ((childOffset[crossSize] ?? 0) - (overlaySize[crossSize] ?? 0)) / 2; + } else if (crossPlacement !== crossAxis) { + // + (button size) - (overlay size) + // at this point the overlay bottom should match the button bottom + position[crossAxis]! += (childOffset[crossSize] ?? 0) - (overlaySize[crossSize] ?? 0); + }/* else { + the overlay top should match the button top + } */ + + position[crossAxis]! += crossOffset; + + // overlay top overlapping arrow with button bottom + const minPosition = childOffset[crossAxis] - overlaySize[crossSize] + arrowSize + arrowBoundaryOffset; + // overlay bottom overlapping arrow with button top + const maxPosition = childOffset[crossAxis] + childOffset[crossSize] - arrowSize - arrowBoundaryOffset; + position[crossAxis] = clamp(position[crossAxis]!, minPosition, maxPosition); + + // Floor these so the position isn't placed on a partial pixel, only whole pixels. Shouldn't matter if it was floored or ceiled, so chose one. + if (placement === axis) { + // If the container is positioned (non-static), then we use the container's actual + // height, as `bottom` will be relative to this height. But if the container is static, + // then it can only be the `document.body`, and `bottom` will be relative to _its_ + // container, which should be as large as boundaryDimensions. + const containerHeight = (isContainerPositioned ? containerOffsetWithBoundary[size] : boundaryDimensions[TOTAL_SIZE[size]]); + position[FLIPPED_DIRECTION[axis]] = Math.floor(containerHeight - childOffset[axis] + offset); + } else { + position[axis] = Math.floor(childOffset[axis] + childOffset[size] + offset); + } + return position; +} + +function getMaxHeight( + position: Position, + boundaryDimensions: Dimensions, + containerOffsetWithBoundary: Offset, + isContainerPositioned: boolean, + margins: Position, + padding: number, + overlayHeight: number, + heightGrowthDirection: HeightGrowthDirection +) { + const containerHeight = (isContainerPositioned ? containerOffsetWithBoundary.height : boundaryDimensions[TOTAL_SIZE.height]); + // For cases where position is set via "bottom" instead of "top", we need to calculate the true overlay top with respect to the boundary. Reverse calculate this with the same method + // used in computePosition. + let overlayTop = position.top != null ? containerOffsetWithBoundary.top + position.top : containerOffsetWithBoundary.top + (containerHeight - (position.bottom ?? 0) - overlayHeight); + let maxHeight = heightGrowthDirection !== 'top' ? + // We want the distance between the top of the overlay to the bottom of the boundary + Math.max(0, + (boundaryDimensions.height + boundaryDimensions.top + (boundaryDimensions.scroll.top ?? 0)) // this is the bottom of the boundary + - overlayTop // this is the top of the overlay + - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding + ) + // We want the distance between the bottom of the overlay to the top of the boundary + : Math.max(0, + (overlayTop + overlayHeight) // this is the bottom of the overlay + - (boundaryDimensions.top + (boundaryDimensions.scroll.top ?? 0)) // this is the top of the boundary + - ((margins.top ?? 0) + (margins.bottom ?? 0) + padding) // save additional space for margin and padding + ); + return Math.min(boundaryDimensions.height - (padding * 2), maxHeight); +} + +function getAvailableSpace( + boundaryDimensions: Dimensions, + containerOffsetWithBoundary: Offset, + childOffset: Offset, + margins: Position, + padding: number, + placementInfo: ParsedPlacement +) { + let {placement, axis, size} = placementInfo; + if (placement === axis) { + return Math.max(0, childOffset[axis] - boundaryDimensions[axis] - (boundaryDimensions.scroll[axis] ?? 0) + containerOffsetWithBoundary[axis] - (margins[axis] ?? 0) - margins[FLIPPED_DIRECTION[axis]] - padding); + } + + return Math.max(0, boundaryDimensions[size] + boundaryDimensions[axis] + boundaryDimensions.scroll[axis] - containerOffsetWithBoundary[axis] - childOffset[axis] - childOffset[size] - (margins[axis] ?? 0) - margins[FLIPPED_DIRECTION[axis]] - padding); +} + +export function calculatePositionInternal( + placementInput: Placement, + childOffset: Offset, + overlaySize: Offset, + scrollSize: Offset, + margins: Position, + padding: number, + flip: boolean, + boundaryDimensions: Dimensions, + containerDimensions: Dimensions, + containerOffsetWithBoundary: Offset, + offset: number, + crossOffset: number, + isContainerPositioned: boolean, + userSetMaxHeight: number | undefined, + arrowSize: number, + arrowBoundaryOffset: number +): PositionResult { + let placementInfo = parsePlacement(placementInput); + let {size, crossAxis, crossSize, placement, crossPlacement} = placementInfo; + let position = computePosition(childOffset, boundaryDimensions, overlaySize, placementInfo, offset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset); + let normalizedOffset = offset; + let space = getAvailableSpace( + boundaryDimensions, + containerOffsetWithBoundary, + childOffset, + margins, + padding + offset, + placementInfo + ); + + // Check if the scroll size of the overlay is greater than the available space to determine if we need to flip + if (flip && scrollSize[size] > space) { + let flippedPlacementInfo = parsePlacement(`${FLIPPED_DIRECTION[placement]} ${crossPlacement}` as Placement); + let flippedPosition = computePosition(childOffset, boundaryDimensions, overlaySize, flippedPlacementInfo, offset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset); + let flippedSpace = getAvailableSpace( + boundaryDimensions, + containerOffsetWithBoundary, + childOffset, + margins, + padding + offset, + flippedPlacementInfo + ); + + // If the available space for the flipped position is greater than the original available space, flip. + if (flippedSpace > space) { + placementInfo = flippedPlacementInfo; + position = flippedPosition; + normalizedOffset = offset; + } + } + + // Determine the direction the height of the overlay can grow so that we can choose how to calculate the max height + let heightGrowthDirection: HeightGrowthDirection = 'bottom'; + if (placementInfo.axis === 'top') { + if (placementInfo.placement === 'top') { + heightGrowthDirection = 'top'; + } else if (placementInfo.placement === 'bottom') { + heightGrowthDirection = 'bottom'; + } + } else if (placementInfo.crossAxis === 'top') { + if (placementInfo.crossPlacement === 'top') { + heightGrowthDirection = 'bottom'; + } else if (placementInfo.crossPlacement === 'bottom') { + heightGrowthDirection = 'top'; + } + } + + let delta = getDelta(crossAxis, position[crossAxis]!, overlaySize[crossSize], boundaryDimensions, containerDimensions, padding, containerOffsetWithBoundary); + position[crossAxis]! += delta; + + let maxHeight = getMaxHeight( + position, + boundaryDimensions, + containerOffsetWithBoundary, + isContainerPositioned, + margins, + padding, + overlaySize.height, + heightGrowthDirection + ); + + if (userSetMaxHeight && userSetMaxHeight < maxHeight) { + maxHeight = userSetMaxHeight; + } + + overlaySize.height = Math.min(overlaySize.height, maxHeight); + + position = computePosition(childOffset, boundaryDimensions, overlaySize, placementInfo, normalizedOffset, crossOffset, containerOffsetWithBoundary, isContainerPositioned, arrowSize, arrowBoundaryOffset); + delta = getDelta(crossAxis, position[crossAxis]!, overlaySize[crossSize], boundaryDimensions, containerDimensions, padding, containerOffsetWithBoundary); + position[crossAxis]! += delta; + + let arrowPosition: Position = {}; + + // All values are transformed so that 0 is at the top/left of the overlay depending on the orientation + // Prefer the arrow being in the center of the trigger/overlay anchor element + // childOffset[crossAxis] + .5 * childOffset[crossSize] = absolute position with respect to the trigger's coordinate system that would place the arrow in the center of the trigger + // position[crossAxis] - margins[AXIS[crossAxis]] = value use to transform the position to a value with respect to the overlay's coordinate system. A child element's (aka arrow) position absolute's "0" + // is positioned after the margin of its parent (aka overlay) so we need to subtract it to get the proper coordinate transform + let origin = childOffset[crossAxis] - position[crossAxis]! - margins[AXIS[crossAxis]]; + let preferredArrowPosition = origin + .5 * childOffset[crossSize]; + + // Min/Max position limits for the arrow with respect to the overlay + const arrowMinPosition = arrowSize / 2 + arrowBoundaryOffset; + // overlaySize[crossSize] - margins = true size of the overlay + const overlayMargin = AXIS[crossAxis] === 'left' ? (margins.left ?? 0) + (margins.right ?? 0) : (margins.top ?? 0) + (margins.bottom ?? 0); + const arrowMaxPosition = overlaySize[crossSize] - overlayMargin - (arrowSize / 2) - arrowBoundaryOffset; + + // Min/Max position limits for the arrow with respect to the trigger/overlay anchor element + // Same margin accomodation done here as well as for the preferredArrowPosition + const arrowOverlappingChildMinEdge = childOffset[crossAxis] + (arrowSize / 2) - (position[crossAxis] + margins[AXIS[crossAxis]]); + const arrowOverlappingChildMaxEdge = childOffset[crossAxis] + childOffset[crossSize] - (arrowSize / 2) - (position[crossAxis] + margins[AXIS[crossAxis]]); + + // Clamp the arrow positioning so that it always is within the bounds of the anchor and the overlay + const arrowPositionOverlappingChild = clamp(preferredArrowPosition, arrowOverlappingChildMinEdge, arrowOverlappingChildMaxEdge); + arrowPosition[crossAxis] = clamp(arrowPositionOverlappingChild, arrowMinPosition, arrowMaxPosition); + + // If there is an arrow, use that as the origin so that animations are smooth. + // Otherwise use the target edge. + ({placement, crossPlacement} = placementInfo); + if (arrowSize) { + origin = arrowPosition[crossAxis]; + } else if (crossPlacement === 'right') { + origin += childOffset[crossSize]; + } else if (crossPlacement === 'center') { + origin += childOffset[crossSize] / 2; + } + + let crossOrigin = placement === 'left' || placement === 'top' ? overlaySize[size] : 0; + let triggerAnchorPoint = { + x: placement === 'top' || placement === 'bottom' ? origin : crossOrigin, + y: placement === 'left' || placement === 'right' ? origin : crossOrigin + }; + + return { + position, + maxHeight: maxHeight, + arrowOffsetLeft: arrowPosition.left, + arrowOffsetTop: arrowPosition.top, + placement, + triggerAnchorPoint + }; +} + +/** + * Determines where to place the overlay with regards to the target and the position of an optional indicator. + */ +export function calculatePosition(opts: PositionOpts): PositionResult { + let { + placement, + targetNode, + overlayNode, + scrollNode, + padding, + shouldFlip, + boundaryElement, + offset, + crossOffset, + maxHeight, + arrowSize = 0, + arrowBoundaryOffset = 0 + } = opts; + + let container = overlayNode instanceof HTMLElement ? getContainingBlock(overlayNode) : document.documentElement; + let isViewportContainer = container === document.documentElement; + const containerPositionStyle = window.getComputedStyle(container).position; + let isContainerPositioned = !!containerPositionStyle && containerPositionStyle !== 'static'; + let childOffset: Offset = isViewportContainer ? getOffset(targetNode, false) : getPosition(targetNode, container, false); + + if (!isViewportContainer) { + let {marginTop, marginLeft} = window.getComputedStyle(targetNode); + childOffset.top += parseInt(marginTop, 10) || 0; + childOffset.left += parseInt(marginLeft, 10) || 0; + } + + let overlaySize: Offset = getOffset(overlayNode, true); + let margins = getMargins(overlayNode); + overlaySize.width += (margins.left ?? 0) + (margins.right ?? 0); + overlaySize.height += (margins.top ?? 0) + (margins.bottom ?? 0); + + let scrollSize = getScroll(scrollNode); + let boundaryDimensions = getContainerDimensions(boundaryElement); + let containerDimensions = getContainerDimensions(container); + // If the container is the HTML element wrapping the body element, the retrieved scrollTop/scrollLeft will be equal to the + // body element's scroll. Set the container's scroll values to 0 since the overlay's edge position value in getDelta don't then need to be further offset + // by the container scroll since they are essentially the same containing element and thus in the same coordinate system + let containerOffsetWithBoundary: Offset = boundaryElement.tagName === 'BODY' ? getOffset(container, false) : getPosition(container, boundaryElement, false); + if (container.tagName === 'HTML' && boundaryElement.tagName === 'BODY') { + containerDimensions.scroll.top = 0; + containerDimensions.scroll.left = 0; + } + + return calculatePositionInternal( + placement, + childOffset, + overlaySize, + scrollSize, + margins, + padding, + shouldFlip, + boundaryDimensions, + containerDimensions, + containerOffsetWithBoundary, + offset, + crossOffset, + isContainerPositioned, + maxHeight, + arrowSize, + arrowBoundaryOffset + ); +} + +export function getRect(node: Element, ignoreScale: boolean) { + let {top, left, width, height} = node.getBoundingClientRect(); + + // Use offsetWidth and offsetHeight if this is an HTML element, so that + // the size is not affected by scale transforms. + if (ignoreScale && node instanceof node.ownerDocument.defaultView!.HTMLElement) { + width = node.offsetWidth; + height = node.offsetHeight; + } + + return {top, left, width, height}; +} + +function getOffset(node: Element, ignoreScale: boolean): Offset { + let {top, left, width, height} = getRect(node, ignoreScale); + let {scrollTop, scrollLeft, clientTop, clientLeft} = document.documentElement; + return { + top: top + scrollTop - clientTop, + left: left + scrollLeft - clientLeft, + width, + height + }; +} + +function getPosition(node: Element, parent: Element, ignoreScale: boolean): Offset { + let style = window.getComputedStyle(node); + let offset: Offset; + if (style.position === 'fixed') { + offset = getRect(node, ignoreScale); + } else { + offset = getOffset(node, ignoreScale); + let parentOffset = getOffset(parent, ignoreScale); + let parentStyle = window.getComputedStyle(parent); + parentOffset.top += (parseInt(parentStyle.borderTopWidth, 10) || 0) - parent.scrollTop; + parentOffset.left += (parseInt(parentStyle.borderLeftWidth, 10) || 0) - parent.scrollLeft; + offset.top -= parentOffset.top; + offset.left -= parentOffset.left; + } + + offset.top -= parseInt(style.marginTop, 10) || 0; + offset.left -= parseInt(style.marginLeft, 10) || 0; + return offset; +} + +// Returns the containing block of an element, which is the element that +// this element will be positioned relative to. +// https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block +function getContainingBlock(node: HTMLElement): Element { + // The offsetParent of an element in most cases equals the containing block. + // https://w3c.github.io/csswg-drafts/cssom-view/#dom-htmlelement-offsetparent + let offsetParent = node.offsetParent; + + // The offsetParent algorithm terminates at the document body, + // even if the body is not a containing block. Double check that + // and use the documentElement if so. + if ( + offsetParent && + offsetParent === document.body && + window.getComputedStyle(offsetParent).position === 'static' && + !isContainingBlock(offsetParent) + ) { + offsetParent = document.documentElement; + } + + // TODO(later): handle table elements? + + // The offsetParent can be null if the element has position: fixed, or a few other cases. + // We have to walk up the tree manually in this case because fixed positioned elements + // are still positioned relative to their containing block, which is not always the viewport. + if (offsetParent == null) { + offsetParent = node.parentElement; + while (offsetParent && !isContainingBlock(offsetParent)) { + offsetParent = offsetParent.parentElement; + } + } + + // Fall back to the viewport. + return offsetParent || document.documentElement; +} + +// https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block +function isContainingBlock(node: Element): boolean { + let style = window.getComputedStyle(node); + return ( + style.transform !== 'none' || + /transform|perspective/.test(style.willChange) || + style.filter !== 'none' || + style.contain === 'paint' || + ('backdropFilter' in style && style.backdropFilter !== 'none') || + ('WebkitBackdropFilter' in style && style.WebkitBackdropFilter !== 'none') + ); +} diff --git a/packages/@react-spectrum/menu/src/useCloseOnScroll.ts b/packages/@react-spectrum/menu/src/useCloseOnScroll.ts new file mode 100644 index 00000000000..23899dccbf8 --- /dev/null +++ b/packages/@react-spectrum/menu/src/useCloseOnScroll.ts @@ -0,0 +1,64 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import {RefObject} from '@react-types/shared'; +import {useEffect} from 'react'; + +// This behavior moved from useOverlayTrigger to useOverlayPosition. +// For backward compatibility, where useOverlayTrigger handled hiding the popover on close, +// it sets a close function here mapped from the trigger element. This way we can avoid +// forcing users to pass an onClose function to useOverlayPosition which could be considered +// a breaking change. +export const onCloseMap: WeakMap void> = new WeakMap(); + +interface CloseOnScrollOptions { + triggerRef: RefObject, + isOpen?: boolean, + onClose?: (() => void) | null +} + +/** @private */ +export function useCloseOnScroll(opts: CloseOnScrollOptions): void { + let {triggerRef, isOpen, onClose} = opts; + + useEffect(() => { + if (!isOpen || onClose === null) { + return; + } + + let onScroll = (e: Event) => { + // Ignore if scrolling an scrollable region outside the trigger's tree. + let target = e.target; + // window is not a Node and doesn't have contain, but window contains everything + if (!triggerRef.current || ((target instanceof Node) && !target.contains(triggerRef.current))) { + return; + } + + // Ignore scroll events on any input or textarea as the cursor position can cause it to scroll + // such as in a combobox. Clicking the dropdown button places focus on the input, and if the + // text inside the input extends beyond the 'end', then it will scroll so the cursor is visible at the end. + if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement) { + return; + } + + let onCloseHandler = onClose || onCloseMap.get(triggerRef.current); + if (onCloseHandler) { + onCloseHandler(); + } + }; + + window.addEventListener('scroll', onScroll, true); + return () => { + window.removeEventListener('scroll', onScroll, true); + }; + }, [isOpen, onClose, triggerRef]); +} diff --git a/packages/@react-spectrum/menu/src/useOverlayPosition.ts b/packages/@react-spectrum/menu/src/useOverlayPosition.ts new file mode 100644 index 00000000000..59c61a08075 --- /dev/null +++ b/packages/@react-spectrum/menu/src/useOverlayPosition.ts @@ -0,0 +1,327 @@ +/* + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import {calculatePosition, getRect, PositionResult} from './calculatePosition'; +import {DOMAttributes, RefObject} from '@react-types/shared'; +import {Placement, PlacementAxis, PositionProps} from '@react-types/overlays'; +import {useCallback, useEffect, useRef, useState} from 'react'; +import {useCloseOnScroll} from './useCloseOnScroll'; +import {useLayoutEffect, useResizeObserver} from '@react-aria/utils'; +import {useLocale} from '@react-aria/i18n'; + +export interface AriaPositionProps extends PositionProps { + /** + * Cross size of the overlay arrow in pixels. + * @default 0 + */ + arrowSize?: number, + /** + * Element that that serves as the positioning boundary. + * @default document.body + */ + boundaryElement?: Element, + /** + * The ref for the element which the overlay positions itself with respect to. + */ + targetRef: RefObject, + /** + * The ref for the overlay element. + */ + overlayRef: RefObject, + /** + * The ref for the arrow element. + */ + arrowRef?: RefObject, + /** + * A ref for the scrollable region within the overlay. + * @default overlayRef + */ + scrollRef?: RefObject, + /** + * Whether the overlay should update its position automatically. + * @default true + */ + shouldUpdatePosition?: boolean, + /** Handler that is called when the overlay should close. */ + onClose?: (() => void) | null, + /** + * The maxHeight specified for the overlay element. + * By default, it will take all space up to the current viewport height. + */ + maxHeight?: number, + /** + * The minimum distance the arrow's edge should be from the edge of the overlay element. + * @default 0 + */ + arrowBoundaryOffset?: number +} + +export interface PositionAria { + /** Props for the overlay container element. */ + overlayProps: DOMAttributes, + /** Props for the overlay tip arrow if any. */ + arrowProps: DOMAttributes, + /** Placement of the overlay with respect to the overlay trigger. */ + placement: PlacementAxis | null, + /** The origin of the target in the overlay's coordinate system. Useful for animations. */ + triggerAnchorPoint: {x: number, y: number} | null, + /** Updates the position of the overlay. */ + updatePosition(): void +} + +interface ScrollAnchor { + type: 'top' | 'bottom', + offset: number +} + +let visualViewport = typeof document !== 'undefined' ? window.visualViewport : null; + +/** + * Handles positioning overlays like popovers and menus relative to a trigger + * element, and updating the position when the window resizes. + */ +export function useOverlayPosition(props: AriaPositionProps): PositionAria { + let {direction} = useLocale(); + let { + arrowSize, + targetRef, + overlayRef, + arrowRef, + scrollRef = overlayRef, + placement = 'bottom' as Placement, + containerPadding = 12, + shouldFlip = true, + boundaryElement = typeof document !== 'undefined' ? document.body : null, + offset = 0, + crossOffset = 0, + shouldUpdatePosition = true, + isOpen = true, + onClose, + maxHeight, + arrowBoundaryOffset = 0 + } = props; + let [position, setPosition] = useState(null); + + let deps = [ + shouldUpdatePosition, + placement, + overlayRef.current, + targetRef.current, + arrowRef?.current, + scrollRef.current, + containerPadding, + shouldFlip, + boundaryElement, + offset, + crossOffset, + isOpen, + direction, + maxHeight, + arrowBoundaryOffset, + arrowSize + ]; + + // Note, the position freezing breaks if body sizes itself dynamicly with the visual viewport but that might + // just be a non-realistic use case + // Upon opening a overlay, record the current visual viewport scale so we can freeze the overlay styles + let lastScale = useRef(visualViewport?.scale); + useEffect(() => { + if (isOpen) { + lastScale.current = visualViewport?.scale; + } + }, [isOpen]); + + let updatePosition = useCallback(() => { + if (shouldUpdatePosition === false || !isOpen || !overlayRef.current || !targetRef.current || !boundaryElement) { + return; + } + + if (visualViewport?.scale !== lastScale.current) { + return; + } + + // Determine a scroll anchor based on the focused element. + // This stores the offset of the anchor element from the scroll container + // so it can be restored after repositioning. This way if the overlay height + // changes, the focused element appears to stay in the same position. + let anchor: ScrollAnchor | null = null; + if (scrollRef.current && scrollRef.current.contains(document.activeElement)) { + let anchorRect = document.activeElement?.getBoundingClientRect(); + let scrollRect = scrollRef.current.getBoundingClientRect(); + // Anchor from the top if the offset is in the top half of the scrollable element, + // otherwise anchor from the bottom. + anchor = { + type: 'top', + offset: (anchorRect?.top ?? 0) - scrollRect.top + }; + if (anchor.offset > scrollRect.height / 2) { + anchor.type = 'bottom'; + anchor.offset = (anchorRect?.bottom ?? 0) - scrollRect.bottom; + } + } + + // Always reset the overlay's previous max height if not defined by the user so that we can compensate for + // RAC collections populating after a second render and properly set a correct max height + positioning when it populates. + let overlay = (overlayRef.current as HTMLElement); + if (!maxHeight && overlayRef.current) { + overlay.style.top = '0px'; + overlay.style.bottom = ''; + overlay.style.maxHeight = (window.visualViewport?.height ?? window.innerHeight) + 'px'; + } + + let position = calculatePosition({ + placement: translateRTL(placement, direction), + overlayNode: overlayRef.current, + targetNode: targetRef.current, + scrollNode: scrollRef.current || overlayRef.current, + padding: containerPadding, + shouldFlip, + boundaryElement, + offset, + crossOffset, + maxHeight, + arrowSize: arrowSize ?? (arrowRef?.current ? getRect(arrowRef.current, true).width : 0), + arrowBoundaryOffset + }); + + if (!position.position) { + return; + } + + // Modify overlay styles directly so positioning happens immediately without the need of a second render + // This is so we don't have to delay autoFocus scrolling or delay applying preventScroll for popovers + overlay.style.top = ''; + overlay.style.bottom = ''; + overlay.style.left = ''; + overlay.style.right = ''; + + Object.keys(position.position).forEach(key => overlay.style[key] = (position.position!)[key] + 'px'); + overlay.style.maxHeight = position.maxHeight != null ? position.maxHeight + 'px' : ''; + + // Restore scroll position relative to anchor element. + if (anchor && document.activeElement && scrollRef.current) { + let anchorRect = document.activeElement.getBoundingClientRect(); + let scrollRect = scrollRef.current.getBoundingClientRect(); + let newOffset = anchorRect[anchor.type] - scrollRect[anchor.type]; + scrollRef.current.scrollTop += newOffset - anchor.offset; + } + + // Trigger a set state for a second render anyway for arrow positioning + setPosition(position); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, deps); + + // Update position when anything changes + // eslint-disable-next-line react-hooks/exhaustive-deps + useLayoutEffect(updatePosition, deps); + + // Update position on window resize + useResize(updatePosition); + + // Update position when the overlay changes size (might need to flip). + useResizeObserver({ + ref: overlayRef, + onResize: updatePosition + }); + + // Update position when the target changes size (might need to flip). + useResizeObserver({ + ref: targetRef, + onResize: updatePosition + }); + + // Reposition the overlay and do not close on scroll while the visual viewport is resizing. + // This will ensure that overlays adjust their positioning when the iOS virtual keyboard appears. + let isResizing = useRef(false); + useLayoutEffect(() => { + let timeout: ReturnType; + let onResize = () => { + isResizing.current = true; + clearTimeout(timeout); + + timeout = setTimeout(() => { + isResizing.current = false; + }, 500); + + updatePosition(); + }; + + // Only reposition the overlay if a scroll event happens immediately as a result of resize (aka the virtual keyboard has appears) + // We don't want to reposition the overlay if the user has pinch zoomed in and is scrolling the viewport around. + let onScroll = () => { + if (isResizing.current) { + onResize(); + } + }; + + visualViewport?.addEventListener('resize', onResize); + visualViewport?.addEventListener('scroll', onScroll); + return () => { + visualViewport?.removeEventListener('resize', onResize); + visualViewport?.removeEventListener('scroll', onScroll); + }; + }, [updatePosition]); + + let close = useCallback(() => { + if (!isResizing.current) { + onClose?.(); + } + }, [onClose, isResizing]); + + // When scrolling a parent scrollable region of the trigger (other than the body), + // we hide the popover. Otherwise, its position would be incorrect. + useCloseOnScroll({ + triggerRef: targetRef, + isOpen, + onClose: onClose && close + }); + + return { + overlayProps: { + style: { + position: position ? 'absolute' : 'fixed', + top: !position ? 0 : undefined, + left: !position ? 0 : undefined, + zIndex: 100000, // should match the z-index in ModalTrigger + ...position?.position, + maxHeight: position?.maxHeight ?? '100vh' + } + }, + placement: position?.placement ?? null, + triggerAnchorPoint: position?.triggerAnchorPoint ?? null, + arrowProps: { + 'aria-hidden': 'true', + role: 'presentation', + style: { + left: position?.arrowOffsetLeft, + top: position?.arrowOffsetTop + } + }, + updatePosition + }; +} + +function useResize(onResize) { + useLayoutEffect(() => { + window.addEventListener('resize', onResize, false); + return () => { + window.removeEventListener('resize', onResize, false); + }; + }, [onResize]); +} + +function translateRTL(position, direction) { + if (direction === 'rtl') { + return position.replace('start', 'right').replace('end', 'left'); + } + return position.replace('start', 'left').replace('end', 'right'); +} diff --git a/packages/@react-spectrum/menu/src/usePopover.ts b/packages/@react-spectrum/menu/src/usePopover.ts new file mode 100644 index 00000000000..2016a4d7da6 --- /dev/null +++ b/packages/@react-spectrum/menu/src/usePopover.ts @@ -0,0 +1,136 @@ +/* + * Copyright 2022 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import {ariaHideOutside, AriaPositionProps, useOverlay, usePreventScroll} from '@react-aria/overlays'; +import {DOMAttributes, RefObject} from '@react-types/shared'; +import {mergeProps} from '@react-aria/utils'; +import {OverlayTriggerState} from '@react-stately/overlays'; +import {PlacementAxis} from '@react-types/overlays'; +import {useEffect} from 'react'; +import {useOverlayPosition} from './useOverlayPosition'; + +export interface AriaPopoverProps extends Omit { + /** + * The ref for the element which the popover positions itself with respect to. + */ + triggerRef: RefObject, + /** + * The ref for the popover element. + */ + popoverRef: RefObject, + /** A ref for the popover arrow element. */ + arrowRef?: RefObject, + /** + * An optional ref for a group of popovers, e.g. submenus. + * When provided, this element is used to detect outside interactions + * and hiding elements from assistive technologies instead of the popoverRef. + */ + groupRef?: RefObject, + /** + * Whether the popover is non-modal, i.e. elements outside the popover may be + * interacted with by assistive technologies. + * + * Most popovers should not use this option as it may negatively impact the screen + * reader experience. Only use with components such as combobox, which are designed + * to handle this situation carefully. + */ + isNonModal?: boolean, + /** + * Whether pressing the escape key to close the popover should be disabled. + * + * Most popovers should not use this option. When set to true, an alternative + * way to close the popover with a keyboard must be provided. + * + * @default false + */ + isKeyboardDismissDisabled?: boolean, + /** + * When user interacts with the argument element outside of the popover ref, + * return true if onClose should be called. This gives you a chance to filter + * out interaction with elements that should not dismiss the popover. + * By default, onClose will always be called on interaction outside the popover ref. + */ + shouldCloseOnInteractOutside?: (element: Element) => boolean +} + +export interface PopoverAria { + /** Props for the popover element. */ + popoverProps: DOMAttributes, + /** Props for the popover tip arrow if any. */ + arrowProps: DOMAttributes, + /** Props to apply to the underlay element, if any. */ + underlayProps: DOMAttributes, + /** Placement of the popover with respect to the trigger. */ + placement: PlacementAxis | null, + /** The origin of the target in the overlay's coordinate system. Useful for animations. */ + triggerAnchorPoint: {x: number, y: number} | null +} + +/** + * Provides the behavior and accessibility implementation for a popover component. + * A popover is an overlay element positioned relative to a trigger. + */ +export function usePopover(props: AriaPopoverProps, state: OverlayTriggerState): PopoverAria { + let { + triggerRef, + popoverRef, + groupRef, + isNonModal, + isKeyboardDismissDisabled, + shouldCloseOnInteractOutside, + ...otherProps + } = props; + + let isSubmenu = otherProps['trigger'] === 'SubmenuTrigger'; + + let {overlayProps, underlayProps} = useOverlay( + { + isOpen: state.isOpen, + onClose: state.close, + shouldCloseOnBlur: true, + isDismissable: !isNonModal || isSubmenu, + isKeyboardDismissDisabled, + shouldCloseOnInteractOutside + }, + groupRef ?? popoverRef + ); + + let {overlayProps: positionProps, arrowProps, placement, triggerAnchorPoint: origin} = useOverlayPosition({ + ...otherProps, + targetRef: triggerRef, + overlayRef: popoverRef, + isOpen: state.isOpen, + onClose: isNonModal && !isSubmenu ? state.close : null + }); + + usePreventScroll({ + isDisabled: isNonModal || !state.isOpen + }); + + useEffect(() => { + if (state.isOpen && popoverRef.current) { + if (isNonModal) { + return; + } else { + return ariaHideOutside([groupRef?.current ?? popoverRef.current], {shouldUseInert: true}); + } + } + }, [isNonModal, state.isOpen, popoverRef, groupRef]); + + return { + popoverProps: mergeProps(overlayProps, positionProps), + arrowProps, + underlayProps, + placement, + triggerAnchorPoint: origin + }; +}