Badge
A simple, accessible badge component for displaying status, counts, or labels. Supports links, buttons, and numeric indicators with overflow handling.
Usage
Basic usage
import { Badge } from '@nofinite/nui';
<Badge>Beta</Badge>;
With typical interaction
<Badge onClick={() => console.log("clicked")} pill>
5
</Badge>
<Badge href="/profile" variant="primary">
View
</Badge>
Variants
<Badge variant="default">Default</Badge>
<Badge variant="primary">Primary</Badge>
<Badge variant="success">Success</Badge>
<Badge variant="warning">Warning</Badge>
<Badge variant="danger">Danger</Badge>
Available variants:
default– Neutral badge, standard background and border.primary– Emphasizes important badges, primary theme color.success– Indicates positive state (e.g., success, online).warning– Indicates caution or alert.danger– Indicates error or critical state.
Guidelines:
- Use
primaryfor actionable badges or links. - Use
success,warning, ordangerfor semantic state indicators. - Use
defaultfor general labels.
Sizes
<Badge size="sm">Small</Badge>
<Badge size="md">Medium</Badge>
<Badge size="lg">Large</Badge>
Available sizes:
sm– Smaller font and paddingmd– Defaultlg– Larger font and padding
Sizes can be combined with variants and shape modifiers:
<Badge variant="success" size="lg" pill>
99+
</Badge>
States
<Badge disabled>Disabled</Badge>
<Badge dot ariaLabel="Online" />
<Badge loading>Loading</Badge>
disabled– Interaction is blocked and visually muted (if implemented).dot– Small indicator without visible content, requiresariaLabel.loading– Optional state for pending interactions (implementation-dependent).
Native Props / Composition
The Badge supports standard HTML attributes depending on the rendered element (<span>, <a>, or <button>):
<Badge
className="my-badge"
title="Tooltip"
aria-label="5 notifications"
onClick={() => console.log('clicked')}
>
5
</Badge>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "primary" | "success" | "warning" | "danger" | default | Visual style of the badge |
size | "sm" | "md" | "lg" | md | Badge size |
pill | boolean | false | Renders the badge with fully rounded corners |
dot | boolean | false | Renders a small circular indicator without visible content |
count | number | — | Numeric value for the badge; used with overflow handling |
max | number | — | Maximum count before displaying as "max+" |
href | string | — | Renders the badge as an anchor element |
onClick | () => void | — | Renders the badge as a button |
ariaLabel | string | — | Custom accessible label; auto-generated if omitted for numbers |
iconLeft | React.ReactNode | — | Optional icon before content |
iconRight | React.ReactNode | — | Optional icon after content |
children | React.ReactNode | — | Content inside the badge |
className | string | "" | Additional CSS classes |
title | string | — | Native title tooltip |
...rest | React.HTMLAttributes<HTMLElement> | — | Forwarded native attributes |
Behavior Notes
- Automatically renders as
<span>,<a>(whenhrefis provided), or<button>(whenonClickis provided). - Numeric badges automatically generate an accessible label (e.g.,
"5 notifications"). - Dot badges hide content visually but remain accessible via
ariaLabel. - The
pillprop renders the badge with fully rounded corners.
<Badge count={120} max={99} pill>
120
</Badge>
Accessibility
- Renders as
<span>by default,<a>withhref, or<button>withonClick. - Keyboard focus is enabled for interactive variants.
- Automatically generates
aria-labelfor numeric badges. - Always provide
ariaLabelfor dot-only or icon-only badges.
<Badge dot ariaLabel="Online status" />
Layout
- Uses
inline-flexby default. - Maintains consistent spacing between icons and content.
- Use
BadgeGroupto manage multiple badges with overflow handling:
<BadgeGroup
max={3}
items={[
<Badge>React</Badge>,
<Badge>Next.js</Badge>,
<Badge>TypeScript</Badge>,
<Badge>Node</Badge>,
]}
/>
- Supports
classNameandstylefor layout customization.
Best Practices
Do
- Use badges for short labels, counts, and status indicators.
- Use
dotfor presence or notification indicators. - Group multiple badges using
BadgeGroupfor consistent spacing. - Provide
ariaLabelfor numeric-only or icon-only badges.
Don’t
- Avoid long or descriptive text inside badges.
- Do not use badges as primary action buttons unless explicitly interactive.
- Do not hide critical information behind unlabeled dot indicators.