shadcn.io is not affiliated with official shadcn/ui
Kbd examples · Pressed State
Focus State Kbd for React
A ⌘K shortcut with a focus ring around the Command key.
A tutorial about keyboard navigation sometimes needs to draw the focus ring itself. This React example puts ring-2 ring-ring ring-offset-2 ring-offset-background on the Command key of a ⌘K KbdGroup, the same ring the components use for keyboard focus. It's a picture of focus: the Kbd itself can't be focused.
Preview
Installation
FAQ
When do key caps need a focus state?
Only when the shortcut sits inside something focusable, such as a button or a menu item, and then the ring belongs on that control, not on the cap. Drawing it on the cap, as here, is for illustrations that explain focus.
What are the requirements for a focus indicator?
WCAG asks for a visible focus indicator on every focusable control, with enough contrast against its surroundings (3 to 1). A 2-pixel ring in the ring color with an offset, as here, meets that on the default themes.
How do focus and active states differ?
Focus says which control will receive the keyboard; active says which key is held. The Focus State example draws a ring around the cap, the Active State example fills it with color, and a real interface can show both at once.
Should focus always be visible or only when using the keyboard?
Only for keyboard focus: use focus-visible: classes on the control so a mouse click doesn't leave a ring behind. The components do this already.
Related
- Active State KbdA ⌘K shortcut whose Command key is filled
- Hover State Kbd⌘ and K keys that change to the accent
- Pressed Down KbdA ⌘K shortcut whose Command key sits
- Command Palette Shortcut KbdThe ⌘K shortcut that opens a command
- Disabled State KbdA faded Command key with a not-allowed
- Arrow Keys Cross Pattern KbdFour arrow keys in the inverted T of a