suiss/ui · Component
Kbd
Displays a keyboard key or shortcut, with a platform-aware modifier symbol.
Kbd is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
Kbd renders a single key as a native kbd element in a small rounded box. Set mod to prepend the platform's primary modifier: ⌘ on Apple platforms, Ctrl elsewhere (Ctrl+ when the key has children).
Wrap several keys in KbdGroup to present a key combination. Use Kbd inline in text, in tooltips, or inside an input group addon to hint at a shortcut.
Installation
CLI
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Manual
-
Add the
cnhelper. -
Add the pieces it builds on:
use-platform. -
Copy the source into
components/ui/kbd.tsx:
- Update the import paths to match your project.
Usage
Pass the key label as children. Use mod instead of hard-coding ⌘ or Ctrl so the hint matches the user's platform. Icons placed inside Kbd are sized to size-3 unless they set their own size-* class.
Examples
Modifiers
The platform modifier alongside common modifier and named keys such as Shift, Option, Control, Esc and Tab.
Group
Key combinations wrapped in KbdGroup, both inline in a sentence and on their own.
With icon
Keys that use an icon instead of, or next to, a text label.
In input group
A shortcut hint placed in the trailing addon of a search input group.
API Reference
Kbd
Renders one key as a kbd element, optionally prefixed with the platform modifier.
| Prop | Type | Default | Description |
|---|---|---|---|
mod | boolean | - | Prepends the platform's primary modifier key: ⌘ on macOS and iOS, Ctrl on other platforms. |
Also accepts the props of React.ComponentProps<"kbd">.
KbdGroup
Groups several keys into a single inline key combination.
Also accepts the props of React.ComponentProps<"div">.
Accessibility
- Keys render as native
kbdelements, so the text is announced as keyboard input. KbdandKbdGroupare hidden below themdbreakpoint, so do not rely on them as the only place a shortcut is documented.Kbdsetspointer-events-none; clicks pass through to the surrounding control, as in an input group addon.
Notes
- Styling hooks:
data-slot="kbd"anddata-slot="kbd-group". - Platform detection reads
navigatorthroughuseSyncExternalStore; during server rendering the non-Apple modifier is used. - Inside an input group or tooltip content,
Kbdadapts its background and text colors automatically. - Uses the
muted,backgroundandmuted-foregroundcolors.