'use client'
import { Command, useCommandCollection } from '@chakra-ui/react'
import {
LuCalendar,
LuFileText,
LuMail,
LuSearch,
LuSettings,
LuUser,
LuX,
} from 'react-icons/lu'
export const CommandBasic = () => {
const command = useCommandCollection({
initialItems: items,
groupBy: (item) => item.group,
})
return (
<Command.Root command={command} borderWidth="1px" maxW="md">
<Command.Label>Search commands</Command.Label>
<Command.Control>
<Command.Indicator>
<LuSearch />
</Command.Indicator>
<Command.Input placeholder="Type a command or search..." />
<Command.ClearTrigger>
<LuX />
</Command.ClearTrigger>
</Command.Control>
<Command.List>
<Command.Empty>No results found.</Command.Empty>
{command.collection.group().map(([group, groupItems]) => (
<Command.ItemGroup key={group}>
<Command.ItemGroupLabel>{group}</Command.ItemGroupLabel>
{groupItems.map((item) => (
<Command.Item key={item.value} item={item}>
{item.icon}
<Command.ItemText highlight>{item.label}</Command.ItemText>
{item.shortcut?.map((key) => (
<Command.ItemCommand key={key}>{key}</Command.ItemCommand>
))}
</Command.Item>
))}
</Command.ItemGroup>
))}
</Command.List>
</Command.Root>
)
}
const items = [
{
value: 'calendar',
label: 'Calendar',
group: 'Suggestions',
icon: <LuCalendar />,
},
{
value: 'search-emails',
label: 'Search emails',
group: 'Suggestions',
icon: <LuMail />,
keywords: ['inbox'],
},
{
value: 'new-document',
label: 'New document',
group: 'Suggestions',
icon: <LuFileText />,
shortcut: ['⌘', 'N'],
},
{
value: 'profile',
label: 'Profile',
group: 'Settings',
icon: <LuUser />,
shortcut: ['⌘', 'P'],
},
{
value: 'settings',
label: 'Settings',
group: 'Settings',
icon: <LuSettings />,
shortcut: ['⌘', ','],
},
]
Anatomy
import { Command, useCommandCollection } from '@chakra-ui/react'<Command.Root>
<Command.Label />
<Command.Control>
<Command.Indicator />
<Command.Input />
<Command.ClearTrigger />
</Command.Control>
<Command.List>
<Command.Empty />
<Command.Loading />
<Command.ItemGroup>
<Command.ItemGroupLabel />
<Command.Item>
<Command.ItemText />
<Command.ItemCommand />
</Command.Item>
</Command.ItemGroup>
<Command.Separator />
</Command.List>
<Command.Footer />
</Command.Root>useCommandCollection holds the items and the query. It ranks items with a
fuzzy match on their label and keywords, so pass it to Command.Root as
command and render command.collection.
⌘K, use KbdGroup (registry item kbd-group), which was called
Command before the rc.Examples
Basic
Group items with groupBy and render each group with Command.ItemGroup. Set
highlight on Command.ItemText to emphasize the characters that match the
query, and use Command.ItemCommand to show an item's shortcut.
'use client'
import { Command, useCommandCollection } from '@chakra-ui/react'
import {
LuCalendar,
LuFileText,
LuMail,
LuSearch,
LuSettings,
LuUser,
LuX,
} from 'react-icons/lu'
export const CommandBasic = () => {
const command = useCommandCollection({
initialItems: items,
groupBy: (item) => item.group,
})
return (
<Command.Root command={command} borderWidth="1px" maxW="md">
<Command.Label>Search commands</Command.Label>
<Command.Control>
<Command.Indicator>
<LuSearch />
</Command.Indicator>
<Command.Input placeholder="Type a command or search..." />
<Command.ClearTrigger>
<LuX />
</Command.ClearTrigger>
</Command.Control>
<Command.List>
<Command.Empty>No results found.</Command.Empty>
{command.collection.group().map(([group, groupItems]) => (
<Command.ItemGroup key={group}>
<Command.ItemGroupLabel>{group}</Command.ItemGroupLabel>
{groupItems.map((item) => (
<Command.Item key={item.value} item={item}>
{item.icon}
<Command.ItemText highlight>{item.label}</Command.ItemText>
{item.shortcut?.map((key) => (
<Command.ItemCommand key={key}>{key}</Command.ItemCommand>
))}
</Command.Item>
))}
</Command.ItemGroup>
))}
</Command.List>
</Command.Root>
)
}
const items = [
{
value: 'calendar',
label: 'Calendar',
group: 'Suggestions',
icon: <LuCalendar />,
},
{
value: 'search-emails',
label: 'Search emails',
group: 'Suggestions',
icon: <LuMail />,
keywords: ['inbox'],
},
{
value: 'new-document',
label: 'New document',
group: 'Suggestions',
icon: <LuFileText />,
shortcut: ['⌘', 'N'],
},
{
value: 'profile',
label: 'Profile',
group: 'Settings',
icon: <LuUser />,
shortcut: ['⌘', 'P'],
},
{
value: 'settings',
label: 'Settings',
group: 'Settings',
icon: <LuSettings />,
shortcut: ['⌘', ','],
},
]
Dialog
Wrap the palette in Command.Dialog to open it as an overlay, and register a
hotkey with useHotkeys. Put the collection in Command.Provider so the query
resets when the dialog closes.
This example opens with ⌘J because the docs search already uses ⌘K. In your
own app, mod+k is the usual choice.
'use client'
import { useState } from 'react'
import {
Command,
Kbd,
Text,
useCommandCollection,
useHotkeys,
} from '@chakra-ui/react'
import { Button } from '#components/ui/button'
import {
LuCreditCard,
LuLayoutDashboard,
LuLogOut,
LuSearch,
LuSettings,
LuUsers,
} from 'react-icons/lu'
export const CommandDialog = () => {
const [open, setOpen] = useState(false)
useHotkeys({
commands: [
{
// The docs search already uses mod+k; use it in your own app.
hotkey: 'mod+j',
action: () => setOpen((open) => !open),
},
],
})
const command = useCommandCollection({
initialItems: items,
groupBy: (item) => item.group,
})
return (
<Command.Provider value={command}>
<Command.Dialog open={open} onOpenChange={(e) => setOpen(e.open)}>
<Command.DialogTrigger asChild>
<Button variant="outline" size="sm" gap="3">
<LuSearch />
<Text color="fg.muted">Search...</Text>
<Kbd size="sm">⌘J</Kbd>
</Button>
</Command.DialogTrigger>
<Command.DialogBackdrop />
<Command.DialogContent maxW="lg" p="0" overflow="hidden">
<Command.Root>
<Command.Control>
<Command.Indicator>
<LuSearch />
</Command.Indicator>
<Command.Input placeholder="Search for a page or action..." />
</Command.Control>
<Command.List>
<Command.Empty>No results found.</Command.Empty>
{command.collection.group().map(([group, groupItems]) => (
<Command.ItemGroup key={group}>
<Command.ItemGroupLabel>{group}</Command.ItemGroupLabel>
{groupItems.map((item) => (
<Command.Item key={item.value} item={item}>
{item.icon}
<Command.ItemText highlight>{item.label}</Command.ItemText>
</Command.Item>
))}
</Command.ItemGroup>
))}
</Command.List>
<Command.Footer>
<span>
<Kbd size="sm">↑</Kbd> <Kbd size="sm">↓</Kbd> to navigate
</span>
<span>
<Kbd size="sm">↵</Kbd> to select
</span>
<span>
<Kbd size="sm">esc</Kbd> to close
</span>
</Command.Footer>
</Command.Root>
</Command.DialogContent>
</Command.Dialog>
</Command.Provider>
)
}
const items = [
{
value: 'dashboard',
label: 'Dashboard',
group: 'Pages',
icon: <LuLayoutDashboard />,
},
{ value: 'members', label: 'Members', group: 'Pages', icon: <LuUsers /> },
{
value: 'billing',
label: 'Billing',
group: 'Pages',
icon: <LuCreditCard />,
keywords: ['invoices', 'plan', 'subscription'],
},
{
value: 'settings',
label: 'Settings',
group: 'Pages',
icon: <LuSettings />,
},
{ value: 'log-out', label: 'Log out', group: 'Account', icon: <LuLogOut /> },
]
Sizes
Use the size prop to change the height of the input and the density of the
list.
'use client'
import { Command, Stack, useCommandCollection } from '@chakra-ui/react'
import { LuSearch } from 'react-icons/lu'
export const CommandWithSizes = () => {
return (
<Stack gap="6" maxW="md">
{(['sm', 'md', 'lg'] as const).map((size) => (
<SizedCommand key={size} size={size} />
))}
</Stack>
)
}
const SizedCommand = (props: { size: 'sm' | 'md' | 'lg' }) => {
const command = useCommandCollection({ initialItems: items })
return (
<Command.Root command={command} size={props.size} borderWidth="1px">
<Command.Control>
<Command.Indicator>
<LuSearch />
</Command.Indicator>
<Command.Input placeholder={`Search (size="${props.size}")`} />
</Command.Control>
<Command.List>
{command.collection.items.map((item) => (
<Command.Item key={item.value} item={item}>
<Command.ItemText>{item.label}</Command.ItemText>
</Command.Item>
))}
</Command.List>
</Command.Root>
)
}
const items = [
{ value: 'inbox', label: 'Inbox' },
{ value: 'drafts', label: 'Drafts' },
{ value: 'archive', label: 'Archive' },
]