Files
outline/app/components/Menu/ContextMenu.tsx
T
ZhuoYang Wu(阿离)andTom Moor 19da56c2c7 Improve sidebar menu and navigation tree performance (#12873)
* Improve sidebar menu and navigation tree performance

* remote some test and rollback useOnScreen

* feat: introduce ActionFactory type and update context menu components to support it

- Added ActionFactory type to allow functions returning ActionWithChildren.
- Updated ContextMenu and DropdownMenu components to handle ActionFactory.
- Modified DocumentRow, SidebarLink, and other components to accept ActionFactory for context actions.
- Refactored useDocumentMenuAction to utilize createRootMenuAction for better action management.

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
2026-07-18 12:01:35 -04:00

94 lines
2.8 KiB
TypeScript

import * as React from "react";
import { actionToMenuItem } from "~/actions";
import useActionContext from "~/hooks/useActionContext";
import useMobile from "~/hooks/useMobile";
import type { ActionFactory, ActionVariant, ActionWithChildren } from "~/types";
import { preventDefault } from "~/utils/events";
import { toMenuItems } from "./transformer";
import { observer } from "mobx-react";
import { useComputed } from "~/hooks/useComputed";
import { Menu, MenuContent, MenuTrigger } from "~/components/primitives/Menu";
import { MenuProvider } from "~/components/primitives/Menu/MenuContext";
type Props = {
/** Root action with children representing the menu items */
action?: ActionWithChildren | ActionFactory;
/** Trigger for the menu */
children: React.ReactNode;
/** ARIA label for the menu */
ariaLabel: string;
/** Callback when menu is opened */
onOpen?: () => void;
/** Callback when menu is closed */
onClose?: () => void;
};
export const ContextMenu = observer(
({ action, children, ariaLabel, onOpen, onClose }: Props) => {
const [open, setOpen] = React.useState(false);
const isMobile = useMobile();
const contentRef = React.useRef<React.ElementRef<typeof MenuContent>>(null);
const actionContext = useActionContext({
isMenu: true,
});
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]);
const handleOpenChange = React.useCallback(
(open: boolean) => {
setOpen(open);
if (open) {
onOpen?.();
} else {
onClose?.();
}
},
[onOpen, onClose]
);
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 (isMobile || !action) {
return <>{children}</>;
}
const content = open ? toMenuItems(menuItems) : null;
return (
<MenuProvider variant="context">
<Menu open={open} onOpenChange={handleOpenChange}>
<MenuTrigger aria-label={ariaLabel}>{children}</MenuTrigger>
<MenuContent
aria-label={ariaLabel}
onAnimationStart={disablePointerEvents}
onAnimationEnd={enablePointerEvents}
onCloseAutoFocus={preventDefault}
>
{content}
</MenuContent>
</Menu>
</MenuProvider>
);
}
);