{
  "version": 3,
  "sources": ["../../../../../src/lib/ui/components/ContextMenu/DefaultContextMenu.tsx"],
  "sourcesContent": ["import {\n\tpreventDefault,\n\tuseContainer,\n\tuseEditor,\n\tuseEditorComponents,\n\tuseValue,\n} from '@tldraw/editor'\nimport { ContextMenu as _ContextMenu } from 'radix-ui'\nimport { ReactNode, memo, useCallback, useEffect, useRef } from 'react'\nimport { useMenuIsOpen } from '../../hooks/useMenuIsOpen'\nimport { useDirection, useTranslation } from '../../hooks/useTranslation/useTranslation'\nimport { TldrawUiMenuContextProvider } from '../primitives/menus/TldrawUiMenuContext'\nimport { DefaultContextMenuContent } from './DefaultContextMenuContent'\n\n/** @public */\nexport interface TLUiContextMenuProps {\n\tchildren?: ReactNode\n\tdisabled?: boolean\n}\n\n/** @public @react */\nexport const DefaultContextMenu = memo(function DefaultContextMenu({\n\tchildren,\n\tdisabled = false,\n}: TLUiContextMenuProps) {\n\tconst editor = useEditor()\n\tconst msg = useTranslation()\n\n\tconst { Canvas } = useEditorComponents()\n\n\t// The context menu opens from a right-click in any tool, and from a touch\n\t// long-press only in the select tool (where it acts on the selection). A\n\t// right-click (fine pointer) is routed through the select tool by the editor; a\n\t// long-press in any other tool belongs to that tool's gesture (creating, erasing,\n\t// drawing, panning), so it opens nothing.\n\t//\n\t// The right-click case keys off the pointer type, not the tool, on purpose: a\n\t// right-click switches to select synchronously within the same gesture, so gating\n\t// it on the tool would race the React render and wrongly suppress the menu. The\n\t// select-tool long-press case is safe to gate on the tool \u2014 a long-press never\n\t// switches tools, so there is nothing to race.\n\tconst menuCanOpen = useValue(\n\t\t'context menu can open',\n\t\t() => !editor.getInstanceState().isCoarsePointer || editor.isIn('select'),\n\t\t[editor]\n\t)\n\n\t// When hitting `Escape` while the context menu is open, we want to prevent\n\t// the default behavior of losing focus on the shape. Otherwise,\n\t// it's pretty annoying from an accessibility perspective.\n\tconst preventEscapeFromLosingShapeFocus = useCallback(\n\t\t(e: KeyboardEvent) => {\n\t\t\tif (e.key === 'Escape') {\n\t\t\t\te.stopPropagation()\n\t\t\t\teditor.getContainer().focus()\n\t\t\t}\n\t\t},\n\t\t[editor]\n\t)\n\n\tuseEffect(() => {\n\t\tconst body = editor.getContainerDocument().body\n\t\treturn () => {\n\t\t\tbody.removeEventListener('keydown', preventEscapeFromLosingShapeFocus, {\n\t\t\t\tcapture: true,\n\t\t\t})\n\t\t}\n\t}, [editor, preventEscapeFromLosingShapeFocus])\n\n\t// On touch devices, the same touch that triggers Radix's long-press open is still\n\t// down when the menu mounts. The release fires events the dismissable layer treats\n\t// as an outside interaction and closes the menu. We swallow dismissals during a\n\t// short grace window after open so the menu stays put until the user actually\n\t// interacts again.\n\tconst suppressDismissUntilRef = useRef(0)\n\n\tconst cb = useCallback(\n\t\t(isOpen: boolean) => {\n\t\t\tconst body = editor.getContainerDocument().body\n\t\t\tif (!isOpen) {\n\t\t\t\tconst onlySelectedShape = editor.getOnlySelectedShape()\n\n\t\t\t\tif (onlySelectedShape && editor.isShapeOrAncestorLocked(onlySelectedShape)) {\n\t\t\t\t\teditor.setSelectedShapes([])\n\t\t\t\t}\n\n\t\t\t\teditor.timers.requestAnimationFrame(() => {\n\t\t\t\t\tbody.removeEventListener('keydown', preventEscapeFromLosingShapeFocus, {\n\t\t\t\t\t\tcapture: true,\n\t\t\t\t\t})\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tbody.addEventListener('keydown', preventEscapeFromLosingShapeFocus, {\n\t\t\t\t\tcapture: true,\n\t\t\t\t})\n\n\t\t\t\tif (editor.getInstanceState().isCoarsePointer) {\n\t\t\t\t\tsuppressDismissUntilRef.current = Date.now() + 500\n\n\t\t\t\t\t// Weird route: selecting locked shapes on long press\n\t\t\t\t\tconst selectedShapes = editor.getSelectedShapes()\n\t\t\t\t\tconst currentPagePoint = editor.inputs.getCurrentPagePoint()\n\n\t\t\t\t\t// get all of the shapes under the current pointer\n\t\t\t\t\tconst shapesAtPoint = editor.getShapesAtPoint(currentPagePoint)\n\n\t\t\t\t\tif (\n\t\t\t\t\t\t// if there are no selected shapes\n\t\t\t\t\t\t!editor.getSelectedShapes().length ||\n\t\t\t\t\t\t// OR if none of the shapes at the point include the selected shape\n\t\t\t\t\t\t!shapesAtPoint.some((s) => selectedShapes.includes(s))\n\t\t\t\t\t) {\n\t\t\t\t\t\t// then are there any locked shapes under the current pointer?\n\t\t\t\t\t\tconst lockedShapes = shapesAtPoint.filter((s) => editor.isShapeOrAncestorLocked(s))\n\n\t\t\t\t\t\tif (lockedShapes.length) {\n\t\t\t\t\t\t\t// nice, let's select them\n\t\t\t\t\t\t\teditor.select(...lockedShapes.map((s) => s.id))\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t\t[editor, preventEscapeFromLosingShapeFocus]\n\t)\n\n\tconst container = useContainer()\n\tconst dir = useDirection()\n\tconst [isOpen, handleOpenChange] = useMenuIsOpen('context menu', cb)\n\n\t// Get the context menu content, either the default component or the user's\n\t// override. If there's no menu content, then the user has set it to null,\n\t// so skip rendering the menu.\n\tconst content = children ?? <DefaultContextMenuContent />\n\n\treturn (\n\t\t<_ContextMenu.Root dir={dir} onOpenChange={handleOpenChange} modal={false}>\n\t\t\t<_ContextMenu.Trigger\n\t\t\t\t// When suppressed, disabling the trigger stops Radix from opening the\n\t\t\t\t// menu, but it also stops Radix from preventing the native contextmenu \u2014\n\t\t\t\t// so prevent the browser's own menu ourselves in that case.\n\t\t\t\tonContextMenu={menuCanOpen ? undefined : preventDefault}\n\t\t\t\tdir=\"ltr\"\n\t\t\t\tdisabled={disabled || !menuCanOpen}\n\t\t\t>\n\t\t\t\t{Canvas ? <Canvas /> : null}\n\t\t\t</_ContextMenu.Trigger>\n\t\t\t{isOpen && (\n\t\t\t\t<_ContextMenu.Portal container={container}>\n\t\t\t\t\t<_ContextMenu.Content\n\t\t\t\t\t\tclassName=\"tlui-menu tlui-scrollable\"\n\t\t\t\t\t\tdata-testid=\"context-menu\"\n\t\t\t\t\t\taria-label={msg('context-menu.title')}\n\t\t\t\t\t\talignOffset={-4}\n\t\t\t\t\t\tcollisionPadding={4}\n\t\t\t\t\t\tonContextMenu={preventDefault}\n\t\t\t\t\t\tonPointerDownOutside={(e) => {\n\t\t\t\t\t\t\tif (Date.now() < suppressDismissUntilRef.current) e.preventDefault()\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tonInteractOutside={(e) => {\n\t\t\t\t\t\t\tif (Date.now() < suppressDismissUntilRef.current) e.preventDefault()\n\t\t\t\t\t\t}}\n\t\t\t\t\t\tonFocusOutside={(e) => {\n\t\t\t\t\t\t\tif (Date.now() < suppressDismissUntilRef.current) e.preventDefault()\n\t\t\t\t\t\t}}\n\t\t\t\t\t>\n\t\t\t\t\t\t<TldrawUiMenuContextProvider type=\"context-menu\" sourceId=\"context-menu\">\n\t\t\t\t\t\t\t{content}\n\t\t\t\t\t\t</TldrawUiMenuContextProvider>\n\t\t\t\t\t</_ContextMenu.Content>\n\t\t\t\t</_ContextMenu.Portal>\n\t\t\t)}\n\t\t</_ContextMenu.Root>\n\t)\n})\n"],
  "mappings": "AAqI6B,cAG3B,YAH2B;AArI7B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,eAAe,oBAAoB;AAC5C,SAAoB,MAAM,aAAa,WAAW,cAAc;AAChE,SAAS,qBAAqB;AAC9B,SAAS,cAAc,sBAAsB;AAC7C,SAAS,mCAAmC;AAC5C,SAAS,iCAAiC;AASnC,MAAM,qBAAqB,KAAK,SAASA,oBAAmB;AAAA,EAClE;AAAA,EACA,WAAW;AACZ,GAAyB;AACxB,QAAM,SAAS,UAAU;AACzB,QAAM,MAAM,eAAe;AAE3B,QAAM,EAAE,OAAO,IAAI,oBAAoB;AAavC,QAAM,cAAc;AAAA,IACnB;AAAA,IACA,MAAM,CAAC,OAAO,iBAAiB,EAAE,mBAAmB,OAAO,KAAK,QAAQ;AAAA,IACxE,CAAC,MAAM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