From a9497f78c5ac1735f0af7faeb75505fadf000517 Mon Sep 17 00:00:00 2001 From: kennyyennk <92975553+kennyyennk@users.noreply.github.com> Date: Thu, 18 Jun 2026 02:05:54 +0800 Subject: [PATCH] fix reading find refocus --- src/app.tsx | 3 +++ src/components/editor/reading-find.tsx | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/app.tsx b/src/app.tsx index a8ed3c2..0beea02 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -363,6 +363,7 @@ export function App() { // ⌘F only bound while reading — CM owns it in editor mode. const [findOpen, setFindOpen] = useState(false); + const [findFocusRequest, setFindFocusRequest] = useState(0); const [proseEl, setProseEl] = useState(null); useEffect(() => { if (!readingMode) { @@ -708,6 +709,7 @@ export function App() { "mod+f": (e: KeyboardEvent) => { e.preventDefault(); setFindOpen(true); + setFindFocusRequest((v) => v + 1); }, } : {}), @@ -857,6 +859,7 @@ export function App() { setFindOpen(false)} scope={proseEl} contentKey={debouncedPreview} diff --git a/src/components/editor/reading-find.tsx b/src/components/editor/reading-find.tsx index c18b820..3d97671 100644 --- a/src/components/editor/reading-find.tsx +++ b/src/components/editor/reading-find.tsx @@ -3,6 +3,8 @@ import { useCallback, useEffect, useRef, useState } from "react"; type Props = { /** when true, the find bar is mounted + visible */ open: boolean; + /** increments when the parent wants the existing input focused again */ + focusRequest?: number; /** parent calls this to close (Esc, × button, or external trigger) */ onClose: () => void; /** the rendered `
` to search through */ @@ -23,7 +25,7 @@ type Props = { * Cleanup is guaranteed on unmount + close: marks are unwrapped back to plain * text so the next render of the prose stays clean. */ -export function ReadingFind({ open, onClose, scope, contentKey }: Props) { +export function ReadingFind({ open, focusRequest = 0, onClose, scope, contentKey }: Props) { const [query, setQuery] = useState(""); const [matches, setMatches] = useState([]); const [activeIdx, setActiveIdx] = useState(0); @@ -64,13 +66,14 @@ export function ReadingFind({ open, onClose, scope, contentKey }: Props) { }; }, [scope]); - // when the bar opens, focus the input + select any existing query for fast retype + // when the bar opens or ⌘F/Ctrl+F is pressed again, focus the input + select + // any existing query for fast retype. useEffect(() => { if (open) { inputRef.current?.focus(); inputRef.current?.select(); } - }, [open]); + }, [open, focusRequest]); const go = useCallback( (dir: 1 | -1) => {