command/01-basic-command
An always-visible floating palette scoped to its preview, with grouped commands, icons, descriptions, shortcuts, keyboard navigation, and selection feedback.
Loading interactive preview
'use client';
import React from 'react';
import { Icon } from 'ui-lab-components/icon';
import { Command, type CommandItemData, useCommandContext } from 'ui-lab-components/command';
const commandData: Omit<CommandItemData, 'action'>[] = [
{ id: 'search', label: 'Search documents', description: 'Find anything in your workspace', category: 'Workspace', shortcut: '⌘F', icon: <Icon id="search" size={16} /> },
{ id: 'projects', label: 'Browse projects', description: 'Jump to an active project', category: 'Workspace', shortcut: '⌘P', icon: <Icon id="folder" size={16} /> },
{ id: 'create', label: 'Create document', description: 'Start with a blank page', category: 'Actions', shortcut: '⌘N', icon: <Icon id="file-plus" size={16} /> },
{ id: 'invite', label: 'Invite teammate', description: 'Bring someone into your workspace', category: 'Actions', icon: <Icon id="users" size={16} /> },
{ id: 'settings', label: 'Workspace settings', category: 'Settings', shortcut: '⌘,', icon: <Icon id="gear" size={16} /> },
];
function Results() {
const { filteredItems } = useCommandContext();
return (
<Command.List empty-message="No commands found. Try ‘project’ or ‘create’.">
{filteredItems.length > 0 ? (
<Command.Groups
renderCategory={(category) => category ? <Command.Category>{category}</Command.Category> : null}
renderItem={(cmd) => (
<Command.Item value={cmd.id} textValue={cmd.label} action={cmd.action} icon={cmd.icon} hint={cmd.shortcut}>
<div className="min-w-0">
<Command.Label>{cmd.label}</Command.Label>
{cmd.description && <Command.Description>{cmd.description}</Command.Description>}
</div>
</Command.Item>
)}
/>
) : null}
</Command.List>
);
}
export default function Example() {
const [container, setContainer] = React.useState<HTMLDivElement | null>(null);
const [, setSelected] = React.useState('');
const commands = React.useMemo(() => commandData.map((cmd) => ({ ...cmd, action: () => setSelected(cmd.label) })), []);
return (
<div className="w-full">
<div ref={setContainer} className="[&>[data-slot=overlay]]:bg-transparent! [&>[data-slot=overlay]]:backdrop-filter-none!" style={{ position: 'relative', isolation: 'isolate', overflow: 'hidden', width: '100%', height: 520 }}>
<Command
open
container={container}
items={commands}
style={{ '--origin': '50% 6%', '--width': '28rem', '--height': '28rem', height: '28rem', minHeight: '28rem' } as React.CSSProperties}
>
<Command.Input autoFocus={false} placeholder="Search your workspace..." />
<Results />
<Command.Footer actions={{ left: [{ label: 'Navigate', hint: ['↑', '↓'] }], right: [{ label: 'Select', hint: 'Enter' }] }} />
</Command>
</div>
</div>
);
}