Skip to main content
Light Dark System

Badge

<sc-badge> | ScBadge
Since 2.0 stable

Badges are used to draw attention and display statuses or counts.

Badge
<sc-badge>Badge</sc-badge>
import ScBadge from '@styledc-dev/styledc/dist/react/badge';

const App = () => <ScBadge>Badge</ScBadge>;

Examples

Variants

Set the variant attribute to change the badge’s variant.

Primary Success Neutral Warning Danger
<sc-badge variant="primary">Primary</sc-badge>
<sc-badge variant="success">Success</sc-badge>
<sc-badge variant="neutral">Neutral</sc-badge>
<sc-badge variant="warning">Warning</sc-badge>
<sc-badge variant="danger">Danger</sc-badge>
import ScBadge from '@styledc-dev/styledc/dist/react/badge';

const App = () => (
  <>
    <ScBadge variant="primary">Primary</ScBadge>
    <ScBadge variant="success">Success</ScBadge>
    <ScBadge variant="neutral">Neutral</ScBadge>
    <ScBadge variant="warning">Warning</ScBadge>
    <ScBadge variant="danger">Danger</ScBadge>
  </>
);

Pill Badges

Use the pill attribute to give badges rounded edges.

Primary Success Neutral Warning Danger
<sc-badge variant="primary" pill>Primary</sc-badge>
<sc-badge variant="success" pill>Success</sc-badge>
<sc-badge variant="neutral" pill>Neutral</sc-badge>
<sc-badge variant="warning" pill>Warning</sc-badge>
<sc-badge variant="danger" pill>Danger</sc-badge>
import ScBadge from '@styledc-dev/styledc/dist/react/badge';

const App = () => (
  <>
    <ScBadge variant="primary" pill>
      Primary
    </ScBadge>
    <ScBadge variant="success" pill>
      Success
    </ScBadge>
    <ScBadge variant="neutral" pill>
      Neutral
    </ScBadge>
    <ScBadge variant="warning" pill>
      Warning
    </ScBadge>
    <ScBadge variant="danger" pill>
      Danger
    </ScBadge>
  </>
);

Pulsating Badges

Use the pulse attribute to draw attention to the badge with a subtle animation.

1 1 1 1 1
<div class="badge-pulse">
  <sc-badge variant="primary" pill pulse>1</sc-badge>
  <sc-badge variant="success" pill pulse>1</sc-badge>
  <sc-badge variant="neutral" pill pulse>1</sc-badge>
  <sc-badge variant="warning" pill pulse>1</sc-badge>
  <sc-badge variant="danger" pill pulse>1</sc-badge>
</div>

<style>
  .badge-pulse sc-badge:not(:last-of-type) {
    margin-right: 1rem;
  }
</style>
import ScBadge from '@styledc-dev/styledc/dist/react/badge';

const css = `
  .badge-pulse sc-badge:not(:last-of-type) {
    margin-right: 1rem;
  }
`;

const App = () => (
  <>
    <div className="badge-pulse">
      <ScBadge variant="primary" pill pulse>
        1
      </ScBadge>
      <ScBadge variant="success" pill pulse>
        1
      </ScBadge>
      <ScBadge variant="neutral" pill pulse>
        1
      </ScBadge>
      <ScBadge variant="warning" pill pulse>
        1
      </ScBadge>
      <ScBadge variant="danger" pill pulse>
        1
      </ScBadge>
    </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.

Requests 30 Warnings 8 Errors 6
<sc-button>
  Requests
  <sc-badge pill>30</sc-badge>
</sc-button>

<sc-button style="margin-inline-start: 1rem;">
  Warnings
  <sc-badge variant="warning" pill>8</sc-badge>
</sc-button>

<sc-button style="margin-inline-start: 1rem;">
  Errors
  <sc-badge variant="danger" pill>6</sc-badge>
</sc-button>
import ScBadge from '@styledc-dev/styledc/dist/react/badge';
import ScButton from '@styledc-dev/styledc/dist/react/button';

const App = () => (
  <>
    <ScButton>
      Requests
      <ScBadge pill>30</ScBadge>
    </ScButton>

    <ScButton style={{ marginInlineStart: '1rem' }}>
      Warnings
      <ScBadge variant="warning" pill>
        8
      </ScBadge>
    </ScButton>

    <ScButton style={{ marginInlineStart: '1rem' }}>
      Errors
      <ScBadge variant="danger" pill>
        6
      </ScBadge>
    </ScButton>
  </>
);

With Menu Items

When including badges in menu items, use the suffix slot to make sure they’re aligned correctly.

Messages Comments 4 Replies 12
<sc-menu style="max-width: 240px;">
  <sc-menu-label>Messages</sc-menu-label>
  <sc-menu-item>Comments <sc-badge slot="suffix" variant="neutral" pill>4</sc-badge></sc-menu-item>
  <sc-menu-item>Replies <sc-badge slot="suffix" variant="neutral" pill>12</sc-badge></sc-menu-item>
</sc-menu>
import ScBadge from '@styledc-dev/styledc/dist/react/badge';
import ScButton from '@styledc-dev/styledc/dist/react/button';
import ScMenu from '@styledc-dev/styledc/dist/react/menu';
import ScMenuItem from '@styledc-dev/styledc/dist/react/menu-item';
import ScMenuLabel from '@styledc-dev/styledc/dist/react/menu-label';

const App = () => (
  <ScMenu
    style={{
      maxWidth: '240px',
      border: 'solid 1px var(--sc-panel-border-color)',
      borderRadius: 'var(--sc-border-radius-medium)'
    }}
  >
    <ScMenuLabel>Messages</ScMenuLabel>
    <ScMenuItem>
      Comments
      <ScBadge slot="suffix" variant="neutral" pill>
        4
      </ScBadge>
    </ScMenuItem>
    <ScMenuItem>
      Replies
      <ScBadge slot="suffix" variant="neutral" pill>
        12
      </ScBadge>
    </ScMenuItem>
  </ScMenu>
);

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.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/badge/badge.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/@styledc-dev/styledc@1.0.0/cdn/components/badge/badge.js';

To import this component using a bundler:

import '@styledc-dev/styledc/dist/components/badge/badge.js';

To import this component as a React component:

import ScBadge from '@styledc-dev/styledc/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.