Focus markdown preview search input from ref (#3322)

This commit is contained in:
Neil
2026-05-30 18:11:12 -07:00
committed by GitHub
parent 9ac390233d
commit 00a56a967d
@@ -434,6 +434,16 @@ export default function MarkdownPreview({
const rootRef = useRef<HTMLDivElement>(null)
const bodyRef = useRef<HTMLDivElement>(null)
const inputRef = useRef<HTMLInputElement>(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<HTMLElement[]>([])
const lastAppliedInitialAnchorRef = useRef<string | null>(null)
const pendingEditorRevealFrameIdsRef = useRef<number[]>([])
@@ -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({
<div className="markdown-preview-search" onKeyDown={(event) => event.stopPropagation()}>
<div className="markdown-preview-search-field">
<Input
ref={inputRef}
ref={setSearchInputElement}
value={query}
onChange={(event) => setQuery(event.target.value)}
onKeyDown={(event) => {