Switch
A Switch component is a UI element that allows users to toggle between two states, typically representing an on/off or enabled/disabled choice. It is visually represented as a sliding toggle or checkbox and provides immediate feedback when the user interacts with it. Switch components are commonly used for settings, preferences, and feature activations in applications, enhancing user experience by simplifying the selection process.
Rendered live, with the same build of the library you install.
Switch.tsx
import { useState } from 'react';import { NSwitch } from '@nayan-ui/react';const Switch = () => {const [airplane, setAirplane] = useState(false);const [wifi, setWifi] = useState(true);return (<div><div><div className="max-w-sm space-y-3"><NSwitch label="Airplane Mode" enabled={airplane} onChange={setAirplane} /><NSwitch label="Wi-Fi" enabled={wifi} onChange={setWifi} /><NSwitch label="Bluetooth (disabled)" enabled={false} disabled onChange={() => {}} /></div></div></div>);};export default Switch;
| Name | Type | Default | Details |
|---|---|---|---|
| enabled | boolean | Optional | Whether the switch is enabled. |
| defaultChecked | boolean | Optional | Default checked state. |
| label | React.ReactNode | Optional | Label for the switch. |
| className | string | ' ' | You can customise by passing tailwind classes. |
| labelClassName | string | ' ' | You can customise by passing tailwind classes. |
| switchClassName | string | ' ' | You can customise by passing tailwind classes. |
| onChange | (checked: boolean) => void | Optional | Callback when switch state changes. |
| id | string | Optional | ID for the switch. |
| checked | boolean | Optional | Alias of enabled for controlled state. |
| disabled | boolean | false | Whether the switch is disabled. |
Other components solving nearby problems.
- React Switch
- Toggle Switch Component
- React Toggle Button
- React UI Switch
- React Checkbox Switch
- React Switch Component
- Custom React Switch
- React On/Off Switch
- Animated React Switch
- Example of React Switch
- Accessible React Switch
- Styled React Switch
- React Switch with Hooks
- Multi-State React Switch
- React Switch Library
- Responsive React Switch
- React Switch with Label
- Toggle Switch Animation
- React Switch Demo
- React Switch Integration