Skip to content

Commit e13e606

Browse files
committed
fix: focus only on non-touch devices
1 parent ed73a8b commit e13e606

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

app/components/SearchBox.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<script setup lang="ts">
22
import { debounce } from 'perfect-debounce'
33
4-
const isMobile = useIsMobile()
5-
64
withDefaults(
75
defineProps<{
86
inputClass?: string
@@ -60,6 +58,15 @@ watch(
6058
},
6159
)
6260
61+
const autofocus = import.meta.client && !isTouchDevice()
62+
const headerSearchRef = useTemplateRef('headerSearchRef')
63+
onMounted(() => {
64+
// Autofocus the search input on page load for non-touch devicese
65+
if (!isTouchDevice()) {
66+
headerSearchRef.value?.focus()
67+
}
68+
})
69+
6370
function handleSearchBlur() {
6471
isSearchFocused.value = false
6572
emit('blur')
@@ -86,9 +93,10 @@ function handleSearchFocus() {
8693

8794
<input
8895
id="header-search"
89-
:autofocus="!isMobile"
96+
ref="headerSearchRef"
9097
v-model="searchQuery"
9198
type="search"
99+
:autofocus="autofocus"
92100
name="q"
93101
:placeholder="$t('search.placeholder')"
94102
v-bind="noCorrect"

app/composables/useIsMobile.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

app/pages/index.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ const searchQuery = ref('')
66
const searchInputRef = useTemplateRef('searchInputRef')
77
const { focused: isSearchFocused } = useFocus(searchInputRef)
88
9-
const isMobile = useIsMobile()
9+
const autofocus = import.meta.client && !isTouchDevice()
10+
onMounted(() => {
11+
// Autofocus the search input on page load for non-touch devices
12+
if (!isTouchDevice()) {
13+
searchInputRef.value?.focus()
14+
}
15+
})
1016
1117
const debouncedNavigate = debounce(() => {
1218
router.push({
@@ -78,7 +84,7 @@ defineOgImageComponent('Default')
7884
name="q"
7985
:placeholder="$t('search.placeholder')"
8086
v-bind="noCorrect"
81-
:autofocus="!isMobile"
87+
:autofocus="autofocus"
8288
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"
8389
@input="handleSearch"
8490
/>

app/utils/responsive.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @public */
2+
export function isTouchDevice() {
3+
if (import.meta.server) {
4+
return false
5+
}
6+
return (
7+
'ontouchstart' in window ||
8+
navigator.maxTouchPoints > 0 ||
9+
// @ts-ignore
10+
navigator.msMaxTouchPoints > 0
11+
)
12+
}

0 commit comments

Comments
 (0)