import * as React from "react"; import styled from "styled-components"; import Scrollable from "~/components/Scrollable"; import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger, } from "~/components/primitives/Drawer"; import { Menu, MenuContent, MenuTrigger } from "~/components/primitives/Menu"; import { MenuProvider } from "~/components/primitives/Menu/MenuContext"; import { actionToMenuItem, hasVisibleActions } from "~/actions"; import useActionContext from "~/hooks/useActionContext"; import useMobile from "~/hooks/useMobile"; import { preventDefault } from "~/utils/events"; import type { ActionFactory, ActionVariant, ActionWithChildren, MenuItem, MenuItemWithChildren, } from "~/types"; import { MenuFilter, MenuFilterEmpty, useMenuFilter } from "./MenuFilter"; import { toMenuItems, toMobileMenuItems } from "./transformer"; import { observer } from "mobx-react"; import { useComputed } from "~/hooks/useComputed"; type Props = { /** Root action with children representing the menu items */ action: ActionWithChildren | ActionFactory; /** Trigger for the menu */ children: React.ReactNode; /** Alignment w.r.t trigger - defaults to start */ align?: "start" | "end"; /** ARIA label for the menu */ ariaLabel: string; /** * Whether the menu should lock page scroll and trap focus while open. * Defaults to true. Set to false to avoid the scrollbar-removal layout * shift when the menu lives inside a scrollable container. */ modal?: boolean; /** Additional component to display at the bottom of the top-level menu */ append?: React.ReactNode; /** Callback when menu is opened */ onOpen?: () => void; /** Callback when menu is closed */ onClose?: () => void; } & Omit, "children">; export const DropdownMenu = observer(function DropdownMenu({ ref, action, children, align = "start", ariaLabel, modal = true, append, onOpen, onClose, ...rest }: Props) { const [open, setOpen] = React.useState(false); const isMobile = useMobile(); const contentRef = React.useRef>(null); const actionContext = useActionContext({ isMenu: true, }); const { query, setQuery, handleKeyDownCapture, handleEscapeKeyDown, reset: resetFilter, } = useMenuFilter(); const menuItems = useComputed(() => { if (!open) { return []; } const resolvedAction = typeof action === "function" ? action() : action; return (resolvedAction.children as ActionVariant[]).map((childAction) => actionToMenuItem(childAction, actionContext) ); }, [open, action, actionContext]); // Only visibility is resolved while the menu is closed, the remainder of // each item is resolved when it is opened. const isEmpty = useComputed(() => { const resolvedAction = typeof action === "function" ? action() : action; return !hasVisibleActions( resolvedAction.children as ActionVariant[], actionContext ); }, [action, actionContext]); const handleOpenChange = React.useCallback( (open: boolean) => { setOpen(open); if (open) { resetFilter(); onOpen?.(); } else { onClose?.(); } }, [onOpen, onClose, resetFilter] ); const enablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "auto"; } }, []); const disablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "none"; } }, []); if (isEmpty && !append) { return null; } if (isMobile) { return ( ); } const content = toMenuItems(menuItems, query); return ( {children} {content ?? (query ? : null)} {query ? null : append} ); }); type MobileDropdownProps = { open: boolean; onOpenChange: (open: boolean) => void; items: MenuItem[]; trigger: React.ReactNode; } & Pick; function MobileDropdown({ open, onOpenChange, items, trigger, ariaLabel, append, }: MobileDropdownProps) { const [submenuName, setSubmenuName] = React.useState(); const contentRef = React.useRef>(null); const enablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "auto"; } }, []); const disablePointerEvents = React.useCallback(() => { if (contentRef.current) { contentRef.current.style.pointerEvents = "none"; } }, []); const closeDrawer = React.useCallback(() => { onOpenChange(false); setTimeout(() => setSubmenuName(undefined), 500); // needed for a Vaul bug where 'onAnimationEnd' is not called for controlled state. }, [onOpenChange]); const resetSubmenu = React.useCallback((isOpen: boolean) => { if (!isOpen) { setSubmenuName(undefined); } }, []); const menuItems = React.useMemo(() => { if (!items.length || !submenuName) { return items; } const submenu = items.find( (item) => item.type === "submenu" && (item.title as string) === submenuName )! as MenuItemWithChildren; return submenu.items; }, [items, submenuName]); const content = toMobileMenuItems(menuItems, closeDrawer, setSubmenuName); return ( {trigger} {content} {!submenuName ? append : null} ); } const StyledScrollable = styled(Scrollable)` max-height: 75vh; `;