diff --git a/web/components/menu.react.js b/web/components/menu.react.js --- a/web/components/menu.react.js +++ b/web/components/menu.react.js @@ -3,6 +3,7 @@ import classnames from 'classnames'; import * as React from 'react'; +import { useRenderMenu } from '../menu-provider.react'; import css from './menu.css'; type MenuVariant = 'thread-actions' | 'member-actions'; @@ -11,53 +12,99 @@ +icon: React.Node, +children?: React.Node, +variant?: MenuVariant, + +onChange?: boolean => void, }; function Menu(props: MenuProps): React.Node { - const [isOpen, setIsOpen] = React.useState(false); + const buttonRef = React.useRef(); + const { + renderMenu, + setMenuPosition, + closeMenu, + currentMenu: currentlyRenderedMenu, + } = useRenderMenu(); + const { icon, children, variant = 'thread-actions', onChange } = props; - const { icon, children, variant = 'thread-actions' } = props; + const menuActionListClasses = React.useMemo( + () => + classnames(css.menuActionList, { + [css.menuActionListThreadActions]: variant === 'thread-actions', + [css.menuActionListMemberActions]: variant === 'member-actions', + }), + [variant], + ); - const closeMenuCallback = React.useCallback(() => { - document.removeEventListener('click', closeMenuCallback); - if (isOpen) { - setIsOpen(false); + const menuActionList = React.useMemo( + () =>
{children}
, + [children, menuActionListClasses], + ); + + const updatePosition = React.useCallback(() => { + if (buttonRef.current && currentlyRenderedMenu === menuActionList) { + const { top, left } = buttonRef.current.getBoundingClientRect(); + setMenuPosition({ top, left }); + } + }, [currentlyRenderedMenu, menuActionList, setMenuPosition]); + + React.useEffect(() => { + if (!window) { + return undefined; } - }, [isOpen]); + + window.addEventListener('resize', updatePosition); + return () => window.removeEventListener('resize', updatePosition); + }, [updatePosition]); + + // useLayoutEffect is necessary so that the menu position is immediately + // updated in the first render of component + React.useLayoutEffect(() => { + updatePosition(); + }, [updatePosition]); + + const closeMenuCallback = React.useCallback(() => { + closeMenu(menuActionList); + onChange?.(false); + }, [closeMenu, menuActionList, onChange]); React.useEffect(() => { - if (!document || !isOpen) { + if (currentlyRenderedMenu !== menuActionList) { return undefined; } document.addEventListener('click', closeMenuCallback); - return () => document.removeEventListener('click', closeMenuCallback); - }, [closeMenuCallback, isOpen]); + return () => { + document.removeEventListener('click', closeMenuCallback); + }; + }, [closeMenuCallback, currentlyRenderedMenu, menuActionList]); - const switchMenuCallback = React.useCallback(() => { - setIsOpen(isMenuOpen => !isMenuOpen); - }, []); + const prevActionListRef = React.useRef(null); + React.useEffect(() => { + if (prevActionListRef.current === currentlyRenderedMenu) { + renderMenu(menuActionList); + } + prevActionListRef.current = menuActionList; + }, [currentlyRenderedMenu, menuActionList, renderMenu]); - if (React.Children.count(children) === 0) { - return null; - } + React.useEffect(() => { + return () => closeMenu(prevActionListRef.current); + }, [closeMenu]); - let menuActionList = null; - if (isOpen) { - const menuActionListClasses = classnames(css.menuActionList, { - [css.menuActionListThreadActions]: variant === 'thread-actions', - [css.menuActionListMemberActions]: variant === 'member-actions', - }); + const onClickMenuCallback = React.useCallback(() => { + renderMenu(menuActionList); + onChange?.(true); + }, [menuActionList, onChange, renderMenu]); - menuActionList =
{children}
; + if (React.Children.count(children) === 0) { + return null; } return ( -
- - {menuActionList} -
+ ); }