Presets

Blocks

Get PRO

Need premium blocks and templates? Upgrade to PRO and get access.

Upgrade
JoinLogin
Presets
Monochrome
Overview
  • Introduction
  • Components
  • Installation
Components
  • Accordion
  • Alert
  • Alert Dialog
  • Aspect Ratio
  • Avatar
  • Badge
  • Breadcrumb
  • Button
  • Button Group
  • CalendarNEW
  • Card
  • CarouselNEW
  • Checkbox
  • Collapsible
  • Dialog
  • Dropdown Menu
  • Empty
  • Field
  • Input
  • Input Group
  • Item
  • Kbd
  • Label
  • Native Select
  • Scroll Area
  • SelectNEW
  • Separator
  • SwitchNEW
  • Table
  • Tabs
  • Textarea
  • TooltipNEW

Badge

PreviousNext

Displays a badge or a component that looks like a badge.

1import { Badge } from "@/components/ui/badge";23export default function Preview() {4  return (

Installation

pnpm dlx sprawlify@latest add badge
npx sprawlify@latest add badge
yarn sprawlify@latest add badge
bunx --bun sprawlify@latest add badge

Install the following dependencies:

pnpm add @sprawlify/primitives @sprawlify/react
npm install @sprawlify/primitives @sprawlify/react
yarn add @sprawlify/primitives @sprawlify/react
bun add @sprawlify/primitives @sprawlify/react

Add the following files to your project:

badge.tsx
1"use client";23import * as React from "react";4import { cva, type VariantProps } from "class-variance-authority";5import { cn } from "@/lib/utils";6import { sprawlify } from "@sprawlify/react";78const badgeVariants = cva(9  "inline-flex items-center justify-center rounded-full border border-transparent px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden",10  {11    variants: {12      variant: {13        default: "bg-primary text-primary-foreground [a&]:hover:bg-primary/90",14        secondary: "bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",15        destructive:16          "bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",17        outline:18          "border-border text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",19        ghost: "[a&]:hover:bg-accent [a&]:hover:text-accent-foreground",20        link: "text-primary underline-offset-4 [a&]:hover:underline",21      },22    },23    defaultVariants: {24      variant: "default",25    },26  },27);2829function Badge({30  className,31  variant = "default",32  ...props33}: React.ComponentProps<typeof sprawlify.span> & VariantProps<typeof badgeVariants>) {34  return (35    <sprawlify.span36      data-scope="badge"37      data-part="root"38      data-slot="badge"39      data-variant={variant}40      className={cn(badgeVariants({ variant }), className)}41      {...props}42    />43  );44}4546export { Badge, badgeVariants };47

Update the import paths to match your project setup.

Usage

1import { Badge } from "@/components/ui/badge"
1<Badge variant="outline">Badge</Badge>

Examples

Custom Colors

You can customize the colors of a badge by adding custom classes such as bg-green-50 dark:bg-green-800 to the Badge component.

1import { Badge } from "@/components/ui/badge";23export default function Preview() {4  return (

Link

Use the asChild prop to render a link as a badge.

1import { Badge } from "@/components/ui/badge";2import { ArrowUpRightIcon } from "lucide-react";34export default function Preview() {

Variants

Use the variant prop to change the variant of the badge.

1import { Badge } from "@/components/ui/badge";23export default function Preview() {4  return (

With Icon

You can render an icon inside the badge. Use data-icon="inline-start" to render the icon on the left and data-icon="inline-end" to render the icon on the right.

1import { Badge } from "@/components/ui/badge";2import { BadgeCheck, BookmarkIcon } from "lucide-react";34export default function Preview() {

On This Page

InstallationUsageExamplesCustom ColorsLinkVariantsWith Icon

Get PRO

Need premium blocks and templates? Upgrade to PRO and get access.