Switch
A control that allows the user to toggle between checked and not checked.
import { Label } from "~/components/ui/label"
import { Switch } from "~/components/ui/switch"
export default function SwitchDemo() {
return (
<div class="flex items-center gap-2">
<Switch id="switch-airplane-mode" />
<Label for="switch-airplane-mode">Airplane Mode</Label>
</div>
)
}Installation
Install the following dependencies:
Copy and paste the following code into your project.
import type { ComponentProps } from "@solidjs/web"
import {
Switch as SdkSwitch,
SwitchThumb as SdkSwitchThumb
} from "@ui-sdk/solid/switch"
import { omit } from "solid-js"
import { cn } from "~/lib/utils"
type SwitchProps = Omit<ComponentProps<typeof SdkSwitch>, "class"> & {
class?: string
size?: "sm" | "default"
}
const Switch = (props: SwitchProps) => {
const rest = omit(props, "class", "children", "size")
return (
<SdkSwitch
{...rest}
data-size={props.size ?? "default"}
class={cn(
"shrink-0 rounded-full border border-transparent group-has-[:focus-visible]/field-label:border-transparent group-has-[:focus-visible]/field-label:ring-0 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-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:bg-primary data-unchecked:bg-input dark:data-unchecked:bg-input/80 peer group/switch relative inline-flex items-center justify-start p-0 transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 data-disabled:cursor-not-allowed data-disabled:opacity-50",
props.class
)}
>
<SdkSwitchThumb
class={cn(
"rounded-full bg-background group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:ltr:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:ltr:translate-x-[calc(100%-2px)] group-data-[size=default]/switch:data-checked:rtl:-translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:rtl:-translate-x-[calc(100%-2px)] dark:data-checked:bg-primary-foreground group-data-[size=default]/switch:data-unchecked:ltr:translate-x-0 group-data-[size=sm]/switch:data-unchecked:ltr:translate-x-0 group-data-[size=default]/switch:data-unchecked:rtl:-translate-x-0 group-data-[size=sm]/switch:data-unchecked:rtl:-translate-x-0 dark:data-unchecked:bg-foreground pointer-events-none block rounded-[inherit] ring-0 transition-transform"
)}
/>
</SdkSwitch>
)
}
export { Switch, type SwitchProps }Update the import paths to match your project setup.
Usage
import { Switch } from "~/components/ui/switch"<Switch />Description
Focus is shared across devices, and turns off when you leave the app.
import {
Field,
FieldContent,
FieldDescription,
FieldLabel
} from "~/components/ui/field"
import { Switch } from "~/components/ui/switch"
export default function SwitchDescription() {
return (
<Field class="w-full max-w-sm" orientation="horizontal">
<FieldContent>
<FieldLabel for="switch-focus-mode">Share across devices</FieldLabel>
<FieldDescription>
Focus is shared across devices, and turns off when you leave the app.
</FieldDescription>
</FieldContent>
<Switch id="switch-focus-mode" />
</Field>
)
}Choice Card
Card-style selection where FieldLabel wraps the entire Field for a clickable card pattern.
import {
Field,
FieldContent,
FieldDescription,
FieldGroup,
FieldLabel,
FieldTitle
} from "~/components/ui/field"
import { Switch } from "~/components/ui/switch"
export default function SwitchChoiceCard() {
return (
<FieldGroup class="w-full max-w-sm">
<FieldLabel for="switch-share">
<Field orientation="horizontal">
<FieldContent>
<FieldTitle>Share across devices</FieldTitle>
<FieldDescription>
Focus is shared across devices, and turns off when you leave the
app.
</FieldDescription>
</FieldContent>
<Switch id="switch-share" />
</Field>
</FieldLabel>
<FieldLabel for="switch-notifications">
<Field orientation="horizontal">
<FieldContent>
<FieldTitle>Enable notifications</FieldTitle>
<FieldDescription>
Receive notifications when focus mode is enabled or disabled.
</FieldDescription>
</FieldContent>
<Switch defaultChecked id="switch-notifications" />
</Field>
</FieldLabel>
</FieldGroup>
)
}Disabled
Set disabled on Switch to prevent interaction. Add data-disabled to Field to style the rest of the field.
import { Field, FieldLabel } from "~/components/ui/field"
import { Switch } from "~/components/ui/switch"
export default function SwitchDisabled() {
return (
<Field class="w-fit" data-disabled orientation="horizontal">
<Switch disabled id="switch-disabled-unchecked" />
<FieldLabel for="switch-disabled-unchecked">Disabled</FieldLabel>
</Field>
)
}Invalid
Set aria-invalid on Switch and data-invalid on Field to show the invalid state.
You must accept the terms and conditions to continue.
import {
Field,
FieldContent,
FieldDescription,
FieldLabel
} from "~/components/ui/field"
import { Switch } from "~/components/ui/switch"
export default function SwitchInvalid() {
return (
<Field class="w-full max-w-sm" data-invalid orientation="horizontal">
<FieldContent>
<FieldLabel for="switch-terms">Accept terms and conditions</FieldLabel>
<FieldDescription>
You must accept the terms and conditions to continue.
</FieldDescription>
</FieldContent>
<Switch aria-invalid="true" id="switch-terms" />
</Field>
)
}Size
Use the size prop to change the size of the switch.
import { Field, FieldGroup, FieldLabel } from "~/components/ui/field"
import { Switch } from "~/components/ui/switch"
export default function SwitchSizes() {
return (
<FieldGroup class="w-full max-w-[10rem]">
<Field orientation="horizontal">
<Switch id="switch-size-sm" size="sm" />
<FieldLabel for="switch-size-sm">Small</FieldLabel>
</Field>
<Field orientation="horizontal">
<Switch id="switch-size-default" size="default" />
<FieldLabel for="switch-size-default">Default</FieldLabel>
</Field>
</FieldGroup>
)
}API Reference
See the UI SDK documentation for more information.