Skip to content

useCommandPalette

Manages command palette state and command registration.

Signature

ts
function useCommandPalette(
  options?: UseCommandPaletteOptions
): UseCommandPaletteReturn

Parameters

UseCommandPaletteOptions

PropertyTypeDefaultDescription
initialCommandsCommand[][]Commands to register initially
enableShortcutbooleantrueEnable Cmd/Ctrl+K shortcut
onExecute(command: Command) => void-Called when command executes

Return Type

UseCommandPaletteReturn

PropertyTypeDescription
isOpenbooleanWhether palette is open
commandsCommand[]All registered commands
groupedCommandsMap<string, Command[]>Commands grouped by group property
open() => voidOpen the palette
close() => voidClose the palette
toggle() => voidToggle open state
register(commands: Command[]) => voidReplace all commands
add(commands: Command[]) => voidAdd commands (skips duplicates)
remove(ids: string[]) => voidRemove commands by ID
clear() => voidRemove all commands
execute(id: string) => Promise<void>Execute command by ID

Types

Command

PropertyTypeRequiredDescription
idstringYesUnique identifier
labelstringYesDisplay label
action() => void | Promise<void>YesAction to execute
keywordsstring[]NoSearch keywords
iconReactNodeNoIcon element
groupstringNoGroup name (default: 'Commands')
shortcutstringNoShortcut display text
disabledbooleanNoWhether command is disabled

Example

tsx
const palette = useCommandPalette({
  initialCommands: [
    { id: 'home', label: 'Go Home', group: 'Navigation', action: () => navigate('/') },
  ],
})

palette.add([
  { id: 'settings', label: 'Settings', action: () => openSettings() },
])

<CommandPalette
  open={palette.isOpen}
  onOpenChange={(open) => open ? palette.open() : palette.close()}
  commands={palette.commands}
  groupedCommands={palette.groupedCommands}
  onSelect={(cmd) => palette.execute(cmd.id)}
/>

See also