|
1
|
"use client" |
|
2
|
|
|
3
|
import { VariantProps, cva } from "cva" |
|
4
|
import { Switch as RadixSwitch } from "radix-ui" |
|
5
|
import * as React from "react" |
|
6
|
|
|
7
|
import { clx } from "@/utils/clx" |
|
8
|
|
|
9
|
const switchVariants = cva({ |
|
10
|
base: "bg-ui-bg-switch-off hover:bg-ui-bg-switch-off-hover data-[state=unchecked]:hover:after:bg-switch-off-hover-gradient before:shadow-details-switch-background focus-visible:shadow-details-switch-background-focus data-[state=checked]:bg-ui-bg-interactive disabled:opacity-50 group relative inline-flex items-center rounded-full outline-none transition-all before:absolute before:inset-0 before:rounded-full before:content-[''] after:absolute after:inset-0 after:rounded-full after:content-[''] disabled:cursor-not-allowed", |
|
11
|
variants: { |
|
12
|
size: { |
|
13
|
small: "h-[16px] w-[28px]", |
|
14
|
base: "h-[18px] w-[32px]", |
|
15
|
}, |
|
16
|
}, |
|
17
|
defaultVariants: { |
|
18
|
size: "base", |
|
19
|
}, |
|
20
|
}) |
|
21
|
|
|
22
|
const thumbVariants = cva({ |
|
23
|
base: "bg-ui-fg-on-color shadow-details-switch-handle pointer-events-none h-[14px] w-[14px] rounded-full transition-all", |
|
24
|
variants: { |
|
25
|
size: { |
|
26
|
small: |
|
27
|
"h-[12px] w-[12px] data-[state=checked]:translate-x-3.5 data-[state=unchecked]:translate-x-0.5", |
|
28
|
base: "h-[14px] w-[14px] transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0.5", |
|
29
|
}, |
|
30
|
}, |
|
31
|
defaultVariants: { |
|
32
|
size: "base", |
|
33
|
}, |
|
34
|
}) |
|
35
|
|
|
36
|
interface SwitchProps |
|
37
|
extends Omit< |
|
38
|
React.ComponentPropsWithoutRef<typeof RadixSwitch.Root>, |
|
39
|
"asChild" |
|
40
|
>, |
|
41
|
VariantProps<typeof switchVariants> {} |
|
42
|
|
|
43
|
/** |
|
44
|
* This component is based on the [Radix UI Switch](https://www.radix-ui.com/primitives/docs/components/switch) primitive. |
|
45
|
*/ |
|
46
|
const Switch = React.forwardRef< |
|
47
|
React.ElementRef<typeof RadixSwitch.Root>, |
|
48
|
SwitchProps |
|
49
|
>( |
|
50
|
( |
|
51
|
{ |
|
52
|
className, |
|
53
|
/** |
|
54
|
* The switch's size. |
|
55
|
*/ |
|
56
|
size = "base", |
|
57
|
...props |
|
58
|
}: SwitchProps, |
|
59
|
ref |
|
60
|
) => ( |
|
61
|
<RadixSwitch.Root |
|
62
|
className={clx(switchVariants({ size }), className)} |
|
63
|
{...props} |
|
64
|
ref={ref} |
|
65
|
> |
|
66
|
<RadixSwitch.Thumb className={clx(thumbVariants({ size }))} /> |
|
67
|
</RadixSwitch.Root> |
|
68
|
) |
|
69
|
) |
|
70
|
Switch.displayName = "Switch" |
|
71
|
|
|
72
|
export { Switch } |