|
1 | 1 | <script setup lang="ts"> |
2 | 2 | import { debounce } from 'perfect-debounce' |
3 | 3 |
|
4 | | -const router = useRouter() |
5 | 4 | const searchQuery = shallowRef('') |
6 | 5 | const searchInputRef = useTemplateRef('searchInputRef') |
7 | 6 | const { focused: isSearchFocused } = useFocus(searchInputRef) |
8 | 7 |
|
9 | 8 | const isMobile = useIsMobile() |
10 | 9 |
|
11 | | -const debouncedNavigate = debounce(() => { |
12 | | - router.push({ |
| 10 | +async function search() { |
| 11 | + const query = searchQuery.value.trim() |
| 12 | + await navigateTo({ |
13 | 13 | path: '/search', |
14 | | - query: searchQuery.value.trim() ? { q: searchQuery.value.trim() } : undefined, |
| 14 | + query: query ? { q: query } : undefined, |
15 | 15 | }) |
16 | | -}, 250) |
17 | | -
|
18 | | -function handleSearch() { |
19 | | - // If input is empty, navigate immediately (no need to debounce) |
20 | | - return searchQuery.value.trim() ? debouncedNavigate() : router.push('/search') |
21 | 16 | } |
22 | 17 |
|
| 18 | +const handleInput = isTouchDevice() |
| 19 | + ? search |
| 20 | + : debounce(search, 250, { leading: true, trailing: true }) |
| 21 | +
|
23 | 22 | useSeoMeta({ |
24 | 23 | title: () => $t('seo.home.title'), |
25 | 24 | description: () => $t('seo.home.description'), |
@@ -64,7 +63,7 @@ defineOgImageComponent('Default', { |
64 | 63 | class="w-full max-w-xl motion-safe:animate-slide-up motion-safe:animate-fill-both" |
65 | 64 | style="animation-delay: 0.2s" |
66 | 65 | > |
67 | | - <form method="GET" action="/search" class="relative" @submit.prevent="handleSearch"> |
| 66 | + <form method="GET" action="/search" class="relative" @submit.prevent.trim="search"> |
68 | 67 | <label for="home-search" class="sr-only"> |
69 | 68 | {{ $t('search.label') }} |
70 | 69 | </label> |
@@ -93,7 +92,7 @@ defineOgImageComponent('Default', { |
93 | 92 | v-bind="noCorrect" |
94 | 93 | :autofocus="!isMobile" |
95 | 94 | class="w-full bg-bg-subtle border border-border rounded-lg ps-8 pe-24 py-4 font-mono text-base text-fg placeholder:text-fg-subtle transition-border-color duration-300 focus:border-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/50" |
96 | | - @input="handleSearch" |
| 95 | + @input="handleInput" |
97 | 96 | /> |
98 | 97 |
|
99 | 98 | <button |
|
0 commit comments