Skip to content

Commit cc90b75

Browse files
authored
Merge pull request #485 from easyops-cn/steve/sync-ssr
fix: sync ssr the input value and focus state for SSR
2 parents 904577e + be7c125 commit cc90b75

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,26 @@ export default function SearchBar({
387387
)
388388
: false;
389389

390+
// Sync the input value and focus state for SSR
391+
useEffect(
392+
() => {
393+
const searchBar = searchBarRef.current;
394+
const domValue = searchBar?.value;
395+
if (domValue) {
396+
setInputValue(domValue);
397+
}
398+
if (searchBar && document.activeElement === searchBar) {
399+
focusAfterIndexLoaded.current = true;
400+
loadIndex();
401+
setFocused(true);
402+
handleSearchBarToggle?.(true);
403+
}
404+
},
405+
// Only run this effect on mount
406+
// eslint-disable-next-line react-hooks/exhaustive-deps
407+
[]
408+
);
409+
390410
useEffect(() => {
391411
if (!searchBarShortcut) {
392412
return;

0 commit comments

Comments
 (0)