Skip to content

Commit 53c40ba

Browse files
authored
fix: command palette keyboard event handling (#26)
1 parent d9c7453 commit 53c40ba

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuxt-bedtime",
3-
"version": "0.0.10",
3+
"version": "0.10.1",
44
"description": "Nuxt module for creating component stories",
55
"repository": "timhanlon/bedtime",
66
"license": "MIT",
@@ -55,4 +55,4 @@
5555
"vue-tsc": "^2.2.0"
5656
},
5757
"packageManager": "pnpm@9.15.3"
58-
}
58+
}

src/runtime/components/CommandPalette.vue

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
2-
import { ref, computed, watch, nextTick } from 'vue'
3-
import { onKeyStroke, useMagicKeys, onClickOutside } from '@vueuse/core'
2+
import { computed, nextTick, ref, watch } from 'vue'
3+
import { onClickOutside, useMagicKeys } from '@vueuse/core'
44
import Icon from './elements/Icon.vue'
55
66
interface CommandItem {
@@ -56,33 +56,31 @@ function handleSelect(item: CommandItem) {
5656
emits('update', item.id)
5757
}
5858
59-
onKeyStroke('ArrowDown', (e) => {
60-
e.preventDefault()
59+
function onArrowDown() {
6160
isKeyboardNavigation.value = true
6261
if (selectedIndex.value < filteredItems.value.length - 1) {
6362
selectedIndex.value++
6463
scrollIntoView(selectedIndex.value)
6564
}
66-
})
65+
}
6766
68-
onKeyStroke('ArrowUp', (e) => {
69-
e.preventDefault()
67+
function onArrowUp() {
7068
isKeyboardNavigation.value = true
7169
if (selectedIndex.value > 0) {
7270
selectedIndex.value--
7371
scrollIntoView(selectedIndex.value)
7472
}
75-
})
73+
}
7674
77-
onKeyStroke('Enter', () => {
75+
function onEnter() {
7876
if (filteredItems.value[selectedIndex.value])
7977
handleSelect(filteredItems.value[selectedIndex.value])
80-
})
78+
}
8179
82-
onKeyStroke('Escape', () => {
80+
function onEscape() {
8381
isOpen.value = false
8482
searchQuery.value = ''
85-
})
83+
}
8684
8785
onClickOutside(paletteRef, () => {
8886
isOpen.value = false
@@ -114,6 +112,10 @@ function scrollIntoView(index: number) {
114112
v-if="isOpen"
115113
ref="paletteRef"
116114
class="command-palette"
115+
@keydown.up.prevent="onArrowUp"
116+
@keydown.down.prevent="onArrowDown"
117+
@keydown.enter.prevent="onEnter"
118+
@keydown.esc.prevent="onEscape"
117119
>
118120
<div
119121
class="command-palette-inner"
@@ -126,6 +128,7 @@ function scrollIntoView(index: number) {
126128
placeholder="Search..."
127129
class="command-palette-input"
128130
@focus="isOpen = true"
131+
@keydown.tab.prevent
129132
>
130133
<Icon
131134
name="search"

0 commit comments

Comments
 (0)