Skip to content

Commit 69c24f1

Browse files
authored
Merge pull request #566 from easyops-cn/copilot/fix-search-input-caret-issue
fix: move search caret to end on focus, small screens only
2 parents 379d32f + d1d6e08 commit 69c24f1

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ export default function SearchBar({
364364
search.current?.autocomplete.open();
365365
}
366366
input.focus();
367+
// On small screens the input is collapsed by default; after expanding via
368+
// focus, move the caret to the end so backspace works as expected.
369+
if (window.matchMedia("(max-width: 576px)").matches) {
370+
input.setSelectionRange(input.value.length, input.value.length);
371+
}
367372
}
368373
}, [hidden, searchContext, versionUrl, baseUrl, history]);
369374

@@ -404,6 +409,17 @@ export default function SearchBar({
404409
loadIndex();
405410
setFocused(true);
406411
handleSearchBarToggle?.(true);
412+
// On small screens the input is collapsed by default; after expanding via
413+
// focus, move the caret to the end so backspace works as expected.
414+
// On large screens we leave the native caret behaviour untouched.
415+
if (window.matchMedia("(max-width: 576px)").matches) {
416+
const input = searchBarRef.current;
417+
if (input) {
418+
setTimeout(() => {
419+
input.setSelectionRange(input.value.length, input.value.length);
420+
}, 0);
421+
}
422+
}
407423
}, [handleSearchBarToggle, loadIndex]);
408424

409425
const onInputBlur = useCallback(() => {

0 commit comments

Comments
 (0)