Search
MaterialComponentsDocs
Components overviewApp barBadgeButtonsButton groupButtonExtended FABFAB menuFABIcon buttonSplit buttonCardCarouselCheckboxChipsDate & time pickersDate pickerTime pickerDialogDividerListLoading & progressLoading indicatorProgressMenuNavigationNavigation barNavigation drawerNavigation railRadio buttonsSearchSelectSheetsBottom sheetSide sheetSliderSnackbarSwitchTabsText fieldToolbarTooltip

Badge

Draw attention to something new. Try dots, counts, and labels attached to an action.

InboxFavoritesSent
FactoryWeb ComponentsReactVueSvelteSolidJS
PublishedUpdated

A badge shows a count or a status on another element: unread messages on an icon, a new item in a navigation bar. A large badge holds up to four characters; a small one is a 6dp dot for news without a number.

Usage

Vanilla
import { createBadge } from 'material';

const badge = createBadge({ label: 3 });
document.body.append(badge.element);
Web Components
<m-badge label="3"></m-badge>
React
import { Badge } from 'material/react';

export function Example() {
  return (
    <Badge label="3" />
  );
}
Vue
<script setup lang="ts">
import { MBadge } from 'material/vue';
</script>

<template>
  <MBadge label="3" />
</template>
Svelte
<script lang="ts">
  import { Badge } from 'material/svelte';
</script>

<Badge label="3" />
SolidJS
import { Badge } from 'material/solid';

export function Example() {
  return (
    <Badge label="3" />
  );
}

The factory's target wraps an element and puts the badge on its corner, set by position. The web component is the badge alone, placed where it is written. Navigation items and tabs take a badge of their own, through their badge option.

Examples

A maximum

Above max, a number shows as {max}+. A label longer than four characters is shortened: 12500 to 999+, text to its first four characters.

Vanilla
import { createBadge } from 'material';

const badge = createBadge({ label: 1200, max: 999, color: 'primary' });
document.body.append(badge.element);
Web Components
<m-badge label="1200" max="999" color="primary"></m-badge>
React
import { Badge } from 'material/react';

export function Example() {
  return (
    <Badge label="1200" max={999} color="primary" />
  );
}
Vue
<script setup lang="ts">
import { MBadge } from 'material/vue';
</script>

<template>
  <MBadge label="1200" :max="999" color="primary" />
</template>
Svelte
<script lang="ts">
  import { Badge } from 'material/svelte';
</script>

<Badge label="1200" max={999} color="primary" />
SolidJS
import { Badge } from 'material/solid';

export function Example() {
  return (
    <Badge label="1200" max={999} color="primary" />
  );
}

Hidden at zero

An empty label or 0 hides the badge, and any other label shows it again.

Vanilla
import { createBadge } from 'material';

const badge = createBadge({ label: 5 });
document.body.append(badge.element);

function markAllRead() {
  badge.setLabel(0);
}
Web Components
<m-badge label="5"></m-badge>

<script type="module">
  const badge = document.querySelector('m-badge');

  function markAllRead() {
    badge.setAttribute('label', "0");
  }
</script>
React
import { useState } from 'react';
import { Badge } from 'material/react';

export function Example() {
  const [label, setLabel] = useState(5);
  const markAllRead = () => setLabel(0);

  return (
    <Badge label={label} />
  );
}
Vue
<script setup lang="ts">
import { ref } from 'vue';
import { MBadge } from 'material/vue';

const label = ref(5);

function markAllRead() {
  label.value = 0;
}
</script>

<template>
  <MBadge :label="label" />
</template>
Svelte
<script lang="ts">
  import { Badge } from 'material/svelte';

  let label = $state(5);

  function markAllRead() {
    label = 0;
  }
</script>

<Badge label={label} />
SolidJS
import { createSignal } from 'solid-js';
import { Badge } from 'material/solid';

export function Example() {
  const [label, setLabel] = createSignal(5);
  const markAllRead = () => setLabel(0);

  return (
    <Badge label={label()} />
  );
}

API

Options

label

What a large badge shows

Typestring | number
Default''
variant

A dot, or a badge with a label

Type'small' | 'large'
Default'large'
max

Above it, a number shows as {max}+

Typenumber
Defaultundefined
color

Its color role

Type'error' | 'primary' | 'secondary' | 'tertiary' | 'success' | 'warning' | 'info'
Default'error'
target

The element it is attached to, which it wraps; it needs a parent

TypeHTMLElement
Defaultundefined
position

Which corner of the target

Type'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
Default'top-right'
visible

Whether it starts shown

Typeboolean
Defaulttrue
class

Additional CSS classes

Typestring
Defaultundefined
prefix

Prefix for CSS class names

Typestring
Default'mtrl'

The web component takes label (or its text), variant, color and max as attributes, and visible as a property.

Methods

setLabel(label) / getLabel()

The label, as shown; setting it hides the badge when it is empty or 0, and shows it otherwise

Parameterslabel: string | number
ReturnsBadgeComponent / string
setContent(content) / getContent()

The same as setLabel() and getLabel()

Parameterscontent: string | number
ReturnsBadgeComponent / string
setMax(max)

The maximum, applied to the label

Parametersmax: number
ReturnsBadgeComponent
show() / hide() / toggle(visible?)

Shows or hides it

Parametersvisible?: boolean
ReturnsBadgeComponent
isVisible()

Whether it is shown

Parametersnone
Returnsboolean
setVariant(variant) / setColor(color) / setPosition(position)

The variant, the color, the corner

Parametersstring
ReturnsBadgeComponent
attachTo(target) / detach()

Wraps a target and moves the badge onto it; detached, it moves to the end of document.body

Parameterstarget: HTMLElement
ReturnsBadgeComponent
addClass(...classes) / removeClass(...classes)

CSS classes

Parameters...classes: string[]
ReturnsBadgeComponent
destroy()

Removes it, and unwraps its target

Parametersnone
Returnsvoid
element

The badge

TypeHTMLElement
wrapper

The element wrapping the target and the badge

TypeHTMLElement | undefined

A badge has no events: it has on() and off(), and emits nothing.

Styling

.mtrl-badge, .mtrl-badge--small, .mtrl-badge--large { }
.mtrl-badge--error, .mtrl-badge--primary, .mtrl-badge--secondary, .mtrl-badge--tertiary { }
.mtrl-badge--success, .mtrl-badge--warning, .mtrl-badge--info { }
.mtrl-badge--positioned { }            /* on a target */
.mtrl-badge--top-right, .mtrl-badge--top-left, .mtrl-badge--bottom-right, .mtrl-badge--bottom-left { }
.mtrl-badge--invisible, .mtrl-badge--overflow { }
.mtrl-badge__wrapper { }               /* the target's wrapper */

Measurements

Attribute Value
Small 6dp dot, 3dp beyond the target's corner
Large 16dp high, at least 16dp wide, 8dp corners, 4dp padding, 8dp beyond the target's corner
Large, with a maximum 34dp wide at most
Colors The role and its on- pair: error and on-error by default