Click files to view code →
Create a toggle switch component that allows users to switch between two states (ON/OFF, true/false). The switch should provide visual feedback and can be used for settings, preferences, or any binary choice scenario.
[!TIP] Goal
Use React's
useState[!IMPORTANT] Requirements
No external dependencies needed.
Key Topics:
useStatePlan of Action:
ToggleSwitchuseState(false)import React, { useState } from 'react';
import './ToggleSwitch.css';
function ToggleSwitch({
initialState = false,
onToggle,
label = "Toggle",
disabled = false
}) {
const [isOn, setIsOn] = useState(initialState);
const handleToggle = () => {
if (disabled) return;
const newState = !isOn;
setIsOn(newState);
// Call parent callback if provided
if (onToggle) {
onToggle(newState);
}
};
return (
<div className="toggle-switch-container">
<span className="toggle-label">{label}</span>
<button
className={`toggle-switch ${isOn ? 'on' : 'off'} ${disabled ? 'disabled' : ''}`}
onClick={handleToggle}
disabled={disabled}
role="switch"
aria-checked={isOn}
aria-label={label}
>
<span className="toggle-slider" />
</button>
<span className="toggle-status">
{isOn ? 'ON' : 'OFF'}
</span>
</div>
);
}
export default ToggleSwitch;CSS Styling (ToggleSwitch.css):
.toggle-switch-container {
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem;
font-family: Arial, sans-serif;
}
.toggle-label {
font-size: 1rem;
color: #333;
font-weight: 500;
}
.toggle-switch {
position: relative;
width: 60px;
height: 30px;
background-color: #ccc;
border: none;
border-radius: 30px;
cursor: pointer;
transition: background-color 0.3s ease;
outline: none;
}
.toggle-switch:focus {
box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.3);
}
.toggle-switch.on {
background-color: #4caf50;
}
.toggle-switch.off {
background-color: #ccc;
}
.toggle-switch.disabled {
opacity: 0.5;
cursor: not-allowed;
}
.toggle-slider {
position: absolute;
top: 3px;
left: 3px;
width: 24px;
height: 24px;
background-color: white;
// ...Usage Example:
import React, { useState } from 'react';
import ToggleSwitch from './ToggleSwitch';
function App() {
const [darkMode, setDarkMode] = useState(false);
const [notifications, setNotifications] = useState(true);
return (
<div>
<h2>Settings</h2>
<ToggleSwitch
label="Dark Mode"
initialState={darkMode}
onToggle={setDarkMode}
/>
<ToggleSwitch
label="Notifications"
initialState={notifications}
onToggle={setNotifications}
/>
<ToggleSwitch
label="Disabled Option"
disabled={true}
/>
</div>
);
}Best Practices:
Common Pitfalls:
Enhancements:
Enhanced Version with Icons:
import React, { useState } from 'react';
function ToggleSwitch({
initialState = false,
onToggle,
label = "Toggle",
disabled = false,
showIcons = false
}) {
const [isOn, setIsOn] = useState(initialState);
const handleToggle = () => {
if (disabled) return;
const newState = !isOn;
setIsOn(newState);
if (onToggle) onToggle(newState);
};
return (
<div className="toggle-switch-container">
<span className="toggle-label">{label}</span>
<button
className={`toggle-switch ${isOn ? 'on' : 'off'} ${disabled ? 'disabled' : ''}`}
onClick={handleToggle}
disabled={disabled}
role="switch"
aria-checked={isOn}
>
<span className="toggle-slider">
{showIcons && (
<span className="toggle-icon">
{isOn ? '✓' : '×'}
</span>
)}
</span>
</button>
<span className="toggle-status">{isOn ? 'ON' : 'OFF'}</span>
</div>
);
}Controlled Component Pattern:
// Controlled version - state managed by parent
function ToggleSwitch({ isOn, onChange, label }) {
return (
<button
className={`toggle-switch ${isOn ? 'on' : 'off'}`}
onClick={() => onChange(!isOn)}
role="switch"
aria-checked={isOn}
aria-label={label}
>
<span className="toggle-slider" />
</button>
);
}
// Usage
function Parent() {
const [enabled, setEnabled] = useState(false);
return (
<ToggleSwitch
isOn={enabled}
onChange={setEnabled}
label="Feature Toggle"
/>
);
}Testing Considerations:
Real-World Use Cases:
This toggle switch component is fundamental for building modern UIs with clear binary choices.
No files open
Click a file in the explorer to view