Skip to content

Commit 45c2ab6

Browse files
committed
fix: don't autofocus search on mobile
1 parent 42be73a commit 45c2ab6

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

app/components/SearchBox.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
22
import { debounce } from 'perfect-debounce'
33
4+
const isMobile = useIsMobile()
5+
46
withDefaults(
57
defineProps<{
68
inputClass?: string
@@ -84,7 +86,7 @@ function handleSearchFocus() {
8486

8587
<input
8688
id="header-search"
87-
autofocus
89+
:autofocus="!isMobile"
8890
v-model="searchQuery"
8991
type="search"
9092
name="q"

app/composables/useIsMobile.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Composable for detecting mobile devices using media query.
3+
* Uses the same breakpoint as Tailwind's `md:` (768px).
4+
*
5+
* Returns `false` during SSR to avoid hydration mismatches.
6+
* @public
7+
*/
8+
export function useIsMobile() {
9+
return useMediaQuery('(max-width: 767px)')
10+
}

app/pages/index.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const searchQuery = ref('')
66
const searchInputRef = useTemplateRef('searchInputRef')
77
const { focused: isSearchFocused } = useFocus(searchInputRef)
88
9+
const isMobile = useIsMobile()
10+
911
const debouncedNavigate = debounce(() => {
1012
router.push({
1113
path: '/search',
@@ -76,7 +78,7 @@ defineOgImageComponent('Default')
7678
name="q"
7779
:placeholder="$t('search.placeholder')"
7880
v-bind="noCorrect"
79-
autofocus
81+
:autofocus="!isMobile"
8082
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"
8183
@input="handleSearch"
8284
/>

0 commit comments

Comments
 (0)