| 1 | "use client"; |
| 2 | |
| 3 | import { mergeProps } from "@base-ui/react/merge-props"; |
| 4 | import { useRender } from "@base-ui/react/use-render"; |
| 5 | import { cva, type VariantProps } from "class-variance-authority"; |
| 6 | |
| 7 | import { cn } from "~/lib/utils"; |
| 8 | |
| 9 | const badgeVariants = cva( |
| 10 | "relative inline-flex shrink-0 items-center justify-center gap-1 whitespace-nowrap rounded-sm border border-transparent font-medium outline-none transition-shadow focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-3.5 sm:[&_svg:not([class*='size-'])]:size-3 [&_svg]:pointer-events-none [&_svg]:shrink-0 [button&,a&]:cursor-pointer [button&,a&]:pointer-coarse:after:absolute [button&,a&]:pointer-coarse:after:size-full [button&,a&]:pointer-coarse:after:min-h-11 [button&,a&]:pointer-coarse:after:min-w-11", |
| 11 | { |
| 12 | defaultVariants: { |
| 13 | size: "default", |
| 14 | variant: "default", |
| 15 | }, |
| 16 | variants: { |
| 17 | size: { |
| 18 | control: |
| 19 | "h-7 min-w-7 rounded-[var(--control-radius)] px-[calc(--spacing(2)-1px)] text-sm sm:h-6 sm:min-w-6 sm:text-xs", |
| 20 | default: |
| 21 | "h-5.5 min-w-5.5 px-[calc(--spacing(1)-1px)] text-sm sm:h-4.5 sm:min-w-4.5 sm:text-xs", |
| 22 | lg: "h-6.5 min-w-6.5 px-[calc(--spacing(1.5)-1px)] text-base sm:h-5.5 sm:min-w-5.5 sm:text-sm", |
| 23 | // leading-none: with the inherited fractional leading the rounded font metrics |
| 24 | // leave the label sitting high in the fixed-height box, worse under renderer zoom. |
| 25 | sm: "h-5 min-w-5 rounded-[.25rem] px-[calc(--spacing(1)-1px)] text-xs leading-none sm:h-4 sm:min-w-4 sm:text-[.625rem]", |
| 26 | }, |
| 27 | variant: { |
| 28 | default: "bg-primary text-primary-foreground [button&,a&]:hover:bg-primary/90", |
| 29 | destructive: "bg-destructive text-white [button&,a&]:hover:bg-destructive/90", |
| 30 | error: "bg-destructive/8 text-destructive-foreground dark:bg-destructive/16", |
| 31 | info: "bg-info/8 text-info-foreground dark:bg-info/16", |
| 32 | outline: |
| 33 | "border-input bg-background text-foreground dark:bg-input/32 [button&,a&]:hover:bg-accent/50 dark:[button&,a&]:hover:bg-input/48", |
| 34 | secondary: "bg-secondary text-secondary-foreground [button&,a&]:hover:bg-secondary/90", |
| 35 | success: "bg-success/8 text-success-foreground dark:bg-success/16", |
| 36 | warning: "bg-warning/8 text-warning-foreground dark:bg-warning/16", |
| 37 | // A code-host label tinted from the --label color the consumer sets in style. |
| 38 | label: |
| 39 | "bg-[color-mix(in_srgb,var(--label)_8%,transparent)] text-[color-mix(in_srgb,var(--label)_30%,var(--color-foreground))] dark:bg-[color-mix(in_srgb,var(--label)_12%,transparent)] dark:text-[color-mix(in_srgb,var(--label)_45%,var(--color-foreground))]", |
| 40 | }, |
| 41 | }, |
| 42 | }, |
| 43 | ); |
| 44 | |
| 45 | interface BadgeProps extends useRender.ComponentProps<"span"> { |
| 46 | variant?: VariantProps<typeof badgeVariants>["variant"]; |
| 47 | size?: VariantProps<typeof badgeVariants>["size"]; |
| 48 | } |
| 49 | |
| 50 | function Badge({ className, variant, size, render, ...props }: BadgeProps) { |
| 51 | const defaultProps = { |
| 52 | className: cn(badgeVariants({ className, size, variant })), |
| 53 | "data-slot": "badge", |
| 54 | }; |
| 55 | |
| 56 | return useRender({ |
| 57 | defaultTagName: "span", |
| 58 | props: mergeProps<"span">(defaultProps, props), |
| 59 | render, |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | // The *Variants function is for components/ui modules only. |
| 64 | export { Badge, badgeVariants }; |