badge.tsx 1.9 KB

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