Badge
<cw-badge> | CwBadge
Badges are used to draw attention and display statuses or counts.
<cw-badge>Badge</cw-badge>
import CwBadge from '@cordwainer/cw-elements/dist/react/badge'; const App = () => <CwBadge>Badge</CwBadge>;
Examples
Variants
Set the variant attribute to change the badge’s variant.
<cw-badge variant="primary">Primary</cw-badge> <cw-badge variant="success">Success</cw-badge> <cw-badge variant="neutral">Neutral</cw-badge> <cw-badge variant="warning">Warning</cw-badge> <cw-badge variant="danger">Danger</cw-badge>
import CwBadge from '@cordwainer/cw-elements/dist/react/badge'; const App = () => ( <> <CwBadge variant="primary">Primary</CwBadge> <CwBadge variant="success">Success</CwBadge> <CwBadge variant="neutral">Neutral</CwBadge> <CwBadge variant="warning">Warning</CwBadge> <CwBadge variant="danger">Danger</CwBadge> </> );
Pill Badges
Use the pill attribute to give badges rounded edges.
<cw-badge variant="primary" pill>Primary</cw-badge> <cw-badge variant="success" pill>Success</cw-badge> <cw-badge variant="neutral" pill>Neutral</cw-badge> <cw-badge variant="warning" pill>Warning</cw-badge> <cw-badge variant="danger" pill>Danger</cw-badge>
import CwBadge from '@cordwainer/cw-elements/dist/react/badge'; const App = () => ( <> <CwBadge variant="primary" pill> Primary </CwBadge> <CwBadge variant="success" pill> Success </CwBadge> <CwBadge variant="neutral" pill> Neutral </CwBadge> <CwBadge variant="warning" pill> Warning </CwBadge> <CwBadge variant="danger" pill> Danger </CwBadge> </> );
Pulsating Badges
Use the pulse attribute to draw attention to the badge with a subtle animation.
<div class="badge-pulse"> <cw-badge variant="primary" pill pulse>1</cw-badge> <cw-badge variant="success" pill pulse>1</cw-badge> <cw-badge variant="neutral" pill pulse>1</cw-badge> <cw-badge variant="warning" pill pulse>1</cw-badge> <cw-badge variant="danger" pill pulse>1</cw-badge> </div> <style> .badge-pulse cw-badge:not(:last-of-type) { margin-right: 1rem; } </style>
import CwBadge from '@cordwainer/cw-elements/dist/react/badge'; const css = ` .badge-pulse cw-badge:not(:last-of-type) { margin-right: 1rem; } `; const App = () => ( <> <div className="badge-pulse"> <CwBadge variant="primary" pill pulse> 1 </CwBadge> <CwBadge variant="success" pill pulse> 1 </CwBadge> <CwBadge variant="neutral" pill pulse> 1 </CwBadge> <CwBadge variant="warning" pill pulse> 1 </CwBadge> <CwBadge variant="danger" pill pulse> 1 </CwBadge> </div> <style>{css}</style> </> );
With Buttons
One of the most common use cases for badges is attaching them to buttons. To make this easier, badges will be automatically positioned at the top-right when they’re a child of a button.
<cw-button> Requests <cw-badge pill>30</cw-badge> </cw-button> <cw-button style="margin-inline-start: 1rem;"> Warnings <cw-badge variant="warning" pill>8</cw-badge> </cw-button> <cw-button style="margin-inline-start: 1rem;"> Errors <cw-badge variant="danger" pill>6</cw-badge> </cw-button>
import CwBadge from '@cordwainer/cw-elements/dist/react/badge'; import CwButton from '@cordwainer/cw-elements/dist/react/button'; const App = () => ( <> <CwButton> Requests <CwBadge pill>30</CwBadge> </CwButton> <CwButton style={{ marginInlineStart: '1rem' }}> Warnings <CwBadge variant="warning" pill> 8 </CwBadge> </CwButton> <CwButton style={{ marginInlineStart: '1rem' }}> Errors <CwBadge variant="danger" pill> 6 </CwBadge> </CwButton> </> );
With Menu Items
When including badges in menu items, use the suffix slot to make sure they’re aligned
correctly.
<cw-menu style="max-width: 240px;"> <cw-menu-label>Messages</cw-menu-label> <cw-menu-item>Comments <cw-badge slot="suffix" variant="neutral" pill>4</cw-badge></cw-menu-item> <cw-menu-item>Replies <cw-badge slot="suffix" variant="neutral" pill>12</cw-badge></cw-menu-item> </cw-menu>
import CwBadge from '@cordwainer/cw-elements/dist/react/badge'; import CwButton from '@cordwainer/cw-elements/dist/react/button'; import CwMenu from '@cordwainer/cw-elements/dist/react/menu'; import CwMenuItem from '@cordwainer/cw-elements/dist/react/menu-item'; import CwMenuLabel from '@cordwainer/cw-elements/dist/react/menu-label'; const App = () => ( <CwMenu style={{ maxWidth: '240px', border: 'solid 1px var(--cw-panel-border-color)', borderRadius: 'var(--cw-border-radius-medium)' }} > <CwMenuLabel>Messages</CwMenuLabel> <CwMenuItem> Comments <CwBadge slot="suffix" variant="neutral" pill> 4 </CwBadge> </CwMenuItem> <CwMenuItem> Replies <CwBadge slot="suffix" variant="neutral" pill> 12 </CwBadge> </CwMenuItem> </CwMenu> );
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@cordwainer/cw-elements@1.1.1/cdn/components/badge/badge.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@cordwainer/cw-elements@1.1.1/cdn/components/badge/badge.js';
To import this component using a bundler:
import '@cordwainer/cw-elements/dist/components/badge/badge.js';
To import this component as a React component:
import CwBadge from '@cordwainer/cw-elements/dist/react/badge';
Slots
| Name | Description |
|---|---|
| (default) | The badge’s content. |
Learn more about using slots.
Properties
| Name | Description | Reflects | Type | Default |
|---|---|---|---|---|
variant
|
The badge’s theme variant. |
|
'primary' | 'success' | 'neutral' | 'warning' | 'danger'
|
'primary'
|
pill
|
Draws a pill-style badge with rounded edges. |
|
boolean
|
false
|
pulse
|
Makes the badge pulsate to draw attention. |
|
boolean
|
false
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Parts
| Name | Description |
|---|---|
base |
The component’s base wrapper. |
Learn more about customizing CSS parts.