MCPcopy Create free account
hub / github.com/adobe/react-spectrum / Switch

Function Switch

starters/hooks/src/Switch.tsx:11–48  ·  view source on GitHub ↗
(props: AriaSwitchProps)

Source from the content-addressed store, hash-verified

9import './Switch.css';
10
11export function Switch(props: AriaSwitchProps) {
12 let state = useToggleState(props);
13 let ref = useRef<HTMLInputElement>(null);
14 /*- begin highlight -*/
15 let {labelProps, inputProps, isSelected, isPressed, isDisabled, isReadOnly, isInvalid} =
16 useSwitch(props, state, ref);
17 /*- end highlight -*/
18 let {hoverProps, isHovered} = useHover({isDisabled: isDisabled || isReadOnly});
19 let {isFocused, isFocusVisible, focusProps} = useFocusRing();
20
21 return (
22 <div className="react-aria-SwitchField" data-disabled={isDisabled || undefined}>
23 {/* The label wraps a visually hidden native input plus the styled track/handle. */}
24 <label
25 {...mergeProps(labelProps, hoverProps)}
26 className="react-aria-SwitchButton"
27 data-selected={isSelected || undefined}
28 data-pressed={isPressed || undefined}
29 data-hovered={isHovered || undefined}
30 data-focused={isFocused || undefined}
31 data-focus-visible={isFocusVisible || undefined}
32 data-disabled={isDisabled || undefined}
33 data-readonly={isReadOnly || undefined}
34 data-invalid={isInvalid || undefined}>
35 <VisuallyHidden elementType="span">
36 <input {...mergeProps(inputProps, focusProps)} ref={ref} />
37 </VisuallyHidden>
38 <div className="track indicator">
39 <div
40 data-disabled={isDisabled || undefined}
41 className={isSelected ? 'handle' : 'handle indicator'}
42 />
43 </div>
44 {props.children}
45 </label>
46 </div>
47 );
48}

Callers

nothing calls this directly

Calls 5

useToggleStateFunction · 0.90
useSwitchFunction · 0.90
useHoverFunction · 0.90
useFocusRingFunction · 0.90
mergePropsFunction · 0.90

Tested by

no test coverage detected