mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-24 13:15:45 +00:00
126 lines
3.0 KiB
Plaintext
126 lines
3.0 KiB
Plaintext
---
|
|
title: Command
|
|
description: Fast, composable, unstyled command menu for React.
|
|
component: true
|
|
---
|
|
|
|
<ComponentExample src="/components/examples/command/demo.tsx" align="start">
|
|
<CommandDemo />
|
|
</ComponentExample>
|
|
|
|
## About
|
|
|
|
The `<Command />` component uses the [`cmdk`](https://cmdk.paco.me) component by [pacocoursey](https://twitter.com/pacocoursey).
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npx shadcn-ui add command
|
|
```
|
|
|
|
<Accordion type="single" collapsible>
|
|
<AccordionItem value="manual-installation">
|
|
<AccordionTrigger>Manual Installation</AccordionTrigger>
|
|
<AccordionContent>
|
|
|
|
1. Install the `cmdk` package:
|
|
|
|
```bash
|
|
npm install cmdk
|
|
```
|
|
|
|
2. The `<Command />` component depends on the `<Dialog />` component. Follow the [Dialog installation instructions](/docs/primitives/dialog#installation).
|
|
|
|
3. Copy and paste the following code into your project.
|
|
|
|
<ComponentSource src="/components/ui/command.tsx" />
|
|
|
|
<Callout>
|
|
This is the `<Command />` primitive. You can place it in a file at
|
|
`components/ui/command.tsx`.
|
|
</Callout>
|
|
|
|
</AccordionContent>
|
|
|
|
</AccordionItem>
|
|
</Accordion>
|
|
|
|
## Usage
|
|
|
|
```tsx
|
|
import {
|
|
CommandDialog,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList,
|
|
CommandSeparator,
|
|
CommandShortcut,
|
|
} from "@/components/ui/command"
|
|
```
|
|
|
|
```tsx
|
|
<Command>
|
|
<CommandInput placeholder="Type a command or search..." />
|
|
<CommandList>
|
|
<CommandEmpty>No results found.</CommandEmpty>
|
|
<CommandGroup heading="Suggestions">
|
|
<CommandItem>Calendar</CommandItem>
|
|
<CommandItem>Search Emoji</CommandItem>
|
|
<CommandItem>Calculator</CommandItem>
|
|
</CommandGroup>
|
|
<CommandSeparator />
|
|
<CommandGroup heading="Settings">
|
|
<CommandItem>Profile</CommandItem>
|
|
<CommandItem>Billing</CommandItem>
|
|
<CommandItem>Settings</CommandItem>
|
|
</CommandGroup>
|
|
</CommandList>
|
|
</Command>
|
|
```
|
|
|
|
## Examples
|
|
|
|
### Dialog
|
|
|
|
<ComponentExample src="/components/examples/command/dialog.tsx">
|
|
<CommandDialogDemo />
|
|
</ComponentExample>
|
|
|
|
To show the command menu in a dialog, use the `<CommandDialog />` component.
|
|
|
|
```tsx
|
|
export function CommandMenu() {
|
|
const [open, setOpen] = React.useState(false)
|
|
|
|
React.useEffect(() => {
|
|
const down = (e: KeyboardEvent) => {
|
|
if (e.key === "k" && e.metaKey) {
|
|
setOpen((open) => !open)
|
|
}
|
|
}
|
|
document.addEventListener("keydown", down)
|
|
return () => document.removeEventListener("keydown", down)
|
|
}, [])
|
|
|
|
return (
|
|
<CommandDialog open={open} onOpenChange={setOpen}>
|
|
<CommandInput placeholder="Type a command or search..." />
|
|
<CommandList>
|
|
<CommandEmpty>No results found.</CommandEmpty>
|
|
<CommandGroup heading="Suggestions">
|
|
<CommandItem>Calendar</CommandItem>
|
|
<CommandItem>Search Emoji</CommandItem>
|
|
<CommandItem>Calculator</CommandItem>
|
|
</CommandGroup>
|
|
</CommandList>
|
|
</CommandDialog>
|
|
)
|
|
}
|
|
```
|
|
|
|
## Combobox
|
|
|
|
You can use the `<Command />` component as a combobox. See the [Combobox](/docs/components/combobox) page for more information.
|