create react typescript toggle component as a pro react dev
- User
create react typescript toggle component as a pro react dev
Assistant
Here's a professional React TypeScript Toggle Component with accessibility, customizability, and proper state management:
✅ Features:
- Controlled Component: Uses
isCheckedandonChangeprops for external state management. - Accessibility: ARIA roles, keyboard support, and focus handling.
- Customizable: Accepts
className,disabled, and more. - Responsive UI: Clean, modern toggle with CSS (you can style it as needed).
🧩 Code Implementation
import React from 'react'; /** * Toggle Component Props */ interface ToggleProps { isChecked: boolean; onChange: (checked: boolean) => void; disabled?: boolean; className?: string; label?: string; // Optional label for accessibility } /** * Toggle Component */ const Toggle: React.FC<ToggleProps> = ({ isChecked, onChange, disabled = false, className = '', label = '', }) => { const handleChange = () => { if (disabled) return; onChange(!isChecked); }; return ( <div role="switch" aria-checked={isChecked} aria-label={label} tabIndex={0} onClick={handleChange} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleChange(); } }} className={`toggle ${className} ${isChecked ? 'checked' : ''} ${disabled ? 'disabled' : ''}`} style={{ cursor: disabled ? 'not-allowed' : 'pointer' }} > <div className="toggle-thumb" /> </div> ); }; export default Toggle;
🎨 Example CSS (Optional)
You can import this CSS or use a CSS-in-JS solution:
/* Toggle Base Styles */ .toggle { width: 50px; height: 25px; background-color: #ccc; border-radius: 15px; position: relative; display: inline-block; transition: background-color 0.3s ease; cursor: pointer; } .toggle.checked { background-color: #4caf50; } .toggle.disabled { cursor: not-allowed; opacity: 0.5; } /* Toggle Thumb */ .toggle-thumb { width: 20px; height: 20px; background-color: white; border-radius: 50%; position: absolute; top: 2px; left: 2px; transition: left 0.3s ease; } .toggle.checked .toggle-thumb { left: 28px; }
📦 Usage Example
import React, { useState } from 'react'; import Toggle from './Toggle'; const App: React.FC = () => { const [isChecked, setIsChecked] = useState(false); return ( <div> <label> <Toggle isChecked={isChecked} onChange={setIsChecked} label="Toggle Switch" /> <span> Toggle Label </span> </label> </div> ); }; export default App;
🧠 Best Practices
- Accessibility: Always associate labels with controls using
<label>oraria-label. - State Management: Use
isCheckedandonChangefor external control. - Styling: Keep UI logic separate from component logic (use CSS or CSS-in-JS).
- Keyboard Support: Ensure the toggle is fully operable with keyboard (Enter/Space).
This component is production-ready, testable, and easily extendable for advanced use cases (e.g., custom icons, animations, or themes). Let me know if you want to add more features like themes, animations, or form integration! 🚀
- Controlled Component: Uses
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.