~/components/kbd
Kbd
A keyboard key badge with a thick bottom edge, for single keys or combos like Ctrl + K.
- open command palette
- Ctrl+K
- show shortcuts
- Shift+?
- close
- Esc
- next / previous
- j/k
press ? for help
kbd.tsx
import { Fragment, type ReactNode } from "react";
export type KbdProps = {
/**
* The key, or the keys of a combo, e.g. "K" or ["Ctrl", "K"].
* Ignored when `children` is set.
*/
keys?: string | string[];
/** Custom content for a single key. Takes precedence over `keys`. */
children?: ReactNode;
/** Text shown between the keys of a combo. @default "+" */
separator?: ReactNode;
/** "sm" for inline hints in small text, "md" for body text. @default "md" */
size?: "sm" | "md";
/** Extra classes for the outer element. */
className?: string;
};
function cx(...parts: (string | false | undefined)[]): string {
return parts.filter(Boolean).join(" ");
}
const KEY =
"inline-flex items-center justify-center border border-b-2 border-mpk-line bg-mpk-bg font-mpk-mono leading-none text-mpk-ink";
const SIZE = {
sm: "min-w-[1.6em] px-1 py-0.5 text-[11px]",
md: "min-w-[1.8em] px-1.5 py-1 text-xs",
} as const;
/**
* A keyboard key badge in the mpkcn style: hairline border, thicker bottom edge,
* square corners, mono text. Pass an array to show a combo like Ctrl + K.
* Combos follow the HTML spec: one outer <kbd> wrapping one <kbd> per key.
*/
export function Kbd({ keys, children, separator = "+", size = "md", className }: KbdProps) {
const list = children !== undefined ? null : Array.isArray(keys) ? keys : keys !== undefined ? [keys] : [];
if (list === null || list.length === 1) {
return <kbd className={cx(KEY, SIZE[size], className)}>{list === null ? children : list[0]}</kbd>;
}
return (
<kbd className={cx("inline-flex items-center gap-1 font-mpk-mono text-mpk-dim2", size === "sm" ? "text-[11px]" : "text-xs", className)}>
{list.map((k, i) => (
<Fragment key={`${k}-${i}`}>
{i > 0 && <span>{separator}</span>}
<kbd className={cx(KEY, SIZE[size])}>{k}</kbd>
</Fragment>
))}
</kbd>
);
}Installation
terminal
npx mpkcn add kbd01Add mpkcn-theme.css if you haven't yet (see Installation)
02Copy the source into your components folder
components/kbd.tsx
import { Fragment, type ReactNode } from "react";
export type KbdProps = {
/**
* The key, or the keys of a combo, e.g. "K" or ["Ctrl", "K"].
* Ignored when `children` is set.
*/
keys?: string | string[];
/** Custom content for a single key. Takes precedence over `keys`. */
children?: ReactNode;
/** Text shown between the keys of a combo. @default "+" */
separator?: ReactNode;
/** "sm" for inline hints in small text, "md" for body text. @default "md" */
size?: "sm" | "md";
/** Extra classes for the outer element. */
className?: string;
};
function cx(...parts: (string | false | undefined)[]): string {
return parts.filter(Boolean).join(" ");
}
const KEY =
"inline-flex items-center justify-center border border-b-2 border-mpk-line bg-mpk-bg font-mpk-mono leading-none text-mpk-ink";
const SIZE = {
sm: "min-w-[1.6em] px-1 py-0.5 text-[11px]",
md: "min-w-[1.8em] px-1.5 py-1 text-xs",
} as const;
/**
* A keyboard key badge in the mpkcn style: hairline border, thicker bottom edge,
* square corners, mono text. Pass an array to show a combo like Ctrl + K.
* Combos follow the HTML spec: one outer <kbd> wrapping one <kbd> per key.
*/
export function Kbd({ keys, children, separator = "+", size = "md", className }: KbdProps) {
const list = children !== undefined ? null : Array.isArray(keys) ? keys : keys !== undefined ? [keys] : [];
if (list === null || list.length === 1) {
return <kbd className={cx(KEY, SIZE[size], className)}>{list === null ? children : list[0]}</kbd>;
}
return (
<kbd className={cx("inline-flex items-center gap-1 font-mpk-mono text-mpk-dim2", size === "sm" ? "text-[11px]" : "text-xs", className)}>
{list.map((k, i) => (
<Fragment key={`${k}-${i}`}>
{i > 0 && <span>{separator}</span>}
<kbd className={cx(KEY, SIZE[size])}>{k}</kbd>
</Fragment>
))}
</kbd>
);
}03Update the import paths to match your project
deps:none beyond react· tested with react@19.3.0
Usage
tsx
import { Kbd } from "@/components/mpkcn/kbd";
export function SearchHint() {
return (
<p>
Press <Kbd keys={["Ctrl", "K"]} /> to search.
</p>
);
}Kbd is a plain server-friendly component: no state, no effects, no "use client".
Examples
Single key
EscEnter ↵
tsx
<Kbd keys="Esc" />
<Kbd>Enter ↵</Kbd>Combos and separators
An array renders one badge per key. separator changes what goes between them, for example / for alternatives or then for sequences.
Ctrl+Shift+Pj/kgtheng
tsx
<Kbd keys={["Ctrl", "Shift", "P"]} />
<Kbd keys={["j", "k"]} separator="/" />
<Kbd keys={["g", "g"]} separator="then" />Inline in small text
Use size="sm" inside captions and footers.
tsx
<p className="font-mpk-mono text-[11px] text-mpk-dim2">
press <Kbd keys="?" size="sm" /> for shortcuts
</p>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| keys | string | string[] | — | The key, or the keys of a combo, e.g. "K" or ["Ctrl", "K"]. Ignored when children is set. |
| children | React.ReactNode | — | Custom content for a single key. Takes precedence over keys. |
| separator | React.ReactNode | "+" | Text shown between the keys of a combo. |
| size | "sm" | "md" | "md" | "sm" for inline hints in small text, "md" for body text. |
| className | string | — | Extra classes for the outer element. |
Accessibility
- Renders the semantic
<kbd>element. A combo is one outer<kbd>wrapping one<kbd>per key, as the HTML spec describes for key combinations. - The separator is real text, so a screen reader reads "Ctrl + K" rather than "Ctrl K".
- It is not interactive and takes no focus. If you want the badge to trigger the shortcut on click, wrap it in a
buttonwith a label.