Core components / Badges & tags
Kbd
A key or shortcut in mono. Ochre shows keys where other systems show icons: esc on dialogs, ⌘ ↵ beside the confirming action. Raised in prose and tooltips, Flat inside menus and inputs; Key combo strings one to three keys together.
Code
The component itself, ready to drop into your project. Switch between React, Vue and plain HTML; every version uses the same Tailwind classes on Ochre's tokens.
import * as React from "react";
import { cx } from "@/components/ochre/utils";
export type KbdSize = "s" | "m" | "l";
export type KbdVariant = "raised" | "flat";
const sizes: Record<KbdSize, string> = {
s: "h-[18px] min-w-[18px] rounded-4 px-1 text-[11px] leading-4",
m: "h-5 min-w-5 rounded-6 px-1.5 text-[12px] leading-4",
l: "h-6 min-w-6 rounded-6 px-1.5 text-[13px] leading-[18px]",
};
// Raised in prose and tooltips: a card-coloured key with an edge. Flat inside menus and inputs: a muted chip.
const variants: Record<KbdVariant, string> = {
raised: "border-border-default bg-surface-card shadow-xs",
flat: "border-transparent bg-surface-muted",
};
export type KbdProps = React.HTMLAttributes<HTMLElement> & { size?: KbdSize; variant?: KbdVariant; pressed?: boolean };
// A key or shortcut, in mono. Pass several children for a combination: <Kbd>⌘</Kbd><Kbd>K</Kbd>.
// pressed sinks the key a pixel and drops its shadow, for showing a shortcut as it's used.
export function Kbd({ size = "m", variant = "raised", pressed = false, className, ...rest }: KbdProps) {
return (
<kbd
{...rest}
data-pressed={pressed || undefined}
className={cx(
"inline-flex items-center justify-center border font-mono font-medium text-text-muted transition-ui",
"data-[pressed]:translate-y-px data-[pressed]:bg-surface-muted data-[pressed]:text-text-strong data-[pressed]:shadow-none",
variants[variant],
sizes[size],
className,
)}
/>
);
}
export function KbdGroup({ className, ...rest }: React.HTMLAttributes<HTMLSpanElement>) {
return <span {...rest} className={cx("inline-flex items-center gap-1", className)} />;
}
export type KeyComboProps = Omit<React.HTMLAttributes<HTMLElement>, "children"> & {
// One to three keys, in the order they're pressed: ["⌘", "⇧", "K"].
keys: React.ReactNode[];
size?: KbdSize;
variant?: KbdVariant;
// Keys currently held, by index, so each one sinks as it's pressed.
pressed?: number[];
};
// A shortcut of one to three keys. A <kbd> of <kbd>s, which is how HTML spells a key combination.
export function KeyCombo({ keys, size = "m", variant = "raised", pressed, className, ...rest }: KeyComboProps) {
return (
<kbd {...rest} className={cx("inline-flex items-center gap-1 font-mono", className)}>
{keys.map((key, i) => (
<Kbd key={i} size={size} variant={variant} pressed={pressed?.includes(i)}>
{key}
</Kbd>
))}
</kbd>
);
}
Installation
With the @ochre registry set up, one command adds the component and what it imports (utils). Or copy the code above into components/ochre/kbd.tsx.
npx shadcn@latest add @ochre/kbdExamples
Raised and flat
Key combos of one, two and three keys in both styles; Flat sits inside a menu.
- Duplicate⌘D
- Move to project⌘⇧M
- ArchiveE
import { Kbd, KeyCombo } from "@/components/ochre/kbd";
const menu = [
{ label: "Duplicate", keys: ["⌘", "D"] },
{ label: "Move to project", keys: ["⌘", "⇧", "M"] },
{ label: "Archive", keys: ["E"] },
];
export default function KbdStyles() {
return (
<div className="flex flex-wrap items-start justify-center gap-10">
<div className="grid gap-3">
<div className="flex items-center gap-3">
<KeyCombo keys={["K"]} />
<KeyCombo keys={["⌘", "K"]} />
<KeyCombo keys={["⌘", "⇧", "P"]} />
</div>
<div className="flex items-center gap-3">
<KeyCombo keys={["K"]} variant="flat" />
<KeyCombo keys={["⌘", "K"]} variant="flat" />
<KeyCombo keys={["⌘", "⇧", "P"]} variant="flat" />
</div>
<div className="flex items-center gap-3">
<Kbd size="s">⌘K</Kbd>
<Kbd size="m">⌘K</Kbd>
<Kbd size="s" variant="flat">⌘K</Kbd>
<Kbd size="m" variant="flat">⌘K</Kbd>
</div>
</div>
<ul className="w-56 rounded-12 border border-border-default bg-surface-card p-1 text-[14px] leading-5 text-text-default">
{menu.map((item) => (
<li key={item.label} className="flex items-center justify-between gap-3 rounded-8 px-2.5 py-1.5 hover:bg-state-hover">
{item.label}
<KeyCombo keys={item.keys} variant="flat" size="s" />
</li>
))}
</ul>
</div>
);
}
Pressed
Keys sink a pixel while their shortcut is held.
Duplicate⇧DTry it: hold Shift and press D.
"use client";
import { useEffect, useState } from "react";
import { Kbd, KbdGroup } from "@/components/ochre/kbd";
// Hold Shift and press D: the keys sink as you use them.
export default function KbdLive() {
const [down, setDown] = useState<Set<string>>(new Set());
useEffect(() => {
const key = (e: KeyboardEvent) => (e.key === "Shift" ? "shift" : e.key.toLowerCase());
const on = (e: KeyboardEvent) => setDown((d) => new Set(d).add(key(e)));
const off = (e: KeyboardEvent) =>
setDown((d) => {
const next = new Set(d);
next.delete(key(e));
return next;
});
const clear = () => setDown(new Set());
window.addEventListener("keydown", on);
window.addEventListener("keyup", off);
window.addEventListener("blur", clear);
return () => {
window.removeEventListener("keydown", on);
window.removeEventListener("keyup", off);
window.removeEventListener("blur", clear);
};
}, []);
return (
<p className="flex flex-wrap items-center gap-2 text-[14px] text-text-muted">
Duplicate
<KbdGroup>
<Kbd size="l" pressed={down.has("shift")}>⇧</Kbd>
<Kbd size="l" pressed={down.has("d")}>D</Kbd>
</KbdGroup>
<span className="text-text-subtle">Try it: hold Shift and press D.</span>
</p>
);
}
Motion
Built on Ochre's ease-out curves. With reduced motion switched on, everything still works, it just doesn't move.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "s" | "m" | "l" | "m" | 18, 20 or 24 pixels high. |
| variant | "raised" | "flat" | "raised" | Raised with an edge and a shadow, or a flat muted chip. |
| children | ReactNode | – | The key: ⌘, ⇧, ↵, esc, K. |
| pressed | boolean | false | Sinks the key a pixel and drops its shadow, while its shortcut is held. |
| keys (KeyCombo) | ReactNode[] | – | Key combo: one to three keys in the order they're pressed. Takes size and variant too. |
| pressed (KeyCombo) | number[] | – | Indexes of the keys being held, so each sinks on its own. |
Usage
Use it for
- Shortcut hints in menus, inputs and tooltips.
- The esc key that closes a dialog or panel.
- Flat inside menus and inputs, where a raised key would be too loud.
Avoid
- Words longer than a key name. Use a Badge for labels.