Open History to see the last commit
1import { cva } from 'class-variance-authority'
2import { ark } from '@ark-ui/solid/factory'
3import { splitProps } from 'solid-js'
4import type { VariantProps } from 'class-variance-authority'
5import type { HTMLArkProps } from '@ark-ui/solid/factory'
6import { cn } from '@/utils'
7β
8// Ported from the original shadcn/ui + Radix UI + React component.
9// This wraps an Ark UI factory element and preserves shadcn's Tailwind-based
10// design-system API, variants, and styling.
11// https://ui.shadcn.com/
12β
13const badgeVariants = cva(
14 '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!',
15 {
16 variants: {
17 variant: {
18 default: 'bg-primary text-primary-foreground [a]:hover:bg-primary/80',
19 secondary:
20 'bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80',
21 destructive:
22 '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',
23 outline:
24 'border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground',
25 ghost:
26 'hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50',
27 link: 'text-primary underline-offset-4 hover:underline',
28 },
29 },
30 defaultVariants: {
31 variant: 'default',
32 },
33 },
34)
35β
36type BadgeProps = HTMLArkProps<'span'> & VariantProps<typeof badgeVariants>
37β
38/**
39 * A compact label for statuses, categories, and counts.
40 *
41 * Use Ark's render-function `asChild` prop when the badge should render as a
42 * link or another element.
43 *
44 * Add `data-icon="inline-start"` or `data-icon="inline-end"` to child icons
45 * and spinners so the badge adjusts its padding for their position. Customize
46 * the badge's colors with Tailwind utilities passed through `class`.
47 *
48 * @example
49 * ```tsx
50 * <Badge variant="secondary">
51 * <CheckIcon data-icon="inline-start" />
52 * Verified
53 * </Badge>
54 *
55 * <Badge
56 * class="bg-green-50 text-green-700 dark:bg-green-800 dark:text-green-100"
57 * >
58 * Available
59 * </Badge>
60 * ```
61 */
62function Badge(props: BadgeProps) {
63 const [local, others] = splitProps(props, ['class', 'variant'])
64β
65 const variant = () => local.variant ?? 'default'
66β
67 return (
68 <ark.span
69 {...others}
70 data-slot="badge"
71 data-variant={variant()}
72 class={cn(badgeVariants({ variant: variant() }), local.class)}
73 />
74 )
75}
76β
77export { Badge, badgeVariants, type BadgeProps }