UNPKG

3.22 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import * as React from 'react';
5import { unstable_useControlled as useControlled, unstable_useForkRef as useForkRef, unstable_useIsFocusVisible as useIsFocusVisible } from '@mui/utils';
6/**
7 * The basic building block for creating custom switches.
8 *
9 * Demos:
10 *
11 * - [Switch](https://mui.com/base-ui/react-switch/#hook)
12 *
13 * API:
14 *
15 * - [useSwitch API](https://mui.com/base-ui/react-switch/hooks-api/#use-switch)
16 */
17export function useSwitch(props) {
18 const {
19 checked: checkedProp,
20 defaultChecked,
21 disabled,
22 onBlur,
23 onChange,
24 onFocus,
25 onFocusVisible,
26 readOnly,
27 required
28 } = props;
29 const [checked, setCheckedState] = useControlled({
30 controlled: checkedProp,
31 default: Boolean(defaultChecked),
32 name: 'Switch',
33 state: 'checked'
34 });
35 const createHandleInputChange = otherProps => event => {
36 var _otherProps$onChange;
37 // Workaround for https://github.com/facebook/react/issues/9023
38 if (event.nativeEvent.defaultPrevented) {
39 return;
40 }
41 setCheckedState(event.target.checked);
42 onChange == null || onChange(event);
43 (_otherProps$onChange = otherProps.onChange) == null || _otherProps$onChange.call(otherProps, event);
44 };
45 const {
46 isFocusVisibleRef,
47 onBlur: handleBlurVisible,
48 onFocus: handleFocusVisible,
49 ref: focusVisibleRef
50 } = useIsFocusVisible();
51 const [focusVisible, setFocusVisible] = React.useState(false);
52 if (disabled && focusVisible) {
53 setFocusVisible(false);
54 }
55 React.useEffect(() => {
56 isFocusVisibleRef.current = focusVisible;
57 }, [focusVisible, isFocusVisibleRef]);
58 const inputRef = React.useRef(null);
59 const createHandleFocus = otherProps => event => {
60 var _otherProps$onFocus;
61 // Fix for https://github.com/facebook/react/issues/7769
62 if (!inputRef.current) {
63 inputRef.current = event.currentTarget;
64 }
65 handleFocusVisible(event);
66 if (isFocusVisibleRef.current === true) {
67 setFocusVisible(true);
68 onFocusVisible == null || onFocusVisible(event);
69 }
70 onFocus == null || onFocus(event);
71 (_otherProps$onFocus = otherProps.onFocus) == null || _otherProps$onFocus.call(otherProps, event);
72 };
73 const createHandleBlur = otherProps => event => {
74 var _otherProps$onBlur;
75 handleBlurVisible(event);
76 if (isFocusVisibleRef.current === false) {
77 setFocusVisible(false);
78 }
79 onBlur == null || onBlur(event);
80 (_otherProps$onBlur = otherProps.onBlur) == null || _otherProps$onBlur.call(otherProps, event);
81 };
82 const handleInputRef = useForkRef(focusVisibleRef, inputRef);
83 const getInputProps = (otherProps = {}) => _extends({
84 checked: checkedProp,
85 defaultChecked,
86 disabled,
87 readOnly,
88 ref: handleInputRef,
89 required,
90 type: 'checkbox',
91 role: 'switch',
92 'aria-checked': checkedProp
93 }, otherProps, {
94 onChange: createHandleInputChange(otherProps),
95 onFocus: createHandleFocus(otherProps),
96 onBlur: createHandleBlur(otherProps)
97 });
98 return {
99 checked,
100 disabled: Boolean(disabled),
101 focusVisible,
102 getInputProps,
103 inputRef: handleInputRef,
104 readOnly: Boolean(readOnly)
105 };
106}
\No newline at end of file