The Toggle component provides a switch-like control for binary states. It's designed for on/off toggles, settings, and any interface where users need to switch between two states.
import { Toggle } from '@dashflowx/core'export default function ToggleExample() {return (<Toggle />)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | '' | Additional CSS classes |
disabled | boolean | false | Whether the toggle is disabled |
fullwidth | boolean | false | Whether the toggle takes full width |
lable | string | 'Toggle' | Label for the toggle |
required | boolean | false | Whether the field is required |
prefixElement | JSX.Element | - | Element to display before the toggle |
sufixElement | JSX.Element | - | Element to display after the toggle |
sucessMsg | string | - | Success message to display |
errorMsg | string | - | Error message to display |
import { Toggle } from '@dashflowx/core'export default function BasicToggle() {return (<Toggle />)}
Preview:
import { Toggle } from '@dashflowx/core'export default function CustomStyledToggle() {return (<Toggle className="border border-blue-500 rounded-lg p-4" />)}
Preview:
import { Toggle } from '@dashflowx/core'export default function ToggleWithLabel() {return (<Toggle lable="Enable notifications" />)}
Preview:
import { Toggle } from '@dashflowx/core'export default function RequiredToggle() {return (<Toggle lable="Accept terms" required={true} />)}
Preview:
import { Toggle } from '@dashflowx/core'export default function DisabledToggle() {return (<Toggle lable="Disabled option" disabled={true} />)}
Preview:
import { Toggle } from '@dashflowx/core'export default function FullWidthToggle() {return (<Toggle lable="Full width toggle" fullwidth={true} />)}
Preview:
const settingsToggle = (<Toggle lable="Enable dark mode" />);
const featureToggle = (<Toggle lable="Enable new features" />);
const notificationToggle = (<Toggle lable="Receive email notifications" />);
const privacyToggle = (<Toggle lable="Make profile public" />);
Use clear and descriptive labels for toggles.
// ✅ Good - Clear labels<Toggle lable="Enable email notifications" />// ❌ Avoid - Unclear labels<Toggle lable="Toggle" />
Use appropriate states for different contexts.
// ✅ Good - Appropriate states<Toggle lable="Enable feature" /><Toggle lable="Disable feature" />// ❌ Avoid - Confusing states<Toggle lable="Maybe enable" />
Maintain consistent styling across toggles.
// ✅ Good - Consistent styling<Toggle className="standard-toggle-style" />// ❌ Avoid - Inconsistent styling<Toggle className="random-styles" />
Ensure toggle labels are accessible to all users.
// ✅ Good - Accessible labels<Toggle lable="Enable accessibility features" />// ❌ Avoid - Not accessible<Toggle lable="Toggle" />
Use disabled states appropriately.
// ✅ Good - Appropriate disabled state<Toggle lable="Feature unavailable" disabled={true} />// ❌ Avoid - Inappropriate disabled state<Toggle lable="Available feature" disabled={true} />
You can customize the toggle appearance using CSS classes:
<Toggle className="border border-gray-300 rounded-lg p-4" />
Use different themes for different contexts:
// Light theme<Toggle className="bg-white border-gray-200" />// Dark theme<Toggle className="bg-gray-900 border-gray-700 text-white" />// Brand theme<Toggle className="bg-blue-50 border-blue-200" />
Use different sizes for different emphasis levels:
// Small toggle<Toggle className="text-sm" />// Standard toggle<Toggle className="text-base" />// Large toggle<Toggle className="text-lg" />
Manage toggle state dynamically:
const [isEnabled, setIsEnabled] = useState(false);<Togglelable="Dynamic toggle"checked={isEnabled}onChange={setIsEnabled}/>
Add validation to toggle states:
const [hasError, setHasError] = useState(false);<Togglelable="Required setting"required={true}errorMsg={hasError ? "This setting is required" : ""}/>
Show success messages for toggle actions:
const [showSuccess, setShowSuccess] = useState(false);<Togglelable="Feature enabled"sucessMsg={showSuccess ? "Feature enabled successfully" : ""}/>
- Switch: For similar toggle functionality
- Checkbox: For checkbox-style toggles
- Button: For button-style toggles
- Form: For form containers
- Label: For form labels
For the complete API reference and advanced usage patterns, see the Toggle API documentation.
