Actions and pages
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Command, type CommandGroup } from "@senvenli/react/overlay";
import { useState } from "react";
const GROUPS: CommandGroup[] = [
{
id: "actions",
title: "Actions",
items: [
{ id: "create-order", label: "Create order", icon: "plus", shortcut: "N", kind: "action" },
{ id: "send-invoice", label: "Send invoice", icon: "send", kind: "action" },
{ id: "invite-member", label: "Invite team member", icon: "user-plus", kind: "action", keywords: ["add user"] },
],
},
{
id: "pages",
title: "Pages",
items: [
{ id: "orders", label: "Orders", icon: "shopping-cart", kind: "page" },
{ id: "customers", label: "Customers", icon: "users", kind: "page" },
{ id: "settings", label: "Settings", icon: "settings", kind: "page" },
],
},
];
export default function Example() {
const [isOpen, setIsOpen] = useState(false);
const [chosen, setChosen] = useState("nothing yet");
return (
<Stack direction="horizontal" space="space-150">
<Button iconBefore="search" onPress={() => setIsOpen(true)}>
Search
</Button>
<Text tone="subtle">Chosen: {chosen}</Text>
<Command isOpen={isOpen} onOpenChange={setIsOpen} groups={GROUPS} onSelect={(item) => setChosen(item.label)} />
</Stack>
);
}import { Command } from "@senvenli/react/overlay";Examples
With ⌘K and recent items
The shortcut prop binds ⌘K (Ctrl K elsewhere); recent records show while the query is empty.import { Button } from "@senvenli/react/action";
import { Kbd, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Command, toast, type CommandGroup, type CommandItem } from "@senvenli/react/overlay";
import { useState } from "react";
const GROUPS: CommandGroup[] = [
{
id: "actions",
title: "Actions",
items: [
{ id: "create-invoice", label: "Create invoice", icon: "plus", shortcut: "I", kind: "action" },
{ id: "export", label: "Export customers", icon: "download", kind: "action" },
],
},
];
const RECENT: CommandItem[] = [
{ id: "inv-2041", label: "Analytical Engines Ltd", detail: "INV-2041", kind: "record", status: { label: "Paid", tone: "success" } },
{ id: "inv-2038", label: "Northwind Traders", detail: "INV-2038", kind: "record", status: { label: "Overdue", tone: "danger" } },
];
export default function Example() {
const [isOpen, setIsOpen] = useState(false);
return (
<Stack direction="horizontal" space="space-150">
<Button onPress={() => setIsOpen(true)}>Open the palette</Button>
<Text tone="subtle">
or press <Kbd isModifier /> <Kbd>K</Kbd>
</Text>
<Command
shortcut
isOpen={isOpen}
onOpenChange={setIsOpen}
groups={GROUPS}
recent={RECENT}
onSelect={(item) => toast(`Opened ${item.detail ?? item.label}`)}
/>
</Stack>
);
}Records from the server
The search prop finds records as people type; they show before the actions.import { Button } from "@senvenli/react/action";
import { Command, toast, type CommandGroup, type CommandItem } from "@senvenli/react/overlay";
import { useState } from "react";
const GROUPS: CommandGroup[] = [
{ id: "actions", title: "Actions", items: [{ id: "create-customer", label: "Create customer", icon: "user-plus", kind: "action" }] },
];
const CUSTOMERS: CommandItem[] = [
{ id: "ada", label: "Ada Lovelace", detail: "CUS-0101", icon: "user", kind: "record", keywords: ["ada@example.com"] },
{ id: "alan", label: "Alan Turing", detail: "CUS-0102", icon: "user", kind: "record", keywords: ["alan@example.com"] },
{ id: "grace", label: "Grace Hopper", detail: "CUS-0103", icon: "user", kind: "record", keywords: ["grace@example.com"] },
];
// Stands in for `fetch("/api/search?q=…", { signal })`.
async function search(query: string): Promise<CommandGroup[]> {
await new Promise((resolve) => setTimeout(resolve, 300));
const text = query.toLowerCase();
const items = CUSTOMERS.filter((customer) => [customer.label, customer.detail, ...(customer.keywords ?? [])].join(" ").toLowerCase().includes(text));
return items.length ? [{ id: "customers", title: "Customers", items }] : [];
}
export default function Example() {
const [isOpen, setIsOpen] = useState(false);
return (
<>
<Button iconBefore="search" onPress={() => setIsOpen(true)}>
Find a customer
</Button>
<Command isOpen={isOpen} onOpenChange={setIsOpen} groups={GROUPS} search={search} onSelect={(item) => toast(`Opened ${item.label}`)} />
</>
);
}Guidelines
When to use it
- An app with many pages and actions people want to reach by typing.
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Ctrl K / ⌘K | Opens and closes it when bound. |
| Arrow keys, Enter | Moves and runs the focused item. |
Built in
- Matching ignores case and accents.
- Announces "No results".
You provide
- Group names and item labels.
API reference
Command
| Prop | Type | Default | Description |
|---|---|---|---|
isOpen | boolean | – | |
defaultOpen | boolean | – | |
onOpenChange | (isOpen: boolean) => void | – | |
groupsRequired | readonly CommandGroup[] | – | Actions and pages, most used first; matched here, diacritic-insensitive. |
search | (query: string, signal: AbortSignal) => Promise<readonly CommandGroup[]> | – | Records found by the server (codes, order numbers, names, phones); shown before the groups. |
recent | readonly CommandItem[] | [] | Shown while the query is empty, above the five most used actions. |
onSelectRequired | (item: CommandItem) => void | – | |
shortcut | boolean | – | Bind ⌘K / Ctrl K to open and close it. |
isOpenbooleandefaultOpenbooleanonOpenChange(isOpen: boolean) => voidgroupsRequiredreadonly CommandGroup[]Actions and pages, most used first; matched here, diacritic-insensitive.
search(query: string, signal: AbortSignal) => Promise<readonly CommandGroup[]>Records found by the server (codes, order numbers, names, phones); shown before the groups.
recentreadonly CommandItem[]Default[]Shown while the query is empty, above the five most used actions.
onSelectRequired(item: CommandItem) => voidshortcutbooleanBind ⌘K / Ctrl K to open and close it.