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.
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
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
-
Add the
cnhelper. -
Add the pieces it builds on:
use-platform. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a Kbd:
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. Icons inside Kbd are sized to size-3 unless they set their own size-* class.
In input group
A shortcut hint placed in the trailing addon of a search input group. Inside an input group or tooltip content, Kbd adapts its background and text colors.
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.
API Reference
Kbd
Renders one key as a kbd element, optionally prefixed with the platform modifier.
| Prop | Type | Default |
|---|---|---|
mod | boolean | - |
Also accepts the props of React.ComponentProps<"kbd">.
Pass the key label as children. mod prepends the platform's primary modifier: ⌘ on macOS and iOS, Ctrl on other platforms (Ctrl+ when the key has children). Use it instead of hard-coding ⌘ or Ctrl so the hint matches the user's platform. Platform detection reads navigator, so server rendering uses the non-Apple modifier.
KbdGroup
Groups several keys into a single inline key combination.
Also accepts the props of React.ComponentProps<"div">.
Both parts set a data-slot, kbd and kbd-group, for styling.