The Switch component provides a toggle control for turning options on or off with a sliding animation.
Basic usage
<flx-switch
label="Enable notifications"
[(ngModel)]="notificationsEnabled">
</flx-switch>
Properties
Label text displayed next to the switch
labelPosition
'before' | 'after'
default:"after"
Position of the label relative to the switch
size
'small' | 'medium' | 'large'
default:"medium"
Size of the switch
Custom color for the active state
Events
Emitted when the switch state changes
Examples
Basic switches
Sizes
Label positions
Disabled state
Reactive forms
With change handler
Settings list
<flx-switch
label="Dark mode"
[(ngModel)]="darkMode">
</flx-switch>
<flx-switch
label="Auto-save"
[(ngModel)]="autoSave">
</flx-switch>
<flx-switch
label="Email notifications"
[(ngModel)]="emailNotifications">
</flx-switch>
export class Settings {
darkMode = false;
autoSave = true;
emailNotifications = true;
}
<flx-switch
label="Small switch"
size="small"
[(ngModel)]="value1">
</flx-switch>
<flx-switch
label="Medium switch"
size="medium"
[(ngModel)]="value2">
</flx-switch>
<flx-switch
label="Large switch"
size="large"
[(ngModel)]="value3">
</flx-switch>
<flx-switch
label="Label after (default)"
labelPosition="after"
[(ngModel)]="value1">
</flx-switch>
<flx-switch
label="Label before"
labelPosition="before"
[(ngModel)]="value2">
</flx-switch>
<flx-switch
label="Disabled (off)"
[disabled]="true"
[(ngModel)]="disabledOff">
</flx-switch>
<flx-switch
label="Disabled (on)"
[disabled]="true"
[(ngModel)]="disabledOn">
</flx-switch>
export class DisabledExample {
disabledOff = false;
disabledOn = true;
}
<form [formGroup]="preferencesForm">
<flx-switch
label="Enable two-factor authentication"
formControlName="twoFactor">
</flx-switch>
<flx-switch
label="Show profile publicly"
formControlName="publicProfile">
</flx-switch>
<flx-switch
label="Send weekly digest"
formControlName="weeklyDigest">
</flx-switch>
</form>
import { FormBuilder } from '@angular/forms';
export class PreferencesForm {
preferencesForm = this.fb.group({
twoFactor: [false],
publicProfile: [true],
weeklyDigest: [true]
});
constructor(private fb: FormBuilder) {}
}
<flx-switch
label="Maintenance mode"
[(ngModel)]="maintenanceMode"
(checkedChange)="onMaintenanceModeChange($event)">
</flx-switch>
export class AdminPanel {
maintenanceMode = false;
onMaintenanceModeChange(enabled: boolean) {
if (enabled) {
this.enableMaintenanceMode();
} else {
this.disableMaintenanceMode();
}
}
enableMaintenanceMode() {
console.log('Maintenance mode enabled');
// API call to enable maintenance mode
}
disableMaintenanceMode() {
console.log('Maintenance mode disabled');
// API call to disable maintenance mode
}
}
<div class="settings-list">
<div class="setting-item">
<div class="setting-info">
<h4>Push notifications</h4>
<p>Receive notifications on your device</p>
</div>
<flx-switch
[(ngModel)]="settings.pushNotifications"
labelPosition="before">
</flx-switch>
</div>
<div class="setting-item">
<div class="setting-info">
<h4>Email digest</h4>
<p>Get a daily summary via email</p>
</div>
<flx-switch
[(ngModel)]="settings.emailDigest"
labelPosition="before">
</flx-switch>
</div>
<div class="setting-item">
<div class="setting-info">
<h4>Auto-update</h4>
<p>Automatically install updates</p>
</div>
<flx-switch
[(ngModel)]="settings.autoUpdate"
labelPosition="before">
</flx-switch>
</div>
</div>
Styling
flx-switch {
--flx-switch-width: 44px;
--flx-switch-height: 24px;
--flx-switch-track-background: #d1d5db;
--flx-switch-track-checked-background: #3b82f6;
--flx-switch-handle-size: 20px;
--flx-switch-handle-background: #ffffff;
--flx-switch-border-radius: 12px;
--flx-switch-disabled-opacity: 0.5;
--flx-switch-transition-duration: 200ms;
}
Accessibility
The Switch component provides full accessibility:
role="switch" with aria-checked state
- Space key toggles the switch
- Focus indicators
- Proper label association
- Disabled state handling
Switch vs Checkbox
Use Switch for settings that take immediate effect (like toggling dark mode). Use Checkbox for form selections that require submission (like accepting terms).
| Use Switch when | Use Checkbox when |
|---|
| Effect is immediate | Part of a form submission |
| Binary on/off state | Multiple selections allowed |
| System/app settings | Agreement/consent |
| Real-time toggles | Data collection |
Best practices
- Use clear, action-oriented labels (“Enable notifications” not “Notifications”)
- Provide immediate visual feedback when state changes
- Show a loading state for switches that trigger async operations
- Group related switches together
- Consider adding descriptions for complex settings
- Use consistent switch placement in settings lists
- Checkbox - For multiple selections or form submissions
- Segmented Button - For multiple mutually exclusive options
- Radio - For single selection from multiple options