@activelylearn/material-ui
Version:
Material-UI's workspace package
183 lines (176 loc) • 4.7 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import withStyles from '../styles/withStyles';
import { capitalize } from '../utils/helpers';
import SwitchBase from '../internal/SwitchBase';
export const styles = theme => ({
root: {
display: 'inline-flex',
width: 62,
position: 'relative',
flexShrink: 0,
// For correct alignment with the text.
verticalAlign: 'middle',
},
icon: {
boxShadow: theme.shadows[1],
backgroundColor: 'currentColor',
width: 20,
height: 20,
borderRadius: '50%',
},
iconChecked: {
boxShadow: theme.shadows[2],
},
switchBase: {
zIndex: 1,
color: theme.palette.type === 'light' ? theme.palette.grey[50] : theme.palette.grey[400],
transition: theme.transitions.create('transform', {
duration: theme.transitions.duration.shortest,
}),
},
checked: {
transform: 'translateX(14px)',
'& + $bar': {
opacity: 0.5,
},
},
colorPrimary: {
'&$checked': {
color: theme.palette.primary.main,
'& + $bar': {
backgroundColor: theme.palette.primary.main,
},
},
},
colorSecondary: {
'&$checked': {
color: theme.palette.secondary.main,
'& + $bar': {
backgroundColor: theme.palette.secondary.main,
},
},
},
disabled: {
'& + $bar': {
opacity: theme.palette.type === 'light' ? 0.12 : 0.1,
},
'& $icon': {
boxShadow: theme.shadows[1],
},
'&$switchBase': {
color: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[800],
'& + $bar': {
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
},
},
},
bar: {
borderRadius: 7,
display: 'block',
position: 'absolute',
width: 34,
height: 14,
top: '50%',
left: '50%',
marginTop: -7,
marginLeft: -17,
transition: theme.transitions.create(['opacity', 'background-color'], {
duration: theme.transitions.duration.shortest,
}),
backgroundColor:
theme.palette.type === 'light' ? theme.palette.common.black : theme.palette.common.white,
opacity: theme.palette.type === 'light' ? 0.38 : 0.3,
},
});
function Switch(props) {
const { classes, className, color, ...other } = props;
return (
<span className={classNames(classes.root, className)}>
<SwitchBase
icon={<span className={classes.icon} />}
classes={{
root: classNames(classes.switchBase, classes[`color${capitalize(color)}`]),
checked: classes.checked,
disabled: classes.disabled,
}}
checkedIcon={<span className={classNames(classes.icon, classes.iconChecked)} />}
{...other}
/>
<span className={classes.bar} />
</span>
);
}
Switch.propTypes = {
/**
* If `true`, the component is checked.
*/
checked: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
/**
* The icon to display when the component is checked.
*/
checkedIcon: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css-api) below for more details.
*/
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
*/
color: PropTypes.oneOf(['primary', 'secondary', 'default']),
/**
* @ignore
*/
defaultChecked: PropTypes.bool,
/**
* If `true`, the switch will be disabled.
*/
disabled: PropTypes.bool,
/**
* If `true`, the ripple effect will be disabled.
*/
disableRipple: PropTypes.bool,
/**
* The icon to display when the component is unchecked.
*/
icon: PropTypes.node,
/**
* The id of the `input` element.
*/
id: PropTypes.string,
/**
* Properties applied to the `input` element.
*/
inputProps: PropTypes.object,
/**
* Use that property to pass a ref callback to the native input component.
*/
inputRef: PropTypes.func,
/**
* Callback fired when the state is changed.
*
* @param {object} event The event source of the callback.
* You can pull out the new value by accessing `event.target.checked`.
* @param {boolean} checked The `checked` value of the switch
*/
onChange: PropTypes.func,
/**
* The input component property `type`.
*/
type: PropTypes.string,
/**
* The value of the component.
*/
value: PropTypes.string,
};
Switch.defaultProps = {
color: 'secondary',
};
export default withStyles(styles, { name: 'MuiSwitch' })(Switch);