package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/label" switchcomp "github.com/axadrn/shadcn-templ/v2/components/switch") templ SwitchDemo() { <div class="flex items-center space-x-2"> @switchcomp.Switch(switchcomp.Props{ID: "airplane-mode"}) @label.Label(label.Props{For: "airplane-mode"}) { Airplane Mode } </div>}Installation
Usage
Description
Focus is shared across devices, and turns off when you leave the app.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/field" switchcomp "github.com/axadrn/shadcn-templ/v2/components/switch") templ SwitchDescription() { @field.Field(field.Props{Orientation: field.OrientationHorizontal, Class: "max-w-sm"}) { @field.Content() { @field.Label(field.LabelProps{For: "switch-focus-mode"}) { Share across devices } @field.Description() { Focus is shared across devices, and turns off when you leave the app. } } @switchcomp.Switch(switchcomp.Props{ID: "switch-focus-mode"}) }}Choice Card
Card-style selection where field.Label wraps the entire field.Field for a clickable card pattern.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/field" switchcomp "github.com/axadrn/shadcn-templ/v2/components/switch") templ SwitchChoiceCard() { @field.Group(field.GroupProps{Class: "w-full max-w-sm"}) { @field.Label(field.LabelProps{For: "switch-share"}) { @field.Field(field.Props{Orientation: field.OrientationHorizontal}) { @field.Content() { @field.Title() { Share across devices } @field.Description() { Focus is shared across devices, and turns off when you leave the app. } } @switchcomp.Switch(switchcomp.Props{ID: "switch-share"}) } } @field.Label(field.LabelProps{For: "switch-notifications"}) { @field.Field(field.Props{Orientation: field.OrientationHorizontal}) { @field.Content() { @field.Title() { Enable notifications } @field.Description() { Receive notifications when focus mode is enabled or disabled. } } @switchcomp.Switch(switchcomp.Props{ID: "switch-notifications", Checked: true}) } } }}Disabled
Add the Disabled prop to the Switch component to disable the switch. Add the Disabled prop to the field.Field component for styling.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/field" switchcomp "github.com/axadrn/shadcn-templ/v2/components/switch") templ SwitchDisabled() { @field.Field(field.Props{Orientation: field.OrientationHorizontal, Class: "w-fit", Attributes: templ.Attributes{"data-disabled": "true"}}) { @switchcomp.Switch(switchcomp.Props{ID: "switch-disabled-unchecked", Disabled: true}) @field.Label(field.LabelProps{For: "switch-disabled-unchecked"}) { Disabled } }}Invalid
Add the aria-invalid attribute to the Switch component to indicate an invalid state. Add the Invalid prop to the field.Field component for styling.
You must accept the terms and conditions to continue.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/field" switchcomp "github.com/axadrn/shadcn-templ/v2/components/switch") templ SwitchInvalid() { @field.Field(field.Props{Orientation: field.OrientationHorizontal, Class: "max-w-sm", Attributes: templ.Attributes{"data-invalid": "true"}}) { @field.Content() { @field.Label(field.LabelProps{For: "switch-terms"}) { Accept terms and conditions } @field.Description() { You must accept the terms and conditions to continue. } } @switchcomp.Switch(switchcomp.Props{ ID: "switch-terms", Attributes: templ.Attributes{ "aria-invalid": "true", }, }) }}Size
Use the Size prop to change the size of the switch.
package examples import (package examples import ( "github.com/axadrn/shadcn-templ/v2/components/field" switchcomp "github.com/axadrn/shadcn-templ/v2/components/switch") templ SwitchSizes() { @field.Group(field.GroupProps{Class: "w-full max-w-[10rem]"}) { @field.Field(field.Props{Orientation: field.OrientationHorizontal}) { @switchcomp.Switch(switchcomp.Props{ID: "switch-size-sm", Size: switchcomp.SizeSm}) @field.Label(field.LabelProps{For: "switch-size-sm"}) { Small } } @field.Field(field.Props{Orientation: field.OrientationHorizontal}) { @switchcomp.Switch(switchcomp.Props{ID: "switch-size-default", Size: switchcomp.SizeDefault}) @field.Label(field.LabelProps{For: "switch-size-default"}) { Default } } }}API Reference
Switch
The Switch component renders the toggle as a role="switch" root with a hidden native checkbox beside it that carries form participation and label association.