From e8de56410dfd5e9a4ce2e76e80ecac52a348fa25 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:18:56 -0800 Subject: [PATCH] Key presses no longer run one listener per mounted menu (#44076) * perf(menus): one document keydown listener for every Radix menu root Every mounted Radix Menu root, open or closed, added its own capture keydown listener on document, and hidden workspaces keep their tab bars mounted, so a key press ran one callback per tab context menu and create menu in every opened workspace. One reference-counted listener now serves all roots; each root still reads the keyboard-modality value its own listener would have held. * perf(menus): keep a menu root's own writes ordered with shared keyboard events A write to a root's keyboard-modality value now takes its place in the shared event sequence, so a pointer reset armed before the write still clears it, as the per-root listener did. --- .../@radix-ui__react-menu@2.1.24.patch | 190 ++++++++++++++++++ pnpm-lock.yaml | 11 +- pnpm-workspace.yaml | 1 + 3 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 config/patches/@radix-ui__react-menu@2.1.24.patch diff --git a/config/patches/@radix-ui__react-menu@2.1.24.patch b/config/patches/@radix-ui__react-menu@2.1.24.patch new file mode 100644 index 00000000000..e17470e050c --- /dev/null +++ b/config/patches/@radix-ui__react-menu@2.1.24.patch @@ -0,0 +1,190 @@ +diff --git a/dist/index.js b/dist/index.js +index b3f6113cdcdb157f778848ad88bc816ac5bacbf1..e208eada130923cdc280cb860a899dc334857c63 100644 +--- a/dist/index.js ++++ b/dist/index.js +@@ -114,25 +114,79 @@ var usePopperScope = (0, import_react_popper.createPopperScope)(); + var useRovingFocusGroupScope = (0, import_react_roving_focus.createRovingFocusGroupScope)(); + var [MenuProvider, useMenuContext] = createMenuContext(MENU_NAME); + var [MenuRootProvider, useMenuRootContext] = createMenuContext(MENU_NAME); ++// Orca patch: one document keydown listener serves every Menu root instead of one per root, so a key press ++// costs the same however many menus are mounted. Each root still reads the value its own listener would have held. ++var sharedKeyboardModality = { connectedRoots: 0, sequence: 0, lastKeyDown: 0, lastPointer: 0 }; ++function handleSharedModalityPointer() { ++ sharedKeyboardModality.lastPointer = ++sharedKeyboardModality.sequence; ++} ++function handleSharedModalityKeyDown() { ++ sharedKeyboardModality.lastKeyDown = ++sharedKeyboardModality.sequence; ++ document.addEventListener("pointerdown", handleSharedModalityPointer, { capture: true, once: true }); ++ document.addEventListener("pointermove", handleSharedModalityPointer, { capture: true, once: true }); ++} ++function readRootKeyboardModality(root) { ++ // A root sees only events from while its effect is connected (pointer resets only once one of its keydowns armed ++ // them); the latest of those and its own last write wins. ++ if (!root.connected) { ++ return root.retained; ++ } ++ const { lastKeyDown, lastPointer } = sharedKeyboardModality; ++ let value = root.retained; ++ let at = root.retainedAt; ++ if (lastKeyDown > root.connectedAt) { ++ if (lastKeyDown > at) { ++ value = true; ++ at = lastKeyDown; ++ } ++ if (lastPointer > lastKeyDown && lastPointer > at) { ++ value = false; ++ } ++ } ++ return value; ++} ++function createRootKeyboardModality() { ++ return { ++ connected: false, ++ connectedAt: 0, ++ retained: false, ++ retainedAt: 0, ++ get current() { ++ return readRootKeyboardModality(this); ++ }, ++ set current(value) { ++ this.retained = value; ++ this.retainedAt = ++sharedKeyboardModality.sequence; ++ } ++ }; ++} + var Menu = /* @__PURE__ */ __name((props) => { + const { __scopeMenu, open = false, children, dir, onOpenChange, modal = true } = props; + const popperScope = usePopperScope(__scopeMenu); + const [content, setContent] = React.useState(null); +- const isUsingKeyboardRef = React.useRef(false); ++ const keyboardModalityRef = React.useRef(null); ++ if (keyboardModalityRef.current === null) { ++ keyboardModalityRef.current = createRootKeyboardModality(); ++ } ++ const isUsingKeyboardRef = keyboardModalityRef.current; + const handleOpenChange = (0, import_react_use_callback_ref.useCallbackRef)(onOpenChange); + const direction = (0, import_react_direction.useDirection)(dir); + React.useEffect(() => { +- const handleKeyDown = /* @__PURE__ */ __name(() => { +- isUsingKeyboardRef.current = true; +- document.addEventListener("pointerdown", handlePointer, { capture: true, once: true }); +- document.addEventListener("pointermove", handlePointer, { capture: true, once: true }); +- }, "handleKeyDown"); +- const handlePointer = /* @__PURE__ */ __name(() => isUsingKeyboardRef.current = false, "handlePointer"); +- document.addEventListener("keydown", handleKeyDown, { capture: true }); ++ const root = isUsingKeyboardRef; ++ root.connected = true; ++ root.connectedAt = sharedKeyboardModality.sequence; ++ root.retainedAt = root.connectedAt; ++ if (sharedKeyboardModality.connectedRoots++ === 0) { ++ document.addEventListener("keydown", handleSharedModalityKeyDown, { capture: true }); ++ } + return () => { +- document.removeEventListener("keydown", handleKeyDown, { capture: true }); +- document.removeEventListener("pointerdown", handlePointer, { capture: true }); +- document.removeEventListener("pointermove", handlePointer, { capture: true }); ++ root.retained = readRootKeyboardModality(root); ++ root.connected = false; ++ if (--sharedKeyboardModality.connectedRoots === 0) { ++ document.removeEventListener("keydown", handleSharedModalityKeyDown, { capture: true }); ++ document.removeEventListener("pointerdown", handleSharedModalityPointer, { capture: true }); ++ document.removeEventListener("pointermove", handleSharedModalityPointer, { capture: true }); ++ } + }; + }, []); + React.useEffect(() => { +diff --git a/dist/index.mjs b/dist/index.mjs +index cd4303a0124fd5593f3dbd392d1803e994ce2bfb..6eb2406ae4324ec1a84f1beefcdea0f8cd7cf463 100644 +--- a/dist/index.mjs ++++ b/dist/index.mjs +@@ -48,25 +48,79 @@ var usePopperScope = createPopperScope(); + var useRovingFocusGroupScope = createRovingFocusGroupScope(); + var [MenuProvider, useMenuContext] = createMenuContext(MENU_NAME); + var [MenuRootProvider, useMenuRootContext] = createMenuContext(MENU_NAME); ++// Orca patch: one document keydown listener serves every Menu root instead of one per root, so a key press ++// costs the same however many menus are mounted. Each root still reads the value its own listener would have held. ++var sharedKeyboardModality = { connectedRoots: 0, sequence: 0, lastKeyDown: 0, lastPointer: 0 }; ++function handleSharedModalityPointer() { ++ sharedKeyboardModality.lastPointer = ++sharedKeyboardModality.sequence; ++} ++function handleSharedModalityKeyDown() { ++ sharedKeyboardModality.lastKeyDown = ++sharedKeyboardModality.sequence; ++ document.addEventListener("pointerdown", handleSharedModalityPointer, { capture: true, once: true }); ++ document.addEventListener("pointermove", handleSharedModalityPointer, { capture: true, once: true }); ++} ++function readRootKeyboardModality(root) { ++ // A root sees only events from while its effect is connected (pointer resets only once one of its keydowns armed ++ // them); the latest of those and its own last write wins. ++ if (!root.connected) { ++ return root.retained; ++ } ++ const { lastKeyDown, lastPointer } = sharedKeyboardModality; ++ let value = root.retained; ++ let at = root.retainedAt; ++ if (lastKeyDown > root.connectedAt) { ++ if (lastKeyDown > at) { ++ value = true; ++ at = lastKeyDown; ++ } ++ if (lastPointer > lastKeyDown && lastPointer > at) { ++ value = false; ++ } ++ } ++ return value; ++} ++function createRootKeyboardModality() { ++ return { ++ connected: false, ++ connectedAt: 0, ++ retained: false, ++ retainedAt: 0, ++ get current() { ++ return readRootKeyboardModality(this); ++ }, ++ set current(value) { ++ this.retained = value; ++ this.retainedAt = ++sharedKeyboardModality.sequence; ++ } ++ }; ++} + var Menu = /* @__PURE__ */ __name((props) => { + const { __scopeMenu, open = false, children, dir, onOpenChange, modal = true } = props; + const popperScope = usePopperScope(__scopeMenu); + const [content, setContent] = React.useState(null); +- const isUsingKeyboardRef = React.useRef(false); ++ const keyboardModalityRef = React.useRef(null); ++ if (keyboardModalityRef.current === null) { ++ keyboardModalityRef.current = createRootKeyboardModality(); ++ } ++ const isUsingKeyboardRef = keyboardModalityRef.current; + const handleOpenChange = useCallbackRef(onOpenChange); + const direction = useDirection(dir); + React.useEffect(() => { +- const handleKeyDown = /* @__PURE__ */ __name(() => { +- isUsingKeyboardRef.current = true; +- document.addEventListener("pointerdown", handlePointer, { capture: true, once: true }); +- document.addEventListener("pointermove", handlePointer, { capture: true, once: true }); +- }, "handleKeyDown"); +- const handlePointer = /* @__PURE__ */ __name(() => isUsingKeyboardRef.current = false, "handlePointer"); +- document.addEventListener("keydown", handleKeyDown, { capture: true }); ++ const root = isUsingKeyboardRef; ++ root.connected = true; ++ root.connectedAt = sharedKeyboardModality.sequence; ++ root.retainedAt = root.connectedAt; ++ if (sharedKeyboardModality.connectedRoots++ === 0) { ++ document.addEventListener("keydown", handleSharedModalityKeyDown, { capture: true }); ++ } + return () => { +- document.removeEventListener("keydown", handleKeyDown, { capture: true }); +- document.removeEventListener("pointerdown", handlePointer, { capture: true }); +- document.removeEventListener("pointermove", handlePointer, { capture: true }); ++ root.retained = readRootKeyboardModality(root); ++ root.connected = false; ++ if (--sharedKeyboardModality.connectedRoots === 0) { ++ document.removeEventListener("keydown", handleSharedModalityKeyDown, { capture: true }); ++ document.removeEventListener("pointerdown", handleSharedModalityPointer, { capture: true }); ++ document.removeEventListener("pointermove", handleSharedModalityPointer, { capture: true }); ++ } + }; + }, []); + React.useEffect(() => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2903319fd79..d1d2e230b2d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -198,6 +198,7 @@ pnpmfileChecksum: sha256-6l6qdgUHKyTLYIQLx/UGfGRnEiFXxIaSlGMgK4mttEw= patchedDependencies: '@radix-ui/react-collapsible@1.1.20': 0a7d7ed8025eb36c149a62238cb71a4eff052e8f159b60414d5946776e134f62 + '@radix-ui/react-menu@2.1.24': a21d18697be25b83f91656d94ba7a64aaaf9eead32e404dacc23297c3363083b '@tiptap/extension-list@3.31.3': afc553bf4f7cb888dfcdc30d52b7fed589176fd1987736fd72fd1f4f7173f299 '@tiptap/markdown@3.31.3': 4fe6aebb0d868ea9e8977f371bd088e96d5018ea8cf83a7ae3f3f924b87ee7cf '@vscode/windows-process-tree@0.8.0': a09f2e8b2b4e73ef20c317af91f9e06c09abf62fa2c891d99b16f53047f596fd @@ -9819,7 +9820,7 @@ snapshots: dependencies: '@radix-ui/primitive': 1.1.7 '@radix-ui/react-context': 1.2.2(@types/react@19.2.18)(react@19.2.9) - '@radix-ui/react-menu': 2.1.24(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) + '@radix-ui/react-menu': 2.1.24(patch_hash=a21d18697be25b83f91656d94ba7a64aaaf9eead32e404dacc23297c3363083b)(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-primitive': 2.1.10(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-use-controllable-state': 1.2.6(@types/react@19.2.18)(react@19.2.9) react: 19.2.9 @@ -9923,7 +9924,7 @@ snapshots: '@radix-ui/react-compose-refs': 1.1.5(@types/react@19.2.18)(react@19.2.9) '@radix-ui/react-context': 1.2.2(@types/react@19.2.18)(react@19.2.9) '@radix-ui/react-id': 1.1.4(@types/react@19.2.18)(react@19.2.9) - '@radix-ui/react-menu': 2.1.24(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) + '@radix-ui/react-menu': 2.1.24(patch_hash=a21d18697be25b83f91656d94ba7a64aaaf9eead32e404dacc23297c3363083b)(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-primitive': 2.1.10(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-use-controllable-state': 1.2.6(@types/react@19.2.18)(react@19.2.9) react: 19.2.9 @@ -10020,7 +10021,7 @@ snapshots: '@types/react': 19.2.18 '@types/react-dom': 19.2.7(@types/react@19.2.18) - '@radix-ui/react-menu@2.1.24(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9)': + '@radix-ui/react-menu@2.1.24(patch_hash=a21d18697be25b83f91656d94ba7a64aaaf9eead32e404dacc23297c3363083b)(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9)': dependencies: '@radix-ui/primitive': 1.1.7 '@radix-ui/react-collection': 1.1.15(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) @@ -10054,7 +10055,7 @@ snapshots: '@radix-ui/react-context': 1.2.2(@types/react@19.2.18)(react@19.2.9) '@radix-ui/react-direction': 1.1.4(@types/react@19.2.18)(react@19.2.9) '@radix-ui/react-id': 1.1.4(@types/react@19.2.18)(react@19.2.9) - '@radix-ui/react-menu': 2.1.24(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) + '@radix-ui/react-menu': 2.1.24(patch_hash=a21d18697be25b83f91656d94ba7a64aaaf9eead32e404dacc23297c3363083b)(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-primitive': 2.1.10(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-roving-focus': 1.1.19(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-use-controllable-state': 1.2.6(@types/react@19.2.18)(react@19.2.9) @@ -14685,7 +14686,7 @@ snapshots: '@radix-ui/react-form': 0.1.16(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-hover-card': 1.1.23(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-label': 2.1.15(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) - '@radix-ui/react-menu': 2.1.24(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) + '@radix-ui/react-menu': 2.1.24(patch_hash=a21d18697be25b83f91656d94ba7a64aaaf9eead32e404dacc23297c3363083b)(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-menubar': 1.1.24(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-navigation-menu': 1.2.22(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) '@radix-ui/react-one-time-password-field': 0.1.16(@types/react-dom@19.2.7(@types/react@19.2.18))(@types/react@19.2.18)(react-dom@19.2.9(@types/react@19.2.18)(react@19.2.9))(react@19.2.9) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index e31806469f4..1b1d7b1f7b2 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -128,6 +128,7 @@ patchedDependencies: minimatch@10.2.6: config/patches/minimatch@10.2.6.patch picomatch@4.0.4: config/patches/picomatch@4.0.4.patch picomatch@4.0.7: config/patches/picomatch@4.0.7.patch + '@radix-ui/react-menu@2.1.24': config/patches/@radix-ui__react-menu@2.1.24.patch permissions: agent-browser: skills: false