Directory
- Welcome
Switch Component
The Switch component creates an interactive on/off control perfect for settings, preferences, and feature toggles. It’s accessible, customizable, and easy to implement in your AstroJS project.
Key Features
-
Multiple Variants: Support for default, success, and destructive states
-
Size Options: Three size variants for different contexts
-
Accessibility: Full ARIA support and keyboard navigation
Basic Usage
Add a simple switch by importing and using the component:
<Switch label="Notifications" checked={true}/>
Size Variations
The switch comes in three sizes to match your design needs:
<Switch size="sm" label="Compact Switch" /><Switch label="Standard Switch" /><Switch size="lg" label="Large Switch" />
Color Variants
The switch supports different color variants to indicate various states or actions:
<Switch variant="default" label="Default Switch" /><Switch variant="success" label="Success Switch" /><Switch variant="destructive" label="Destructive Switch" />
Switch Component Props API
Prop | Type | Default | Description |
---|---|---|---|
label | string | - | Main label for the switch |
sublabel | string | - | Additional descriptive text |
variant | "default" | "success" | "destructive" | default | Color variant |
size | "sm" | "default" | "lg" | default | Size of the switch |
checked | boolean | false | Initial switch state |
Custom Event Listeners
Listen for switch state changes:
Waiting for events to be emitted… (Click on the switch to see the events)
document.querySelector('switch-wrapper').addEventListener(SwitchEventNames.Change, (event) => { const isChecked = event.detail.checked; // Handle state change});
Limited Time Launch Sale
Get started with our AstroJS starter template and access a complete collection of pre-built components, including this Switch. Save development time and build better websites faster.
GET 60% OFF!