Command
Uma paleta de comandos (⌘K) para pesquisa rápida e ações. Suporta navegação por teclado, itens agrupados, páginas aninhadas e padrões orientados a dados e de componentes compostos.
Importação
Seção intitulada “Importação”import { Command } from 'asterui'Exemplos
Seção intitulada “Exemplos”Uso Básico
Paleta de comandos orientada a dados com itens agrupados.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
const items = [
{ key: 'new-file', label: 'Create New File', group: 'Actions' },
{ key: 'new-folder', label: 'Create New Folder', group: 'Actions' },
{ key: 'search', label: 'Search Files', group: 'Actions' },
{ key: 'home', label: 'Go to Home', group: 'Navigation' },
{ key: 'settings', label: 'Open Settings', group: 'Navigation' },
{ key: 'profile', label: 'View Profile', group: 'Navigation' },
];
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open Command Palette
</Button>
<Command
open={open}
onOpenChange={setOpen}
items={items}
placeholder="Type a command or search..."
/>
</>
)
}
export default App Componentes Compostos
Construir paletas de comandos usando componentes compostos para mais controle.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open Command Palette
</Button>
<Command open={open} onOpenChange={setOpen}>
<Command.List>
<Command.Empty>No results found</Command.Empty>
<Command.Group heading="Actions">
<Command.Item onSelect={() => alert('Creating file...')}>
Create New File
</Command.Item>
<Command.Item onSelect={() => alert('Creating folder...')}>
Create New Folder
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Navigation">
<Command.Item onSelect={() => alert('Going home...')}>
Go to Home
</Command.Item>
<Command.Item onSelect={() => alert('Opening settings...')}>
Open Settings
</Command.Item>
</Command.Group>
</Command.List>
</Command>
</>
)
}
export default App Com Ícones
Adicionar ícones aos itens de comando para contexto visual.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
const items = [
{ key: 'new-file', label: 'Create New File', group: 'Actions', icon: <FileIcon /> },
{ key: 'new-folder', label: 'Create New Folder', group: 'Actions', icon: <FolderIcon /> },
{ key: 'search', label: 'Search Files', group: 'Actions', icon: <SearchIcon /> },
{ key: 'home', label: 'Go to Home', group: 'Navigation', icon: <HomeIcon /> },
{ key: 'settings', label: 'Open Settings', group: 'Navigation', icon: <SettingsIcon /> },
];
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open Command Palette
</Button>
<Command
open={open}
onOpenChange={setOpen}
items={items}
placeholder="Type a command or search..."
/>
</>
)
}
export default App Palavras-chave de Pesquisa
Adicionar palavras-chave extras para tornar itens encontráveis por termos alternativos.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
const items = [
{
key: 'new-file',
label: 'Create New File',
group: 'Actions',
keywords: ['add', 'create', 'new', 'file', 'document']
},
{
key: 'search',
label: 'Search Files',
group: 'Actions',
keywords: ['find', 'lookup', 'query']
},
{
key: 'settings',
label: 'Open Settings',
group: 'Navigation',
keywords: ['preferences', 'config', 'options']
},
{
key: 'profile',
label: 'View Profile',
group: 'Navigation',
keywords: ['user', 'account', 'me']
},
];
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open (try searching "preferences")
</Button>
<Command
open={open}
onOpenChange={setOpen}
items={items}
placeholder="Try 'preferences' or 'config'..."
/>
</>
)
}
export default App Páginas Aninhadas
Navegar para subpáginas com suporte de navegação de retorno. Pressione Backspace (quando a pesquisa estiver vazia) ou clique na seta de voltar para retornar.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open Command Palette
</Button>
<Command open={open} onOpenChange={setOpen}>
<Command.List>
<Command.Empty>No results found</Command.Empty>
{/* Root page content */}
<Command.Group heading="Quick Actions">
<Command.Item onSelect={() => alert('Creating file...')}>
Create New File
</Command.Item>
<Command.Item onSelect={() => alert('Searching...')}>
Search Files
</Command.Item>
</Command.Group>
<Command.Separator />
<Command.Group heading="Navigate">
<Command.Item value="settings-menu">
Settings →
</Command.Item>
<Command.Item value="theme-menu">
Theme →
</Command.Item>
</Command.Group>
{/* Settings page */}
<Command.Page id="settings">
<Command.Group heading="Settings">
<Command.Item onSelect={() => alert('Opening account settings...')}>
Account
</Command.Item>
<Command.Item onSelect={() => alert('Opening privacy settings...')}>
Privacy
</Command.Item>
<Command.Item onSelect={() => alert('Opening notification settings...')}>
Notifications
</Command.Item>
</Command.Group>
</Command.Page>
{/* Theme page */}
<Command.Page id="theme">
<Command.Group heading="Choose Theme">
<Command.Item onSelect={() => alert('Theme set to Light')}>
Light
</Command.Item>
<Command.Item onSelect={() => alert('Theme set to Dark')}>
Dark
</Command.Item>
<Command.Item onSelect={() => alert('Theme set to System')}>
System
</Command.Item>
</Command.Group>
</Command.Page>
</Command.List>
</Command>
</>
)
}
export default App Itens Desabilitados
Desabilitar itens específicos para prevenir seleção.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
const items = [
{ key: 'new-file', label: 'Create New File', group: 'Actions' },
{ key: 'new-folder', label: 'Create New Folder', group: 'Actions', disabled: true },
{ key: 'search', label: 'Search Files', group: 'Actions' },
{ key: 'delete', label: 'Delete Files', group: 'Danger', disabled: true },
];
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open Command Palette
</Button>
<Command
open={open}
onOpenChange={setOpen}
items={items}
placeholder="Some items are disabled..."
/>
</>
)
}
export default App Atalho Personalizado
Mudar o atalho de teclado global de ⌘K para outra tecla.
import { Command, Button, Space } from 'asterui'
import { useState } from 'react'
function App() {
const [open1, setOpen1] = useState(false);
const [open2, setOpen2] = useState(false);
const items = [
{ key: 'action1', label: 'Action 1' },
{ key: 'action2', label: 'Action 2' },
];
return (
<>
<Space direction="horizontal" size="sm">
<Button onClick={() => setOpen1(true)}>
Default (⌘K)
</Button>
<Button onClick={() => setOpen2(true)}>
Custom (⌘J)
</Button>
</Space>
<Command
open={open1}
onOpenChange={setOpen1}
items={items}
placeholder="Default shortcut ⌘K..."
/>
<Command
open={open2}
onOpenChange={setOpen2}
items={items}
shortcut={['j']}
placeholder="Custom shortcut ⌘J..."
/>
</>
)
}
export default App Filtro Personalizado
Fornecer uma função de filtro personalizada para lógica de correspondência avançada.
import { Command, Button } from 'asterui'
import { useState } from 'react'
function App() {
const [open, setOpen] = useState(false);
const items = [
{ key: 'apple', label: 'Apple', keywords: ['fruit', 'red'] },
{ key: 'banana', label: 'Banana', keywords: ['fruit', 'yellow'] },
{ key: 'carrot', label: 'Carrot', keywords: ['vegetable', 'orange'] },
{ key: 'date', label: 'Date', keywords: ['fruit', 'brown'] },
];
// Custom filter that only matches from the start of words
const customFilter = (value: string, search: string, keywords?: string[]) => {
const searchLower = search.toLowerCase();
if (value.toLowerCase().startsWith(searchLower)) return true;
return (keywords || []).some(k => k.toLowerCase().startsWith(searchLower));
};
return (
<>
<Button color="primary" onClick={() => setOpen(true)}>
Open (prefix matching only)
</Button>
<Command
open={open}
onOpenChange={setOpen}
items={items}
filter={customFilter}
placeholder="Type 'fr' for fruits..."
/>
</>
)
}
export default App Command
Seção intitulada “Command”| Propriedade | Descrição | Tipo | Padrão |
|---|---|---|---|
open | Estado aberto controlado | boolean | - |
onOpenChange | Callback quando o estado aberto muda | (open: boolean) => void | - |
defaultOpen | Estado aberto padrão (não controlado) | boolean | false |
items | Configuração de itens orientada a dados | CommandItemConfig[] | - |
filter | Função de filtro personalizada | (value: string, search: string, keywords?: string[]) => boolean | - |
loop | Loop de navegação por teclado nas bordas | boolean | true |
shortcut | Tecla(s) de atalho de teclado global | string[] | ['k'] |
placeholder | Placeholder do input de pesquisa | string | 'Type a command or search...' |
emptyMessage | Mensagem quando nenhum resultado é encontrado | ReactNode | 'No results found.' |
data-testid | ID de teste para testes | string | - |
CommandItemConfig
Seção intitulada “CommandItemConfig”Ao usar a prop items orientada a dados:
| Propriedade | Descrição | Tipo | Padrão |
|---|---|---|---|
key | Chave única para o item | string | - |
label | Rótulo de exibição | ReactNode | - |
group | Nome do grupo para categorização | string | - |
keywords | Palavras-chave de pesquisa adicionais | string[] | - |
disabled | Desabilitar o item | boolean | false |
onSelect | Callback quando o item é selecionado | () => void | - |
icon | Elemento de ícone para exibir | ReactNode | - |
data-testid | ID de teste para testes | string | - |
Componentes Compostos
Seção intitulada “Componentes Compostos”Para mais controle, use componentes compostos:
| Componente | Descrição |
|---|---|
Command.List | Contêiner para grupos e itens |
Command.Group | Agrupa itens com título opcional |
Command.Item | Item de comando selecionável |
Command.Empty | Mostrado quando nenhum resultado corresponde |
Command.Page | Página aninhada para navegação |
Command.Separator | Separador visual entre seções |
Acessibilidade
Seção intitulada “Acessibilidade”- Usa elemento
<dialog>nativo para captura de foco e tratamento de ESC - Input de pesquisa tem
role="combobox"com atributos ARIA apropriados - Lista usa
role="listbox"com itensrole="option" - Navegação por teclado com teclas de seta, Enter para selecionar, Escape para fechar
aria-activedescendantrastreia o item destacado- Grupos usam
role="group"comaria-label
Atalhos de Teclado
Seção intitulada “Atalhos de Teclado”⌘K/Ctrl+K- Abrir paleta de comandos (personalizável)↑↓- Navegar itensEnter- Selecionar item destacadoEscape- Fechar paleta (ou voltar na página aninhada)Backspace- Voltar para página anterior (quando a pesquisa está vazia)