Command
A command palette component built with Dialog and Autocomplete for searching and executing commands.
<script lang="ts">
import { ArrowDown, ArrowUp, CornerDownLeft } from "@lucide/svelte";
import {
Command,
CommandDialog,
CommandDialogPopup,
CommandDialogTrigger,
CommandEmpty,
CommandFooter,
CommandGroup,
CommandGroupLabel,
CommandInput,
CommandItem,
CommandList,
CommandPanel,
CommandSeparator,
CommandShortcut,
Kbd,
KbdGroup,
} from "coss-svelte";
const groups = [
{
label: "Suggestions",
items: [
{ label: "Linear", shortcut: "⌘L", value: "linear" },
{ label: "Figma", shortcut: "⌘F", value: "figma" },
{ label: "Slack", shortcut: "⌘S", value: "slack" },
{ label: "YouTube", shortcut: "⌘Y", value: "youtube" },
{ label: "Raycast", shortcut: "⌘R", value: "raycast" },
],
},
{
label: "Commands",
items: [
{ label: "Clipboard History", shortcut: "⌘⇧C", value: "clipboard-history" },
{ label: "Import Extension", shortcut: "⌘I", value: "import-extension" },
{ label: "Create Snippet", shortcut: "⌘N", value: "create-snippet" },
{ label: "System Preferences", shortcut: "⌘,", value: "system-preferences" },
{ label: "Window Management", shortcut: "⌘⇧W", value: "window-management" },
],
},
];
let open = $state(false);
function handleKeydown(event: KeyboardEvent) {
if (event.key === "j" && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
open = !open;
}
}
</script>
<svelte:window onkeydown={handleKeydown} />
<CommandDialog bind:open>
<CommandDialogTrigger>
Open Command Palette
<KbdGroup>
<Kbd>⌘</Kbd>
<Kbd>J</Kbd>
</KbdGroup>
</CommandDialogTrigger>
<CommandDialogPopup>
<Command label="Command palette">
<CommandInput placeholder="Search for apps and commands..." />
<CommandList>
<CommandPanel>
<CommandEmpty>No results found.</CommandEmpty>
{#each groups as group, index}
{#if index > 0}
<CommandSeparator />
{/if}
<CommandGroup>
<CommandGroupLabel>{group.label}</CommandGroupLabel>
{#each group.items as item}
<CommandItem value={item.value} onclick={() => (open = false)}>
<span class="flex-1">{item.label}</span>
<CommandShortcut>{item.shortcut}</CommandShortcut>
</CommandItem>
{/each}
</CommandGroup>
{/each}
</CommandPanel>
</CommandList>
<CommandFooter>
<div class="flex items-center gap-4">
<div class="flex items-center gap-2">
<KbdGroup>
<Kbd><ArrowUp /></Kbd>
<Kbd><ArrowDown /></Kbd>
</KbdGroup>
<span>Navigate</span>
</div>
<div class="flex items-center gap-2">
<Kbd><CornerDownLeft /></Kbd>
<span>Open</span>
</div>
</div>
<div class="flex items-center gap-2">
<Kbd>Esc</Kbd>
<span>Close</span>
</div>
</CommandFooter>
</Command>
</CommandDialogPopup>
</CommandDialog>
Installation
Install the component package, shared coss-svelte theme, and Bits UI peer.
npm install coss-svelte @coss-svelte/theme bits-uiUsage
Import the Svelte component exports directly from the package.
import { Command, CommandCollection, CommandDialog, CommandDialogPopup, CommandDialogTrigger, CommandEmpty, CommandFooter, CommandGroup, CommandGroupLabel, CommandInput, CommandItem, CommandList, CommandPanel, CommandSeparator, CommandShortcut } from "coss-svelte";Anatomy
- Command
- CommandCollection
- CommandDialog
- CommandDialogPopup
- CommandDialogTrigger
- CommandEmpty
- CommandFooter
- CommandGroup
- CommandGroupLabel
- CommandInput
- CommandItem
- CommandList
- CommandPanel
- CommandSeparator
- CommandShortcut
API Reference
Command
A command palette component built with Dialog and Autocomplete for searching and executing commands.
| Prop | Type | Default | Description |
|---|---|---|---|
bind:value | string | "" | Current value for the component. Bind with `bind:value` when supported. |
items | CommandItem[] | [] | Items rendered by the component's built-in fallback composition. |
placeholder | string | "Type a command" | Placeholder text shown by the built-in input. |
label | string | "Command menu" | Accessible label or fallback visible label for the control. |
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.Root.
CommandCollection
Connects repeated items to the parent collection state.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.GroupItems.
CommandDialog
Composes the parent component into a dialog-style surface.
| Prop | Type | Default | Description |
|---|---|---|---|
bind:open | boolean | false | Open state for the popup or disclosure. Bind with `bind:open`. |
children : Snippet<[]>Inherits from Bits UI Dialog.Root.
CommandDialogPopup
Renders the floating or modal surface for the parent component.
| Prop | Type | Default | Description |
|---|---|---|---|
portalProps | { disabled?: boolean; to?: PortalTarget } | {} | Configures the exact Bits Portal target (`to`) or renders inline when `disabled`. |
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Dialog.Content.
CommandDialogTrigger
Renders the control that opens, closes, or selects related content.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Dialog.Trigger.
CommandEmpty
Renders fallback content when the collection has no results.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.Empty.
CommandGroup
Groups related items inside the parent component.
| Prop | Type | Description |
|---|---|---|
forceMount | boolean | Keeps content mounted while closed so transitions can complete. |
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.Group.
CommandGroupLabel
Labels a group of related items.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.GroupHeading.
CommandInput
Renders the input field for the parent component.
| Prop | Type | Default | Description |
|---|---|---|---|
bind:value | string | "" | Current value for the component. Bind with `bind:value` when supported. |
bind:ref : HTMLElement | nullInherits from Bits UI Command.Input.
CommandItem
Renders one selectable or repeated item.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.Item.
CommandList
Wraps a list of related items.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.List.
CommandPanel
Renders the main panel surface for the parent component.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.Viewport.
CommandSeparator
Visually separates related content.
children : Snippet<[]>bind:ref : HTMLElement | nullInherits from Bits UI Command.Separator.
CommandShortcut
Displays a keyboard shortcut hint.
children : Snippet<[]>Inherits from Svelte component attributes.
Implementation Details
- Status
- Stable
- Foundation
- compound
- Category
- Overlays & Popups
- Particles
- 2