Skip to content

Commit 700550f

Browse files
authored
Merge pull request #198 from c4dt/195
focus the search bar not only on first load of main page, but also when navigating to it from other subpages
2 parents 7348408 + 3b46905 commit 700550f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

components/NavBar.vue

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ const searchQuery = useSearchQuery();
1010
1111
const searchInput = ref<HTMLInputElement | null>(null);
1212
13-
onMounted(() => {
14-
searchInput.value?.focus(); // Focus the search input when the component is mounted
15-
});
13+
watch(
14+
() => route.path,
15+
(newPath) => {
16+
if (newPath === "/") {
17+
// focus the search input field when navigating to the main page
18+
nextTick(() => {
19+
searchInput.value?.focus();
20+
});
21+
}
22+
},
23+
{ immediate: true }
24+
);
1625
1726
watch(searchQuery, () => {
1827
// if the search query is empty and the search query in the URL is empty,

0 commit comments

Comments
 (0)