Skip to content

Commit c20bd28

Browse files
feat(theme): open search box on pressing slash too (#2328)
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
1 parent 58795d2 commit c20bd28

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/client/theme-default/components/VPNavBarSearch.vue

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ onMounted(() => {
6161
6262
preconnect()
6363
64-
const handleSearchHotKey = (e: KeyboardEvent) => {
65-
if (e.key === 'k' && (e.ctrlKey || e.metaKey)) {
66-
e.preventDefault()
64+
const handleSearchHotKey = (event: KeyboardEvent) => {
65+
if (
66+
(event.key.toLowerCase() === 'k' && (event.metaKey || event.ctrlKey)) ||
67+
(!isEditingContent(event) && event.key === '/')
68+
) {
69+
event.preventDefault()
6770
load()
6871
remove()
6972
}
@@ -101,6 +104,18 @@ function poll() {
101104
}, 16)
102105
}
103106
107+
function isEditingContent(event: KeyboardEvent): boolean {
108+
const element = event.target as HTMLElement
109+
const tagName = element.tagName
110+
111+
return (
112+
element.isContentEditable ||
113+
tagName === 'INPUT' ||
114+
tagName === 'SELECT' ||
115+
tagName === 'TEXTAREA'
116+
)
117+
}
118+
104119
// Local search
105120
106121
const showSearch = ref(false)
@@ -112,6 +127,13 @@ if (__VP_LOCAL_SEARCH__) {
112127
showSearch.value = true
113128
}
114129
})
130+
131+
onKeyStroke('/', (event) => {
132+
if (!isEditingContent(event)) {
133+
event.preventDefault()
134+
showSearch.value = true
135+
}
136+
})
115137
}
116138
117139
const metaKey = ref(`'Meta'`)

0 commit comments

Comments
 (0)