Skip to content

Commit

Permalink
refactor(plugin-docsearch): introduce vueuse package (#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 27, 2023
1 parent 8a63e19 commit e9b35cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions ecosystem/plugin-docsearch/package.json
Expand Up @@ -44,6 +44,7 @@
"@vuepress/core": "workspace:*",
"@vuepress/shared": "workspace:*",
"@vuepress/utils": "workspace:*",
"@vueuse/core": "^9.13.0",
"ts-debounce": "^4.0.0",
"vue": "^3.2.47",
"vue-router": "^4.1.6"
Expand Down
@@ -1,16 +1,14 @@
import { onMounted, onUnmounted } from 'vue'
import { useEventListener } from '@vueuse/core'

/**
* Add hotkey listener, remove it after triggered
*/
export const useDocsearchHotkeyListener = (callback: () => void): void => {
const hotkeyListener = (event: KeyboardEvent): void => {
const remove = useEventListener('keydown', (event) => {
if (event.key === 'k' && (event.ctrlKey || event.metaKey)) {
event.preventDefault()
window.removeEventListener('keydown', hotkeyListener)
callback()
remove()
}
}
onMounted(() => window.addEventListener('keydown', hotkeyListener))
onUnmounted(() => window.removeEventListener('keydown', hotkeyListener))
})
}
12 changes: 11 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9b35cf

Please sign in to comment.