|
| 1 | +import React, { useState } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import ReactCustomScrollbars from 'react-custom-scrollbars' |
| 4 | + |
| 5 | +import css from './Scrollbar.sss' |
| 6 | + |
| 7 | +const getFaderStyle = (type, bgColor, top) => ({ |
| 8 | + background: `linear-gradient(to ${type}, transparent, ${bgColor} 90%)`, |
| 9 | + opacity: (type === 'top' && top === 0.0) |
| 10 | + || (type === 'bottom' && top === 1.0) |
| 11 | + ? '0.0' : '1.0', |
| 12 | +}) |
| 13 | + |
| 14 | +const Track = ({ style }) => <div className={css.track} style={style} /> |
| 15 | + |
| 16 | +Track.propTypes = { |
| 17 | + style: PropTypes.shape({ |
| 18 | + opacity: PropTypes.number.isRequired, |
| 19 | + position: PropTypes.string.isRequired, |
| 20 | + transition: PropTypes.string.isRequired, |
| 21 | + width: PropTypes.number.isRequired, |
| 22 | + }).isRequired, |
| 23 | +} |
| 24 | + |
| 25 | +const Scrollbar = ({ bgColor, children }) => { |
| 26 | + const [top, setTop] = useState(0.0) |
| 27 | + |
| 28 | + return ( |
| 29 | + <div className={css.scrollWrapper}> |
| 30 | + <ReactCustomScrollbars |
| 31 | + autoHide |
| 32 | + onScrollFrame={({ top: newTop }) => setTop(newTop)} |
| 33 | + renderTrackVertical={Track} |
| 34 | + > |
| 35 | + {children} |
| 36 | + </ReactCustomScrollbars> |
| 37 | + <div |
| 38 | + className={css.faderTop} |
| 39 | + style={getFaderStyle('top', bgColor, top)} |
| 40 | + /> |
| 41 | + <div |
| 42 | + className={css.faderBottom} |
| 43 | + style={getFaderStyle('bottom', bgColor, top)} |
| 44 | + /> |
| 45 | + </div> |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +Scrollbar.propTypes = { |
| 50 | + bgColor: PropTypes.string.isRequired, |
| 51 | + children: PropTypes.node.isRequired, |
| 52 | +} |
| 53 | + |
| 54 | +export default Scrollbar |
0 commit comments