Command
Installation
npx @mielui/svelte add commandyarn dlx @mielui/svelte add commandbunx @mielui/svelte add commandUsage
Compose Trigger and Content inside Root, with Search and Results in Content. Content is a modal dialog that manages focus and closes on Escape. Search updates the matching results when items change, including while a query is active.
Search composes native input and keyboard handlers; preventDefault in onkeydown cancels command navigation or activation. Enter during text composition confirms the text without running a command. Item forwards data attributes to its button or link.
<script lang="ts">
import * as Command from '@mielui/svelte/components/command';
</script>
<Command.Root>
<Command.Trigger>Open palette</Command.Trigger>
<Command.Content>
<Command.Header>Workspace</Command.Header>
<Command.Search />
<Command.Results>
<Command.Item name="New project">New project</Command.Item>
<Command.Item name="Settings" href="/settings">Settings</Command.Item>
</Command.Results>
</Command.Content>
</Command.Root>Search and empty-state slots
Search accepts icon, count(total) and announcement(message) snippets. The count is decorative; announcement stays inside the shared polite live region. Results accepts an empty snippet. Omit any slot to retain its default, or supply an empty snippet to hide its content. These slots use the same filtered result count and announcement as the built-in rendering.
<Command.Search placeholder="Find a command">
{#snippet icon()}
<span aria-hidden="true">โ</span>
{/snippet}
{#snippet count(total)}
{total} matches
{/snippet}
{#snippet announcement(message)}
{message}
{/snippet}
</Command.Search>
<Command.Results>
{#snippet empty()}
<p>Try a different search.</p>
{/snippet}
<Command.Item name="Open settings">Open settings</Command.Item>
</Command.Results>Examples
Workspace palette
A search field opens a palette with a header, three groups, and a shortcut hint on one item.
No command selected
With groups
No command selected
Glass surface
Set surface="glass" on Content for a translucent panel with blur, or surface="solid" for an opaque one. Without surface, the panel follows the theme. Glass falls back to an opaque panel when backdrop filtering is unavailable or reduced transparency is on.
Controlling the palette
Bind open on Root to show the palette from a keyboard shortcut or a menu item, and use onOpenChange to hear when it opens or closes. A press outside closes it unless you set allowClickOutside={false} on Content. labels.palette on Root is the palette's accessible name and defaults to Command palette.
Search filters with fuzzy matching. threshold sets how forgiving it is, from 0 for an exact match to 1 for nearly anything, and defaults to 0.2.
Nothing picked yet
Trigger and items
Trigger is built on Button. disabled blocks it, loading with labels shows progress, element binds the DOM node, unstyled removes the Button classes, and onclick runs before the palette opens.
Give an Item callback to run an action, or href to navigate. The palette closes after either, so do not add a second click handler that repeats the action. onclick runs right after callback, name is the text the search matches, and value identifies the item when names repeat. disabled keeps it visible but unselectable.
Root, Content, Header, Results, Group, Item, Separator and Trigger render their children and accept class and style like any element.
Labels
Built-in text is English. Pass a CommandLabels object to labels on Root to translate the palette's name, the search placeholder, the names of the search input and the results list, the empty message, and the result count announced to screen readers. available is a function of the count so the number can sit anywhere in the sentence. Every key is optional, and an omitted key keeps its English default. A placeholder on Search wins over labels.searchPlaceholder.
<script lang="ts">
import * as Command from '@mielui/svelte/components/command';
import type { CommandLabels } from '@mielui/svelte/components/command';
const labels: CommandLabels = {
palette: 'Palette de commandes',
searchPlaceholder: 'Rechercher une commandeโฆ',
search: 'Rechercher',
results: 'Commandes',
empty: 'Aucun rรฉsultat',
available: (count) => {
return count === 0 ? 'Aucune commande.' : `${count} commandes disponibles.`;
}
};
</script>
<Command.Root {labels}>
<Command.Trigger>Ouvrir la palette</Command.Trigger>
<Command.Content>
<Command.Search />
<Command.Results>
<Command.Item name="Nouveau projet">Nouveau projet</Command.Item>
</Command.Results>
</Command.Content>
</Command.Root>API reference
Command.Root
Provides command menu state.
labelsCommandLabelsOverrides the built-in text and accessible names. Every key is optional and English is the fallback.
openBindablebooleanWhether it is open. Bind it to control the state from outside.
Default
falseonOpenChange((open: boolean) => void)Called with the new state whenever it opens or closes.
childrenSnippet<[]>Content rendered inside.
Command.Content
Contains command menu controls.
surface"solid" | "glass"Surface treatment. Omit it to inherit
--mielui-surfacefrom the theme.childrenSnippet<[]>Content rendered inside.
classstringClasses added to the element.
allowClickOutsidebooleanCloses when the pointer is pressed outside it. Set false to keep it open until it is dismissed another way.
Default
true
Command.Group
Groups command items.
childrenRequiredSnippet<[]>Content rendered inside.
classstringClasses added to the element.
headingRequiredstringHeading shown above the group.
Command.Header
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
Command.Item
Defines a command action.
namestringText the search matches.
valuestringIdentifier for the item when names repeat.
callback(() => void)Called when the item is chosen.
disabledbooleanPrevents interaction and dims the control.
Default
falsehrefstringNavigates to this address when the item is chosen.
onclick(() => void)Called when it is clicked or activated.
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
Command.Results
Lists matching commands.
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
emptySnippet<[]>Content shown when nothing matches.
Command.Search
Filters command items.
thresholdnumberHow loosely typed text may match, from 0 for exact to 1 for anything.
Default
DEFAULT_COMMAND_SEARCH_THRESHOLDiconSnippet<[]>Replaces the search icon.
countSnippet<[count: number]>Renders the result count.
announcementSnippet<[message: string]>Renders the screen reader announcement.
Command.Separator
Separates command menu items.
childrenSnippet<[]>Content rendered inside.
classstringClasses added to the element.
Command.Trigger
Opens the command menu.
disabledbooleanPrevents activation. A disabled link loses its destination and is skipped by Tab.
variantButtonVariantVisual style. Choose it for what the action means, not for decoration.
size"sm" | "md" | "lg" | "icon"Control height.
iconis a square button for a single icon.childrenSnippet<[]>Content rendered inside.
elementHTMLButtonElement | HTMLAnchorElementBindable reference to the rendered DOM element. Type is the union of the two possible element types -- narrow at the use site:
unstyledbooleanSkip the variant/size base classes and render with
classalone.statusButtonStatusControlled visual state. Loading remains focusable and refuses activation.
loadingbooleanConvenience alias for
status="loading".labelsButtonLabelsOverrides the built-in text and accessible names. Every key is optional and English is the fallback.
onclick((event: MouseEvent) => void)Called when the button is activated. Loading and disabled buttons do not call it.
onkeydown((event: KeyboardEvent) => void)Called on key down, before the button handles the key itself.
hrefstringRenders a link to this address instead of a button.
pnpm dlx @mielui/svelte add command