On its own, a switch is just the toggle control, so you can pair it with your own label or drop it into a custom layout. Add a field attribute (label, description, ...) and it wraps itself as a field instead.
<div class="flex items-center gap-3">
<c-ui.switch name="wifi" checked />
<span class="text-sm text-zinc-700 dark:text-zinc-300">Wi-Fi</span>
</div>
<div class="flex items-center gap-3">
{% cotton ui.switch name="wifi" checked /%}
<span class="text-sm text-zinc-700 dark:text-zinc-300">Wi-Fi</span>
</div>
Add contextual information with the description attribute.
<c-ui.switch
name="email_notifications"
label="Email Notifications"
description="Receive updates and alerts via email"
/>
<c-ui.switch
name="marketing_emails"
label="Marketing Emails"
description="Get the latest news and offers"
/>
<c-ui.switch
name="security_alerts"
label="Security Alerts"
description="Important account security notifications"
checked
/>
{% cotton ui.switch
name="email_notifications"
label="Email Notifications"
description="Receive updates and alerts via email"
/%}
{% cotton ui.switch
name="marketing_emails"
label="Marketing Emails"
description="Get the latest news and offers"
/%}
{% cotton ui.switch
name="security_alerts"
label="Security Alerts"
description="Important account security notifications"
checked
/%}
Set the initial state to checked with checked.
<c-ui.switch
name="dark_mode"
label="Dark Mode"
description="Enable dark theme"
checked
/>
{% cotton ui.switch
name="dark_mode"
label="Dark Mode"
description="Enable dark theme"
checked
/%}
Disable interaction with disabled.
<c-ui.switch
name="feature_disabled_off"
label="Premium Feature"
description="Upgrade to enable this feature"
disabled
/>
<c-ui.switch
name="feature_disabled_on"
label="Auto-save"
description="Automatically save your work"
checked
disabled
/>
{% cotton ui.switch
name="feature_disabled_off"
label="Premium Feature"
description="Upgrade to enable this feature"
disabled
/%}
{% cotton ui.switch
name="feature_disabled_on"
label="Auto-save"
description="Automatically save your work"
checked
disabled
/%}
Size variants run from size="xs" to size="2xl". The default is md.
<c-ui.switch
name="small_switch"
label="Small Switch"
description="Compact size for dense layouts"
size="sm"
/>
<c-ui.switch
name="default_switch"
label="Default Switch"
description="Standard size for most use cases"
/>
<c-ui.switch
name="large_switch"
label="Large Switch"
description="Larger touch target for accessibility"
size="lg"
/>
{% cotton ui.switch
name="small_switch"
label="Small Switch"
description="Compact size for dense layouts"
size="sm"
/%}
{% cotton ui.switch
name="default_switch"
label="Default Switch"
description="Standard size for most use cases"
/%}
{% cotton ui.switch
name="large_switch"
label="Large Switch"
description="Larger touch target for accessibility"
size="lg"
/%}
Give it any field attribute (label, description, error or badge) and the switch wraps itself in a Field component with no extra markup. By default the label sits above the control (block layout); add inline to lay it out as a toggle row with the switch on the right.
{# Block layout (default) #}
<c-ui.switch
name="notifications"
label="Enable notifications"
description="Get notified about account activity."
/>
{# Toggle row #}
<c-ui.switch
name="notifications"
label="Enable notifications"
description="Get notified about account activity."
inline
/>
{# Block layout (default) #}
{% cotton ui.switch
name="notifications"
label="Enable notifications"
description="Get notified about account activity."
/%}
{# Toggle row #}
{% cotton ui.switch
name="notifications"
label="Enable notifications"
description="Get notified about account activity."
inline
/%}
| Name | Description | Type | Options | Default |
|---|---|---|---|---|
name |
Form field name for submission | str | — | |
value |
Value submitted when the switch is checked | str | — | on |
label |
Label text displayed next to the switch | str | — | |
description |
Helper text displayed below the label | str | — | |
description_trailing |
Additional helper text displayed after the switch | str | — | |
error |
Error message to display | str | — | |
badge |
Badge text to display next to the label | str | — | |
form |
Django form object for error handling | Form | — | |
:disabled |
Disables the switch and prevents interaction | bool |
TrueFalse
|
False |
:checked |
Sets the initial checked state of the switch | bool |
TrueFalse
|
False |
:accent |
Uses accent color when checked. Set to False for neutral colors | bool |
TrueFalse
|
True |
size |
Size variant from xs to 2xl. | str |
xssmmdlgxl2xl
|
md |
| Name | Description | Type | Options | Default |
|---|---|---|---|---|
checkedChange |
Dispatched when the switch state changes. Use @checkedChange to listen | CustomEvent | — |
The switch component is built with accessibility in mind:
role="switch" for proper screen reader supportaria-checked automatically reflects the current statearia-label is automatically derived from the associated label