@material-ui/unstyled
Version:
Unstyled React components with which to implement custom design systems.
105 lines (94 loc) • 2.81 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { unstable_useControlled as useControlled, unstable_useEventCallback as useEventCallback, unstable_useForkRef as useForkRef, unstable_useIsFocusVisible as useIsFocusVisible } from '@material-ui/utils';
/**
* The basic building block for creating custom switches.
*
* Demos:
*
* - [Switches](https://material-ui.com/components/switches/)
*/
export default function useSwitch(props) {
const {
checked: checkedProp,
defaultChecked,
disabled,
onBlur,
onChange,
onFocus,
onFocusVisible,
readOnly,
required
} = props;
const [checked, setCheckedState] = useControlled({
controlled: checkedProp,
default: Boolean(defaultChecked),
name: 'Switch',
state: 'checked'
});
const handleInputChange = useEventCallback((event, otherHandler) => {
// Workaround for https://github.com/facebook/react/issues/9023
if (event.nativeEvent.defaultPrevented) {
return;
}
setCheckedState(event.target.checked);
onChange?.(event);
otherHandler?.(event);
});
const {
isFocusVisibleRef,
onBlur: handleBlurVisible,
onFocus: handleFocusVisible,
ref: focusVisibleRef
} = useIsFocusVisible();
const [focusVisible, setFocusVisible] = React.useState(false);
if (disabled && focusVisible) {
setFocusVisible(false);
}
React.useEffect(() => {
isFocusVisibleRef.current = focusVisible;
}, [focusVisible, isFocusVisibleRef]);
const inputRef = React.useRef(null);
const handleFocus = useEventCallback((event, otherHandler) => {
// Fix for https://github.com/facebook/react/issues/7769
if (!inputRef.current) {
inputRef.current = event.currentTarget;
}
handleFocusVisible(event);
if (isFocusVisibleRef.current === true) {
setFocusVisible(true);
onFocusVisible?.(event);
}
onFocus?.(event);
otherHandler?.(event);
});
const handleBlur = useEventCallback((event, otherHandler) => {
handleBlurVisible(event);
if (isFocusVisibleRef.current === false) {
setFocusVisible(false);
}
onBlur?.(event);
otherHandler?.(event);
});
const handleRefChange = useForkRef(focusVisibleRef, inputRef);
const getInputProps = (otherProps = {}) => _extends({
checked: checkedProp,
defaultChecked,
disabled,
readOnly,
required,
type: 'checkbox'
}, otherProps, {
onChange: event => handleInputChange(event, otherProps.onChange),
onFocus: event => handleFocus(event, otherProps.onFocus),
onBlur: event => handleBlur(event, otherProps.onBlur),
ref: handleRefChange
});
return {
checked,
disabled: Boolean(disabled),
focusVisible,
getInputProps,
readOnly: Boolean(readOnly)
};
}