Skip to content

useCommandPalette ​

Opening and closing the palette. One ref, so the navbar button, Ctrl+K and the shortcut layer cannot disagree.

Open in demo

Import ​

ts
import { useCommandPalette } from '@shared/composables/useCommandPalette'

Signature ​

ts
function useCommandPalette(): {
  open: Ref<boolean>
  show: () => void
  hide: () => void
  toggle: () => void
}

Returns ​

MemberTypeDescription
openRef<boolean>Writable, so it can be bound with v-model.
show() => voidOpens it.
hide() => voidCloses it.
toggle() => voidFlips it.

Example ​

vue
<script setup lang="ts">
import CommandPalette from '@shared/components/common/CommandPalette.vue'
import { useCommandPalette } from '@shared/composables/useCommandPalette'

const { show } = useCommandPalette()
</script>

<template>
  <div>
    <button type="button" @click="show()">
      Search
      <kbd>Ctrl K</kbd>
    </button>

    <CommandPalette />
  </div>
</template>

Notes ​

  • It is one module-scope ref. Three things open the palette — the navbar trigger, the Ctrl+K listener and the command that opens it — and they all write the same state, so it cannot end up half-open.
  • The state only says whether the dialog is showing. What it lists comes from useCommandRegistry.
  • Ctrl+K checks the commandPalette feature flag inside its handler, so switching it off in Settings takes effect immediately. This composable does not check the flag; if you build your own trigger, check it yourself.
  • Mount CommandPalette once, in your app shell. The template already does.