Skip to content
shadcn.io

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.

Framework
UI library

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

Preview width
⌘K

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

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.

What Tab order makes sense for several shortcuts?

Kbd caps aren't in the Tab order at all, and shouldn't be: they're labels. The controls that contain them follow the DOM order, so lay them out in reading order.

How do screen readers announce focused shortcuts?

They announce the focused control, such as a button, along with its text, which includes the key caps inside it. For a dedicated shortcut hint, aria-keyshortcuts on the control tells assistive technology the shortcut too.