Switch
A toggle switch component for binary choices with multiple sizes and label support.
Props
size
- Switch size:sm
,md
, orlg
label
- Text label for the switcherror
- Show error statedisabled
- Disable the switch- All standard HTML input attributes (except
type
andsize
)
Sizes
States
Controlled Usage
Feature Toggles
Accessibility
The Switch component includes ARIA attributes for proper screen reader support:
TypeScript
Switch is fully typed with TypeScript:
import { Switch, SwitchProps } from '@beauginbey/vanilla-components';
// Type-safe props
const MySwitch = (props: SwitchProps) => {
return <Switch {...props} />;
};
// Controlled switch
const ControlledSwitch = () => {
const [isEnabled, setIsEnabled] = useState<boolean>(false);
return (
<Switch
label="Enable feature"
size="md"
checked={isEnabled}
onChange={(e) => setIsEnabled(e.target.checked)}
/>
);
};
// Custom switch component
interface FeatureToggleProps {
feature: string;
description?: string;
onChange: (enabled: boolean) => void;
}
const FeatureToggle = ({ feature, description, onChange }: FeatureToggleProps) => {
return (
<div>
<Switch
label={feature}
onChange={(e) => onChange(e.target.checked)}
aria-describedby={description ? `${feature}-desc` : undefined}
/>
{description && (
<span id={`${feature}-desc`}>{description}</span>
)}
</div>
);
};