MCPcopy Create free account
hub / github.com/decaporg/decap-cms / Toggle

Function Toggle

packages/decap-cms-ui-default/src/Toggle.js:47–87  ·  view source on GitHub ↗
({
  id,
  active,
  onChange,
  onFocus,
  onBlur,
  className,
  Container = ToggleContainer,
  Background = ToggleBackground,
  Handle = ToggleHandle,
})

Source from the content-addressed store, hash-verified

45`;
46
47function 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
89Toggle.propTypes = {
90 id: PropTypes.string,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected