Switch
A control that allows the user to toggle between on and off states.
Examples
Default
<div class="flex flex-col gap-6">
<div class="flex items-center gap-3">
<c-switch-element id="demo-1"></c-switch-element>
<c-label label-for="demo-1">Airplane Mode</c-label>
</div>
</div>
With Label
Receive emails about new products, features, and more.
<div class="flex flex-col gap-6">
<div class="flex items-center gap-3">
<c-switch-element id="switch-with-label"></c-switch-element>
<c-label label-for="switch-with-label">Enable notifications</c-label>
</div>
<div class="flex items-start gap-3">
<c-switch-element id="switch-with-description" checked></c-switch-element>
<div class="grid gap-2">
<c-label label-for="switch-with-description">Marketing emails</c-label>
<p class="text-muted-foreground text-sm">
Receive emails about new products, features, and more.
</p>
</div>
</div>
</div>
Checked
<div class="flex items-center gap-3">
<c-switch-element id="switch-checked" checked></c-switch-element>
<c-label label-for="switch-checked">Airplane Mode</c-label>
</div>
Disabled
<div class="flex flex-col gap-6">
<div class="flex items-center gap-3">
<c-switch-element id="switch-disabled-off" disabled></c-switch-element>
<c-label label-for="switch-disabled-off">Disabled (Off)</c-label>
</div>
<div class="flex items-center gap-3">
<c-switch-element
id="switch-disabled-on"
disabled
checked
></c-switch-element>
<c-label label-for="switch-disabled-on">Disabled (On)</c-label>
</div>
</div>
Form Example
Email Notifications
Configure your email notification preferences.
Receive emails about new products and features.
Receive emails for friend requests and more.
Receive emails about your account security.
<div class="flex flex-col gap-4 rounded-lg border p-4 max-w-md">
<div>
<h3 class="text-lg font-medium">Email Notifications</h3>
<p class="text-sm text-muted-foreground">
Configure your email notification preferences.
</p>
</div>
<div class="flex flex-col gap-4">
<div class="flex items-center justify-between">
<div class="flex flex-col gap-1">
<c-label label-for="notif-marketing">Marketing emails</c-label>
<p class="text-sm text-muted-foreground">
Receive emails about new products and features.
</p>
</div>
<c-switch-element id="notif-marketing" checked></c-switch-element>
</div>
<div class="flex items-center justify-between">
<div class="flex flex-col gap-1">
<c-label label-for="notif-social">Social emails</c-label>
<p class="text-sm text-muted-foreground">
Receive emails for friend requests and more.
</p>
</div>
<c-switch-element id="notif-social"></c-switch-element>
</div>
<div class="flex items-center justify-between">
<div class="flex flex-col gap-1">
<c-label label-for="notif-security">Security emails</c-label>
<p class="text-sm text-muted-foreground">
Receive emails about your account security.
</p>
</div>
<c-switch-element id="notif-security" checked></c-switch-element>
</div>
</div>
</div>
API Reference
Switch
A switch component that allows users to toggle between on and off states.
Events
change
Fired when the switch state changes.
Detail:
checked(boolean) - The new checked state
switchElement.addEventListener('change', (event) => {
console.log('Checked:', event.detail.checked);
});