Badge

A compact count, status label, or dot indicator.

VerifiedSince 0.3.4

Usage

Variants

Loading demo...

Pop Motion

Toggling open slides the badge in or pops it out, keeping its layout box; numbers roll only the digits that change.

Loading demo...

Custom Slot Content

<template>
  <TxBadge variant="primary">
    <strong>Beta</strong>
  </TxBadge>
</template>

Custom Color

<template>
  <TxBadge value="Internal" color="#111827" />
</template>

Dot Indicator

<template>
  <TxFlex align="center" gap="8px">
    <TxBadge dot variant="error" />
    <span>Service degraded</span>
  </TxFlex>
</template>

Best Practices

  • Use TxBadge for counts and short states; use TxStatusBadge when you need a status icon or permission mapping.
  • Keep the text short; badges never wrap.
  • Position corner badges in the host; bind open to the unread state and add no transform of your own.
  • Don't rely on dot color alone for critical states; put text next to it.
  • Use variant for semantic states; reserve color for brand or custom categories.

API Reference

Props

PropTypeDefaultDescription
variant'default' | 'primary' | 'success' | 'warning' | 'error''default'Semantic color preset.
valuenumber | string0Text when there is no slot and dot is off; numbers roll by digit.
colorstring-Custom background; turns the text white.
dotbooleanfalseRenders a dot with no text.
openbooleantruePresence; toggling plays the motion, and the closed badge keeps its layout box.

Slots

SlotPropsDescription
default-Custom content; takes priority over value and is ignored with dot.

CSS Variables

VariableSourceDescription
--tx-badge-bgvariant / colorBackground.
--tx-badge-textvariant / colorText color.
--tx-badge-bordervariantBorder color.
--tx-badge-dotcolorFill of the dot; falls back to the text color.
--tx-badge-offset-x / --tx-badge-offset-ycallerSlide-in start offset; defaults -8.2px / 12.4px.
--tx-badge-slide-durcallerSlide-in duration; default 260ms.
--tx-badge-pop-dur / --tx-badge-pop-close-durcallerPop-in / pop-out duration; defaults 500ms / 180ms.
--tx-badge-fade-dur / --tx-badge-fade-close-durcallerFade-in / fade-out duration; defaults 400ms / 180ms.
--tx-badge-blurcallerBlur of the closed state; default 2px.
--tx-badge-slide-ease / --tx-badge-pop-ease / --tx-badge-close-easecallerEasing of the three motion phases.

Overview

  • The root is an inline pill span.
  • With dot, only .tx-badge__dot renders; otherwise the default slot wins over value.
  • value defaults to 0, so a badge with no slot and no dot shows 0.
  • A numeric value rolls by place value (9 → 10 grows a digit instead of swapping the number); strings and slot content render as-is.
  • The open motion plays only on toggles, never on first mount; under reduced motion it is off and numbers update in place.
  • There is no click event, positioning, overflow truncation, or anchoring; attach interactions to the surrounding control.

Technologies

  • Numbers render through TxTextMorph; the pill's width follows its container animation instead of being measured.
  • Source: packages/tuffex/packages/components/src/badge/.
查看源码
packages/tuffex/packages/components/src/badge/index.ts