2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
2022-08-02 10:31:11 +00:00
|
|
|
import React, { useCallback } from 'react';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { makeStyles } from 'tss-react/mui';
|
2022-08-02 10:31:11 +00:00
|
|
|
|
|
|
|
import { isMobileBrowser } from '../../../environment/utils';
|
2022-10-20 09:11:27 +00:00
|
|
|
import { ISwitchProps } from '../types';
|
2022-08-02 10:31:11 +00:00
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
interface IProps extends ISwitchProps {
|
2022-08-02 10:31:11 +00:00
|
|
|
|
2022-11-02 10:49:30 +00:00
|
|
|
className?: string;
|
|
|
|
|
2022-08-02 10:31:11 +00:00
|
|
|
/**
|
|
|
|
* Id of the toggle.
|
|
|
|
*/
|
|
|
|
id?: string;
|
|
|
|
}
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
const useStyles = makeStyles()((theme: Theme) => {
|
2022-08-02 10:31:11 +00:00
|
|
|
return {
|
|
|
|
container: {
|
|
|
|
position: 'relative',
|
|
|
|
backgroundColor: theme.palette.ui05,
|
|
|
|
borderRadius: '12px',
|
|
|
|
width: '40px',
|
|
|
|
height: '24px',
|
|
|
|
border: 0,
|
|
|
|
outline: 0,
|
|
|
|
cursor: 'pointer',
|
|
|
|
transition: '.3s',
|
|
|
|
display: 'inline-block',
|
|
|
|
|
|
|
|
'&.disabled': {
|
|
|
|
backgroundColor: theme.palette.ui05,
|
|
|
|
cursor: 'default',
|
|
|
|
|
|
|
|
'& .toggle': {
|
|
|
|
backgroundColor: theme.palette.ui03
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'&.is-mobile': {
|
|
|
|
height: '32px',
|
|
|
|
width: '50px',
|
|
|
|
borderRadius: '32px'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
containerOn: {
|
|
|
|
backgroundColor: theme.palette.action01
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle: {
|
|
|
|
width: '16px',
|
|
|
|
height: '16px',
|
|
|
|
position: 'absolute',
|
|
|
|
top: '4px',
|
|
|
|
left: '4px',
|
|
|
|
backgroundColor: theme.palette.ui10,
|
|
|
|
borderRadius: '100%',
|
|
|
|
transition: '.3s',
|
|
|
|
|
|
|
|
'&.is-mobile': {
|
|
|
|
width: '24px',
|
|
|
|
height: '24px'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleOn: {
|
|
|
|
left: '20px',
|
|
|
|
|
|
|
|
'&.is-mobile': {
|
|
|
|
left: '22px'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
checkbox: {
|
|
|
|
height: 0,
|
|
|
|
width: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-11-02 10:49:30 +00:00
|
|
|
const Switch = ({ className, id, checked, disabled, onChange }: IProps) => {
|
2022-09-13 07:36:00 +00:00
|
|
|
const { classes: styles, cx } = useStyles();
|
2022-08-02 10:31:11 +00:00
|
|
|
const isMobile = isMobileBrowser();
|
|
|
|
|
|
|
|
const change = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
onChange(e.target.checked);
|
|
|
|
}, []);
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
return (
|
|
|
|
<label
|
|
|
|
className = { cx('toggle-container', styles.container, checked && styles.containerOn,
|
2022-11-02 10:49:30 +00:00
|
|
|
isMobile && 'is-mobile', disabled && 'disabled', className) }>
|
2022-09-13 07:36:00 +00:00
|
|
|
<input
|
|
|
|
type = 'checkbox'
|
|
|
|
{ ...(id ? { id } : {}) }
|
|
|
|
checked = { checked }
|
|
|
|
className = { styles.checkbox }
|
|
|
|
disabled = { disabled }
|
|
|
|
onChange = { change } />
|
|
|
|
<div className = { cx('toggle', styles.toggle, checked && styles.toggleOn, isMobile && 'is-mobile') } />
|
|
|
|
</label>
|
|
|
|
);
|
2022-08-02 10:31:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Switch;
|