When to use it#
- Use it in help text, instructions and shortcut lists to show a key the user presses:
Tab,Esc,CtrlandC. - One key per Kbd. Show a combination by nesting them.
- Say what the key does in words: the key is never the only instruction.
- Not for something users can press: a key drawn as text isn’t a control. Use a Button.
Example#
You can move between the fields with Tab.
import { Kbd } from '@kvirn-ui/react'
import { useKbdTexts } from './texts.ts'
export function DefaultKbd() {
const { texts } = useKbdTexts()
return (
<p>
{texts.moveBetweenFields} <Kbd>Tab</Kbd>.
</p>
)
}Use cases#
Two keys pressed together
An outer Kbd groups one Kbd per key, with the separator as plain text. The default theme draws the keys and leaves the outer one plain, and the group never breaks across lines.
Copy the text with Ctrl+C.
import { Kbd } from '@kvirn-ui/react'
import { useKbdTexts } from './texts.ts'
export function Combination() {
const { texts } = useKbdTexts()
return (
<p>
{texts.copy}{' '}
<Kbd>
<Kbd>Ctrl</Kbd>+<Kbd>C</Kbd>
</Kbd>
.
</p>
)
}Information: Did you know?
Don’t translate key names: write them as printed on the keyboard. In Nordic text, Ctrl, Shift, Tab and Esc need nothing more.
A list of shortcuts
Each item names the key or keys, then says what they do in words. Give the list a name, so a screen reader user hears what the list is for.
Shortcuts in the editor
- Ctrl+S: Save the draft
- Ctrl+Z: Undo the last change
- Esc: Close the dialog
import { Kbd } from '@kvirn-ui/react'
import { useId } from 'react'
import { useKbdTexts } from './texts.ts'
export function ShortcutList() {
const { texts } = useKbdTexts()
const titleId = useId()
return (
<div>
<p id={titleId}>{texts.shortcuts}</p>
<ul aria-labelledby={titleId}>
<li>
<Kbd>
<Kbd>Ctrl</Kbd>+<Kbd>S</Kbd>
</Kbd>
: {texts.save}
</li>
<li>
<Kbd>
<Kbd>Ctrl</Kbd>+<Kbd>Z</Kbd>
</Kbd>
: {texts.undo}
</li>
<li>
<Kbd>Esc</Kbd>: {texts.closeDialog}
</li>
</ul>
</div>
)
}Information: Did you know?
A plain <kbd> inside a Prose gets the same look as a Kbd, so text you don’t write in React, such as Markdown, needs no component.
A shortcut that differs on a Mac
A key can have a different name on another system: Ctrl on Windows is Cmd on a Mac. Write both, and say in words which system each is for.
Print your application with Ctrl+P. On a Mac, use Cmd+P.
import { Kbd } from '@kvirn-ui/react'
import { useKbdTexts } from './texts.ts'
export function PerSystem() {
const { texts } = useKbdTexts()
return (
<p>
{texts.printApplication}{' '}
<Kbd>
<Kbd>Ctrl</Kbd>+<Kbd>P</Kbd>
</Kbd>
. {texts.onAMac}{' '}
<Kbd>
<Kbd>Cmd</Kbd>+<Kbd>P</Kbd>
</Kbd>
.
</p>
)
}Information: Did you know?
A Swedish Mac prints Retur on the key a Windows keyboard calls Enter. Write the name your users have on their keyboards.
A key in a text in another script
Key names stay as printed on the keyboard, in Latin letters. In a text in Arabic, wrap the key in an element that says it is English, so a screen reader says it in the right language.
اضغط على Tab للانتقال بين الحقول.
import { Kbd } from '@kvirn-ui/react'
import { useKbdTexts } from './texts.ts'
export function OtherScript() {
const { texts } = useKbdTexts()
return (
<p lang="ar" dir="rtl">
{texts.arabicBefore}{' '}
<span lang="en">
<Kbd>Tab</Kbd>
</span>{' '}
{texts.arabicAfter}
</p>
)
}Warning: Don't forget
Mark the key with lang="en" on your own element around the Kbd. Without it, a screen reader may say the key in the wrong language (3.1.2 Language of Parts).
Accessibility#
What it does for you#
- APG pattern: none. Kbd is inline text, not a widget.
- Deviations: none
- Native elements used:
<kbd>.asaccepts onlykbd(allowed elements below).
Kbd is the native <kbd> element with a class, so the theme can draw it as a key. It adds no role, ARIA, text or behaviour, so what a user perceives is the text inside it, in the flow of the sentence.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Kbd | <kbd> → no role | none | class="kv-kbd". One key per Kbd. Attributes (id, aria-*) and the ref reach the element unchanged. The key name is the content: Tab, Esc, Ctrl |
| Kbd | nested <kbd> | none | A combination is an outer Kbd around one Kbd per key, with the separator as text: <Kbd><Kbd>Ctrl</Kbd>+<Kbd>C</Kbd></Kbd>. The theme draws the innermost keys and leaves the outer one plain |
| Kbd | as | none | kbd is the only value. Any other value, samp included, is a type error and, in JavaScript, warns once in development and renders the default element. className joins, the ref gets the element |
| Kbd | never | no role, tabindex | No click handler, no live region. A key name is text, so screen readers read it as part of the sentence, and the theme draws no content (no generated text, no icons) that would be skipped or read twice |
It announces nothing.
What you need to do#
- Name the key as printed on the keyboard, and say what it does in words.
Tabis the same in every locale, so it is not translated. The sentence around it says what the key is for, and the key is never the only instruction (1.3.3 Sensory Characteristics (A): "press the key in the box" is not enough). - Language. Key names that are the surrounding language's vernacular (Ctrl, Shift, Tab, Esc in Nordic text) need nothing. A key in another script (Arabic text, say) goes in your own
<span lang="en"><Kbd>Tab</Kbd></span>(3.1.2 Language of Parts (AA)). - Name the keys your users have. A key name differs between keyboards:
CtrlisCmdon macOS, andEnterisReturon a Swedish Mac. Name what your users have. - A character shortcut needs a way to turn it off or remap it (2.1.4 Character Key Shortcuts (A)). Kbd only documents a shortcut: it doesn't make one.
Warning: Don't forget
If your service has a shortcut of a single letter or number, let people turn it off or change it (2.1.4 Character Key Shortcuts). Kbd only shows a shortcut. It doesn’t make one work.
Visual and modes#
Headless: Kbd ships no CSS. With the default theme, kv-kbd (and a bare kbd inside Prose, which gets the same rule) draws a flat key in the same family as inline code: the body font at the code size, a light background, a 1px edge on all four sides, a small corner radius, and no line break inside the key. There is no depth, because depth means "press me" and is for buttons only.
- Grouping. An outer
kbdthat holds other keys is plain: it inherits the font, stays on one line and puts a small space between its keys. - Contrast. Text on the key's background is checked automatically at 4.5:1 or more (1.4.3 Contrast (Minimum), AA).
- The edge is not required by 1.4.11 Non-text Contrast (AA), since the key name is text. It keeps the contrast of a control's border, so a low-vision user at 200% sees a key and not a word.
- Text size. The size is relative to the surrounding text, so it follows the browser's text size (1.4.4 Resize Text, AA).
- Forced colours. The edge uses the system's text colour. The shape and edge are not colour cues.
- Target size, focus indicator and motion: not applicable, because Kbd is not a control.
Warning: Don't forget
If you restyle the key, keep its text at 4.5:1 contrast against its background (1.4.3 Contrast (Minimum)), and size it relative to the surrounding text so it follows the browser’s text size (1.4.4 Resize Text).
Focus management#
- Initial focus: not moved. Kbd never moves focus.
- Trap: no.
- Restore to: not applicable.
- A key drawn in text is not a control. Don't make it look pressable: if it must act, use a Button.
Allowed elements#
A tag outside the list changes the page's outline or semantics (1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A)). as is a string, so it works from a Server Component.
| Part | `as` | Why |
|---|---|---|
| Kbd | kbd (default) | samp is text a program prints, not a key, so it is not allowed. Nothing else names keyboard input |
WCAG success criteria#
- 1.3.1 Info and Relationships (A): keyboard input is marked up as
<kbd>and not as styled text. - 1.4.3 Contrast (Minimum) (AA): the key's text is dark enough on its background, checked automatically in the default theme.
- 1.4.4 Resize Text (AA): the size follows the browser's text size.
Keyboard#
Kbd has no keys of its own. What you put in it keeps its own keys.
API reference#
import { Kbd, useKbd } from '@kvirn-ui/react'Kbd
Renders <kbd class="kv-kbd"> with no role. It adds no ARIA, text or behaviour, takes every attribute of an HTML element and passes ref to it. The default theme is described on Theming.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'kbd' | 'kbd' | Only kbd is allowed. Program output (samp) is not a key, and nothing else names keyboard input. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-kbd | always | The part class. A className you pass is joined with it, never replaces it. |
useKbd
Takes no options.
Result
| Prop | Type | Default | Description |
|---|---|---|---|
element | 'kbd' | – | The element: always kbd. |
rootProps | KbdPartProps | – | Spread on the element: only the class kv-kbd. No role, ARIA or state. |
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | kbd.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | kbd.test.tsx › renders a kbd, no role or ARIA, and passes attributes and the ref through; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | kbd.test.tsx › is not a Tab stop |
| Focus order and management | n/a | n/a | n/a | nothing focusable: the component never takes or moves focus |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | n/a | n/a | n/a | native <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate |
| VoiceOver + Safari + iOS | n/a | n/a | n/a | native <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | n/a | n/a | n/a | native <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate |
| TalkBack + Chrome + Android | n/a | n/a | n/a | native <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate |
| VoiceOver + Safari + macOS | n/a | n/a | n/a | native <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate |