组件

Badge

A small mark that sits next to a word: a marker stamp, a highlighter swipe, a red-hatched warning, a circled word, and a count that can be written as tally marks. Same variants and asChild as shadcn.

安装

terminal
npx shadcn@latest add @wobko/badge

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Variants

A marker stamp, a highlighter swipe, a red-hatched pill, a circled word, and two that only show up when they are links.

Sizes, icons and tones

Three heights, an icon slot in front of the label (the icon keeps its own 14 px drawing), and the stationery accents for the marker fill.

Counts and tally marks

A number in tabular figures that stops at 99+, and the same number written the way a person keeps score: four uprights and a slash. Screen readers get the digits either way.

As a link

asChild hands the badge to your own element. Hover a link badge: the filled ones lift a drop-line, the outline gets a highlighter wash, ghost swipes and link underlines. Tab to it for the felt ring.

Release notes

Badges doing real work in a card: a status next to the title, the kind of each change in front of its line, and a tally of how many are left to read.

API 参考

Badge

A span (or, with asChild, the element you hand it) with a hand-drawn layer behind its label. The label is DOM text in the hand font; the drawing is measured from the element and centered on it. Unlike shadcn's it has no overflow-hidden (the drawing bleeds a few px outside the box, and clipping would cut it), so to truncate a long label put a truncating span inside it. It draws no aria-invalid state. Each badge is one small ink host (about 2 to 6 KB of svg, drawn at 0.3 to 0.8 ms): fine for a screenful, and for a status column of a few hundred rows if you page it; the ghost variant draws nothing until it is a hovered link.

属性类型默认值说明
variant"default" | "secondary" | "destructive" | "outline" | "ghost" | "link""default"default = a terracotta marker blot that misses a felt-tip pill; secondary = one highlighter swipe; destructive = red-pencil hatch in a red pill (a pattern, not only a hue); outline = the label circled once; ghost and link draw nothing until they are hovered as links.
size"sm" | "default" | "lg""default"20, 24 and 28 px high. sm sets its label at 13 px: keep it for dense tables, use default anywhere a badge stands alone.
tone"marker" | "hi" | "tangerine" | "lemon" | "grass" | "teal" | "sky" | "bubblegum""marker"The stationery accent of the default variant's fill. Tomato and grape are left out on purpose: ink on them is 3.95:1.
countnumber-A number shown when there are no children, in tabular figures and a min-width, so a column of counts lines up.
maxnumber99Largest count written in digits; a bigger one shows as `${max}+`.
tallybooleanfalseWrite `count` (1 to 20) as tally marks instead of digits: four uprights and a slash per five. The number stays in the DOM, visually hidden, so screen readers still read it.
asChildbooleanfalseRender the child element (a link, a button) with the badge's classes and drawing, drawn inside it (Radix Slot). A badge that is a link or has an onClick also gets a hover layer. count and tally are ignored.
seedstring | number-Pins the hand: the same seed always draws the same badge, also across remounts. Default: derived from useId().

badgeVariants

The cva function behind Badge, for styling another element like a badge. Layout and typography classes only: the drawing comes from Badge.

属性类型默认值说明
variantBadgeVariant"default"Same values as Badge.
sizeBadgeSize"default"Same values as Badge.
classNamestring-Merged last.

无障碍

  • A badge is plain inline text in a span: it is read in reading order with what is around it. Give a status badge words ("Beta"), never only a color or a mark.
  • A tally is decoration for the eye; the number is real text in the DOM (visually hidden), so `<Badge tally count={7}>` reads as "7".
  • Ink on the marker fill is 4.6:1, on the highlighter 12:1; destructive is told apart by its hatch and its word as well as its red.
  • As a link (asChild), the badge keeps the link's role and href, takes keyboard focus and draws the blue felt ring; hover adds a drop-line, a swipe or an underline and is off on touch screens.
  • Windows high contrast replaces the drawing with a real 1 px outline. Reduced motion removes the hover tilt and the fill nudge.

源码

CLI 会复制进你项目的文件。

正在加载源码