badge.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. 'h-5 gap-1 rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium transition-all has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&>svg]:size-3! inline-flex items-center justify-center w-fit whitespace-nowrap shrink-0 [&>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 overflow-hidden group/badge',
  7. {
  8. variants: {
  9. variant: {
  10. default: 'bg-primary text-primary-foreground [a]:hover:bg-primary/80',
  11. secondary: 'bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80',
  12. destructive:
  13. 'bg-destructive/10 [a]:hover:bg-destructive/20 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 text-destructive dark:bg-destructive/20',
  14. outline: 'border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground',
  15. ghost: 'hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50',
  16. link: 'text-primary underline-offset-4 hover:underline',
  17. },
  18. },
  19. defaultVariants: {
  20. variant: 'default',
  21. },
  22. },
  23. );
  24. function Badge({
  25. className,
  26. variant = 'default',
  27. asChild = false,
  28. ...props
  29. }: React.ComponentProps<'span'> & VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
  30. const Comp = asChild ? Slot.Root : 'span';
  31. return (
  32. <Comp
  33. data-slot="badge"
  34. data-variant={variant}
  35. className={cn(badgeVariants({ variant }), className)}
  36. {...props}
  37. />
  38. );
  39. }
  40. export { Badge, badgeVariants };