Component
Command Button
Opens a command palette. A wide trigger with a search icon, placeholder text, and a kbd hint showing the platform shortcut. Wire `onOpen` to mount the palette; when `bindShortcut` is true, listens for the global shortcut (Cmd/Ctrl+K).
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/buttons/command-button/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Buttons/command-button React/Components/Buttons/command-button import { useEffect } from "react";
import type { ButtonHTMLAttributes } from "react";
/* DevSnips React — CommandButton
* Opens a command palette. Wide trigger + search icon + placeholder + kbd
* hint. bindShortcut listens for Cmd/Ctrl+K to call onOpen.
*/
export type ButtonSize = "xs" | "sm" | "md" | "lg" | "xl";
export type CommandVariant = "outline" | "secondary";
export interface CommandButtonProps
extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "onClick"> {
placeholder?: string;
shortcut?: string;
onOpen?: () => void;
variant?: CommandVariant;
size?: ButtonSize;
bindShortcut?: boolean;
}
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
const SIZES: Record<ButtonSize, string> = {
xs: "h-7 gap-1 px-2 text-xs [&_svg]:size-[14px]",
sm: "h-8 gap-1.5 px-3 text-xs [&_svg]:size-[14px]",
md: "h-9 gap-2 px-3.5 text-[13px] [&_svg]:size-4",
lg: "h-10 gap-2 px-4 text-[13px] [&_svg]:size-[18px]",
xl: "h-11 gap-2 px-5 text-sm [&_svg]:size-5",
};
const VARIANTS: Record<CommandVariant, string> = {
outline: "border-[var(--ds-color-border-strong)] bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
secondary: "border-[var(--ds-color-border)] bg-[var(--ds-color-secondary)] text-[var(--ds-color-secondary-foreground)] hover:bg-[var(--ds-color-surface-active)] active:bg-[var(--ds-color-surface-active)]",
};
const KBD =
"inline-flex items-center rounded-[var(--ds-radius-xs)] border border-[var(--ds-color-border)] " +
"bg-[var(--ds-color-surface-subtle)] px-1.5 py-0.5 font-mono text-[11px] leading-none " +
"text-[var(--ds-color-muted-foreground)]";
export function CommandButton({
placeholder = "Search or run a command…",
shortcut = "⌘K",
onOpen,
variant = "outline",
size = "md",
bindShortcut = true,
className,
type = "button",
...rest
}: CommandButtonProps) {
useEffect(() => {
if (!bindShortcut) return;
function onKey(e: KeyboardEvent) {
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
e.preventDefault();
onOpen?.();
}
}
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [bindShortcut, onOpen]);
return (
<button
type={type}
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
"w-full max-w-[420px] justify-between text-[var(--ds-color-muted-foreground)]",
className,
)}
onClick={onOpen}
{...rest}
>
<span className="inline-flex items-center gap-2">
<svg className="h-[1em] w-[1em] shrink-0" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<circle cx="11" cy="11" r="7" stroke="currentColor" strokeWidth="1.75" />
<path d="m21 21-4.3-4.3" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
<span>{placeholder}</span>
</span>
<kbd className={KBD} aria-hidden="true">{shortcut}</kbd>
</button>
);
}
export default CommandButton; /* DevSnips React — JavaScript parity build.
* Same API, behavior, and classes as code.tsx; TypeScript types removed.
* Regenerated from code.tsx — edit code.tsx and re-run the generator.
*/
import { useEffect } from "react";
function cx(...parts) {
return parts.filter(Boolean).join(" ");
}
const SIZES = {
xs: "h-7 gap-1 px-2 text-xs [&_svg]:size-[14px]",
sm: "h-8 gap-1.5 px-3 text-xs [&_svg]:size-[14px]",
md: "h-9 gap-2 px-3.5 text-[13px] [&_svg]:size-4",
lg: "h-10 gap-2 px-4 text-[13px] [&_svg]:size-[18px]",
xl: "h-11 gap-2 px-5 text-sm [&_svg]:size-5"
};
const VARIANTS = {
outline: "border-[var(--ds-color-border-strong)] bg-transparent text-[var(--ds-color-foreground)] hover:bg-[var(--ds-color-surface-hover)] active:bg-[var(--ds-color-surface-active)]",
secondary: "border-[var(--ds-color-border)] bg-[var(--ds-color-secondary)] text-[var(--ds-color-secondary-foreground)] hover:bg-[var(--ds-color-surface-active)] active:bg-[var(--ds-color-surface-active)]"
};
const KBD = "inline-flex items-center rounded-[var(--ds-radius-xs)] border border-[var(--ds-color-border)] bg-[var(--ds-color-surface-subtle)] px-1.5 py-0.5 font-mono text-[11px] leading-none text-[var(--ds-color-muted-foreground)]";
export function CommandButton({
placeholder = "Search or run a command\u2026",
shortcut = "\u2318K",
onOpen,
variant = "outline",
size = "md",
bindShortcut = true,
className,
type = "button",
...rest
}) {
useEffect(() => {
if (!bindShortcut) return;
function onKey(e) {
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
e.preventDefault();
onOpen?.();
}
}
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [bindShortcut, onOpen]);
return <button
type={type}
className={cx(
"inline-flex select-none items-center justify-center whitespace-nowrap rounded-[var(--ds-radius-sm)] border font-medium leading-none transition-colors duration-150 ease-out motion-reduce:transition-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:opacity-50",
VARIANTS[variant],
SIZES[size],
"w-full max-w-[420px] justify-between text-[var(--ds-color-muted-foreground)]",
className
)}
onClick={onOpen}
{...rest}
>
<span className="inline-flex items-center gap-2">
<svg className="h-[1em] w-[1em] shrink-0" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<circle cx="11" cy="11" r="7" stroke="currentColor" strokeWidth="1.75" />
<path d="m21 21-4.3-4.3" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" />
</svg>
<span>{placeholder}</span>
</span>
<kbd className={KBD} aria-hidden="true">{shortcut}</kbd>
</button>;
}
export default CommandButton; # Command Button
Opens a command palette. A wide trigger with a search icon, placeholder text, and a kbd hint showing the platform shortcut. Wire `onOpen` to mount the palette; when `bindShortcut` is true, listens for the global shortcut (Cmd/Ctrl+K).
## Installation
This component requires **React** and **Tailwind CSS**. Drop `code.tsx` (or `code.jsx` for JavaScript projects) into your project. Tailwind utility classes are included directly in the component, so no separate CSS file is required.
The component consumes the DevSnips semantic design tokens through Tailwind arbitrary values (for example `bg-[var(--ds-color-primary)]`). Define the `--ds-*` tokens once in your theme — see [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the full token spec.
## Usage
```tsx
<CommandButton onOpen={openPalette} placeholder="Search or run a command…" shortcut="⌘K" />
```
## JavaScript
A `code.jsx` build is provided for projects that ship plain JSX. It exposes the same API and behavior as `code.tsx` — only the TypeScript types are removed.
## Props
| Prop | Type | Default |
|---|---|---|
| `placeholder` | `string` | `"Search or run a command…"` |
| `shortcut` | `string` | `"⌘K"` |
| `onOpen` | `() => void` | — |
| `variant` | `outline \| secondary` | `outline` |
| `size` | `ButtonSize` | `md` |
| `bindShortcut` | `boolean` | `true` (listen for Cmd/Ctrl+K globally) |
Plus all native `ButtonHTMLAttributes<HTMLButtonElement>`.
## Variants
outline (default) · secondary. A wide trigger that reads as a search field; muted text + a kbd hint on the trailing edge.
## Sizes
xs (28px) · sm (32px) · **md (36px, default)** · lg (40px) · xl (44px). Horizontal padding scales 8 → 20px; icons scale 14 → 20px.
## States
default · hover · focus-visible · disabled (reduced opacity).
## Accessibility
Renders a native `<button>`. Focus-visible ring uses `color.focus-ring`. Loading sets `aria-busy` and disables to prevent double-submit. Disabled never removes the affordance (reduced opacity, not hidden). Meets the 44px touch target at lg/xl. The kbd hint is decorative (`aria-hidden`); the button's accessible name is the placeholder. When `bindShortcut` is on, Cmd/Ctrl+K calls `onOpen` so the palette is reachable without a visible click.
## Styling
Tailwind classes are included directly in the component and consume the DevSnips semantic design tokens (`--ds-*`) via arbitrary values. The button themes with the surface automatically in light and dark mode. No component-specific CSS file is needed.
## Design Tokens
See [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the authoritative token specification. This button uses the semantic color, radius, and motion tokens; define them once in your project theme and every button in the family stays in sync.
## Notes
Use once per app for a global command palette. The trigger is a button (not an input) — it opens the palette, which holds the real search input. 92 lines UTF-8 · LF · Spaces: 2
Continue browsing