Badge
Display badge, pill or tag
Polymorphic
Source
LLM docs
Docs
PackageIcon
Usage
Gradient variant
When the variant prop is set to gradient, you can control the gradient with the gradient prop, which accepts an object with from, to and deg properties. If thegradient prop is not set, Badge will use theme.defaultGradient which can be configured on the theme object. The gradient prop is ignored when variant is not gradient.
Note that variant="gradient" supports only linear gradients with two colors. If you need a more complex gradient, use the Styles API to modify Badge styles.
Rounded
Set the circle prop to reduce horizontal padding and make the badge width equal to its height:
Left and right sections
Full width
Set fullWidth to make the badge span the full width of its parent element:
Customize variant colors
You can customize colors for Badge and other component variants by adding
variantColorResolver to your theme.
autoContrast
Badge supports the autoContrast prop and theme.autoContrast. If autoContrast is set either on Badge or on the theme, the content color will be adjusted to have sufficient contrast with the value specified in the color prop.
Note that the autoContrast feature works only if you use the color prop to change the background color. autoContrast works only with the filled variant.
Styles API
Badge supports the Styles API; you can add styles to any inner element of the component with the classNames prop. Follow the Styles API documentation to learn more.
Component Styles API
Hover over selectors to highlight corresponding elements
Polymorphic component
Badge is a polymorphic component – its default root element is div, but it can be changed to any other element or component with the component prop:
You can also use components in the component prop, for example, Next.js Link:
Polymorphic components with TypeScript
Note that polymorphic component prop types are different from regular components – they do not extend HTML element props of the default element. For example,
BadgePropsdoes not extendReact.ComponentProps'<'div'>'althoughdivis the default element.If you want to create a wrapper for a polymorphic component that is not polymorphic (does not support the
componentprop), then your component props interface should extend HTML element props, for example:If you want your component to remain polymorphic after wrapping, use the
polymorphicfunction described in this guide.