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
import { createBadge } from 'material';
const badge = createBadge({ label: 3 });
document.body.append(badge.element);
<m-badge label="3"></m-badge>
import { Badge } from 'material/react';
export function Example() {
return (
<Badge label="3" />
);
}
<script setup lang="ts">
import { MBadge } from 'material/vue';
</script>
<template>
<MBadge label="3" />
</template>
<script lang="ts">
import { Badge } from 'material/svelte';
</script>
<Badge label="3" />
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.
import { createBadge } from 'material';
const badge = createBadge({ label: 1200, max: 999, color: 'primary' });
document.body.append(badge.element);
<m-badge label="1200" max="999" color="primary"></m-badge>
import { Badge } from 'material/react';
export function Example() {
return (
<Badge label="1200" max={999} color="primary" />
);
}
<script setup lang="ts">
import { MBadge } from 'material/vue';
</script>
<template>
<MBadge label="1200" :max="999" color="primary" />
</template>
<script lang="ts">
import { Badge } from 'material/svelte';
</script>
<Badge label="1200" max={999} color="primary" />
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.
import { createBadge } from 'material';
const badge = createBadge({ label: 5 });
document.body.append(badge.element);
function markAllRead() {
badge.setLabel(0);
}
<m-badge label="5"></m-badge>
<script type="module">
const badge = document.querySelector('m-badge');
function markAllRead() {
badge.setAttribute('label', "0");
}
</script>
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} />
);
}
<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>
<script lang="ts">
import { Badge } from 'material/svelte';
let label = $state(5);
function markAllRead() {
label = 0;
}
</script>
<Badge label={label} />
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
labelWhat a large badge shows
string | number''variantA dot, or a badge with a label
'small' | 'large''large'maxAbove it, a number shows as {max}+
numberundefinedcolorIts color role
'error' | 'primary' | 'secondary' | 'tertiary' | 'success' | 'warning' | 'info''error'targetThe element it is attached to, which it wraps; it needs a parent
HTMLElementundefinedpositionWhich corner of the target
'top-right' | 'top-left' | 'bottom-right' | 'bottom-left''top-right'visibleWhether it starts shown
booleantrueclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'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
label: string | numberBadgeComponent / stringsetContent(content) / getContent()The same as setLabel() and getLabel()
content: string | numberBadgeComponent / stringsetMax(max)The maximum, applied to the label
max: numberBadgeComponentshow() / hide() / toggle(visible?)Shows or hides it
visible?: booleanBadgeComponentisVisible()Whether it is shown
booleansetVariant(variant) / setColor(color) / setPosition(position)The variant, the color, the corner
stringBadgeComponentattachTo(target) / detach()Wraps a target and moves the badge onto it; detached, it moves to the end of document.body
target: HTMLElementBadgeComponentaddClass(...classes) / removeClass(...classes)CSS classes
...classes: string[]BadgeComponentdestroy()Removes it, and unwraps its target
voidelementThe badge
HTMLElementwrapperThe element wrapping the target and the badge
HTMLElement | undefinedA 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 |