2021-10-08 08:05:16 +00:00
|
|
|
import React from 'react';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { makeStyles } from 'tss-react/mui';
|
2021-10-08 08:05:16 +00:00
|
|
|
|
2022-11-03 08:35:51 +00:00
|
|
|
interface IProps {
|
2021-10-08 08:05:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'aria-label' text.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
ariaLabel: string;
|
2021-10-08 08:05:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum value for slider value.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
max: number;
|
2021-10-08 08:05:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimum value for slider value.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
min: number;
|
2021-10-08 08:05:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked on change.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
2021-10-08 08:05:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The granularity that the value must adhere to.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
step: number;
|
2021-10-08 08:05:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The current value where the knob is positioned.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
value: number;
|
2022-11-03 08:35:51 +00:00
|
|
|
}
|
2021-10-08 08:05:16 +00:00
|
|
|
|
2022-11-15 07:50:22 +00:00
|
|
|
const useStyles = makeStyles()(theme => {
|
2022-07-14 07:10:08 +00:00
|
|
|
// keep the same height for all elements:
|
2021-10-08 08:05:16 +00:00
|
|
|
// input, input track & fake track(div)
|
|
|
|
const height = 6;
|
|
|
|
|
|
|
|
const inputTrack = {
|
|
|
|
background: 'transparent',
|
|
|
|
height
|
|
|
|
};
|
|
|
|
const inputThumb = {
|
|
|
|
background: theme.palette.text01,
|
|
|
|
border: 0,
|
|
|
|
borderRadius: '50%',
|
|
|
|
height: 24,
|
|
|
|
width: 24
|
|
|
|
};
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
const focused = { // @ts-ignore
|
2021-10-08 08:05:16 +00:00
|
|
|
outline: `1px solid ${theme.palette.action03Focus}`
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
sliderContainer: {
|
|
|
|
cursor: 'pointer',
|
|
|
|
width: '100%',
|
|
|
|
position: 'relative',
|
|
|
|
textAlign: 'center'
|
|
|
|
|
|
|
|
},
|
|
|
|
knobContainer: {
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
marginLeft: 2,
|
|
|
|
marginRight: 2,
|
|
|
|
position: 'absolute',
|
|
|
|
width: '100%'
|
|
|
|
},
|
|
|
|
knob: {
|
|
|
|
background: theme.palette.text01,
|
|
|
|
borderRadius: '50%',
|
|
|
|
display: 'inline-block',
|
|
|
|
height,
|
|
|
|
width: 6
|
|
|
|
},
|
|
|
|
track: {
|
|
|
|
background: theme.palette.ui02,
|
2022-09-13 07:36:00 +00:00
|
|
|
borderRadius: Number(theme.shape.borderRadius) / 2,
|
2021-10-08 08:05:16 +00:00
|
|
|
height
|
|
|
|
},
|
|
|
|
slider: {
|
|
|
|
// Use an additional class here to override global CSS specificity
|
|
|
|
'&.custom-slider': {
|
|
|
|
'-webkit-appearance': 'none',
|
|
|
|
background: 'transparent',
|
2021-10-29 11:26:23 +00:00
|
|
|
height,
|
2021-10-08 08:05:16 +00:00
|
|
|
left: 0,
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
width: '100%',
|
|
|
|
|
|
|
|
'&:focus': {
|
|
|
|
// override global styles in order to use our own color
|
|
|
|
outline: 'none !important',
|
|
|
|
|
|
|
|
'&::-webkit-slider-runnable-track': focused,
|
|
|
|
'&::ms-track': focused,
|
|
|
|
'&::-moz-range-track': focused
|
|
|
|
},
|
|
|
|
|
|
|
|
'&::-webkit-slider-runnable-track': {
|
|
|
|
'-webkit-appearance': 'none',
|
|
|
|
...inputTrack
|
|
|
|
},
|
|
|
|
'&::-webkit-slider-thumb': {
|
|
|
|
'-webkit-appearance': 'none',
|
|
|
|
position: 'relative',
|
|
|
|
top: -6,
|
|
|
|
...inputThumb
|
|
|
|
},
|
|
|
|
|
|
|
|
'&::ms-track': {
|
|
|
|
...inputTrack
|
|
|
|
},
|
|
|
|
'&::-ms-thumb': {
|
|
|
|
...inputThumb
|
|
|
|
},
|
|
|
|
|
|
|
|
'&::-moz-range-track': {
|
|
|
|
...inputTrack
|
|
|
|
},
|
|
|
|
'&::-moz-range-thumb': {
|
|
|
|
...inputThumb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom slider.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
2022-11-03 08:35:51 +00:00
|
|
|
function Slider({ ariaLabel, max, min, onChange, step, value }: IProps) {
|
2022-09-13 07:36:00 +00:00
|
|
|
const { classes, cx } = useStyles();
|
2021-10-08 08:05:16 +00:00
|
|
|
const knobs = [ ...Array(Math.floor((max - min) / step) + 1) ];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className = { classes.sliderContainer }>
|
2022-09-13 07:36:00 +00:00
|
|
|
<ul className = { cx('empty-list', classes.knobContainer) }>
|
2021-10-08 08:05:16 +00:00
|
|
|
{knobs.map((_, i) => (
|
|
|
|
<li
|
|
|
|
className = { classes.knob }
|
|
|
|
key = { `knob-${i}` } />))}
|
|
|
|
</ul>
|
|
|
|
<div className = { classes.track } />
|
|
|
|
<input
|
|
|
|
aria-label = { ariaLabel }
|
2022-09-13 07:36:00 +00:00
|
|
|
className = { cx(classes.slider, 'custom-slider') }
|
2021-10-08 08:05:16 +00:00
|
|
|
max = { max }
|
|
|
|
min = { min }
|
|
|
|
onChange = { onChange }
|
|
|
|
step = { step }
|
|
|
|
type = 'range'
|
|
|
|
value = { value } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Slider;
|