badge.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import * as React from "react"
  2. import { Slot } from "@radix-ui/react-slot"
  3. import { cva, type VariantProps } from "class-variance-authority"
  4. import { cn } from "@/lib/utils"
  5. const badgeVariants = cva(
  6. "inline-flex items-center justify-center rounded-md border 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",
  7. {
  8. variants: {
  9. variant: {
  10. default:
  11. "border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90",
  12. secondary:
  13. "border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",
  14. destructive:
  15. "border-transparent 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. "text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",
  18. },
  19. },
  20. defaultVariants: {
  21. variant: "default",
  22. },
  23. }
  24. )
  25. function Badge({
  26. className,
  27. variant,
  28. asChild = false,
  29. ...props
  30. }: React.ComponentProps<"span"> &
  31. VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
  32. const Comp = asChild ? Slot : "span"
  33. return (
  34. <Comp
  35. data-slot="badge"
  36. className={cn(badgeVariants({ variant }), className)}
  37. {...props}
  38. />
  39. )
  40. }
  41. export { Badge, badgeVariants }