create react typescript toggle component as a pro react dev
- User
create react typescript toggle component as a pro react dev
Assistant
I'll create a reusable Toggle component with TypeScript, following React best practices, accessibility, and modern patterns. I'll include proper typing, animations, and customization options.
import { forwardRef, ComponentPropsWithoutRef } from 'react'; import styled, { css, keyframes } from 'styled-components'; interface ToggleProps extends ComponentPropsWithoutRef<'button'> { isOn: boolean; onToggle: () => void; size?: 'small' | 'medium' | 'large'; disabled?: boolean; label?: string; color?: string; } const slideAnimation = keyframes` 0% { transform: translateX(0); } 100% { transform: translateX(100%); } `; const reverseSlideAnimation = keyframes` 0% { transform: translateX(100%); } 100% { transform: translateX(0); } `; const ToggleWrapper = styled.button<{ $size: ToggleProps['size']; $isOn: boolean; $color?: string; }>` position: relative; display: inline-flex; align-items: center; border: none; padding: 0; cursor: pointer; background: none; outline: none; ${({ $size }) => { const sizes = { small: { width: '32px', height: '18px' }, medium: { width: '48px', height: '24px' }, large: { width: '64px', height: '32px' }, }; return css` width: ${sizes[$size || 'medium'].width}; height: ${sizes[$size || 'medium'].height}; `; }} &:disabled { cursor: not-allowed; opacity: 0.6; } &:focus-visible { box-shadow: 0 0 0 2px #fff, 0 0 0 4px #000; border-radius: 999px; } `; const Track = styled.div<{ $isOn: boolean; $color?: string }>` position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: ${({ $isOn, $color }) => $isOn ? $color || '#2563eb' : '#e5e7eb'}; border-radius: 999px; transition: background-color 0.2s ease; `; const Thumb = styled.div<{ $isOn: boolean; $size: ToggleProps['size'] }>` position: absolute; top: 2px; left: 2px; background-color: white; border-radius: 50%; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15); ${({ $size }) => { const sizes = { small: { size: '14px' }, medium: { size: '20px' }, large: { size: '28px' }, }; return css` width: ${sizes[$size || 'medium'].size}; height: ${sizes[$size || 'medium'].size}; `; }} animation: ${({ $isOn }) => $isOn ? css` ${slideAnimation} 0.2s forwards ` : css` ${reverseSlideAnimation} 0.2s forwards `}; `; const VisuallyHidden = styled.span` position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; `; export const Toggle = forwardRef<HTMLButtonElement, ToggleProps>( ( { isOn, onToggle, size = 'medium', disabled = false, label, color, ...props }, ref ) => { return ( <ToggleWrapper ref={ref} type="button" role="switch" aria-checked={isOn} aria-label={label} disabled={disabled} onClick={onToggle} $size={size} $isOn={isOn} $color={color} {...props} > <Track $isOn={isOn} $color={color} /> <Thumb $isOn={isOn} $size={size} /> {label && <VisuallyHidden>{label}</VisuallyHidden>} </ToggleWrapper> ); } ); Toggle.displayName = 'Toggle';Usage example:
import { useState } from 'react'; import { Toggle } from './Toggle'; export const App = () => { const [isEnabled, setIsEnabled] = useState(false); return ( <Toggle isOn={isEnabled} onToggle={() => setIsEnabled(!isEnabled)} label="Toggle feature" size="medium" color="#0ea5e9" /> ); };This Toggle component includes:
- Full TypeScript support with proper typing
- Accessibility features (ARIA attributes, keyboard navigation)
- Smooth animations using styled-components
- Three size variants (small, medium, large)
- Customizable colors
- Focus states and disabled states
- Forward ref support
- Proper event handling
- Visual feedback for user interactions
- Screen reader support with hidden labels
The component is built using styled-components for styling, but you could easily adapt it to use CSS modules, Tailwind, or other styling solutions. Let me know if you'd like to see any modifications or have questions about specific features!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.