Badge
A short, non-interactive label or count that states the status of the element beside it or sits on its corner.
import { Badge } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const ORDERS = [ { id: 'SO-10482', status: 'Shipped', tone: 'success' }, { id: 'SO-10483', status: 'Awaiting pickup', tone: 'warning' }, { id: 'SO-10484', status: 'Payment failed', tone: 'danger' }, { id: 'SO-10485', status: 'Draft', tone: 'secondary' },] as const;export default function BadgeBasic() { return ( <Stack gap={3}> {ORDERS.map((order) => ( <Inline key={order.id} align="center" gap={3}> <Text numeric="tabular">{order.id}</Text> <Badge tone={order.tone}>{order.status}</Badge> </Inline> ))} </Stack> );}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesTone and variant
tone picks the semantic color: accent, secondary, info, success, warning or danger. variant picks how the tone is painted: solid fills the badge, soft tints it, outline draws the edge alone. The default is secondary and soft. Pair the color with words, as the status names here do, because color alone is not a signal.
import { Badge } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';const VARIANTS = ['solid', 'soft', 'outline'] as const;const TONES = [ { tone: 'accent', label: 'Accent' }, { tone: 'secondary', label: 'Secondary' }, { tone: 'info', label: 'Info' }, { tone: 'success', label: 'Success' }, { tone: 'warning', label: 'Warning' }, { tone: 'danger', label: 'Danger' },] as const;export default function BadgeAppearance() { return ( <Stack gap={3}> {VARIANTS.map((variant) => ( <Inline key={variant} gap={2}> {TONES.map(({ tone, label }) => ( <Badge key={tone} tone={tone} variant={variant}> {label} </Badge> ))} </Inline> ))} </Stack> );}Sizes
size takes the control scale: sm, md and lg. A badge shrinks and grows with the controls it sits beside, so a table of sm buttons takes sm badges.
import { Badge } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';export default function BadgeSizes() { return ( <Inline align="center" gap={3}> <Badge size="sm" tone="info"> Small </Badge> <Badge size="md" tone="info"> Medium </Badge> <Badge size="lg" tone="info"> Large </Badge> </Inline> );}Shapes
pill rounds the badge fully and square rounds it by one step. dot draws no count: its children are its only content, so the words of a dot go in a visually hidden element, and a text beside it states what it means. The dot below carries the hidden word “Online” next to the visible label of the service.
import { Badge } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';import { VisuallyHidden } from '@slotted/react/visually-hidden';export default function BadgeShapes() { return ( <Stack gap={3}> <Inline align="center" gap={3}> <Badge shape="pill" tone="warning"> Backordered </Badge> <Text size="sm" tone="muted"> pill </Text> </Inline> <Inline align="center" gap={3}> <Badge shape="square" tone="warning"> Backordered </Badge> <Text size="sm" tone="muted"> square </Text> </Inline> <Inline align="center" gap={3}> <Badge shape="dot" tone="success" variant="solid"> <VisuallyHidden>Online</VisuallyHidden> </Badge> <Text>Carrier API</Text> </Inline> </Stack> );}Count
count writes a number before any children and formats it for the locale. Above max, 99 by default, the badge shows the ceiling and a plus sign; max={false} in React and [max]="false" in Angular removes the ceiling. locale sets the language of the digits and falls back to the defaults, then to the runtime. A count of zero removes the badge from the page, and showZero keeps it. In React the badge renders nothing; in Angular it takes display: none.
import { Badge } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function BadgeCount() { return ( <Stack gap={3}> <Inline align="center" gap={3}> <Badge count={4} tone="info" /> <Text>Open orders</Text> </Inline> <Inline align="center" gap={3}> <Badge count={128} tone="warning" /> <Text>Backorders, above the default ceiling of 99</Text> </Inline> <Inline align="center" gap={3}> <Badge count={1204} max={999} tone="warning" /> <Text>Pallets in transit, ceiling of 999</Text> </Inline> <Inline align="center" gap={3}> <Badge count={1234567} locale="de-DE" max={false} tone="success" /> <Text>Units shipped, no ceiling, de-DE digits</Text> </Inline> <Inline align="center" gap={3}> <Badge count={0} showZero tone="secondary" /> <Text>Open returns, zero shown</Text> </Inline> </Stack> );}Beside a title
A badge is taller than a line of text at the same size, so a title that carries one grows its row. inline draws the badge inside the line: the line keeps its height and the text under it keeps its distance.
Glyphs
A glyph goes before or after the label: the start and end props in React, and an element carrying slBadgeStart or slBadgeEnd in Angular. The part sizes and centers the glyph. In Angular the start part draws first and the end part last, whatever their order in the template.
import { Badge } from '@slotted/react/badge';import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';export default function BadgeGlyphs() { return ( <Inline gap={3}> <Badge start={<Glyph name="clock" />} tone="warning"> Due in 2 days </Badge> <Badge end={<Glyph name="inbox" />} tone="info"> Unassigned </Badge> </Inline> );}On the corner of an element
BadgeAnchor in React, and slBadgeAnchor on the element around the host in Angular, places the badge on a corner of the host. React renders a wrapper span; Angular writes the directive on an element the page already has. The badge comes after the host in document order, so it is read after the host. When the host is interactive and its name must carry the count, point aria-describedby from the host to the id of the badge, as the second host does, or put the count in the host’s aria-label. A badge is not focusable and takes no press, so the host stays the only control.
import { useId } from 'react';import { Badge, BadgeAnchor } from '@slotted/react/badge';import { Button, IconButton } from '@slotted/react/button';import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { VisuallyHidden } from '@slotted/react/visually-hidden';export default function BadgeAnchored() { const inboxCountId = useId(); return ( <Inline align="center" gap={8}> <BadgeAnchor> <IconButton aria-label="Notifications"> <Glyph name="bell" /> </IconButton> <Badge count={7} size="sm" tone="danger" variant="solid"> <VisuallyHidden>unread notifications</VisuallyHidden> </Badge> </BadgeAnchor> <BadgeAnchor> <Button aria-describedby={inboxCountId}>Inbox</Button> <Badge count={128} id={inboxCountId} size="sm" tone="accent" variant="solid"> <VisuallyHidden>unread messages</VisuallyHidden> </Badge> </BadgeAnchor> </Inline> );}Placement
placement chooses the corner: top-end, the default, top-start, bottom-end or bottom-start. Start and end follow the inline direction, so a right-to-left document mirrors them.
import { Badge, BadgeAnchor } from '@slotted/react/badge';import { IconButton } from '@slotted/react/button';import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';import { VisuallyHidden } from '@slotted/react/visually-hidden';const PLACEMENTS = ['top-end', 'top-start', 'bottom-end', 'bottom-start'] as const;export default function BadgePlacement() { return ( <Inline gap={8}> {PLACEMENTS.map((placement) => ( <Stack key={placement} gap={2}> <BadgeAnchor placement={placement}> <IconButton aria-label="Open inbox"> <Glyph name="inbox" /> </IconButton> <Badge shape="dot" size="sm" tone="danger" variant="solid"> <VisuallyHidden>New messages</VisuallyHidden> </Badge> </BadgeAnchor> <Text size="sm" tone="muted"> {placement} </Text> </Stack> ))} </Inline> );}Overlap and offset
overlap states the shape of the host. rectangle, the default, sets the badge on the corner point; circle moves it in far enough to meet the curve. offset then moves it by x and y pixels from the computed corner. x runs along the inline axis and mirrors with the document.
import { Avatar } from '@slotted/react/avatar';import { Badge, BadgeAnchor } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';import { VisuallyHidden } from '@slotted/react/visually-hidden';export default function BadgeOverlap() { return ( <Inline gap={8}> <Stack gap={2}> <BadgeAnchor overlap="rectangle" placement="bottom-end"> <Avatar name="Marta Okafor" size="lg" /> <Badge shape="dot" size="sm" tone="success" variant="solid"> <VisuallyHidden>Online</VisuallyHidden> </Badge> </BadgeAnchor> <Text size="sm" tone="muted"> rectangle </Text> </Stack> <Stack gap={2}> <BadgeAnchor overlap="circle" placement="bottom-end"> <Avatar name="Marta Okafor" size="lg" /> <Badge shape="dot" size="sm" tone="success" variant="solid"> <VisuallyHidden>Online</VisuallyHidden> </Badge> </BadgeAnchor> <Text size="sm" tone="muted"> circle </Text> </Stack> <Stack gap={2}> <BadgeAnchor offset={{ x: 2, y: 2 }} overlap="circle" placement="bottom-end"> <Avatar name="Marta Okafor" size="lg" /> <Badge shape="dot" size="sm" tone="success" variant="solid"> <VisuallyHidden>Online</VisuallyHidden> </Badge> </BadgeAnchor> <Text size="sm" tone="muted"> circle, offset 2px </Text> </Stack> </Inline> );}Live updates
live sets whether a change to the badge is announced. off, the default, announces nothing. polite writes role="status" and assertive writes role="alert". A role written on the badge is kept. Use polite for a count that changes while the person works, and assertive only for a failure. Press each button and the matching count changes.
import { useState } from 'react';import { Badge } from '@slotted/react/badge';import { Button } from '@slotted/react/button';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function BadgeLive() { const [queued, setQueued] = useState(3); const [failed, setFailed] = useState(1); return ( <Stack gap={4}> <Inline align="center" gap={3}> <Button onClick={() => setQueued(queued + 1)} size="sm" variant="outline"> Queue order </Button> <Text>Queued</Text> <Badge count={queued} live="polite" tone="info" /> </Inline> <Inline align="center" gap={3}> <Button onClick={() => setFailed(failed + 1)} size="sm" variant="outline"> Fail payment </Button> <Text>Failed</Text> <Badge count={failed} live="assertive" tone="danger" /> </Inline> </Stack> );}A badge that navigates
The root takes the element the page chooses: the render prop in React, and the directive written on an a in Angular. A badge on an anchor is focusable and draws the focus ring; a badge on a span is not a command.
import { Badge } from '@slotted/react/badge';export default function BadgeLink() { return ( <Badge render={(props) => <a {...props} href="#backordered" />} tone="warning" variant="outline" > Backordered </Badge> );}Defaults
BadgeDefaultsProvider in React and provideBadgeDefaults in Angular set tone, variant, size, shape, max and locale once for every badge below them. A badge that states a value of its own keeps it, as the rejected badge does with size and variant.
import { Badge, BadgeDefaultsProvider } from '@slotted/react/badge';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function BadgeDefaults() { return ( <BadgeDefaultsProvider locale="de-DE" max={false} size="sm" variant="outline"> <Stack gap={3}> <Inline gap={2}> <Badge tone="info">Pending</Badge> <Badge tone="success">Approved</Badge> <Badge size="md" tone="danger" variant="solid"> Rejected </Badge> </Inline> <Inline align="center" gap={3}> <Badge count={12480} tone="secondary" /> <Text>Units in stock</Text> </Inline> </Stack> </BadgeDefaultsProvider> );}Accessibility
- There is no badge pattern in the WAI-ARIA Authoring Practices — the pattern index carries no entry — and there is no badge role. The applicable guidance is the status role in WAI-ARIA 1.2, and WCAG 2.2 1.4.1 Use of Color, 1.4.11 Non-text Contrast and 4.1.3 Status Messages.
- The root is a span with no role. What a badge means comes from where it sits.
- live is the announcement policy for a badge whose value changes: polite writes role="status", whose implicit aria-live and aria-atomic are what a changing count needs; assertive writes role="alert". The default is off, so that only the badges the page marks announce.
- A dot has no text for a screen reader to read. Put the words beside it — a VisuallyHidden inside the badge — because color alone is not a signal. Inside a badge that element is out of flow, so it takes no space and the gap reserves nothing for it.
- The dot is the only badge whose whole meaning is a graphic, so its rim is --slotted-border-strong: the one border token the theme holds to 3:1 against the surface. A pill or a square carries text, so its edge is decoration.
- BadgeAnchor wraps its host rather than rewriting it. The badge is read after the host in document order. When the host is interactive and its name must carry the count, either wire aria-describedby from the host to the badge’s own id, or name the count in the host’s aria-label — “Inbox, 4 unread messages”.
- A badge is not focusable and is never a command: it answers no hover and gives no press feedback. render can make the root an anchor, and Badge paints the focus ring for that case.
- Under forced colors every tone ground becomes one system color, so Badge draws its edge in currentColor there. It is the only rule Badge writes for that mode.
- A count of zero removes the badge from the page entirely rather than leaving an empty circle, so nothing announces a value that is no longer there.
- A badge is one line. A string too long for its container is clipped with an ellipsis rather than reflowed, and the clipping is visual only: text-overflow shortens nothing, so the whole value stays in the DOM, stays in the accessibility tree, and a page search still finds it. A value that routinely needs more room is a Tag, not a Badge.
- The count is formatted with Intl.NumberFormat, so a total reads 1.234 in de-DE and ١٬٢٣٤ in ar-EG. Set locale on the provider when the application server-renders counts above its grouping threshold.
API
badge
Props
tone'accent' | 'secondary' | 'info' | 'success' | 'warning' | 'danger'Defaultsecondary- The semantic color of the badge. Six tones are available.
variant'solid' | 'soft' | 'outline'Defaultsoft- How the tone is painted: filled, tinted or as an edge alone. All three variants draw the tone edge.
size'sm' | 'md' | 'lg'Defaultmd- Type and glyph size follow the control scale, so a badge shrinks with the density of the controls beside it.
inlinebooleanDefaultfalse- No taller than the line of text around it, for a title or a sentence: the line keeps its height and what is under it its distance. The badge takes its own size where the line has the room and one line of the text where it does not; its words are at most nine tenths of the text around it.
shape'pill' | 'square' | 'dot'Defaultpill- A dot draws no count. Its children are its only content, so the words of a dot go in a visually hidden element.
countnumber- The number the badge shows, written before any children and formatted for the locale.
maxnumber | falseDefault99- The largest count written in full. Above it the badge shows the ceiling and a plus sign. false removes the ceiling.
showZerobooleanDefaultfalse- Whether a count of zero is shown. When false, a count of zero removes the badge.
localestring- BCP 47 tag for the digits of count. Falls back to the provider, then to the runtime.
startReactNode- A glyph before the label, wrapped in the start part, which sizes and centers it.
endReactNode- A glyph after the label, wrapped in the end part.
live'off' | 'polite' | 'assertive'Defaultoff- Announcement policy for a change to the badge: off, polite (role status) or assertive (role alert).
placement'top-end' | 'top-start' | 'bottom-end' | 'bottom-start'Defaulttop-end- On BadgeAnchorThe corner of the host the badge sits on. Start and end mirror in a right-to-left document.
overlap'rectangle' | 'circle'Defaultrectangle- On BadgeAnchorThe shape of the host, which sets how far in from the corner the badge starts.
offset{ x?: number; y?: number }Default { x: 0, y: 0 }- On BadgeAnchorMoves the badge off the computed corner, in pixels. x runs along the inline axis and mirrors with the document.
render(props) => ReactNode- On Badge, BadgeAnchorRenders the root through another element, such as an anchor for a badge that navigates.
refRef<HTMLElement>- On every partReaches the native element. Each part extends the props of its own element.
BadgeDefaultsProvider{ tone, variant, size, shape, max, locale }- On a subtreeSets the defaults once for every badge under it. An explicit prop wins.
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every badge inside it.
Badge tokens
| Token | Default |
|---|---|
--slotted-badge-anchor-offset-x | 0px |
--slotted-badge-anchor-offset-y | 0px |
--slotted-badge-border-width | var(--slotted-border-width-thin, 1px) |
--slotted-badge-dot-border-color | var(--slotted-border-strong, CanvasText) |
--slotted-badge-font-family | var(--slotted-control-font-family, sans-serif) |
--slotted-badge-font-weight | var(--slotted-font-weight-medium, 500) |
--slotted-badge-radius | var(--slotted-radius-sm, 4px) |
--slotted-badge-ring-color | var(--slotted-surface, Canvas) |
--slotted-badge-ring-width | var(--slotted-border-width-thick, 2px) |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-border-strong | Set per scheme by the theme |
--slotted-border-width-thick | 2px |
--slotted-border-width-thin | 1px |
--slotted-control-font-family | var(--slotted-font-family-sans) |
--slotted-control-font-size-lg | comfortable 14px · compact 13px · dense 12px |
--slotted-control-font-size-md | comfortable 13px · compact 12px · dense 12px |
--slotted-control-font-size-sm | comfortable 12px · compact 12px · dense 12px |
--slotted-control-icon-size-md | comfortable 16px · compact 14px · dense 13px |
--slotted-control-icon-size-sm | comfortable 14px · compact 13px · dense 12px |
--slotted-focus-ring-color | Set per scheme by the theme |
--slotted-focus-ring-offset | var(--slotted-border-width-thick) |
--slotted-focus-ring-width | var(--slotted-border-width-thick) |
--slotted-font-weight-medium | 500 |
--slotted-line-height-xs | 1.4 |
--slotted-radius-full | 9999px |
--slotted-radius-sm | 4px |
--slotted-space-1 | 0.125rem |
--slotted-space-2 | 0.25rem |
--slotted-space-3 | 0.375rem |
--slotted-space-4 | 0.5rem |
--slotted-space-5 | 0.75rem |
--slotted-surface | Set per scheme by the theme |
--slotted-text-box-edge | cap alphabetic |
--slotted-tone-accent-border | Set per scheme by the theme |
--slotted-tone-accent-on-solid | Set per scheme by the theme |
--slotted-tone-accent-solid | Set per scheme by the theme |
--slotted-tone-accent-surface | Set per scheme by the theme |
--slotted-tone-accent-text | Set per scheme by the theme |
--slotted-tone-danger-border | Set per scheme by the theme |
--slotted-tone-danger-on-solid | Set per scheme by the theme |
--slotted-tone-danger-solid | Set per scheme by the theme |
--slotted-tone-danger-surface | Set per scheme by the theme |
--slotted-tone-danger-text | Set per scheme by the theme |
--slotted-tone-info-border | Set per scheme by the theme |
--slotted-tone-info-on-solid | Set per scheme by the theme |
--slotted-tone-info-solid | Set per scheme by the theme |
--slotted-tone-info-surface | Set per scheme by the theme |
--slotted-tone-info-text | Set per scheme by the theme |
--slotted-tone-secondary-border | Set per scheme by the theme |
--slotted-tone-secondary-on-solid | Set per scheme by the theme |
--slotted-tone-secondary-solid-hover | Set per scheme by the theme |
--slotted-tone-secondary-surface | Set per scheme by the theme |
--slotted-tone-secondary-text | Set per scheme by the theme |
--slotted-tone-success-border | Set per scheme by the theme |
--slotted-tone-success-on-solid | Set per scheme by the theme |
--slotted-tone-success-solid | Set per scheme by the theme |
--slotted-tone-success-surface | Set per scheme by the theme |
--slotted-tone-success-text | Set per scheme by the theme |
--slotted-tone-warning-border | Set per scheme by the theme |
--slotted-tone-warning-on-solid | Set per scheme by the theme |
--slotted-tone-warning-solid | Set per scheme by the theme |
--slotted-tone-warning-surface | Set per scheme by the theme |
--slotted-tone-warning-text | Set per scheme by the theme |