The Switch component is a toggle control that allows users to turn an option on or off. It provides a modern, accessible alternative to checkboxes for binary choices and is commonly used in settings panels and configuration forms.
import { Switch } from '@dashflowx/core'export default function SwitchExample() {const [enabled, setEnabled] = useState(false);return (<Switchlabel="Enable notifications"checked={enabled}onChange={(checked) => setEnabled(checked)}/>)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | 'Switch Label' | The label text for the switch |
checked | boolean | false | Whether the switch is checked/enabled |
disabled | boolean | false | Whether the switch is disabled |
onChange | function | - | Callback function when switch state changes |
id | string | - | Unique identifier for the switch |
<Switchlabel="Airplane Mode"checked={false}onChange={handleChange}/>
Preview:
<Switchlabel="Dark Mode"checked={true}onChange={handleChange}/>
Preview:
<Switchlabel="Premium Feature"checked={false}disabled={true}/>
Preview:
<Switchlabel="Enable two-factor authentication"checked={twoFactorEnabled}onChange={setTwoFactorEnabled}/>
Preview:
<div className="space-y-4"><h3 className="text-lg font-semibold">Account Settings</h3><Switchlabel="Email notifications"checked={emailNotifications}onChange={setEmailNotifications}/><Switchlabel="Push notifications"checked={pushNotifications}onChange={setPushNotifications}/><Switchlabel="SMS notifications"checked={smsNotifications}onChange={setSmsNotifications}/><Switchlabel="Marketing emails"checked={marketingEmails}onChange={setMarketingEmails}/></div>
<div className="space-y-3"><h4 className="font-medium">Feature Preferences</h4><Switchlabel="Auto-save drafts"checked={autoSave}onChange={setAutoSave}/><Switchlabel="Show advanced options"checked={showAdvanced}onChange={setShowAdvanced}/><Switchlabel="Enable beta features"checked={betaFeatures}onChange={setBetaFeatures}/></div>
<div className="space-y-4"><h3 className="text-lg font-semibold">Privacy Settings</h3><Switchlabel="Public profile"checked={publicProfile}onChange={setPublicProfile}/><Switchlabel="Show online status"checked={showOnlineStatus}onChange={setShowOnlineStatus}/><Switchlabel="Allow friend requests"checked={allowFriendRequests}onChange={setAllowFriendRequests}/></div>
<div className="space-y-3"><h4 className="font-medium">App Configuration</h4><Switchlabel="Enable location services"checked={locationServices}onChange={setLocationServices}/><Switchlabel="Enable analytics"checked={analytics}onChange={setAnalytics}/><Switchlabel="Enable crash reporting"checked={crashReporting}onChange={setCrashReporting}/></div>
Use descriptive labels that clearly indicate what the switch controls.
// ✅ Good - Clear label<Switchlabel="Enable email notifications"checked={emailNotifications}onChange={setEmailNotifications}/>// ❌ Avoid - Unclear label<Switchlabel="Option 1"checked={option1}onChange={setOption1}/>
Group related switches together with proper spacing and visual hierarchy.
// ✅ Good - Logical grouping<div className="space-y-3"><h4 className="font-medium">Notification Settings</h4><Switch label="Email notifications" checked={email} onChange={setEmail} /><Switch label="Push notifications" checked={push} onChange={setPush} /><Switch label="SMS notifications" checked={sms} onChange={setSms} /></div>
Use consistent state management patterns for switch groups.
// ✅ Good - Consistent state managementconst [settings, setSettings] = useState({email: false,push: true,sms: false,});const updateSetting = (key: string, value: boolean) => {setSettings(prev => ({ ...prev, [key]: value }));};
Ensure proper accessibility with clear labels and keyboard navigation.
// ✅ Good - Accessible switch<Switchlabel="Enable accessibility features"checked={accessibilityEnabled}onChange={setAccessibilityEnabled}id="accessibility-switch"/>
Provide clear visual feedback for the switch state.
// ✅ Good - Clear visual feedback<Switchlabel="Enable dark mode"checked={darkMode}onChange={setDarkMode}/>
You can customize the switch appearance using CSS classes:
<Switchlabel="Custom styled switch"checked={checked}onChange={setChecked}className="custom-switch"/>
Create different switch sizes for different use cases:
// Small switch<Switchlabel="Compact setting"checked={compact}onChange={setCompact}size="sm"/>// Large switch<Switchlabel="Important setting"checked={important}onChange={setImportant}size="lg"/>
The Switch component works well with popular form libraries:
// With React Hook Formimport { useForm, Controller } from 'react-hook-form';function MyForm() {const { control } = useForm();return (<Controllername="notifications"control={control}render={({ field }) => (<Switchlabel="Enable notifications"checked={field.value}onChange={field.onChange}/>)}/>);}
Use proper state management for switch groups:
const [preferences, setPreferences] = useState({darkMode: false,notifications: true,analytics: false,});const togglePreference = (key: keyof typeof preferences) => {setPreferences(prev => ({...prev,[key]: !prev[key]}));};
- Checkbox: For multi-choice selections
- RadioGroup: For single-choice selections
- Toggle: For toggle button groups
- Select: For dropdown selections
- Input: For text input
For the complete API reference and advanced usage patterns, see the Switch API documentation.
