1
0

badge.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import * as React from "react";
  2. import { cva, type VariantProps } from "class-variance-authority";
  3. import { cn } from "@/lib/utils";
  4. const badgeVariants = cva(
  5. "inline-flex items-center gap-1 rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors",
  6. {
  7. variants: {
  8. variant: {
  9. default:
  10. "border-transparent bg-primary text-primary-foreground hover:bg-primary/80",
  11. secondary:
  12. "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
  13. destructive:
  14. "border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
  15. outline: "text-foreground hover:bg-accent/50",
  16. },
  17. },
  18. defaultVariants: {
  19. variant: "default",
  20. },
  21. }
  22. );
  23. export interface BadgeProps
  24. extends React.HTMLAttributes<HTMLDivElement>,
  25. VariantProps<typeof badgeVariants> {
  26. loading?: boolean;
  27. }
  28. function Badge({
  29. className,
  30. variant,
  31. onClick,
  32. loading,
  33. children,
  34. ...props
  35. }: BadgeProps) {
  36. return (
  37. <div
  38. className={cn(
  39. badgeVariants({ variant }),
  40. onClick && "cursor-pointer",
  41. "relative select-none",
  42. className
  43. )}
  44. onClick={onClick}
  45. role={onClick ? "button" : undefined}
  46. tabIndex={onClick ? 0 : undefined}
  47. {...props}
  48. >
  49. {loading && (
  50. <svg
  51. className="mr-1 h-3 w-3 animate-spin text-current"
  52. xmlns="http://www.w3.org/2000/svg"
  53. fill="none"
  54. viewBox="0 0 24 24"
  55. >
  56. <circle
  57. className="opacity-25"
  58. cx="12"
  59. cy="12"
  60. r="10"
  61. stroke="currentColor"
  62. strokeWidth="4"
  63. ></circle>
  64. <path
  65. className="opacity-75"
  66. fill="currentColor"
  67. d="M4 12a8 8 0 018-8v8H4z"
  68. ></path>
  69. </svg>
  70. )}
  71. {children}
  72. </div>
  73. );
  74. }
  75. export { Badge, badgeVariants };