From 2d91a13647885bc048badccbc2fa5c4d3a289e5f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 22 Jul 2026 21:42:32 -0400 Subject: [PATCH] fix: `useKeyDown` binding blocks browser defaults (#13098) * fix: useKeyDown binding blocks browser defaults * feedback --- app/components/AuthenticatedLayout.tsx | 7 +-- app/components/InputSearchPage.tsx | 22 ++++----- app/components/RegisterKeyDown.ts | 22 ++++++++- app/hooks/useKeyDown.ts | 45 +++++++++++++++-- app/scenes/Document/components/Document.tsx | 53 ++++++++------------- 5 files changed, 95 insertions(+), 54 deletions(-) diff --git a/app/components/AuthenticatedLayout.tsx b/app/components/AuthenticatedLayout.tsx index f0db9eb365..4880d4e3b7 100644 --- a/app/components/AuthenticatedLayout.tsx +++ b/app/components/AuthenticatedLayout.tsx @@ -15,7 +15,6 @@ import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; import Logger from "~/utils/Logger"; import history from "~/utils/history"; -import { isModKey } from "@shared/utils/keyboard"; import lazyWithRetry from "~/utils/lazyWithRetry"; import { searchPath, @@ -45,11 +44,7 @@ const AuthenticatedLayout: React.FC = ({ children }: Props) => { const team = useCurrentTeam(); const [spendPostLoginPath] = usePostLoginPath(); - useKeyDown(".", (event) => { - if (isModKey(event)) { - ui.toggleCollapsedSidebar(); - } - }); + useKeyDown(".", () => ui.toggleCollapsedSidebar(), { metaKey: true }); const goToSearch = (ev: KeyboardEvent) => { if (!ev.metaKey && !ev.ctrlKey) { diff --git a/app/components/InputSearchPage.tsx b/app/components/InputSearchPage.tsx index d73601bb36..6ff67aa8d6 100644 --- a/app/components/InputSearchPage.tsx +++ b/app/components/InputSearchPage.tsx @@ -6,11 +6,7 @@ import { useHistory } from "react-router-dom"; import styled, { useTheme } from "styled-components"; import breakpoint from "styled-components-breakpoint"; import { s } from "@shared/styles"; -import { - isModKey, - metaDisplay, - shortcutSeparator, -} from "@shared/utils/keyboard"; +import { metaDisplay, shortcutSeparator } from "@shared/utils/keyboard"; import useBoolean from "~/hooks/useBoolean"; import useKeyDown from "~/hooks/useKeyDown"; import useMobile from "~/hooks/useMobile"; @@ -52,12 +48,16 @@ function InputSearchPage({ const isMobile = useMobile(); const [isFocused, setFocused, setUnfocused] = useBoolean(false); - useKeyDown("f", (ev: KeyboardEvent) => { - if (isModKey(ev) && document.activeElement !== inputRef.current) { - ev.preventDefault(); - inputRef.current?.focus(); - } - }); + useKeyDown( + "f", + (ev: KeyboardEvent) => { + if (document.activeElement !== inputRef.current) { + ev.preventDefault(); + inputRef.current?.focus(); + } + }, + { metaKey: true } + ); const handleKeyDown = React.useCallback( (ev: React.KeyboardEvent) => { diff --git a/app/components/RegisterKeyDown.ts b/app/components/RegisterKeyDown.ts index 0d272b9350..85eae2bb8e 100644 --- a/app/components/RegisterKeyDown.ts +++ b/app/components/RegisterKeyDown.ts @@ -4,6 +4,12 @@ import useKeyDown from "~/hooks/useKeyDown"; type Props = { trigger: KeyFilter; handler: (event: KeyboardEvent) => void; + /** Require the platform modifier key (Cmd on macOS, Ctrl elsewhere). */ + metaKey?: boolean; + /** Require the Alt/Option key. */ + altKey?: boolean; + /** Require the Shift key. */ + shiftKey?: boolean; options?: Options; }; @@ -12,7 +18,19 @@ type Props = { * class components that have not yet been converted to functions. Do not use * this method in functional components. */ -export default function RegisterKeyDown({ trigger, handler, options }: Props) { - useKeyDown(trigger, handler, options); +export default function RegisterKeyDown({ + trigger, + handler, + metaKey, + altKey, + shiftKey, + options, +}: Props) { + useKeyDown(trigger, handler, { + ...options, + metaKey: metaKey ?? options?.metaKey, + altKey: altKey ?? options?.altKey, + shiftKey: shiftKey ?? options?.shiftKey, + }); return null; } diff --git a/app/hooks/useKeyDown.ts b/app/hooks/useKeyDown.ts index 71c14211da..daa81db888 100644 --- a/app/hooks/useKeyDown.ts +++ b/app/hooks/useKeyDown.ts @@ -10,6 +10,12 @@ export type KeyFilter = ((event: KeyboardEvent) => boolean) | string; export type Options = { allowInInput?: boolean; + /** Require the platform modifier key (Cmd on macOS, Ctrl elsewhere) to be held. */ + metaKey?: boolean; + /** Require the Alt/Option key to be held. */ + altKey?: boolean; + /** Require the Shift key to be held. */ + shiftKey?: boolean; }; type RegisteredCallback = { @@ -24,24 +30,57 @@ let callbacks: RegisteredCallback[] = []; // in this case, they should never be triggered from mobile keyboards. let imeOpen = false; +// Returns true when the event's modifier keys satisfy the given options. +const matchesModifiers = (event: KeyboardEvent, options?: Options) => { + if (options?.metaKey ? !isModKey(event) : isModKey(event)) { + return false; + } + if (options?.altKey && !event.altKey) { + return false; + } + if (options?.shiftKey && !event.shiftKey) { + return false; + } + return true; +}; + // Based on implementation in react-use // https://github.com/streamich/react-use/blob/master/src/useKey.ts#L15-L22 -const createKeyPredicate = (keyFilter: KeyFilter) => +// A string filter matches the bare key with the modifiers described by options; +// use a function filter for anything the options cannot express. +const createKeyPredicate = (keyFilter: KeyFilter, options?: Options) => typeof keyFilter === "function" ? keyFilter : typeof keyFilter === "string" - ? (event: KeyboardEvent) => event.key === keyFilter + ? (event: KeyboardEvent) => { + // Match case-insensitively only when Shift is required, since a + // shifted letter reports an uppercase event.key (e.g. Cmd+Shift+P). + // Otherwise match exactly so a bare shortcut does not fire when Shift + // is held (e.g. "n" must not trigger on Shift+N). + const keyMatches = options?.shiftKey + ? event.key.toLowerCase() === keyFilter.toLowerCase() + : event.key === keyFilter; + return keyMatches && matchesModifiers(event, options); + } : keyFilter ? (_event: KeyboardEvent) => true : (_event: KeyboardEvent) => false; +/** + * Registers a global keyboard shortcut for the lifetime of the component. + * + * @param key The key to match, either a string for a single key or a predicate + * function for full control over the event. + * @param fn The callback to invoke when the shortcut matches. + * @param options Behavioral options, including the required modifier keys. + */ export default function useKeyDown( key: KeyFilter, fn: Callback, options?: Options ): void { const { pane, isSplitView } = useContext(SplitViewContext); - const predicate = createKeyPredicate(key); + const predicate = createKeyPredicate(key, options); useEffect(() => { const handler = (event: KeyboardEvent) => { diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index 3b4ef92212..f12327a388 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -12,7 +12,6 @@ import { s } from "@shared/styles"; import type { NavigationNode } from "@shared/types"; import { IconType, TOCPosition, TeamPreference } from "@shared/types"; import { determineIconType } from "@shared/utils/icon"; -import { isModKey } from "@shared/utils/keyboard"; import type Document from "~/models/Document"; import type Revision from "~/models/Revision"; import DocumentMove from "~/components/DocumentExplorer/DocumentMove"; @@ -151,29 +150,26 @@ function DocumentScene({ const onUndoRedo = useCallback( (event: KeyboardEvent) => { - if (isModKey(event)) { - const target = - event.target instanceof Element ? event.target : undefined; + const target = event.target instanceof Element ? event.target : undefined; - // The editor handles undo/redo through its own keymap when focused - if ( - editorRef.current?.view?.hasFocus() || - (target && (isTextInput(target) || !!target.closest(".ProseMirror"))) - ) { - return; - } + // The editor handles undo/redo through its own keymap when focused + if ( + editorRef.current?.view?.hasFocus() || + (target && (isTextInput(target) || !!target.closest(".ProseMirror"))) + ) { + return; + } - event.preventDefault(); + event.preventDefault(); - if (event.shiftKey) { - if (!readOnly) { - editorRef.current?.commands.redo?.(); - } - } else { - if (!readOnly) { - editorRef.current?.commands.undo?.(); - } - } + if (readOnly) { + return; + } + + if (event.shiftKey) { + editorRef.current?.commands.redo?.(); + } else { + editorRef.current?.commands.undo?.(); } }, [readOnly] @@ -259,15 +255,6 @@ function DocumentScene({ [document, dialogs, t, onSave] ); - const handlePublishShortcut = useCallback( - (event: KeyboardEvent) => { - if (isModKey(event) && event.shiftKey) { - onPublish(event); - } - }, - [onPublish] - ); - const goBack = useCallback(() => { if (!readOnly) { history.push({ @@ -309,16 +296,18 @@ function DocumentScene({ return ( - +