Open History to see the last commit
1import { Switch as SwitchPrimitive } from '@ark-ui/solid'
2import { splitProps } from 'solid-js'
3import type { JSX } from 'solid-js'
4import { Label } from '@/components/ui/label'
5import { cn } from '@/utils'
6β
7// Ported from the original shadcn/ui + Radix UI + React component.
8// This wraps the Ark UI + SolidJS primitive and preserves shadcn's Tailwind-based
9// design-system API, variants, and styling.
10// https://ui.shadcn.com/
11β
12type SwitchSize = 'sm' | 'default'
13β
14type SwitchRootProps = SwitchPrimitive.RootProps & {
15 size?: SwitchSize
16}
17β
18type SwitchControlProps = SwitchPrimitive.ControlProps
19β
20type SwitchLabelProps = Omit<SwitchPrimitive.LabelProps, 'asChild'>
21β
22type SwitchProps = Omit<SwitchPrimitive.RootProps, 'children' | 'label'> & {
23 children?: JSX.Element
24 label?: JSX.Element
25 size?: SwitchSize
26}
27β
28/**
29 * The root label wrapper for a binary on/off control.
30 *
31 * @example
32 * ```tsx
33 * <SwitchRoot defaultChecked>
34 * <SwitchControl />
35 * <SwitchLabel>Notifications</SwitchLabel>
36 * </SwitchRoot>
37 * ```
38 */
39function SwitchRoot(props: SwitchRootProps) {
40 const [local, switchProps] = splitProps(props, ['class', 'size', 'children'])
41β
42 const size = () => local.size ?? 'default'
43β
44 return (
45 <SwitchPrimitive.Root
46 {...switchProps}
47 data-slot="switch"
48 data-size={size()}
49 class={cn('group/switch', local.class)}
50 >
51 <SwitchPrimitive.HiddenInput />
52 {local.children}
53 </SwitchPrimitive.Root>
54 )
55}
56β
57/**
58 * The visible switch track and thumb.
59 *
60 * @example
61 * ```tsx
62 * <SwitchRoot defaultChecked>
63 * <SwitchControl />
64 * <SwitchLabel>Notifications</SwitchLabel>
65 * </SwitchRoot>
66 * ```
67 */
68function SwitchControl(props: SwitchControlProps) {
69 const [local, controlProps] = splitProps(props, ['class', 'children'])
70β
71 return (
72 <SwitchPrimitive.Control
73 {...controlProps}
74 data-slot="switch-control"
75 class={cn(
76 'peer relative inline-flex shrink-0 items-center rounded-full border border-transparent transition-all outline-none group-data-[size=default]/switch:h-[18.4px] group-data-[size=default]/switch:w-[32px] group-data-[size=sm]/switch:h-[14px] group-data-[size=sm]/switch:w-[24px] after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-focus-visible:border-ring data-focus-visible:ring-3 data-focus-visible:ring-ring/50 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/20 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 dark:data-invalid:border-destructive/50 dark:data-invalid:ring-destructive/40 dark:data-[state=unchecked]:bg-input/80 data-disabled:cursor-not-allowed data-disabled:opacity-50',
77 local.class,
78 )}
79 >
80 {local.children ?? (
81 <SwitchPrimitive.Thumb
82 data-slot="switch-thumb"
83 class="pointer-events-none block rounded-full bg-background ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 data-[state=checked]:bg-background group-data-[size=default]/switch:data-[state=checked]:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:bg-background group-data-[size=default]/switch:data-[state=unchecked]:translate-x-0 group-data-[size=sm]/switch:data-[state=unchecked]:translate-x-0 dark:data-[state=checked]:bg-primary-foreground dark:data-[state=unchecked]:bg-foreground"
84 />
85 )}
86 </SwitchPrimitive.Control>
87 )
88}
89β
90/**
91 * A styled visible label for a SwitchRoot.
92 *
93 * @example
94 * ```tsx
95 * <SwitchRoot defaultChecked>
96 * <SwitchControl />
97 * <SwitchLabel>Notifications</SwitchLabel>
98 * </SwitchRoot>
99 * ```
100 */
101function SwitchLabel(props: SwitchLabelProps) {
102 const [local, labelProps] = splitProps(props, ['class'])
103β
104 return (
105 <SwitchPrimitive.Label
106 {...labelProps}
107 asChild={(switchLabelProps) => (
108 <Label
109 {...switchLabelProps({ class: local.class })}
110 data-slot="switch-label"
111 // Remember - Root is the label here, not the "label text"
112 asChild={(spanProps) => <span {...spanProps()} />}
113 />
114 )}
115 />
116 )
117}
118β
119/**
120 * A binary on/off control with shadcn-style sizing and state styling.
121 *
122 * @example
123 * ```tsx
124 * <Switch label="Enable notifications" />
125 * ```
126 */
127function Switch(props: SwitchProps) {
128 const [local, switchProps] = splitProps(props, [
129 'class',
130 'children',
131 'label',
132 'size',
133 ])
134β
135 return (
136 <SwitchRoot
137 {...switchProps}
138 size={local.size}
139 class={cn(local.label && 'flex items-center space-x-2', local.class)}
140 >
141 <SwitchControl />
142 {local.label ? <SwitchLabel>{local.label}</SwitchLabel> : local.children}
143 </SwitchRoot>
144 )
145}
146β
147export {
148 Switch,
149 SwitchControl,
150 SwitchLabel,
151 SwitchRoot,
152 type SwitchControlProps,
153 type SwitchLabelProps,
154 type SwitchProps,
155 type SwitchRootProps,
156}