shadcn.io is an independent project built on shadcn/ui
On this page9 sections
Kbd draws a key as a small cap, the way a shortcut is printed in a menu or a help page: ⌘ K beside a search field, a return key on a dialog's confirm button. It only shows the key. Listening for the shortcut is your code's job, and the cap takes no clicks or focus.
To list many shortcuts with what each one does, use a Keyboard Table.
Installation
Anatomy

Usage
import { Kbd, KbdGroup } from "@/components/ui/kbd";Kbd is one key. KbdGroup keeps the keys of a chord together on one line, along with anything you put between them, such as a plus sign.
<p>
Press <Kbd>/</Kbd> to search, or{" "}
<KbdGroup>
<Kbd>Ctrl</Kbd>
<span>+</span>
<Kbd>K</Kbd>
</KbdGroup>{" "}
for commands.
</p>Accessibility
Kbd and KbdGroup render a <kbd> element, the HTML for keyboard input, which has no role, so a screen reader reads the keys as part of the text around them. A cap ignores the pointer and takes no focus. KbdGroup sets dir to ltr, so a chord keeps the order it's pressed in on a right-to-left page.
Inside a DropdownMenuItem, React Aria makes a Kbd or KbdGroup the item's description and hides it from the item's name, so a screen reader hears Copy, then Ctrl C. React Aria's Button and DropdownMenuItem drop aria-keyshortcuts, so on a button the cap's own text is what tells a screen reader about the shortcut.
Demos
Group
A line of help text with Ctrl + B and Ctrl + K as two caps in one KbdGroup, so they stay together inside the sentence. Each cap holds a whole chord, plus sign included, rather than one key per cap. Use a group like this for shortcuts in running text, and put a word between chords that are alternatives.
Button
An outline Accept button with a return-key cap after its label, marked data-icon="inline-end" so the button trims its end padding as it does for a trailing icon. The cap stays readable, since React Aria's Button drops aria-keyshortcuts. Put the key on the button when that button is what the shortcut runs.
Tooltip
Save and Print are plain buttons, and their shortcuts live in the tooltips: S after Save Changes, and Ctrl and P as a KbdGroup after Print Document. Move the key into a Tooltip when the button's label should stay short, and keep it on the button for a command people use all the time.
Dropdown menu
An Edit menu with Undo, Redo, Copy and Paste, each with its chord in a KbdGroup pushed to the inline end with ms-auto. React Aria makes the group the item's description and hides it from the name, so a screen reader hears Copy, then Ctrl C. Use caps like these in any menu whose commands have shortcuts.
Input group
A search field with a magnifying glass at the start and ⌘ and K as two caps in an InputGroupAddon at the inline end. Use that slot when the shortcut focuses the field or opens a command palette. Keep the placeholder for what to type, since it disappears with the first character.
RTL
The modifier caps and Ctrl + B from the first preview, in a container with dir set to rtl. KbdGroup sets dir to ltr on its own element, so both rows keep keyboard order inside the right-to-left container, ⌘ first and Ctrl + B as written. Set dir to rtl on a KbdGroup to have its caps follow the page.
FAQ
Does Kbd listen for the keyboard shortcut it shows?
No. Kbd renders a <kbd> with your text in it and nothing else: no listener and no focus, and pointer-events-none means it can't even take a click. Register the shortcut yourself, with a keydown listener on the document or the hotkeys library your app already uses.
Put aria-keyshortcuts on the control the keys trigger, too, so screen reader users learn the shortcut exists. Keep each shortcut's keys in one place, so the cap and the listener can't disagree.
How do I show a key combination like Ctrl + K with Kbd?
Put one Kbd per key inside a KbdGroup, with or without a plus sign between them, as the Tooltip demo does with Ctrl and P. Separate caps look like the keys people press. For running text, a whole chord can also be the text of one Kbd, as in the Group demo.
Either way, KbdGroup keeps the chord on one line, so a narrow column never wraps Ctrl onto one line and K onto the next.
Does a Kbd inside a button become part of the button's name?
Yes. A button is named by the text inside it, so the Accept button in the Button demo is named Accept followed by the return symbol, and a screen reader says both every time focus lands on it.
React Aria's Button drops aria-keyshortcuts, so hiding the cap would leave the shortcut unsaid. Keep it readable on a button. In a DropdownMenuItem the cap is hidden for you and read as the item's description instead, as the Dropdown menu demo shows.
How do I add a border to a Kbd key cap?
On React Aria, Kbd has no variant prop, so add border border-border to the class of the one Kbd that needs it. To give every key a border, add those classes to the cap in components/ui/kbd, which is your code once installed.
Where should a Kbd go: on the button, in a tooltip or in a menu?
Put it on the button when that button runs the shortcut and its label has room, as the Button demo does. When the button has to stay compact, as in an icon toolbar, move the key into a Tooltip the way the Tooltip demo does. In a Dropdown Menu, set it at the end of each item, which is where people look for a menu command's shortcut, as the Dropdown menu demo does.
For a shortcut that focuses a search field or opens a command palette, put it inside the field, as the Input group demo does.
How do I show ⌘ on Mac and Ctrl on Windows with Kbd in React?
Pick the label in your own code, since Kbd prints exactly what you give it. Keep both spellings of each shortcut, check the reader's platform, and render ⌘ K or Ctrl K. Linux and Windows keyboards both say Ctrl, Alt and Shift.
In a server-rendered app, the server can't know the reader's keyboard. Choose the label after the page hydrates, or the first client render won't match the server's markup.
How do I change the size of a Kbd or put an icon in it?
Pass a class to one Kbd for a one-off. The Keyboard Table uses h-6 min-w-6 px-1.5 for slightly larger caps. To change every key on your site, edit the cap's classes in components/ui/kbd, which is your code once installed.
A cap centers whatever it holds, so an icon from your icon set works in place of a glyph. It is drawn at the cap's small icon size unless it has a size- class of its own.
API Reference
Kbd
data-slot="kbd"
className?string
Merged with the part's classes.
KbdGroup
className?string
Merged with the part's classes.












