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
import { Badge } from '@kvirn-ui/react'
import { useBadgeTexts } from './texts.ts'
export function DefaultBadge() {
const { texts } = useBadgeTexts()
return (
<p>
{texts.statusLabel} <Badge>{texts.draft}</Badge>
</p>
)
}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
import { Badge, Stack, VisuallyHidden } from '@kvirn-ui/react'
import { useBadgeTexts } from './texts.ts'
export function StatusInList() {
const { texts } = useBadgeTexts()
return (
<Stack as="ul" className="kv-stack--gap-4">
{texts.cases.map((item) => (
<Stack as="li" key={item.title} className="kv-stack--gap-2">
<span>{item.title}</span>
<span>
<VisuallyHidden>{texts.statusHidden}</VisuallyHidden>{' '}
<Badge variant={item.variant}>{item.status}</Badge>
</span>
</Stack>
))}
</Stack>
)
}Props in this example:
varianton Badge
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
import { Badge, DefinitionList } from '@kvirn-ui/react'
import { useBadgeTexts } from './texts.ts'
export function WithContext() {
const { texts } = useBadgeTexts()
return (
<DefinitionList.Root>
<DefinitionList.Row>
<DefinitionList.Term>{texts.summary.case}</DefinitionList.Term>
<DefinitionList.Description>{texts.summary.caseValue}</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.summary.status}</DefinitionList.Term>
<DefinitionList.Description>
<Badge variant="success">{texts.summary.statusValue}</Badge>
</DefinitionList.Description>
</DefinitionList.Row>
</DefinitionList.Root>
)
}Props in this example:
varianton Badge
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
import { Badge, Button, useAnnouncer } from '@kvirn-ui/react'
import { useState } from 'react'
import { useBadgeTexts } from './texts.ts'
export function StatusChanges() {
const { texts } = useBadgeTexts()
const { announce } = useAnnouncer()
const [isGranted, setIsGranted] = useState(false)
const [message, setMessage] = useState<string | undefined>()
const checkStatus = () => {
// A real app asks the server. Here the first answer is "granted".
const next = isGranted ? texts.statusCheck.same : texts.statusCheck.changed
setIsGranted(true)
setMessage(next)
// The badge changes silently. Say the result, and show it in words too.
announce(next)
}
return (
<>
<p>
{texts.statusLabel}{' '}
<Badge variant={isGranted ? 'success' : 'info'}>
{isGranted ? texts.statusCheck.granted : texts.statusCheck.beingProcessed}
</Badge>
</p>
<Button onClick={checkStatus}>{texts.statusCheck.check}</Button>
{message === undefined ? null : <p>{message}</p>}
</>
)
}Props in this example:
varianton Badge
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 count of new items next to a link
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.
import { Badge, Link } from '@kvirn-ui/react'
import { useBadgeTexts } from './texts.ts'
export function CountInALink() {
const { texts } = useBadgeTexts()
return (
<p>
<Link href="#messages">
{texts.inbox.link} <Badge variant="primary">{texts.inbox.unread}</Badge>
</Link>
</p>
)
}Props in this example:
varianton Badge
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>withas(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.
| Part | Element / role | ARIA | Notes |
|---|---|---|---|
| Badge | <span> → no role | none | class="kv-badge", plus kv-badge--<variant> for primary, info, success, warning and danger. Attributes (id, aria-*) and the ref reach the element unchanged |
| Badge | as | the chosen element's own | One element, chosen with a string. A value outside the list is a type error. className joins, the ref gets the element |
| Badge | never | no role, tabindex | No 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, withuseLocale().localePropson 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 |
|---|---|---|
| Badge | span (default), strong, em | Inline 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.
| Prop | Type | Default | Description |
|---|---|---|---|
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 attribute | Values | Meaning |
|---|---|---|
kv-badge | always | The part class. The default theme draws a pill. |
kv-badge--primary, --info, --success, --warning, --danger | from variant | Added for every variant but neutral. Theme variants. |
useBadge
Options
| Prop | Type | Default | Description |
|---|---|---|---|
variant | 'neutral' | 'primary' | 'info' | 'success' | 'warning' | 'danger' | 'neutral' | The role of the badge. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
element | 'span' | – | The element to render: always a span. |
rootProps | BadgePartProps | – | 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 + 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 | badge.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | badge.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) | pass | n/a | pass | 1 rows, each names a test in badge.test.tsx |
| Focus order and management | n/a | n/a | n/a | a badge is not a control: nothing in it is focusable |
| 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 <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 + iOS | n/a | n/a | n/a | native <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 + Windows | n/a | n/a | n/a | native <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 + Android | n/a | n/a | n/a | native <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 + macOS | n/a | n/a | n/a | native <span>, <strong> or <em> with no role, read as text in the page; no live region, state, focus or key handling; nothing to operate |