badge.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { mergeProps } from '@base-ui/react/merge-props';
  2. import { useRender } from '@base-ui/react/use-render';
  3. import { cva, type VariantProps } from 'class-variance-authority';
  4. import { cn } from '@/lib/utils';
  5. const badgeVariants = cva(
  6. 'group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-all focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 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/80',
  11. secondary:
  12. 'bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80',
  13. destructive:
  14. 'bg-destructive/10 text-destructive focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-destructive/20',
  15. outline:
  16. 'border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground',
  17. ghost:
  18. 'hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50',
  19. link: 'text-primary underline-offset-4 hover:underline',
  20. },
  21. },
  22. defaultVariants: {
  23. variant: 'default',
  24. },
  25. },
  26. );
  27. function Badge({
  28. className,
  29. variant = 'default',
  30. render,
  31. ...props
  32. }: useRender.ComponentProps<'span'> & VariantProps<typeof badgeVariants>) {
  33. return useRender({
  34. defaultTagName: 'span',
  35. props: mergeProps<'span'>(
  36. {
  37. className: cn(badgeVariants({ variant }), className),
  38. },
  39. props,
  40. ),
  41. render,
  42. state: {
  43. slot: 'badge',
  44. variant,
  45. },
  46. });
  47. }
  48. export { Badge, badgeVariants };