Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-30b-a3b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

create react typescript toggle component as a pro react dev

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-sonnet-20241022’s answer.
  1. User

    create react typescript toggle component as a pro react dev

  2. Qwen Assistant

    Here's a professional React TypeScript Toggle Component with accessibility, customizability, and proper state management:


    ✅ Features:

    • Controlled Component: Uses isChecked and onChange props 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> or aria-label.
    • State Management: Use isChecked and onChange for 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! 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.