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/solid
npm install @sprawlify/primitives @sprawlify/solid
yarn add @sprawlify/primitives @sprawlify/solid
bun add @sprawlify/primitives @sprawlify/solid

Add the following files to your project:

badge.tsx
1import { cva, type VariantProps } from "class-variance-authority";2import { cn } from "@/lib/utils";3import { sprawlify } from "@sprawlify/solid";4import type { ComponentProps } from "solid-js";5import { splitProps } from "solid-js";67const badgeVariants = cva(8  "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",9  {10    variants: {11      variant: {12        default: "bg-primary text-primary-foreground [a&]:hover:bg-primary/90",13        secondary: "bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",14        destructive:15          "bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",16        outline:17          "border-border text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",18        ghost: "[a&]:hover:bg-accent [a&]:hover:text-accent-foreground",19        link: "text-primary underline-offset-4 [a&]:hover:underline",20      },21    },22    defaultVariants: {23      variant: "default",24    },25  },26);2728function Badge(props: ComponentProps<typeof sprawlify.span> & VariantProps<typeof badgeVariants>) {29  const [local, others] = splitProps(props, ["variant", "class"]);30  const variant = () => local.variant ?? "default";3132  return (33    <sprawlify.span34      data-scope="badge"35      data-part="root"36      data-slot="badge"37      data-variant={variant()}38      class={cn(badgeVariants({ variant: variant(), class: local.class }))}39      {...others}40    />41  );42}4344export { Badge, badgeVariants };45

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-solid";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-solid";34export default function Preview() {

On This Page

InstallationUsageExamplesCustom ColorsLinkVariantsWith Icon

Get PRO

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