diff --git a/src/components/ContextualMenu/ContextualMenu.stories.mdx b/src/components/ContextualMenu/ContextualMenu.stories.mdx index 690f580d0..8c036aea5 100644 --- a/src/components/ContextualMenu/ContextualMenu.stories.mdx +++ b/src/components/ContextualMenu/ContextualMenu.stories.mdx @@ -1,5 +1,5 @@ import { ArgsTable, Canvas, Meta, Story } from "@storybook/addon-docs"; - +import Button from "../Button"; import ContextualMenu from "./ContextualMenu"; ( voluptas odit aspernatur alias molestias facere.

)} + ); diff --git a/src/components/ContextualMenu/ContextualMenu.tsx b/src/components/ContextualMenu/ContextualMenu.tsx index 536c17862..dc6335a30 100644 --- a/src/components/ContextualMenu/ContextualMenu.tsx +++ b/src/components/ContextualMenu/ContextualMenu.tsx @@ -191,6 +191,7 @@ const ContextualMenu = ({ const wrapper = useRef(null); const [positionCoords, setPositionCoords] = useState(); const [adjustedPosition, setAdjustedPosition] = useState(position); + const hasToggle = hasToggleIcon || toggleLabel; useEffect(() => { diff --git a/src/components/ContextualMenu/ContextualMenuDropdown/ContextualMenuDropdown.tsx b/src/components/ContextualMenu/ContextualMenuDropdown/ContextualMenuDropdown.tsx index 5c71191f3..5e6420bee 100644 --- a/src/components/ContextualMenu/ContextualMenuDropdown/ContextualMenuDropdown.tsx +++ b/src/components/ContextualMenu/ContextualMenuDropdown/ContextualMenuDropdown.tsx @@ -24,6 +24,7 @@ export enum Label { export type MenuLink = string | ButtonProps | ButtonProps[]; export type Position = "left" | "center" | "right"; +type VerticalPosition = "top" | "bottom"; /** * The props for the ContextualMenuDropdown component. @@ -55,6 +56,7 @@ export type Props = { */ const getPositionStyle = ( position: Position, + verticalPosition: VerticalPosition, positionCoords: Props["positionCoords"], constrainPanelWidth: Props["constrainPanelWidth"] ): React.CSSProperties => { @@ -62,7 +64,10 @@ const getPositionStyle = ( return null; } const { height, left, top, width } = positionCoords; - const topPos = top + height + (window.scrollY || 0); + const topPos = + verticalPosition === "bottom" + ? top + height + (window.scrollY || 0) + : top + (window.scrollY || 0); let leftPos = left; switch (position) { @@ -161,6 +166,23 @@ const generateLink = ( ); }; +const getClosestScrollableParent = ( + node: HTMLElement | null +): HTMLElement | null => { + let currentNode = node; + while (currentNode && currentNode !== document.body) { + const { overflowY, overflowX } = window.getComputedStyle(currentNode); + if ( + ["auto", "scroll", "overlay"].includes(overflowY) && + ["auto", "scroll", "overlay"].includes(overflowX) + ) { + return currentNode; + } + currentNode = currentNode.parentElement; + } + return document.body; +}; + const ContextualMenuDropdown = ({ adjustedPosition, autoAdjust, @@ -180,30 +202,74 @@ const ContextualMenuDropdown = ({ ...props }: Props): JSX.Element => { const dropdown = useRef(); - + const [verticalPosition, setVerticalPosition] = + useState("bottom"); const [positionStyle, setPositionStyle] = useState( - getPositionStyle(adjustedPosition, positionCoords, constrainPanelWidth) + getPositionStyle( + adjustedPosition, + verticalPosition, + positionCoords, + constrainPanelWidth + ) ); const [maxHeight, setMaxHeight] = useState(); - // Update the styles to position the menu. const updatePositionStyle = useCallback(() => { setPositionStyle( - getPositionStyle(adjustedPosition, positionCoords, constrainPanelWidth) + getPositionStyle( + adjustedPosition, + verticalPosition, + positionCoords, + constrainPanelWidth + ) ); - }, [adjustedPosition, positionCoords, constrainPanelWidth]); + }, [adjustedPosition, positionCoords, verticalPosition, constrainPanelWidth]); + + const updateVerticalPosition = useCallback(() => { + if (!positionNode) { + return null; + } + const scrollableParent = getClosestScrollableParent(positionNode); + if (!scrollableParent) { + return null; + } + const scrollableParentRect = scrollableParent.getBoundingClientRect(); + const rect = positionNode.getBoundingClientRect(); + + // Calculate the rect in relation to the scrollableParent + const relativeRect = { + top: rect.top - scrollableParentRect.top, + bottom: rect.bottom - scrollableParentRect.top, + height: rect.height, + }; + + const spaceBelow = scrollableParentRect.height - relativeRect.bottom; + const spaceAbove = relativeRect.top; + const dropdownHeight = relativeRect.height; + + setVerticalPosition( + spaceBelow >= dropdownHeight || spaceBelow > spaceAbove ? "bottom" : "top" + ); + }, [positionNode]); // Update the position when the window fitment info changes. const onUpdateWindowFitment = useCallback( (fitsWindow: WindowFitment) => { if (autoAdjust) { setAdjustedPosition(adjustForWindow(position, fitsWindow)); + updateVerticalPosition(); } if (scrollOverflow) { setMaxHeight(fitsWindow.fromBottom.spaceBelow - 16); } }, - [autoAdjust, position, scrollOverflow, setAdjustedPosition] + [ + autoAdjust, + position, + scrollOverflow, + setAdjustedPosition, + updateVerticalPosition, + ] ); // Handle adjusting the horizontal position and scrolling of the dropdown so that it remains on screen. @@ -220,6 +286,10 @@ const ContextualMenuDropdown = ({ updatePositionStyle(); }, [adjustedPosition, updatePositionStyle]); + useEffect(() => { + updateVerticalPosition(); + }, [updateVerticalPosition]); + return ( // Vanilla Framework uses .p-contextual-menu parent modifier classnames to determine the correct position of the .p-contextual-menu__dropdown dropdown (left, center, right). // Extra span wrapper is required as the dropdown is rendered in a portal. @@ -237,6 +307,7 @@ const ContextualMenuDropdown = ({ ...(scrollOverflow ? { maxHeight, minHeight: "2rem", overflowX: "auto" } : {}), + ...(verticalPosition === "top" ? { bottom: "0" } : {}), }} {...props} >