From 6ec259a0e71174651bae95d4628138bf6fd68742 Mon Sep 17 00:00:00 2001 From: kj_sh604 Date: Sun, 15 Mar 2026 16:19:35 -0400 Subject: refactor: packages/ --- packages/excalidraw/components/ContextMenu.tsx | 128 +++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 packages/excalidraw/components/ContextMenu.tsx (limited to 'packages/excalidraw/components/ContextMenu.tsx') diff --git a/packages/excalidraw/components/ContextMenu.tsx b/packages/excalidraw/components/ContextMenu.tsx new file mode 100644 index 0000000..517e5fa --- /dev/null +++ b/packages/excalidraw/components/ContextMenu.tsx @@ -0,0 +1,128 @@ +import clsx from "clsx"; +import { Popover } from "./Popover"; +import type { TranslationKeys } from "../i18n"; +import { t } from "../i18n"; + +import "./ContextMenu.scss"; +import type { ShortcutName } from "../actions/shortcuts"; +import { getShortcutFromShortcutName } from "../actions/shortcuts"; +import type { Action } from "../actions/types"; +import type { ActionManager } from "../actions/manager"; +import { useExcalidrawAppState, useExcalidrawElements } from "./App"; +import React from "react"; + +export type ContextMenuItem = typeof CONTEXT_MENU_SEPARATOR | Action; + +export type ContextMenuItems = (ContextMenuItem | false | null | undefined)[]; + +type ContextMenuProps = { + actionManager: ActionManager; + items: ContextMenuItems; + top: number; + left: number; + onClose: (callback?: () => void) => void; +}; + +export const CONTEXT_MENU_SEPARATOR = "separator"; + +export const ContextMenu = React.memo( + ({ actionManager, items, top, left, onClose }: ContextMenuProps) => { + const appState = useExcalidrawAppState(); + const elements = useExcalidrawElements(); + + const filteredItems = items.reduce((acc: ContextMenuItem[], item) => { + if ( + item && + (item === CONTEXT_MENU_SEPARATOR || + !item.predicate || + item.predicate( + elements, + appState, + actionManager.app.props, + actionManager.app, + )) + ) { + acc.push(item); + } + return acc; + }, []); + + return ( + { + onClose(); + }} + top={top} + left={left} + fitInViewport={true} + offsetLeft={appState.offsetLeft} + offsetTop={appState.offsetTop} + viewportWidth={appState.width} + viewportHeight={appState.height} + > + + + ); + }, +); -- cgit v1.2.3