import { ReactNode, useEffect, useRef } from "react"; import classNames from "classnames"; import { FocusOn } from "react-focus-on"; import { VuiFlexContainer } from "../flex/FlexContainer"; import { VuiFlexItem } from "../flex/FlexItem"; import { VuiIconButton } from "../button/IconButton"; import { VuiIcon } from "../icon/Icon"; import { BiX } from "react-icons/bi"; import { VuiPortal } from "../portal/Portal"; import { VuiScreenBlock } from "../screenBlock/ScreenBlock"; const COLOR = ["primary", "danger"] as const; type Props = { className?: string; title: ReactNode; children: ReactNode; isOpen?: boolean; onClose?: () => void; color?: (typeof COLOR)[number]; }; export const VuiDrawer = ({ className, color = "primary", title, children, isOpen, onClose, ...rest }: Props) => { const returnFocusElRef = useRef(null); // Return focus on unmount. 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. const onCloseDelayed = () => { window.setTimeout(() => { onClose?.(); }, 0); }; const classes = classNames("vuiDrawer", `vuiDrawer--${color}`, className); return ( {isOpen && (
{title} {onClose && ( } /> )}
{children}
)}
); };