Badge
Display badge, pill or tag
Import
Polymorphic
Source
Docs
Package
Usage
Gradient variant
When variant
prop is set to gradient
, you can control gradient with gradient
prop, it 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. 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, then use Styles API to modify Badge
styles.
Rounded
Set circle
prop, to reduce horizontal padding and make badge width equal to its height:
Left and right sections
Full width
Set fullWidth
to make badge span full width of its parent element:
Customize variants colors
You can customize colors for Badge
and other components variants by adding
variantColorResolver to your theme.
autoContrast
Badge
supports autoContrast
prop and theme.autoContrast. If autoContrast
is set either on Badge
or on theme, content color will be adjusted to have sufficient contrast with the value specified in color
prop.
Note that autoContrast
feature works only if you use color
prop to change background color. autoContrast
works only with filled
variant.
Styles API
Badge
supports Styles API, you can add styles to any inner element of the component withclassNames
prop. Follow 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 component
prop:
You can also use components in component
prop, for example, Next.js Link
:
Polymorphic components with TypeScript
Note that polymorphic components props types are different from regular components – they do not extend HTML element props of the default element. For example,
BadgeProps
does not extendReact.ComponentPropsWithoutRef'<'div'>'
althoughdiv
is the default element.If you want to create a wrapper for a polymorphic component that is not polymorphic (does not support
component
prop), then your component props interface should extend HTML element props, for example:If you want your component to remain polymorphic after wrapping, use
createPolymorphicComponent
function described in this guide.