From 00a56a967d3e9cd2bca8078311d33863c8fcb699 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 18:11:12 -0700 Subject: [PATCH] Focus markdown preview search input from ref (#3322) --- .../src/components/editor/MarkdownPreview.tsx | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/components/editor/MarkdownPreview.tsx b/src/renderer/src/components/editor/MarkdownPreview.tsx index 33a8248543a..12f584b3a00 100644 --- a/src/renderer/src/components/editor/MarkdownPreview.tsx +++ b/src/renderer/src/components/editor/MarkdownPreview.tsx @@ -434,6 +434,16 @@ export default function MarkdownPreview({ const rootRef = useRef(null) const bodyRef = useRef(null) const inputRef = useRef(null) + const setSearchInputElement = useCallback((input: HTMLInputElement | null) => { + inputRef.current = input + if (!input) { + return + } + // Why: opening preview search should select the query once, while typing + // and match-count updates must not keep re-selecting the field. + input.focus() + input.select() + }, []) const matchesRef = useRef([]) const lastAppliedInitialAnchorRef = useRef(null) const pendingEditorRevealFrameIdsRef = useRef([]) @@ -747,13 +757,6 @@ export default function MarkdownPreview({ [scrollToAnchor] ) - useEffect(() => { - if (isSearchOpen) { - inputRef.current?.focus() - inputRef.current?.select() - } - }, [isSearchOpen]) - useEffect(() => { const body = bodyRef.current if (!body) { @@ -1565,7 +1568,7 @@ export default function MarkdownPreview({
event.stopPropagation()}>
setQuery(event.target.value)} onKeyDown={(event) => {