Zaidan

Command Palette

Search for a command to run...

GitHub

Switch

A control that allows the user to toggle between checked and not checked.

Installation

CLI

Manual

Install the following dependencies:

Copy and paste the following code into your project.

import type { PolymorphicProps } from "@kobalte/core/polymorphic";
import * as SwitchPrimitive from "@kobalte/core/switch";
import { type ComponentProps, mergeProps, splitProps, type ValidComponent } from "solid-js";
import { cn } from "~/lib/utils";
type SwitchProps<T extends ValidComponent = "div"> = PolymorphicProps<
T,
SwitchPrimitive.SwitchRootProps<T>
> &
Pick<ComponentProps<T>, "class" | "children"> & {
size?: "sm" | "default";
};
const Switch = <T extends ValidComponent = "div">(props: SwitchProps<T>) => {
const mergedProps = mergeProps({ size: "default" as const }, props);
const [local, others] = splitProps(mergedProps as SwitchProps, ["class", "size", "id"]);
return (
<SwitchPrimitive.Root
data-slot="switch"
data-size={local.size}
class={cn(
"peer group/switch relative z-switch inline-flex items-center outline-none transition-all data-disabled:cursor-not-allowed data-disabled:opacity-50",
local.class,
)}
{...others}
>
<SwitchPrimitive.Input data-slot="switch-input" class="peer sr-only" id={local.id} />
<SwitchPrimitive.Control
data-slot="switch-control"
class="relative z-switch-control inline-flex shrink-0 cursor-pointer items-center rounded-full transition-colors data-disabled:cursor-not-allowed"
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
class="pointer-events-none z-switch-thumb block rounded-full ring-0 transition-transform"
/>
</SwitchPrimitive.Control>
</SwitchPrimitive.Root>
);
};
export { Switch };

Examples

Here are the source code of all the examples from the preview page:

Basic

import { Field, FieldLabel } from "~/components/ui/field";
import { Switch } from "~/components/ui/switch";
function SwitchBasic() {
return (
<Example title="Basic">
<Field orientation="horizontal">
<Switch id="switch-basic" />
<FieldLabel for="switch-basic">Airplane Mode</FieldLabel>
</Field>
</Example>
);
}

With Description

import {
Field,
FieldContent,
FieldDescription,
FieldLabel,
FieldTitle,
} from "~/components/ui/field";
import { Switch } from "~/components/ui/switch";
function SwitchWithDescription() {
return (
<Example title="With Description">
<FieldLabel for="switch-focus-mode">
<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-focus-mode" />
</Field>
</FieldLabel>
</Example>
);
}

Disabled

import { Label } from "~/components/ui/label";
import { Switch } from "~/components/ui/switch";
function SwitchDisabled() {
return (
<Example title="Disabled">
<div class="flex flex-col gap-12">
<div class="flex items-center gap-2">
<Switch id="switch-disabled-unchecked" disabled />
<Label for="switch-disabled-unchecked">Disabled (Unchecked)</Label>
</div>
<div class="flex items-center gap-2">
<Switch id="switch-disabled-checked" defaultChecked disabled />
<Label for="switch-disabled-checked">Disabled (Checked)</Label>
</div>
</div>
</Example>
);
}

Sizes

import { Label } from "~/components/ui/label";
import { Switch } from "~/components/ui/switch";
function SwitchSizes() {
return (
<Example title="Sizes">
<div class="flex flex-col gap-12">
<div class="flex items-center gap-2">
<Switch id="switch-size-sm" size="sm" />
<Label for="switch-size-sm">Small</Label>
</div>
<div class="flex items-center gap-2">
<Switch id="switch-size-default" size="default" />
<Label for="switch-size-default">Default</Label>
</div>
</div>
</Example>
);
}