For a textual object status ("Done", "In progress") take CdsStatus, and CdsAvatar has a built-in
badgeprop for the common indicators on an avatar.
Anatomy
The badge attaches to the element from the default slot and is positioned relative to it. The content is a number or text from content, an icon from icon, or the markup from the badge slot.
Variations
Content
The content prop sets what the badge shows — most often a counter of events. By default the badge sits in the top right corner of the element from the slot.
The max prop caps the counter: anything above it collapses into "99+". Without max the value is printed in full.
Besides a number, content accepts a short text — a "new section" mark in a menu, for example. Here the badge is placed into the line by the inline prop — more on it in the positioning section.
The icon prop shows an icon instead of text.
Dot
The dot prop shrinks the badge to a dot and hides its content — only the "there is something new" signal is left. The diameter is changed via dot-size.
Colors
The color prop accepts any color alias of the design system, text-color changes the color of the content. By default the badge is red — the color of indication.
Positioning
By default the badge is positioned absolutely relative to the element from the slot. The location prop picks the corner.
The floating prop moves the badge 8 pixels away from the content, so it overlaps the element less. For fine-tuning there are offset-x and offset-y.
The inline prop puts the badge into the flow next to the content — as in the menu example above. location, floating and offset-* are ignored then.
Visibility
The model-value prop shows and hides the badge. The change of state is animated, and the animation is configured through transition.
Slots
| Slot | Description |
|---|---|
default | The element the badge is attached to |
badge | Badge content. Replaces content and icon |
Guidelines
Scenarios
Notification center
A counter on the notifications icon: max keeps the badge compact for any number of events, and model-value hides it once nothing is unread — an empty "0" does not hang on the icon.
Menu with updates
Counters in menu items hint where to look. A badge with inline goes into the append slot of a list item, and once the section is opened model-value removes it. Open "Входящие" — the counter disappears.
Contacts online
A dot indicator shows the state without words: color="green" — the contact is online, neutral — not. inline puts the dot into the list row, and the label prop announces the state to screen readers.
API
Props
| Name | Type | Default | Description |
|---|---|---|---|
color | string | undefined | — | Component color |
content | string | number | undefined | — | Content displayed in the component. |
dot | boolean | undefined | — | Shrinks the component and hides its content, rendering it as a dot |
dotSize | string | number | undefined | — | Dot size |
floating | boolean | undefined | — | Moves the component further away from the content — an 8 pixel offset. |
height | string | number | undefined | — | Component height |
icon | IconValue | undefined | — | Icon shown inside the component. Ignored when the dot prop is set. |
inline | boolean | undefined | — | Renders as an inline block instead of using absolute positioning. The location, floating and offset props are ignored. |
label | string | undefined | '$cds.badge' | Aria-label for the component. Falls back to the default value when not set. |
location | Anchor | null | undefined | 'top end' | Position of the element relative to its container, for example "top left", "center", "bottom right". |
max | string | number | undefined | — | Sets the maximum allowed value when content holds a number. If the number exceeds the maximum, a "+" suffix is added. |
maxHeight | string | number | undefined | — | Maximum component height |
maxWidth | string | number | undefined | — | Maximum component width |
minHeight | string | number | undefined | — | Minimum component height |
minWidth | string | number | undefined | — | Minimum component width |
modelValue | boolean | undefined | true | Controls whether the component is shown or hidden. |
offsetX | string | number | undefined | — | Offset along the X axis |
offsetY | string | number | undefined | — | Offset along the Y axis |
tag | string | JSXComponent<any> | undefined | 'div' | Custom HTML tag or component to render |
text | string | number | undefined | — | Text displayed in the component. |
textColor | string | undefined | — | Text color of the component |
theme | string | undefined | — | Component theme |
transition | string | boolean | (TransitionProps & { component?: Component | undefined; }) | null | undefined | 'fade-transition' | Transition animation |
width | string | number | undefined | — | Component width |
Slots
| Name | Type | Description |
|---|---|---|
badge | any | Slot for the content placed inside the badge. |
default | any | Default slot |