<script lang="ts">
import { Label } from "$lib/components/ui/label/index.js";
import { Switch } from "$lib/components/ui/switch/index.js";
</script>
<div class="flex items-center space-x-2">
<Switch id="airplane-mode" />
<Label for="airplane-mode">Airplane Mode</Label>
</div> Installation
pnpm dlx shadcn-svelte@latest add switch Install bits-ui:
pnpm add bits-ui -D Copy and paste the following code into your project.
import Root from './switch.svelte';
export {
Root,
//
Root as Switch
};
<script lang="ts">
import { Switch as SwitchPrimitive } from 'bits-ui';
import { cn, type WithoutChildrenOrChild } from '$UTILS$.js';
let {
ref = $bindable(null),
class: className,
checked = $bindable(false),
size = 'default',
...restProps
}: WithoutChildrenOrChild<SwitchPrimitive.RootProps> & {
size?: 'sm' | 'default';
} = $props();
</script>
<SwitchPrimitive.Root
bind:ref
bind:checked
data-slot="switch"
data-size={size}
class={cn(
'data-checked:bg-[#b9d765] data-unchecked:bg-background data-checked:border-[#b9d765] data-unchecked:border-zinc-700 focus-visible:border-zinc-300 focus-visible:ring-zinc-300/30 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive dark:aria-invalid:border-destructive/50 shrink-0 rounded-full border focus-visible:ring-2 aria-invalid:ring-2 data-[size=default]:h-5 data-[size=default]:w-9 data-[size=sm]:h-4 data-[size=sm]:w-7 peer group/switch relative inline-flex items-center transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 data-disabled:cursor-not-allowed data-disabled:opacity-50',
className
)}
{...restProps}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
class="bg-zinc-50 dark:data-unchecked:bg-zinc-50 dark:data-checked:bg-white group-data-[size=default]/switch:size-3.5 group-data-[size=sm]/switch:size-2.5 data-checked:translate-x-[calc(100%+3px)] data-unchecked:translate-x-0.5 pointer-events-none block rounded-full ring-0 transition-transform rtl:data-[state=checked]:translate-x-[calc(-100%)]"
/>
</SwitchPrimitive.Root>
Usage
<script lang="ts">
import { Switch } from '$lib/components/ui/switch/index.js';
</script> <Switch />