Badge

updated

A small label with Button's variants and colours, five sizes, side-aware icon padding and a square form.

Demo
NewActiveDraft

Open Props in the corner of the example to try variant, color, size and square. xs to xl are 16 to 32px tall, four pixels apart, and the radius grows with the size.

variant and color are Button's: solid, soft, subtle, outline, ghost and link in primary, neutral, destructive, success, warning and info. A badge has no hover or pressed state; link underlines on hover only because it is meant to be rendered as a link.

Badges read the same colour variables as buttons, so a colour declared for a button works on a badge too: <Badge color="brand">.

Installation

Usage

<script setup lang="ts">
import { Badge } from '@/components/ui/badge'
</script>

<template>
  <Badge variant="soft" color="success">Active</Badge>
</template>

Examples

Icons

As in Button, an icon says which side it sits on with data-icon="inline-start" or data-icon="inline-end", and the badge tightens its padding on that side only. Icons without a size-* class take the size's icon size.

With icons
Label only Verified Next

Square

square gives the badge equal padding on all sides and at least its height as width: a single digit or an icon is a square, and a longer count keeps even padding. Give an icon-only badge an aria-label.

Square badge
11299+

as renders another element; as-child merges the badge onto the single element you pass.

Touch target

Smaller than a finger? Add touch-target: expand when it stands alone, wrapper when it sits close to others. How it works.

Only a badge you can tap needs it, one rendered as a link.

Touch target

API Reference

Data attributes

data-slotAttributesDescription
badgedata-variant, data-color, data-sizeThe root element, or your own element under as-child.

Changelog

  1. registry 0.22.0

    2b1e734 — A square Badge with touchTarget="wrapper" reserves room at its sides as well as above and below, so the 48px touch areas of neighbouring square badges no longer overlap.

  2. registry 0.21.0

    e5495ef — size works the same way everywhere. Every component exports its size type (AlertSize, BadgeSize, CardSize, CheckboxSize, RadioSize, SwitchSize, EmptySize, InputFloatingSize, ItemSize, ListboxSize, SeparatorSize, SliderSize, StepperSize, TabsSize, TextareaSize, TimelineSize) and the props take it without null; Button-based parts take ButtonSize, Input TextControlSize, SelectTrigger SelectSize and Tree TreeSize. ToggleGroup and ToolbarToggleGroup carry data-size on the group as well. Table's row heights moved into tableSize, so a class on DataTable's ui.table overrides --table-row-h at every size, not only at md.

  3. registry 0.9.0

    99c55af — Components size their icons with core's icon-size-* instead of [&_svg:not([class*='size-'])]:size-*, so class="icon-size-5" resizes the icons in a Button, a Badge or a menu item, and cn lets the later of two icon-size-* classes win. A Button tightens its padding beside a data-icon="inline-start" or "inline-end" icon on the logical side, so the right side in right-to-left text.

  4. registry 0.8.0

    a2a4d38 — Button and Alert: the outline variant no longer paints bg-background, so an outline button or alert on a tinted surface shows the surface through it, as Badge's outline already did.

  5. registry 0.6.0

    715f6fc — Components are coloured by tones. A colour is three inputs, --tone, --tone-foreground and --tone-text, set on [data-slot][data-color="<name>"]; core derives --tone-soft, --tone-soft-foreground, --tone-border and --tone-border-subtle from them and registers all seven as tone-* colours, so bg-tone, text-tone-text and the rest work as classes. The --c-* variables are gone: a rule that set --c, --c-fg, --c-soft, --c-soft-fg or --c-edge now sets --tone, --tone-foreground and --tone-text, and drops the rest. Button, Badge, Alert, Toggle, the choice controls and Toast read tones; Toast no longer exports toastAccents.

  6. core 0.5.0

    715f6fc — Components are coloured by tones. A colour is three inputs, --tone, --tone-foreground and --tone-text, set on [data-slot][data-color="<name>"]; core derives --tone-soft, --tone-soft-foreground, --tone-border and --tone-border-subtle from them and registers all seven as tone-* colours, so bg-tone, text-tone-text and the rest work as classes. The --c-* variables are gone: a rule that set --c, --c-fg, --c-soft, --c-soft-fg or --c-edge now sets --tone, --tone-foreground and --tone-text, and drops the rest. Button, Badge, Alert, Toggle, the choice controls and Toast read tones; Toast no longer exports toastAccents.

  7. registry 0.3.0

    e58b25a — Add an info colour: --info, --info-foreground and --info-text tokens, data-color="info", and info on Button, Badge and toasts.

  8. core 0.3.0

    e58b25a — Add an info colour: --info, --info-foreground and --info-text tokens, data-color="info", and info on Button, Badge and toasts.

  9. registry 0.2.0

    c935598 — Colour and variant are separate axes: [data-slot][data-color] rules in @kappa-ui/core/tailwind.css set --c-* variables that Button and Badge variants read, so a custom colour is one CSS rule plus color="<name>". Core gains the touch-target, touch-target-margin, touch-target-wrapper, state-halo, choice-row, slider-axis, slider-touch and slider-range-inset utilities and the scrollbar-gutter base rule. The choice-group, text-control, menu-styles and overlay registry items are gone: their styles now live in checkbox, input, menu and popover.

  10. core 0.2.0

    c935598 — Colour and variant are separate axes: [data-slot][data-color] rules in @kappa-ui/core/tailwind.css set --c-* variables that Button and Badge variants read, so a custom colour is one CSS rule plus color="<name>". Core gains the touch-target, touch-target-margin, touch-target-wrapper, state-halo, choice-row, slider-axis, slider-touch and slider-range-inset utilities and the scrollbar-gutter base rule. The choice-group, text-control, menu-styles and overlay registry items are gone: their styles now live in checkbox, input, menu and popover.