A Switch component for toggling between on/off states with support for various sizes, variants, and colors. The switch provides visual feedback for boolean state changes and can be used in forms, settings panels, and interactive controls.
Examples
<.switch />
<.switch checked />
<.switch size="3" variant="soft" color="blue" />
<.switch disabled />
<.switch name="notifications" value="enabled" checked />Props
size- Switch size: "1", "2", "3" (default: "2")variant- Switch variant: "surface", "classic", "soft" (default: "surface")color- Color theme from accent color palette (default: none)high_contrast- Increase color contrast (default: false)checked- Controlled checked state (true/false)default_checked- Initial checked state (default: false)disabled- Whether the switch is disabledname- Form name attributevalue- Form value attribute- Plus margin props (m, mx, my, mt, mr, mb, ml) for spacing control
Summary
Functions
Attributes
color(:string) - Color utility class string. One of: gray, gold, bronze, brown, yellow, amber, orange, tomato, red, ruby, crimson, pink, plum, purple, violet, iris, indigo, blue, cyan, teal, jade, green, grass, lime, mint, sky. Must be one of"gray","gold","bronze","brown","yellow","amber","orange","tomato","red","ruby","crimson","pink","plum","purple","violet","iris","indigo","blue","cyan","teal","jade","green","grass","lime","mint", or"sky".high_contrast(:boolean) - Whether to increase color contrast with the background.m(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.mx(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.my(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.mt(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.mr(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.mb(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.ml(:any) - Margin utility class string or map. Example:1or%{xs: '1', sm: '2'}.id(:string) - Defaults tonil.size(:string) - Switch size from 1 to 3. Controls overall dimensions and thumb size. Defaults to"2". Must be one of"1","2", or"3".variant(:string) - Visual style variant. One of 'surface', 'classic', or 'soft'. Defaults to"surface". Must be one of"surface","classic", or"soft".default_checked(:boolean) - Initial checked state. If true, switch starts checked. Defaults tofalse.checked(:boolean) - Controlled checked state. Can be true or false. Defaults tonil.disabled(:boolean) - Whether the switch is disabled. Defaults tofalse.required(:boolean) - Whether the switch is required. Defaults tofalse.form(:string) - The form ID this switch belongs to. Defaults tonil.on_checked_change(:string) - LiveView event to push when the checked state changes. Defaults tonil.name(:string) - Form name attribute. Defaults tonil.value(:string) - Form value attribute. Defaults tonil.class(:string) - Additional CSS classes to add to the element. Defaults tonil.style(:string) - Defaults to"".- Global attributes are accepted. Global attributes and event handlers. Supports all globals plus:
["aria-label", "aria-labelledby", "aria-describedby"].