Repository navigation
Expand file tree
/
Copy pathcheckbox.tsx
More file actions
109 lines (101 loc) · 4.28 KB
/
Copy pathcheckbox.tsx
File metadata and controls
109 lines (101 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox";
import { cn } from "~/lib/utils";
const rootClassName =
"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";
const indicatorClassName =
"-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";
const variantClassNames = {
default: "",
contrast: "border-foreground/55",
};
type CheckboxVariant = keyof typeof variantClassNames;
function Checkbox({
className,
variant = "default",
...props
}: CheckboxPrimitive.Root.Props & { variant?: CheckboxVariant }) {
return (
<CheckboxPrimitive.Root
className={cn(rootClassName, variantClassNames[variant], className)}
data-slot="checkbox"
{...props}
>
{/* Keep an inner gap in the focus outline. Explicit glyph colors resist row icon-muting rules. */}
<CheckboxPrimitive.Indicator
className={indicatorClassName}
data-slot="checkbox-indicator"
render={(props, state) => (
<span {...props}>
{state.indeterminate ? (
<svg
className="size-3.5 text-current sm:size-3"
fill="none"
height="24"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="3"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M5.252 12h13.496" />
</svg>
) : (
<svg
className="size-3.5 text-current sm:size-3"
fill="none"
height="24"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="3"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
</svg>
)}
</span>
)}
/>
</CheckboxPrimitive.Root>
);
}
// Checkbox's look without its hidden input, for a row that is itself the button.
function CheckboxMark({
checked,
variant = "default",
}: {
checked: boolean;
variant?: CheckboxVariant;
}) {
const state = checked ? { "data-checked": "" } : { "data-unchecked": "" };
return (
<span
aria-hidden
className={cn(rootClassName, variantClassNames[variant])}
data-slot="checkbox"
{...state}
>
<span className={indicatorClassName} data-slot="checkbox-indicator" {...state}>
<svg
className="size-3.5 text-current sm:size-3"
fill="none"
height="24"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="3"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M5.252 12.7 10.2 18.63 18.748 5.37" />
</svg>
</span>
</span>
);
}
export { Checkbox, CheckboxMark };