import React, { cloneElement, useEffect, useRef, useState } from "react"; import classNames from "classnames"; import { VuiPortal } from "../portal/Portal"; import { FocusOn } from "react-focus-on"; export type Props = { button: React.ReactElement; children?: React.ReactNode; className?: string; header?: React.ReactNode; isOpen: boolean; setIsOpen: (isOpen: boolean) => void; padding?: boolean; }; type Position = { top: number; right: number; }; const getPosition = (button: HTMLElement | null): Position | undefined => { if (!button) return undefined; const { bottom, right } = button.getBoundingClientRect(); return { top: bottom + 2 + document.documentElement.scrollTop, right: window.innerWidth - right }; }; export const VuiPopover = ({ button: originalButton, children, className, header, isOpen, setIsOpen, padding, ...rest }: Props) => { const returnFocusElRef = useRef(null); const buttonRef = useRef(null); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [positionMarker, setPositionMarker] = useState(0); const button = cloneElement(originalButton, { isSelected: isOpen, onClick: () => { setIsOpen(!isOpen); }, ref: (node: HTMLElement) => { buttonRef.current = node; } }); useEffect(() => { const updatePosition = () => { // Force a re-render when the window resizes. setPositionMarker(Date.now()); }; window.removeEventListener("resize", updatePosition); // Mostly defensive to prevent weird bugs where the popover ends // up being rendered partially off-screen. window.removeEventListener("scroll", updatePosition); return () => { window.removeEventListener("resize", updatePosition); window.removeEventListener("scroll", updatePosition); }; }, []); useEffect(() => { if (isOpen) { returnFocusElRef.current = document.activeElement as HTMLElement; } else { returnFocusElRef.current?.focus(); returnFocusElRef.current = null; } }, [isOpen]); // Allow contents to respond to blur events before unmounting, and also // enable focus to properly return to the button when the user clicks // outside of the popover. const onCloseDelayed = () => { window.setTimeout(() => { setIsOpen(false); }, 0); }; // Always keep menu position up to date. If we tried to cache this inside // a useEffect based on isOpen then there'd be a flicker if the width // of the button changes. const position = getPosition(buttonRef.current); const classes = classNames("vuiPopover", className); const contentClasses = classNames("vuiPopoverContent", { "vuiPopoverContent--padding": padding }); return ( <> {button} {isOpen && position && (
{header && typeof header === "string" ?
{header}
: header} {children &&
{children}
}
)}
); };