Skip to content

Commit

Permalink
fix(CommandPalette): select first item on search changes (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin authored and benjamincanac committed Jan 9, 2023
1 parent 6a5ee32 commit 4f56921
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/runtime/components/navigation/CommandPalette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { ref, computed, watch, onMounted } from 'vue'
import { Combobox, ComboboxInput, ComboboxOptions } from '@headlessui/vue'
import type { ComputedRef, PropType, ComponentPublicInstance } from 'vue'
import { useFuse } from '@vueuse/integrations/useFuse'
Expand Down Expand Up @@ -174,11 +174,19 @@ const groups = computed(() => map(groupBy(results.value, command => command.item
} as Group
}))
watch(groups, () => {
// Select first item on search changes
setTimeout(() => {
// https://github.com/tailwindlabs/headlessui/blob/6fa6074cd5d3a96f78a2d965392aa44101f5eede/packages/%40headlessui-vue/src/components/combobox/combobox.ts#L804
comboboxInput.value?.$el.dispatchEvent(new KeyboardEvent('keydown', { key: 'PageUp' }))
}, 0)
})
// Methods
function activateFirstOption () {
// hack combobox by using keyboard event
// https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-vue/src/components/combobox/combobox.ts#L692
// https://github.com/tailwindlabs/headlessui/blob/6fa6074cd5d3a96f78a2d965392aa44101f5eede/packages/%40headlessui-vue/src/components/combobox/combobox.ts#L769
setTimeout(() => {
comboboxInput.value?.$el.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }))
}, 0)
Expand Down

0 comments on commit 4f56921

Please sign in to comment.