The Command component provides a command palette interface for searching and executing commands. It's commonly used for keyboard shortcuts, search functionality, and command-line interfaces within applications.
import { Command } from '@/components/command'export default function CommandExample() {const suggestionsItems = [{ title: 'Create New Project', shortcut: '⌘N', description: 'Start a new project' },{ title: 'Open Recent File', shortcut: '⌘O', description: 'Open recently accessed files' },{ title: 'Save Current Work', shortcut: '⌘S', description: 'Save your current progress' }];const items = [{title: 'Quick Actions',childs: [{ title: 'Create New Project', shortcut: '⌘N', description: 'Start a new project' },{ title: 'Open Recent File', shortcut: '⌘O', description: 'Open recently accessed files' },{ title: 'Save Current Work', shortcut: '⌘S', description: 'Save your current progress' }]}];return (<CommandsuggestionsItems={suggestionsItems}items={items}/>)}
Preview:
The Command component is directly re-exported from @dashflowx/core, so it uses the same props as the core component. The component accepts JSON strings or arrays for the data props.
| Prop | Type | Default | Description |
|---|---|---|---|
suggestionsItems | Array | string | [] | Array of suggestion items with title and optional shortcut |
items | Array | string | [] | Array of grouped items with title, shortcut, and childs |
className | string | '' | Additional CSS classes for the container |
children | React.ReactNode | - | Additional content to render inside the command |
The component accepts data in two formats:
const suggestionsItems = [{ title: 'Calendar', shortcut: '⌘K' },{ title: 'Search Emoji', shortcut: '⌘E' }];const items = [{title: 'Suggestions',childs: [{ title: 'Calendar', shortcut: '⌘K' },{ title: 'Search Emoji', shortcut: '⌘E' }]}];
<CommandsuggestionsItems="[{\"title\":\"Create New Project\",\"shortcut\":\"⌘N\",\"description\":\"Start a new project\"},{\"title\":\"Open Recent File\",\"shortcut\":\"⌘O\",\"description\":\"Open recently accessed files\"}]"items="[{\"title\":\"Quick Actions\",\"childs\":[{\"title\":\"Create New Project\",\"shortcut\":\"⌘N\",\"description\":\"Start a new project\"},{\"title\":\"Open Recent File\",\"shortcut\":\"⌘O\",\"description\":\"Open recently accessed files\"}]}]"/>
The Command component exports several sub-components from @dashflowx/core:
import {Command,CommandDialog,CommandEmpty,CommandGroup,CommandInput,CommandItem,CommandList,CommandSeparator,CommandShortcut,} from '@/components/command'
<Command><CommandInput placeholder="Type a command..." /><CommandList><CommandEmpty>No results found.</CommandEmpty><CommandGroup heading="Suggestions"><CommandItem><span>Calendar</span><CommandShortcut>⌘K</CommandShortcut></CommandItem></CommandGroup><CommandSeparator /><CommandGroup heading="Actions"><CommandItem><span>New File</span><CommandShortcut>⌘N</CommandShortcut></CommandItem></CommandGroup></CommandList></Command>
const suggestionsItems = [{ title: 'New File', shortcut: '⌘N' },{ title: 'Open File', shortcut: '⌘O' },{ title: 'Save File', shortcut: '⌘S' }];const items = [{title: 'File',childs: [{ title: 'New File', shortcut: '⌘N' },{ title: 'Open File', shortcut: '⌘O' },{ title: 'Save File', shortcut: '⌘S' }]}];<CommandsuggestionsItems={suggestionsItems}items={items}/>
Preview:
const searchItems = [{ title: 'Find User by Name', shortcut: '⌘U', description: 'Search for users in the system' },{ title: 'Find File by Name', shortcut: '⌘F', description: 'Search for files and documents' },{ title: 'Find Message Content', shortcut: '⌘M', description: 'Search through message history' }];const categories = [{title: 'Search & Discovery',childs: [{ title: 'Find User by Name', shortcut: '⌘U', description: 'Search for users in the system' },{ title: 'Find File by Name', shortcut: '⌘F', description: 'Search for files and documents' },{ title: 'Find Message Content', shortcut: '⌘M', description: 'Search through message history' }]},{title: 'Navigation',childs: [{ title: 'Go to Dashboard', shortcut: '⌘D', description: 'Navigate to main dashboard' },{ title: 'Open Settings', shortcut: '⌘,', description: 'Access application settings' },{ title: 'View Profile', shortcut: '⌘P', description: 'Open user profile page' }]}];<CommandsuggestionsItems={searchItems}items={categories}/>
Preview:
const appSuggestions = [{ title: 'Switch Dark/Light Theme', shortcut: '⌘T', description: 'Toggle between dark and light mode' },{ title: 'Show/Hide Sidebar', shortcut: '⌘B', description: 'Toggle sidebar visibility' },{ title: 'Focus Search Bar', shortcut: '⌘K', description: 'Move focus to search input' }];const appCommands = [{title: 'View & Display',childs: [{ title: 'Switch Dark/Light Theme', shortcut: '⌘T', description: 'Toggle between dark and light mode' },{ title: 'Show/Hide Sidebar', shortcut: '⌘B', description: 'Toggle sidebar visibility' },{ title: 'Enter Full Screen', shortcut: '⌘F', description: 'Maximize application window' }]},{title: 'Edit Operations',childs: [{ title: 'Undo Last Action', shortcut: '⌘Z', description: 'Reverse the last performed action' },{ title: 'Redo Last Action', shortcut: '⌘Y', description: 'Repeat the last undone action' },{ title: 'Cut Selection', shortcut: '⌘X', description: 'Cut selected content to clipboard' },{ title: 'Copy Selection', shortcut: '⌘C', description: 'Copy selected content to clipboard' },{ title: 'Paste from Clipboard', shortcut: '⌘V', description: 'Insert content from clipboard' }]},{title: 'Help & Support',childs: [{ title: 'Open Documentation', shortcut: '⌘?', description: 'View help documentation' },{ title: 'Show Shortcuts', shortcut: '⌘/', description: 'Display keyboard shortcuts guide' },{ title: 'About Application', shortcut: '⌘I', description: 'View application information' }]}];<CommandsuggestionsItems={appSuggestions}items={appCommands}/>
Preview:
<CommandsuggestionsItems={suggestionsItems}items={items}className="border-2 border-blue-500 rounded-xl shadow-lg"/>
Preview:
const editorSuggestions = [{ title: 'Format Document', shortcut: '⌘⇧F' },{ title: 'Go to Line', shortcut: '⌘G' },{ title: 'Find and Replace', shortcut: '⌘⇧H' }];const editorCommands = [{title: 'File',childs: [{ title: 'New File', shortcut: '⌘N' },{ title: 'Open File', shortcut: '⌘O' },{ title: 'Save File', shortcut: '⌘S' },{ title: 'Save As', shortcut: '⌘⇧S' }]},{title: 'Edit',childs: [{ title: 'Undo', shortcut: '⌘Z' },{ title: 'Redo', shortcut: '⌘Y' },{ title: 'Cut', shortcut: '⌘X' },{ title: 'Copy', shortcut: '⌘C' },{ title: 'Paste', shortcut: '⌘V' }]},{title: 'View',childs: [{ title: 'Toggle Terminal', shortcut: '⌘`' },{ title: 'Toggle Sidebar', shortcut: '⌘B' },{ title: 'Toggle Panel', shortcut: '⌘J' }]}];
const projectSuggestions = [{ title: 'Create Task', shortcut: '⌘T' },{ title: 'Create Project', shortcut: '⌘P' },{ title: 'Search Tasks', shortcut: '⌘F' }];const projectCommands = [{title: 'Tasks',childs: [{ title: 'Create Task', shortcut: '⌘T' },{ title: 'Complete Task', shortcut: '⌘D' },{ title: 'Delete Task', shortcut: '⌘⌫' }]},{title: 'Projects',childs: [{ title: 'Create Project', shortcut: '⌘P' },{ title: 'Archive Project', shortcut: '⌘A' },{ title: 'Delete Project', shortcut: '⌘⌫' }]},{title: 'Team',childs: [{ title: 'Invite Member', shortcut: '⌘I' },{ title: 'View Members', shortcut: '⌘M' },{ title: 'Manage Roles', shortcut: '⌘R' }]}];
const ecommerceSuggestions = [{ title: 'Add Product', shortcut: '⌘A' },{ title: 'View Orders', shortcut: '⌘O' },{ title: 'Manage Inventory', shortcut: '⌘I' }];const ecommerceCommands = [{title: 'Products',childs: [{ title: 'Add Product', shortcut: '⌘A' },{ title: 'Edit Product', shortcut: '⌘E' },{ title: 'Delete Product', shortcut: '⌘⌫' }]},{title: 'Orders',childs: [{ title: 'View Orders', shortcut: '⌘O' },{ title: 'Process Order', shortcut: '⌘P' },{ title: 'Cancel Order', shortcut: '⌘C' }]},{title: 'Analytics',childs: [{ title: 'View Sales', shortcut: '⌘S' },{ title: 'View Reports', shortcut: '⌘R' },{ title: 'Export Data', shortcut: '⌘E' }]}];
Use clear and descriptive command names.
// ✅ Good - Clear command namesconst commands = [{ title: 'Create New Document', shortcut: '⌘N' },{ title: 'Save Current File', shortcut: '⌘S' }];// ❌ Avoid - Unclear command namesconst commands = [{ title: 'New', shortcut: '⌘N' },{ title: 'Save', shortcut: '⌘S' }];
Use consistent keyboard shortcuts across your application.
// ✅ Good - Consistent shortcutsconst commands = [{ title: 'New File', shortcut: '⌘N' },{ title: 'New Folder', shortcut: '⌘⇧N' },{ title: 'New Window', shortcut: '⌘⌥N' }];
Group related commands together.
// ✅ Good - Logical groupingconst commands = [{title: 'File Operations',childs: [{ title: 'New File', shortcut: '⌘N' },{ title: 'Open File', shortcut: '⌘O' },{ title: 'Save File', shortcut: '⌘S' }]},{title: 'Edit Operations',childs: [{ title: 'Undo', shortcut: '⌘Z' },{ title: 'Redo', shortcut: '⌘Y' },{ title: 'Cut', shortcut: '⌘X' }]}];
Use standard keyboard shortcuts when possible.
// ✅ Good - Standard shortcutsconst commands = [{ title: 'Copy', shortcut: '⌘C' },{ title: 'Paste', shortcut: '⌘V' },{ title: 'Undo', shortcut: '⌘Z' }];
Make commands searchable and discoverable.
// ✅ Good - Searchable commandsconst commands = [{ title: 'Create New Project', shortcut: '⌘N' },{ title: 'Open Existing Project', shortcut: '⌘O' },{ title: 'Save Current Project', shortcut: '⌘S' }];
You can customize the command palette appearance using CSS classes:
<CommandsuggestionsItems={suggestionsItems}items={items}className="border-2 border-purple-500 rounded-xl shadow-lg bg-purple-50"/>
Use different themes for different contexts:
// Light theme<CommandsuggestionsItems={suggestionsItems}items={items}className="bg-white border-gray-200"/>// Dark theme<CommandsuggestionsItems={suggestionsItems}items={items}className="bg-gray-900 border-gray-700 text-white"/>
Use different sizes for different emphasis levels:
// Small command palette<CommandsuggestionsItems={suggestionsItems}items={items}className="w-64"/>// Large command palette<CommandsuggestionsItems={suggestionsItems}items={items}className="w-full max-w-2xl"/>
Load commands dynamically based on context:
const [commands, setCommands] = useState([]);useEffect(() => {// Load commands based on current contextconst contextCommands = getCommandsForContext(currentContext);setCommands(contextCommands);}, [currentContext]);
Handle command execution:
const handleCommandSelect = (command: string) => {switch (command) {case 'New File':createNewFile();break;case 'Open File':openFileDialog();break;case 'Save File':saveCurrentFile();break;default:console.log('Unknown command:', command);}};
Implement search functionality:
const [searchTerm, setSearchTerm] = useState('');const filteredCommands = useMemo(() => {if (!searchTerm) return commands;return commands.filter(command =>command.title.toLowerCase().includes(searchTerm.toLowerCase()));}, [commands, searchTerm]);
- Dialog: For modal command palettes
- Input: For search input
- List: For command lists
- Shortcut: For keyboard shortcuts
- Search: For search functionality
For the complete API reference and advanced usage patterns, see the Command API documentation.
