Skip to content

Badge

A small dot or a short count or label, placed on an anchor with BadgedBox.

import { Badge, BadgedBox } from '@windmorph/react';

Props

By name or description, across the 14 props below

Badge 8 props

  • childrentype ReactNode

    A count or a short label (large badge); leave it out for the small dot. Numbers are formatted for the locale and capped at max ("999+"). M3 allows four characters, including the "+"; text badges stop at 34px wide and truncate longer text.

  • maxtype numberdefault 999

    The highest count shown; larger counts show as max + "+".

  • labeltype string

    What assistive tech reads instead of the visible content, e.g. "3 unread messages". Without it, a large badge reads its content and a small one labels.newNotification.

  • reftype Ref<HTMLSpanElement>

    Ref to the root element.

Text

  • labelstype Partial<BadgeLabels>

    Words the badge announces, with English defaults. A LabelsProvider sets them for the whole app.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

BadgedBox 6 props

  • badgetype ReactElement<BadgeProps, string | JSXElementConstructor<any>>Required

    The badge, a Badge.

  • childrentype ReactNodeRequired

    The anchor, usually an icon.

  • reftype Ref<HTMLSpanElement>

    Ref to the root element.

Also accepts the root element's HTML attributes (id, data-*, event handlers).

Tokens

M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.

Badge tokens (md-comp-badge)
VariableDefaultPaintsCompose
Small (size)
--md-comp-badge-colorvar(--md-sys-color-error)The small badge, a dot.BadgeTokens.Color
Large (size)
--md-comp-badge-large-colorvar(--md-sys-color-error)The large badge, holding a count or short text.BadgeTokens.LargeColor
--md-comp-badge-large-label-text-colorvar(--md-sys-color-on-error)The large badge's label.BadgeTokens.LargeLabelTextColor

Spec status

Built to the M3 Expressive badge spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.

  • Small badge

    Built

    Variant · Basic, Navigation

  • Large badge

    Built

    Variant · Basic, Navigation

  • Large badge with maximum characters

    Built

    Configuration · Basic, Navigation

  • On navigation items

    Built

    Use case · Navigation

  • On icons

    Built

    Use case · Basic

  • Count cap

    Extension

    max sets the highest count shown (999 by default, "999+"), and text longer than four characters stops at 34px and truncates.

Bundle size

Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.

  • Badge, BadgedBox: 7.7 kB

M3 Expressive specs

Sizes
small, large
Shape
rounded-corner-full

Spec values

  • No children is the small 6px dot; children give the large badge (16px tall, at least 16px wide, with text over four characters cut at 34px, full corners, 4px side padding, label-small), both in error / on-error. Its height and width limits grow with a larger text size, so a single digit stays a circle.
  • BadgedBox places the badge like Compose: a small badge 6px inside the anchor's end at the top, a large one 12px inside the end hanging 2px above and past it.
  • Positioning uses a shared grid cell and a zero-size box, so the badge overflows without changing the anchor's size and mirrors in RTL; Compose's badge rulers (clamping to an outer bound) aren't ported.