Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

Focus State Kbd for Svelte

A ⌘K group with a focus ring drawn on the Command cap. The ring is a picture of focus, and the cap cannot be focused.

UI library: Ark UI

The ⌘ cap has a 2 pixel ring and an offset. K does not. The cap is not in the tab order, and it ignores the pointer.

Draw the ring on the control that can be focused, such as a Button. The cap is only a label. In Svelte, Ark UI does not put these caps in the tab order.

Preview

Preview width

Installation

MCP · Pro
Make your AI a shadcn expert
Your agent installs components, builds pages and restyles your app from one prompt, in Claude Code, Cursor or VS Code.
Part of Pro

Anatomy

Kbd anatomy: kbd, group

Usage

<div class="rounded-md border bg-background p-4"><KbdGroup><Kbd class="ring-2 ring-ring ring-offset-2 ring-offset-background">⌘</Kbd><Kbd>K</Kbd></KbdGroup></div>

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.

API Reference

Kbd

No extra props in this demo. The rest is on the Kbd page.