Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(plugin-docsearch): introduce vueuse package (#1271)
- Loading branch information
1 parent
8a63e19
commit e9b35cf
Showing
3 changed files
with
16 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletions
10
ecosystem/plugin-docsearch/src/client/composables/useDocsearchHotkeyListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.