suiss/ui · Component
Badge
A small inline label for status, counts or metadata.
Badge is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Badge is a single element that renders a span by default and can render as another element, such as a link. Badges can hold text and an icon on either side.
Use a badge to tag content with a short status or category. For actions, use Button instead.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Variants
Use the variant prop to change the look of the badge. default, secondary, destructive and link are text only, outline adds a border and ghost adds horizontal padding.
With icon
Mark an icon with data-icon="inline-start" or data-icon="inline-end" so the outline and ghost variants tighten their padding on that side. Icons are sized to size-3.
As link
Use the render prop to render the badge as a link: render={<a href="/" />}. Linked badges get hover styles that depend on the variant.
Set asChild and pass a link as the only child to render the badge as a link. Linked badges get hover styles that depend on the variant.
Pass a render function that returns a link and spread the props it receives onto it. Linked badges get hover styles that depend on the variant.
Custom color
Override the text color with a className such as text-green-600 dark:text-green-400.
Accessibility
- A badge is static text by default and is not focusable.
- When rendered as a link, it shows a focus ring on keyboard focus.
aria-invalidadds aborder-destructiveborder.
API Reference
Badge
The Badge component displays a short inline label.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "secondary" | "destructive" | "outline" | "ghost" | "link" | "default" |
className | string | - |
Also accepts the props of useRender.ComponentProps<"span">.
| Prop | Type | Default |
|---|---|---|
asChild | boolean | false |
Also accepts the props of React.ComponentProps<"span">.
| Prop | Type | Default |
|---|---|---|
render | (props: React.HTMLAttributes<HTMLElement>) => React.ReactNode | - |
Also accepts the props of React.ComponentProps<"span">.
Badges are a fixed h-5 with fully rounded corners and do not wrap text. The destructive variant also tints the focus ring. Style a badge through data-slot="badge" and data-variant.
Web preview
Badge is a compact pill View that passes a small, bold text style and a 12-point icon size to its children. Six variants are available. default, secondary, destructive and link show colored text with no background, while outline adds a hairline border and ghost adds horizontal padding.
Use default for highlighted labels, secondary for neutral metadata, destructive for errors and outline when the badge needs a visible edge.
Installation
- Install the following dependencies:
- Copy and paste the following code into
components/ui/badge.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy and paste the following code into
components/ui/badge.tsx:
- Update the import paths to match your project setup.
- Copy and paste the following code into
components/ui/badge.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Variants
All six variants side by side.
Web preview
With icon
Badges with a leading icon.
Web preview
Accessibility
- The badge adds no role; it is read as plain text.
- If you render it as a pressable element, give that element a role and label.
API Reference
Badge
The Badge component displays a short inline label.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "secondary" | "destructive" | "outline" | "ghost" | "link" | "default" |
render | RenderProp | - |
Also accepts the props of ViewProps.
Put the label in a Text and icons in an Icon. Use render to render the badge as another component, such as a Pressable, while keeping its styles. badgeVariants and badgeTextVariants are also exported if you need the classes on another element. Text colors come from text-primary, text-muted-foreground, text-destructive and text-foreground.
Badge
The Badge component displays a short inline label.
| Parameter | Type | Default |
|---|---|---|
_ variant | Variant: .default | .secondary | .destructive | .outline | .ghost | .link | .default |
label | () -> Label | - |
_ title | LocalizedStringKey | - |
variant | Variant: .default | .secondary | .destructive | .outline | .ghost | .link | .default |
Overloads: init(_:label:), init(_:variant:).
BadgeVariant
Values: Default, Secondary, Destructive, Outline, Ghost, Link.
Badge
The Badge component displays a short inline label.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
variant | BadgeVariant: Default | Secondary | Destructive | Outline | Ghost | Link | BadgeVariant.Default |
content | @Composable RowScope.() -> Unit | - |
text | String | - |
Overloads: Badge(content), Badge(text).