Open History to see the last commit
1import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox";
2
3import { cn } from "~/lib/utils";
4
5const rootClassName =
6 "relative inline-flex size-4.5 shrink-0 items-center justify-center rounded-[.25rem] border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[3px] not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-ring aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:outline-destructive/48 data-disabled:opacity-64 sm:size-4 dark:not-data-checked:bg-input/32 dark:focus-visible:aria-invalid:outline-destructive/24 dark:not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)] [[data-disabled],[data-checked],[aria-invalid]]:shadow-none";
7
8const indicatorClassName =
9 "-inset-px absolute flex items-center justify-center rounded-[.25rem] text-primary-foreground data-unchecked:hidden data-checked:bg-primary data-indeterminate:text-foreground in-[[data-slot=checkbox]:focus-visible]:inset-shadow-[0_0_0_3px_var(--background)] in-[[data-slot=checkbox]:focus-visible]:outline-2 in-[[data-slot=checkbox]:focus-visible]:outline-solid in-[[data-slot=checkbox]:focus-visible]:-outline-offset-2 in-[[data-slot=checkbox]:focus-visible]:outline-inherit";
10
11function Checkbox({ className, ...props }: CheckboxPrimitive.Root.Props) {
12 return (
13 <CheckboxPrimitive.Root
14 className={cn(rootClassName, className)}
15 data-slot="checkbox"
16 {...props}
17 >
18 {/* Keep an inner gap in the focus outline. Explicit glyph colors resist row icon-muting rules. */}
19 <CheckboxPrimitive.Indicator
20 className={indicatorClassName}
21 data-slot="checkbox-indicator"
22 render={(props, state) => (
23 <span {...props}>
24 {state.indeterminate ? (
25 <svg
26 className="size-3.5 text-current sm:size-3"
27 fill="none"
28 height="24"
29 stroke="currentColor"
30 strokeLinecap="round"
31 strokeLinejoin="round"
32 strokeWidth="3"
33 viewBox="0 0 24 24"
34 width="24"
35 xmlns="http://www.w3.org/2000/svg"
36 >
37 <path d="M5.252 12h13.496" />
38 </svg>
39 ) : (
40 <svg
41 className="size-3.5 text-current sm:size-3"
42 fill="none"
43 height="24"
44 stroke="currentColor"
45 strokeLinecap="round"
46 strokeLinejoin="round"
47 strokeWidth="3"
48 viewBox="0 0 24 24"
49 width="24"
50 xmlns="http://www.w3.org/2000/svg"
51 >
52 <path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
53 </svg>
54 )}
55 </span>
56 )}
57 />
58 </CheckboxPrimitive.Root>
59 );
60}
61
62// Checkbox's look without its hidden input, for a row that is itself the button.
63function CheckboxMark({ checked }: { checked: boolean }) {
64 const state = checked ? { "data-checked": "" } : { "data-unchecked": "" };
65 return (
66 <span aria-hidden className={rootClassName} data-slot="checkbox" {...state}>
67 <span className={indicatorClassName} data-slot="checkbox-indicator" {...state}>
68 <svg
69 className="size-3.5 text-current sm:size-3"
70 fill="none"
71 height="24"
72 stroke="currentColor"
73 strokeLinecap="round"
74 strokeLinejoin="round"
75 strokeWidth="3"
76 viewBox="0 0 24 24"
77 width="24"
78 xmlns="http://www.w3.org/2000/svg"
79 >
80 <path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
81 </svg>
82 </span>
83 </span>
84 );
85}
86
87export { Checkbox, CheckboxMark };