({
id,
active,
onChange,
onFocus,
onBlur,
className,
Container = ToggleContainer,
Background = ToggleBackground,
Handle = ToggleHandle,
})
| 45 | `; |
| 46 | |
| 47 | function Toggle({ |
| 48 | id, |
| 49 | active, |
| 50 | onChange, |
| 51 | onFocus, |
| 52 | onBlur, |
| 53 | className, |
| 54 | Container = ToggleContainer, |
| 55 | Background = ToggleBackground, |
| 56 | Handle = ToggleHandle, |
| 57 | }) { |
| 58 | const [localActive, setLocalActive] = useState(active); |
| 59 | // Follow the `active` prop when given, so changes made outside the toggle are shown |
| 60 | const isControlled = active !== undefined; |
| 61 | const isActive = isControlled ? active : localActive; |
| 62 | |
| 63 | function handleToggle() { |
| 64 | if (!isControlled) { |
| 65 | setLocalActive(!isActive); |
| 66 | } |
| 67 | if (onChange) { |
| 68 | onChange(!isActive); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return ( |
| 73 | <Container |
| 74 | id={id} |
| 75 | onFocus={onFocus} |
| 76 | onBlur={onBlur} |
| 77 | className={className} |
| 78 | onClick={handleToggle} |
| 79 | role="switch" |
| 80 | aria-checked={isActive?.toString()} |
| 81 | aria-expanded={null} |
| 82 | > |
| 83 | <Background isActive={isActive} /> |
| 84 | <Handle isActive={isActive} /> |
| 85 | </Container> |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | Toggle.propTypes = { |
| 90 | id: PropTypes.string, |
nothing calls this directly
no outgoing calls
no test coverage detected