import type { Ref, FunctionComponent } from 'react'; import { forwardRef, useEffect, useMemo, useRef, useState } from 'react'; import { Button, ButtonProps, Icon, Tooltip, TooltipProps } from '@patternfly/react-core'; import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon'; export interface ChatbotHeaderMenuProps extends ButtonProps { /** Callback function to attach to menu toggle on top right of chatbot header. */ onMenuToggle: () => void; /** Custom classname for the header component */ className?: string; /** Props spread to the PF Tooltip component wrapping the display mode dropdown */ tooltipProps?: TooltipProps; /** Aria label for menu */ menuAriaLabel?: string; /** Ref applied to menu */ innerRef?: React.Ref; /** Content used in tooltip */ tooltipContent?: string; /** Sets menu to compact styling. */ isCompact?: boolean; } const ChatbotHeaderMenuBase: FunctionComponent = ({ className, onMenuToggle, tooltipProps, menuAriaLabel = 'Chat history menu', innerRef, tooltipContent = 'Chat history menu', isCompact, ...props }: ChatbotHeaderMenuProps) => { const [isDrawerAnimating, setIsDrawerAnimating] = useState(false); // I'd like to use a prop here later if this works const drawerState = props['aria-expanded']; const isDrawerOpen = drawerState === true; const prevDrawerStateRef = useRef(isDrawerOpen); const buttonRef = useRef(null); useEffect(() => { if (drawerState !== undefined) { const wasDrawerOpen = prevDrawerStateRef.current === true; const isDrawerClosing = wasDrawerOpen && !isDrawerOpen; setIsDrawerAnimating(true); const timeout = setTimeout(() => { setIsDrawerAnimating(false); if (isDrawerClosing) { requestAnimationFrame(() => { buttonRef.current?.focus(); }); } }, 350); prevDrawerStateRef.current = isDrawerOpen; return () => clearTimeout(timeout); } }, [drawerState, isDrawerOpen]); const button = useMemo( () => (