badge.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 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",
  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 [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
  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. },
  20. },
  21. defaultVariants: {
  22. variant: "default",
  23. },
  24. },
  25. );
  26. function Badge({
  27. className,
  28. variant = "default",
  29. asChild = false,
  30. ...props
  31. }: React.ComponentProps<"span"> &
  32. VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
  33. const Comp = asChild ? Slot.Root : "span";
  34. return (
  35. <Comp
  36. data-slot="badge"
  37. data-variant={variant}
  38. className={cn(badgeVariants({ variant }), className)}
  39. {...props}
  40. />
  41. );
  42. }
  43. export { Badge, badgeVariants };