@command-palette/react
v0.0.2
Published
Unstyled React command palette primitives
Maintainers
Readme
@command-palette/react
Unstyled React primitives for building command menus.
Built on top of the internal command store.
Usage
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '@command-palette/react'
export function CommandPalette() {
return (
<Command label="Command Palette">
<CommandInput placeholder="Search…" />
<CommandList>
<CommandEmpty>No results.</CommandEmpty>
<CommandGroup heading="Letters">
<CommandItem value="a">A</CommandItem>
<CommandItem value="b">B</CommandItem>
</CommandGroup>
<CommandItem value="apple" keywords={['fruit']}>
Apple
</CommandItem>
</CommandList>
</Command>
)
}For modal usage:
<CommandDialog open={open} onOpenChange={setOpen} label="Global Command Palette">
<CommandInput placeholder="Type a command…" />
<CommandList>
<CommandItem value="theme">Change theme</CommandItem>
<CommandItem value="settings">Open settings</CommandItem>
</CommandList>
</CommandDialog>Components
Command
Root provider and interactive command surface.
label?: stringAccessible label for the command surface.value?: stringControlled highlighted item value.defaultValue?: stringInitial highlighted item value for uncontrolled usage.onValueChange?: (value: string) => voidCalled when the highlighted item changes.search?: stringControlled search query.defaultSearch?: stringInitial search query for uncontrolled usage.onSearchChange?: (search: string) => voidCalled when the search query changes.filter?: 'fuzzy' | 'contains' | 'none' | FilterFnBuilt-in filter mode or a custom scoring function.loop?: booleanWrap keyboard navigation from end to start and back.selectOnHover?: booleanWhentrue, pointer hover updates selection. Defaults totrue.
CommandInput
Search input wired to the current command store.
- All normal
inputprops exceptonChange value?: stringControlled input value.onValueChange?: (value: string) => voidCalled after the input value is committed.
IME composition is handled so partial composition does not trigger intermediate search updates.
CommandItem
Selectable command row.
value: stringStable item identity.keywords?: readonly string[]Extra aliases used by filtering.disabled?: booleanRenders the item but removes it from selection and keyboard navigation.forceMount?: booleanKeeps the item visible even when filtering would hide it.groupId?: stringExplicit group association. Usually inherited fromCommandGroup.onSelect?: (value: string, event?: Event) => voidCalled when the item is activated.
CommandGroup
Groups related items under an optional heading. Hidden automatically when no item in the group is visible, unless forceMount is set.
CommandDialog
Native <dialog> wrapper around Command.
open: booleanControls whether the dialog is open.onOpenChange: (open: boolean) => voidCalled when the dialog opens or closes.dialogClassName?: stringClass name for the native<dialog>.resetSearchOnClose?: booleanClears uncontrolled search when closing. Defaults totrue.
Other components
CommandListListbox wrapper for items and groups.CommandEmptyOnly renders when no visible items remain.CommandSeparatorHidden while searching unlessalwaysRenderis set.CommandLoadingProgressbar helper for async states.
Hooks
useCommandStore()Returns the underlying store instance.useCommandSlice(selector)Subscribes to derivedCommandState.
function ResultCount() {
const count = useCommandSlice((state) => state.filteredOrder.length)
return <span>{count} results</span>
}Styling
The package is unstyled. Useful selectors:
[command-palette-root][command-palette-dialog][command-palette-input][command-palette-list][command-palette-item][command-palette-group][command-palette-group-heading][command-palette-group-items][command-palette-empty][command-palette-separator][command-palette-loading][data-selected][data-disabled]
