From d76f9abdd718e24848a9b8f67486129aee421427 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Fri, 13 Sep 2024 16:48:28 -0500 Subject: [PATCH] "N" keyboard shortcut to open a new post modal (#5197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Add hook on web app to open composer with 'N' keyboard shortcut * Extract, don't fire open composer if already open * Ignore interactive elements --------- Co-authored-by: João Gabriel Co-authored-by: Hailey --- src/App.web.tsx | 3 ++ .../{composer.tsx => composer/index.tsx} | 0 .../composer/useComposerKeyboardShortcut.tsx | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+) rename src/state/shell/{composer.tsx => composer/index.tsx} (100%) create mode 100644 src/state/shell/composer/useComposerKeyboardShortcut.tsx diff --git a/src/App.web.tsx b/src/App.web.tsx index bef320826f..6efe7cc022 100644 --- a/src/App.web.tsx +++ b/src/App.web.tsx @@ -35,6 +35,7 @@ import { } from '#/state/session' import {readLastActiveAccount} from '#/state/session/util' import {Provider as ShellStateProvider} from '#/state/shell' +import {useComposerKeyboardShortcut} from '#/state/shell/composer/useComposerKeyboardShortcut' import {Provider as LoggedOutViewProvider} from '#/state/shell/logged-out' import {Provider as ProgressGuideProvider} from '#/state/shell/progress-guide' import {Provider as SelectedFeedProvider} from '#/state/shell/selected-feed' @@ -62,6 +63,8 @@ function InnerApp() { useIntentHandler() const hasCheckedReferrer = useStarterPackEntry() + useComposerKeyboardShortcut() + // init useEffect(() => { async function onLaunch(account?: SessionAccount) { diff --git a/src/state/shell/composer.tsx b/src/state/shell/composer/index.tsx similarity index 100% rename from src/state/shell/composer.tsx rename to src/state/shell/composer/index.tsx diff --git a/src/state/shell/composer/useComposerKeyboardShortcut.tsx b/src/state/shell/composer/useComposerKeyboardShortcut.tsx new file mode 100644 index 0000000000..f460621858 --- /dev/null +++ b/src/state/shell/composer/useComposerKeyboardShortcut.tsx @@ -0,0 +1,49 @@ +import React from 'react' + +import {useComposerControls} from './' + +/** + * Based on {@link https://github.com/jaywcjlove/hotkeys-js/blob/b0038773f3b902574f22af747f3bb003a850f1da/src/index.js#L51C1-L64C2} + */ +function shouldIgnore(event: KeyboardEvent) { + const target: any = event.target || event.srcElement + if (!target) return false + const {tagName} = target + if (!tagName) return false + const isInput = + tagName === 'INPUT' && + ![ + 'checkbox', + 'radio', + 'range', + 'button', + 'file', + 'reset', + 'submit', + 'color', + ].includes(target.type) + // ignore: isContentEditable === 'true', and