coss.com Svelte

Command

A command palette component built with Dialog and Autocomplete for searching and executing commands.

API Reference
Loading p-command-1 preview…

Installation

pnpm dlx shadcn-svelte@latest add https://coss-sv.vercel.app/r/command.json

Usage

<script lang="ts">
  import * as Command from "$lib/components/ui/command/index.js";
  import { buttonVariants } from "$lib/components/ui/button/index.js";
</script>
<script lang="ts">
  const items = [
    { value: "linear", label: "Linear" },
    { value: "figma", label: "Figma" },
    { value: "slack", label: "Slack" },
  ];
</script>

<Command.DialogRoot>
  <Command.DialogTrigger class={buttonVariants({ variant: "outline" })}>
    Open Command Palette
  </Command.DialogTrigger>

  <Command.DialogPopup aria-label="Command palette">
    <Command.Root {items}>
      <Command.Input placeholder="Search..." />
      <Command.Empty>No results found.</Command.Empty>
      <Command.List>
        <Command.Collection>
          {#snippet children(item)}
            <Command.Item value={item}>{item.label}</Command.Item>
          {/snippet}
        </Command.Collection>
      </Command.List>
    </Command.Root>
  </Command.DialogPopup>
</Command.DialogRoot>

API Reference

Command.Root

The root component that wraps the autocomplete functionality. It’s an alias for Autocomplete.Root with sensible defaults for command palette behavior: autoHighlight="always", keepHighlight={true}, and open={true}.

PropTypeDefaultDescription
itemsreadonly unknown[]-The array of items to display in the command
openbooleantrueControls whether the command is open
autoHighlightboolean \| "always""always"Controls automatic highlighting of items
keepHighlightbooleantrueWhether to maintain highlight state
childrenSnippet-Command content
...propsCommandRootProps-All Shards Autocomplete props are supported

Command.DialogRoot

A wrapper component that provides the dialog root functionality. It’s an alias for Dialog.Root from Shards UI.

PropTypeDescription
openbooleanControls whether the dialog is open
onOpenChange(open: boolean) => voidCalled when the open state changes
childrenSnippet<[{ payload: Payload \| undefined }]>Dialog content
...propsComponentProps<typeof Command.DialogRoot>All standard dialog props are supported

Command.DialogTrigger

The trigger button that opens the command dialog. Renders as a button by default.

PropTypeDescription
askeyof HTMLElementTagNameMapElement to render
classstringAdditional CSS classes to apply to the component
childrenSnippetTrigger content
...propsCommandDialogTriggerPropsAll standard dialog trigger attributes are supported

Command.DialogPopup

The popup content container that displays the command palette inside a dialog.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
portalPropsCommandDialogPopupProps["portalProps"]Props forwarded to the internal portal
childrenCommandDialogPopupProps["children"]Popup content
...propsCommandDialogPopupPropsAll standard dialog popup attributes are supported

Command.Input

The search input field with an integrated search icon. Automatically includes a search icon via startAddon and is sized to lg by default.

PropTypeDescription
placeholderstringThe placeholder text for the input
classstringAdditional CSS classes to apply to the component
startAddonSnippetReplaces the integrated search icon
...propsCommandInputPropsAll standard autocomplete input attributes are supported

Command.List

A scrollable container for command items. It wraps Autocomplete.List with scroll functionality.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetList content
...propsCommandListPropsAll standard autocomplete list attributes are supported

Command.Panel

A container component that provides styling for command content outside of dialogs. Useful when building standalone command interfaces with a bordered, elevated appearance.

PropTypeDescription
as"div" \| "kbd"Element to render
classstringAdditional CSS classes to apply to the component
childrenSnippetPanel content
...propsCommandPanelPropsAll standard element attributes are supported

Command.Empty

Displays a message when no results are found.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetEmpty-state content
...propsCommandEmptyPropsAll standard autocomplete empty attributes are supported

Command.Group

Groups related command items together. Wraps Autocomplete.Group.

PropTypeDescription
itemsreadonly unknown[]The array of items in this group
classstringAdditional CSS classes to apply to the component
childrenSnippetGroup content
...propsCommandGroupPropsAll standard autocomplete group attributes are supported

Command.GroupLabel

Displays a label for a command group.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetLabel content
...propsCommandGroupLabelPropsAll standard autocomplete group label attributes are supported

Command.Item

An individual selectable command item. Extends Autocomplete.Item.

PropTypeDescription
valueunknownThe value of the item
classstringAdditional CSS classes to apply to the component
childrenSnippetItem content
...propsCommandItemPropsAll standard autocomplete item attributes are supported

Command.Separator

A visual separator between command groups or items. Includes default vertical spacing with the my-2 class.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
...propsCommandSeparatorPropsAll standard autocomplete separator attributes are supported

Command.Shortcut

Displays keyboard shortcuts in a styled kbd element.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetShortcut text
...propsCommandShortcutPropsAll standard element attributes are supported

A footer section for displaying hints or additional keyboard shortcuts. Renders as a styled div with padding and border.

PropTypeDescription
classstringAdditional CSS classes to apply to the component
childrenSnippetFooter content
...propsCommandFooterPropsAll standard element attributes are supported

Command.Collection

Used within Command.Group to wrap items when using grouped data. It’s an alias for Autocomplete.Collection from the Autocomplete component.

PropTypeDescription
childrenSnippet<[unknown]>Renders each filtered item
...propsComponentProps<typeof Command.Collection>All standard autocomplete collection attributes are supported

Examples

With Keyboard Shortcut

You can add a keyboard shortcut to open the command palette:

<script lang="ts">
  let open = $state(false);

  function down(event: KeyboardEvent) {
    if (event.key === "k" && (event.metaKey || event.ctrlKey)) {
      event.preventDefault();
      open = !open;
    }
  }

  $effect(() => {
    document.addEventListener("keydown", down);
    return () => document.removeEventListener("keydown", down);
  });
</script>

<Command.DialogRoot bind:open>...</Command.DialogRoot>

With Grouped Items

<script lang="ts">
  const groupedItems = [
    {
      value: "Suggestions",
      items: [
        { value: "linear", label: "Linear" },
        { value: "figma", label: "Figma" },
      ],
    },
    {
      value: "Commands",
      items: [
        { value: "clipboard", label: "Clipboard History" },
        { value: "settings", label: "System Preferences" },
      ],
    },
  ];
</script>

<Command.Root items={groupedItems}>
  <Command.Input placeholder="Search..." />
  <Command.Empty>No results found.</Command.Empty>
  <Command.List>
    <Command.Collection>
      {#snippet children(group)}
        <Command.Group items={group.items}>
          <Command.GroupLabel>{group.value}</Command.GroupLabel>
          <Command.Collection>
            {#snippet children(item)}
              <Command.Item value={item.value}>{item.label}</Command.Item>
            {/snippet}
          </Command.Collection>
        </Command.Group>
        <Command.Separator />
      {/snippet}
    </Command.Collection>
  </Command.List>
</Command.Root>

Standalone Command (Without Dialog)

You can use the Command component without a dialog wrapper:

<Command.Root open {items}>
  <Command.Input placeholder="Type a command..." />
  <Command.Empty>No results found.</Command.Empty>
  <Command.List>
    <Command.Collection>
      {#snippet children(item)}
        <Command.Item value={item.value}>{item.label}</Command.Item>
      {/snippet}
    </Command.Collection>
  </Command.List>
</Command.Root>