Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Badge

A short status or category in words, such as “Granted” or “Draft”, drawn as a pill. It is plain text in the flow of the page: it can’t be pressed and it announces nothing.

Package
@kvirn-ui/react
Testing
Automated. Core n/a, Release n/a

When to use it#

  • Use a badge for a short status or category next to a title, in a list or in a summary: “Granted”, “Waiting for more information”.
  • Always write the status in words. The colour is only a second cue.
  • Keep it to one word or a few words. Long text belongs in a paragraph, and an important message belongs in an Alert.
  • Not for something the user can press: use a Button or a Toggle. Don’t make a badge look pressable.
  • Not as a dot or an icon alone: an empty badge says nothing to a screen reader (software that reads the screen aloud).

Example#

Application status: Draft

Use cases#

A status for each case in a list

Each item says its status in words, with a hidden “Status:” before it so a screen reader says what the badge is. The variant adds a colour for those who see it: pick it by the role of the status, not by the colour you like. If each case has several details, use a Table instead.

  • Building permit, 4 StorgatanStatus: Granted
  • Parking permitStatus: Waiting for more information
  • Noise complaintStatus: Being processed
  • School transportStatus: Refused
  • Library cardStatus: New
  • Bulky waste collectionStatus: Draft

Props in this example:

Information: Did you know?

A change of status after the page has loaded isn’t announced by a Badge. See A status that changes while the page is open.

A status with a label in a summary

A badge next to a title reads as part of it. Where “Granted” alone is unclear, put it under a term that says what it is, such as “Status” in a description list.

Case
Building permit, 4 Storgatan
Status
Granted

Props in this example:

A status that changes while the page is open

A badge is plain text, so a change in it makes no sound. When the person asks for the status again, change the badge and say the result in words. Announce it, and show the same words on the page.

Application status: Being processed

Props in this example:

Warning: Don't forget

If people need to hear the change, announce it yourself. A Badge never does (4.1.3 Status Messages, Level AA).

A number alone says little, and a badge with no words says nothing to a screen reader. Write the count as words, such as “3 new”. The badge is plain text, so it is read as part of the link.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: none. Badge is inline text, not a widget.
  • Deviations: none
  • Native elements used: <span>. The consumer can pick <strong> or <em> with as (allowed elements below), and its own semantics apply.

Badge is a <span> with a class, so the theme can draw a short status or category as a pill. It adds no role, ARIA, text or behaviour, so what a user perceives is the text inside it, in the flow of the page. The text is the status: the colour only adds to it.

PartElement / roleARIANotes
Badge<span> → no rolenoneclass="kv-badge", plus kv-badge--<variant> for primary, info, success, warning and danger. Attributes (id, aria-*) and the ref reach the element unchanged
Badgeasthe chosen element's ownOne element, chosen with a string. A value outside the list is a type error. className joins, the ref gets the element
Badgeneverno role, tabindexNo role="status", no live region, no click handler, no icon and no generated text: the words are the consumer's children, so nothing is skipped or read twice

It announces nothing.

What you need to do#

  • Words, always. The text says the status ("Granted", "Waiting for more information"): the colour is only a second cue (1.4.1 Use of Color, A). Never an empty badge, a dot or an icon alone.
  • Give it context. A badge next to a title reads as part of it. Where "Granted" alone is unclear, label what it is for in the surrounding text (a "Status" term in a definition list).
  • Keep it short. A badge is one word or a few; it does not wrap. Long text belongs in a paragraph.
  • Language. Text in another language goes in a nested KvirnProvider, with useLocale().localeProps on your own element (3.1.2 Language of Parts, AA).
  • Changing status. Announce a change in your own live region if the user needs to hear it (4.1.3 Status Messages, AA).

Warning: Don't forget

Write the status in words, every time. Colour alone doesn’t reach people who can’t see it or who see it differently (1.4.1 Use of Color).

Visual and modes#

Headless: Badge ships no CSS. With the theme stylesheet (@kvirn-ui/theme), a badge is a pill: small text, fully rounded corners, a little padding and a thin edge. Neutral is the text colour on the surface colour with a control edge; the primary variant is the link colour on a tinted primary background; the info variant is the text colour on that tinted background; success, warning and danger are the text colour on their own tinted backgrounds. Every pair is measured by the theme's contrast check 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 words carry the meaning. The size follows the browser's text size (1.4.4 Resize Text, AA). In forced colours the edge uses the system's text colour. Target size, focus indicator and motion: not applicable, because Badge is not a control.

Focus management#

  • Initial focus: not moved. Badge never moves focus.
  • Trap: no.
  • Restore to: not applicable.
  • A badge is not a control. Don't make it look pressable: if it must act, use a Button or a Toggle.

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
Badgespan (default), strong, emInline phrasing text, with or without emphasis. No block or interactive element: a Badge is a word, not a control

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): a status is text in an element, not an image or a background.
  • 1.4.1 Use of Color (A): the consumer's words are the status, and axe finds no violation in any theme.
  • 1.4.3 Contrast (Minimum) (AA): the text on its background, measured by the theme's contrast check.
  • 1.4.4 Resize Text (AA): the size is in rem.
  • 1.4.10 Reflow (AA): a row of short badges wraps between badges at 320 CSS pixels (the reflow sweep runs the badge stories at 320 CSS pixels and checks for sideways scrolling). A single badge never wraps, so one whose text is wider than the viewport would scroll sideways: keep the text short. The sweeps do not use a badge with long text, so check a long badge by eye. The text-spacing sweep checks that no badge clips its text.

Keyboard#

Badge has no keys of its own. What you put in it keeps its own keys.

API reference#

import { Badge, useBadge } from '@kvirn-ui/react'

Badge

Renders <span class="kv-badge"> with no role, no ARIA and no behaviour. It takes every attribute of an element and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
variant'neutral' | 'primary' | 'info' | 'success' | 'warning' | 'danger''neutral'The role of the badge, which gives it a class the theme colours. The words are yours: the colour is never the only cue.
as'span' | 'strong' | 'em''span'Changes the element to strong or em for emphasis. A Badge is a word, not a control.
Class or attributeValuesMeaning
kv-badgealwaysThe part class. The default theme draws a pill.
kv-badge--primary, --info, --success, --warning, --dangerfrom variantAdded for every variant but neutral. Theme variants.

useBadge

Options

PropTypeDefaultDescription
variant'neutral' | 'primary' | 'info' | 'success' | 'warning' | 'danger''neutral'The role of the badge.

Result

PropTypeDefaultDescription
element'span'–The element to render: always a span.
rootPropsBadgePartProps–Spread on the element: the class kv-badge, plus kv-badge--variant.

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 + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassbadge.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassbadge.test.tsx (axe and role, name and state): renders a span, no role or ARIA, and passes attributes and the ref through
Keyboard contract rows (Vitest browser)passn/apass1 rows, each names a test in badge.test.tsx
Focus order and managementn/an/an/aa badge is not a control: nothing in it is focusable
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowsn/an/an/anative <span>, <strong> or <em> with no role, read as text in the page; no live region, state, focus or key handling; nothing to operate
VoiceOver + Safari + iOSn/an/an/anative <span>, <strong> or <em> with no role, read as text in the page; no live region, state, focus or key handling; nothing to operate
Release (before 1.0 and each minor)
JAWS + Chrome + Windowsn/an/an/anative <span>, <strong> or <em> with no role, read as text in the page; no live region, state, focus or key handling; nothing to operate
TalkBack + Chrome + Androidn/an/an/anative <span>, <strong> or <em> with no role, read as text in the page; no live region, state, focus or key handling; nothing to operate
VoiceOver + Safari + macOSn/an/an/anative <span>, <strong> or <em> with no role, read as text in the page; no live region, state, focus or key handling; nothing to operate