implement
Kbd

Kbd

Keys, for documenting a shortcut.

Open searchK
Toggle the sidebarCtrlB
Then pressGthenH
import { Div, Span } from "@implementjs/core";
import { Kbd, KbdGroup } from "@/lib/components/ui/kbd";

export default function KbdDemo() {
	return Div(
		{ class: "flex flex-col items-center gap-3 text-sm" },
		Div(
			{ class: "flex items-center gap-2" },
			Span({ class: "text-muted-foreground" }, "Open search"),
			KbdGroup(Kbd("⌘"), Kbd("K")),
		),
		Div(
			{ class: "flex items-center gap-2" },
			Span({ class: "text-muted-foreground" }, "Toggle the sidebar"),
			KbdGroup(Kbd("Ctrl"), Kbd("B")),
		),
		Div(
			{ class: "flex items-center gap-2" },
			Span({ class: "text-muted-foreground" }, "Then press"),
			KbdGroup(Kbd("G"), Span({ class: "text-muted-foreground" }, "then"), Kbd("H")),
		),
	);
}

Installation

npx jsrepo add @implementjs/ui/kbd

Nothing else comes with it — this one stands alone on @implementjs/core and @implementjs/primitives.

Usage

import { Kbd, KbdGroup } from "@/lib/components/ui/kbd";

KbdGroup(Kbd("⌘"), Kbd("K"));

Kbd is one key. KbdGroup is a chord or a sequence read as one shortcut — it keeps the keys on the same line with an even gap.

Inside a tooltip

A shortcut is often shown next to what it does, in a tooltip. The muted grey a key uses on the page has no contrast on a tooltip's inverted panel, so Kbd detects that it is inside one and flips its own colors:

TooltipContent("Toggle the sidebar", KbdGroup(Kbd("⌘"), Kbd("B")));

Nothing to pass — the rule keys off [data-slot=tooltip-content] on an ancestor.

Sequences

For a two-step shortcut, put the connecting word between the keys rather than inside one:

KbdGroup(Kbd("G"), Span({ class: "text-muted-foreground" }, "then"), Kbd("H"));

API Reference

Kbd

One key, for documenting a shortcut. Renders a Kbd; extra props are forwarded onto it.

KbdGroup

A chord or a sequence: several keys read as one shortcut. Renders a Kbd; extra props are forwarded onto it.