All components
Badge
Label state and metadata with palette-aware solid, subtle, surface, and outline styles.
Badge
12345678910111213141516171819import { Badge } from "#components/ui/base-ui/badge.tsx";import { indigoPalette } from "@stylexblocks/ui/themes/palettes.ts";
export function Example() { return ( <> {/* variant + size props resolve through the StyleX recipe and are exposed as data-variant / data-size attributes */} <Badge variant="surface" size="md" palette={indigoPalette}> New </Badge>
{/* composition: the render prop replaces the underlying element */} <Badge variant="solid" size="sm" render={<a href="/changelog" />}> 12 new blocks </Badge> </> );}Variants
Badgesolid
Badgesubtle
Badgesurface
Badgeoutline
Badgeplain
Sizes
Badgexs
Badgesm
Badgemd
Badgelg
Install
1npx stylexblocks add badgeSource
components/ui/base-ui/badge.tsx
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394import * as stylex from "@stylexjs/stylex";import { createComponent } from "#lib/base-ui/create-component.tsx";import { defineRecipe } from "#lib/recipe-props.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'import { textStyles } from '@stylexblocks/ui/text-styles.ts'
const badgeStyles = stylex.create({ base: { display: 'inline-flex', alignItems: 'center', borderRadius: radii.full, gap: spacing._1, fontWeight: fontWeights.medium, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap', userSelect: 'none', },})
const badgeVariants = stylex.create({ solid: { backgroundColor: colorPalette.solid, color: colorPalette.contrast, }, subtle: { backgroundColor: colorPalette.subtle, color: colorPalette.fg, }, outline: { color: colorPalette.fg, boxShadow: 'inset 0 0 0px 1px var(--shadow-color)', '--shadow-color': colorPalette.border, }, surface: { backgroundColor: colorPalette.muted, color: colorPalette.fg, boxShadow: 'inset 0 0 0px 1px var(--shadow-color)', '--shadow-color': colorPalette.border, }, plain: { color: colorPalette.fg, },})
const badgeSizes = stylex.create({ xs: { paddingInline: spacing._1, minHeight: sizes._4, }, sm: { paddingInline: spacing._1_5, minHeight: sizes._5, }, md: { paddingInline: spacing._2, minHeight: sizes._6, }, lg: { paddingInline: spacing._2_5, minHeight: sizes._7, },})
const badgeTextStyles = { xs: textStyles._2xs, sm: textStyles.xs, md: textStyles.sm, lg: textStyles.sm,} as const
export type BadgeVariant = keyof typeof badgeVariants
export type BadgeSize = keyof typeof badgeSizes
/** * Badge — recipe and component colocated in one file. Variant/size props * resolve mechanically from the axis maps; `extend` carries the per-size * text styles. Composition via `render`. */const badgeRecipe = defineRecipe({ styles: badgeStyles, axes: { variant: badgeVariants, size: badgeSizes }, defaultVariants: { variant: "surface", size: "md" }, extend: (v) => [badgeTextStyles[v.size]],});
export const Badge = createComponent("span", badgeRecipe, "Badge");
export type BadgeProps = React.ComponentProps<typeof Badge>;