From 21c96d950151fae898b88417568bb5a861c28df0 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 23 Feb 2026 15:59:40 -0700 Subject: [PATCH] Displaying cmd+k/ctrl+k global search keyboard shortcut suggestion. --- app/src/components/HelpModal.js | 26 +++++++++++-- app/src/components/Search.js | 2 + app/src/components/ShortcutHint.js | 58 ++++++++++++++++++++++++++++ app/src/hooks/customHooks.js | 61 +++++++++++++++++++++++++++++- 4 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 app/src/components/ShortcutHint.js diff --git a/app/src/components/HelpModal.js b/app/src/components/HelpModal.js index 731dc17c..e48f1803 100644 --- a/app/src/components/HelpModal.js +++ b/app/src/components/HelpModal.js @@ -3,6 +3,7 @@ import {Table, TableBody, TableCell, TableHeader, TableHeaderCell, TableRow} fro import {Button, Header, Modal} from "./Theme"; import {ThemeContext} from "../contexts/contexts"; import {SHORTCUTS} from "./KeyboardShortcutsProvider"; +import {usePlatformModifier} from "../hooks/customHooks"; // Format keyboard keys for display function KeyboardKey({children}) { @@ -27,9 +28,27 @@ function KeyboardKey({children}) { } // Parse shortcut keys string and render as keyboard keys -function ShortcutKeys({keys}) { +function ShortcutKeys({keys, isMac}) { // Handle multiple key combinations (e.g., "meta+k, ctrl+k") - const combinations = keys.split(', '); + let combinations = keys.split(', '); + + // Filter to show only platform-appropriate shortcuts when there are multiple combinations + if (combinations.length > 1) { + const hasMeta = combinations.some(c => c.toLowerCase().includes('meta')); + const hasCtrl = combinations.some(c => c.toLowerCase().includes('ctrl')); + + // If we have both meta and ctrl variants, show only the platform-appropriate one + if (hasMeta && hasCtrl) { + combinations = combinations.filter(c => { + const lowerCombo = c.toLowerCase(); + if (isMac) { + return lowerCombo.includes('meta'); + } else { + return lowerCombo.includes('ctrl'); + } + }); + } + } return ( @@ -98,6 +117,7 @@ function groupShortcutsByCategory(shortcuts) { export default function HelpModal({open, onClose}) { const {i, t} = useContext(ThemeContext); + const {isMac} = usePlatformModifier(); const groupedShortcuts = groupShortcutsByCategory(SHORTCUTS); // Order categories @@ -116,7 +136,7 @@ export default function HelpModal({open, onClose}) { {groupedShortcuts[category].map((shortcut, idx) => ( - + {shortcut.description} diff --git a/app/src/components/Search.js b/app/src/components/Search.js index 14403883..6becb6e0 100644 --- a/app/src/components/Search.js +++ b/app/src/components/Search.js @@ -2,6 +2,7 @@ import React, {useState} from "react"; import {Link, Route, Routes, useNavigate} from "react-router"; import {FilesSearchView} from "./Files"; import {useLatestRequest, usePages, useSearchChannels, useSearchDate, useSearchFilter} from "../hooks/customHooks"; +import {ShortcutHint} from "./ShortcutHint"; import {ZimSearchView} from "./Zim"; import {searchEstimateFiles, searchEstimateOthers, searchEstimateZims, searchSuggestions} from "../api"; import {filterToMimetypes, fuzzyMatch, normalizeEstimate, SearchResultsInput, TabLinks} from "./Common"; @@ -408,6 +409,7 @@ export function SearchIconButton() { return ( + ); } diff --git a/app/src/components/ShortcutHint.js b/app/src/components/ShortcutHint.js new file mode 100644 index 00000000..53a7b531 --- /dev/null +++ b/app/src/components/ShortcutHint.js @@ -0,0 +1,58 @@ +import React from "react"; +import {useKeyboardDetected, usePlatformModifier} from "../hooks/customHooks"; + +/** + * Displays a keyboard shortcut hint that adapts to the user's platform. + * Shows the correct modifier key (Cmd on macOS, Ctrl on Windows/Linux). + * Hidden on touch devices until a hardware keyboard is detected via key press. + * + * @param {string} shortcutKey - The key part of the shortcut (e.g., "K", "P", "/") + * @param {object} style - Optional additional styles to apply + */ +export function ShortcutHint({shortcutKey, style = {}}) { + const {modifierKey} = usePlatformModifier(); + const keyboardDetected = useKeyboardDetected(); + + if (!keyboardDetected) { + return null; + } + + const defaultStyle = { + fontSize: '0.75em', + opacity: 0.7, + marginLeft: '0.3em', + }; + + return ( + + {modifierKey}{shortcutKey} + + ); +} + +/** + * Displays a simple key hint without a modifier (e.g., for "/" or "?" shortcuts). + * Hidden on touch devices until a hardware keyboard is detected via key press. + * + * @param {string} keyLabel - The key to display (e.g., "?", "/") + * @param {object} style - Optional additional styles to apply + */ +export function KeyHint({keyLabel, style = {}}) { + const keyboardDetected = useKeyboardDetected(); + + if (!keyboardDetected) { + return null; + } + + const defaultStyle = { + fontSize: '0.75em', + opacity: 0.7, + marginLeft: '0.3em', + }; + + return ( + + {keyLabel} + + ); +} diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 95048dc5..9389a7ea 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -52,11 +52,16 @@ import {useForm} from "./useForm"; export const useIsTouchDevice = () => { // Detects if the device has a coarse pointer (touch screen). // Used to disable drag selection on mobile devices. - const [isTouchDevice, setIsTouchDevice] = useState(false); + // Initialize synchronously to avoid race conditions with dependent hooks. + const [isTouchDevice, setIsTouchDevice] = useState(() => { + if (typeof window !== 'undefined') { + return window.matchMedia('(pointer: coarse)').matches; + } + return false; + }); useEffect(() => { const touchQuery = window.matchMedia('(pointer: coarse)'); - setIsTouchDevice(touchQuery.matches); const handleChange = (e) => setIsTouchDevice(e.matches); touchQuery.addEventListener('change', handleChange); @@ -67,6 +72,58 @@ export const useIsTouchDevice = () => { return isTouchDevice; }; +export const usePlatformModifier = () => { + // Detects if the user is on macOS to display the correct keyboard shortcut modifier. + const [isMac, setIsMac] = useState(false); + + useEffect(() => { + // Modern API (Chrome 90+) + if (navigator.userAgentData?.platform) { + setIsMac(navigator.userAgentData.platform === 'macOS'); + } else { + // Fallback for older browsers + setIsMac(/Mac|iPhone|iPad|iPod/.test(navigator.platform)); + } + }, []); + + return { + isMac, + modifierKey: isMac ? '\u2318' : 'Ctrl', + modifierKeyLong: isMac ? 'Cmd' : 'Ctrl', + }; +}; + +export const useKeyboardDetected = () => { + // Detects if a hardware keyboard is present by listening for key presses. + // On desktop devices, returns true immediately. + // On touch devices (iPad with Bluetooth keyboard), returns true after the first key press + // outside of input fields. This allows showing keyboard shortcuts to iPad users with + // external keyboards while hiding them for pure touch users. + const [keyboardDetected, setKeyboardDetected] = useState(false); + const isTouchDevice = useIsTouchDevice(); + + useEffect(() => { + // Desktop devices always have a keyboard. + if (!isTouchDevice) { + setKeyboardDetected(true); + return; + } + + const handleKeyDown = (e) => { + // A key press outside of input fields indicates a hardware keyboard is connected. + if (!e.target.matches('input, textarea, [contenteditable]')) { + setKeyboardDetected(true); + document.removeEventListener('keydown', handleKeyDown); + } + }; + + document.addEventListener('keydown', handleKeyDown); + return () => document.removeEventListener('keydown', handleKeyDown); + }, [isTouchDevice]); + + return keyboardDetected; +}; + const calculatePage = (offset, limit) => { return offset && limit ? Math.round((offset / limit) + 1) : 1; }