Data
Badge
Short status or count, in a coloured pill.
Example
cr-badge shows a short text in a pill: a status ("Paid", "Overdue") or a count. variant picks the colour: neutral (default), primary, success or danger. The text must carry the meaning: the colour only supports it.
With count, a number is added after the text. When it changes, it rolls to its new value through the numbers in between, like an odometer: digits go up when the number increases, down when it decreases.
'use client';
import { useState } from 'react';
import { CrBadge, CrBox, CrButton } from '@cristal-ui/react';
export default function BadgeBasic() {
const [unread, setUnread] = useState(3);
return (
<CrBox display="flex" direction="column" gap="lg" align="start">
<CrBox display="flex" gap="sm" align="center" wrap>
<CrBadge>Draft</CrBadge>
<CrBadge variant="primary">New</CrBadge>
<CrBadge variant="success">Paid</CrBadge>
<CrBadge variant="danger">Overdue</CrBadge>
</CrBox>
<CrBox display="flex" gap="sm" align="center" wrap>
<CrBadge variant="primary" count={unread} live>
Unread
</CrBadge>
<CrButton size="s" variant="secondary" onClick={() => setUnread((count) => count + 4)}>
Receive messages
</CrButton>
<CrButton size="s" variant="outline" onClick={() => setUnread(0)}>
Mark all as read
</CrButton>
</CrBox>
</CrBox>
);
}Announcing changes
By default, a count change is not announced: the badge is read with the rest of the page. With live, it becomes a role="status" region: each change is announced politely ("Unread 7"), once the screen reader has finished its sentence. Keep live for counts whose change matters to the user, not for every badge in a list.
If the user asked their system to reduce motion, the number goes straight to its new value, without rolling.
Accessibility
- The badge is text: it is read with its content and its number ("Unread 3").
- The rolling digits are hidden from screen readers, which only read the final number.
- Each variant pairs a light background with a dark text of the same hue, with a contrast of at least 4.5:1. Colour is never the only information: the text states the status.
- With
live, the announcement is polite (role="status") and does not interrupt the user.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
count | number | — | Nombre affiché après le texte ; il défile jusqu'à chaque nouvelle valeur. |
live | boolean | false | Annonce poliment chaque changement aux lecteurs d'écran (zone role="status") ; désactivé par défaut. |
variant | "danger" | "neutral" | "primary" | "success" | 'neutral' | Couleur : neutral (gris), primary, success, danger. |
Slots
| Slot | Description |
|---|---|
| default | Le texte de la pastille. |
CSS parts
To style it from outside: cr-badge::part(name) { … }
| Part | Description |
|---|---|
badge | La pastille. |
count | Le nombre. |