Skip to content

Commit 8ffde79

Browse files
committed
feat: ctrl+k to focus search
1 parent 44bc4bc commit 8ffde79

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

app/app.vue

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,36 @@ if (import.meta.server) {
4747
setJsonLd(createWebSiteSchema())
4848
}
4949
50+
function focusSearchOrNavigate() {
51+
const searchInput = document.querySelector<HTMLInputElement>(
52+
'input[type="search"], input[name="q"]',
53+
)
54+
55+
if (searchInput) {
56+
searchInput.focus()
57+
return
58+
}
59+
60+
router.push({ name: 'search' })
61+
}
62+
5063
onKeyDown(
5164
'/',
5265
e => {
5366
if (isEditableElement(e.target)) return
5467
e.preventDefault()
68+
focusSearchOrNavigate()
69+
},
70+
{ dedupe: true },
71+
)
5572
56-
const searchInput = document.querySelector<HTMLInputElement>(
57-
'input[type="search"], input[name="q"]',
58-
)
59-
60-
if (searchInput) {
61-
searchInput.focus()
62-
return
63-
}
64-
65-
router.push({ name: 'search' })
73+
onKeyDown(
74+
// Explicitly check Ctrl+K for broader compatibility (especially Firefox)
75+
e => e.ctrlKey && e.key === 'k',
76+
e => {
77+
if (isEditableElement(e.target)) return
78+
e.preventDefault()
79+
focusSearchOrNavigate()
6680
},
6781
{ dedupe: true },
6882
)

app/components/AppFooter.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ const showModal = () => modalRef.value?.showModal?.()
4848
<kbd class="kbd">/</kbd>
4949
<span>{{ $t('shortcuts.focus_search') }}</span>
5050
</li>
51+
<li class="flex gap-2 items-center">
52+
<kbd class="kbd">Ctrl</kbd>+<kbd class="kbd">K</kbd>
53+
<span>{{ $t('shortcuts.focus_search') }}</span>
54+
</li>
5155
<li class="flex gap-2 items-center">
5256
<kbd class="kbd">?</kbd>
5357
<span>{{ $t('shortcuts.show_kbd_hints') }}</span>

0 commit comments

Comments
 (0)