Skip to content

Commit e9b35cf

Browse files
authored
refactor(plugin-docsearch): introduce vueuse package (#1271)
1 parent 8a63e19 commit e9b35cf

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

ecosystem/plugin-docsearch/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@vuepress/core": "workspace:*",
4545
"@vuepress/shared": "workspace:*",
4646
"@vuepress/utils": "workspace:*",
47+
"@vueuse/core": "^9.13.0",
4748
"ts-debounce": "^4.0.0",
4849
"vue": "^3.2.47",
4950
"vue-router": "^4.1.6"
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import { onMounted, onUnmounted } from 'vue'
1+
import { useEventListener } from '@vueuse/core'
22

33
/**
44
* Add hotkey listener, remove it after triggered
55
*/
66
export const useDocsearchHotkeyListener = (callback: () => void): void => {
7-
const hotkeyListener = (event: KeyboardEvent): void => {
7+
const remove = useEventListener('keydown', (event) => {
88
if (event.key === 'k' && (event.ctrlKey || event.metaKey)) {
99
event.preventDefault()
10-
window.removeEventListener('keydown', hotkeyListener)
1110
callback()
11+
remove()
1212
}
13-
}
14-
onMounted(() => window.addEventListener('keydown', hotkeyListener))
15-
onUnmounted(() => window.removeEventListener('keydown', hotkeyListener))
13+
})
1614
}

pnpm-lock.yaml

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)