|
| 1 | +import React, { Component } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import { withStyles } from "@material-ui/core/styles"; |
| 4 | +import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp"; |
| 5 | +import KeyboardArrowDownIcon from "@material-ui/icons/KeyboardArrowDown"; |
| 6 | + |
| 7 | +const styles = () => ({ |
| 8 | + scrollWrapper: { |
| 9 | + position: "relative", |
| 10 | + height: "100vh", |
| 11 | + overflow: "hidden" |
| 12 | + }, |
| 13 | + scrollContent: { |
| 14 | + height: "100%", |
| 15 | + overflowY: "auto", |
| 16 | + scrollbarWidth: "none", |
| 17 | + msOverflowStyle: "none", |
| 18 | + "&::-webkit-scrollbar": { |
| 19 | + display: "none" |
| 20 | + } |
| 21 | + }, |
| 22 | + scrollIndicator: { |
| 23 | + position: "absolute", |
| 24 | + left: 0, |
| 25 | + right: 0, |
| 26 | + height: "40px", |
| 27 | + display: "flex", |
| 28 | + alignItems: "center", |
| 29 | + justifyContent: "center", |
| 30 | + background: "linear-gradient(to transparent, rgba(34, 34, 34, 0.8))", |
| 31 | + color: "#aaaaaa", |
| 32 | + pointerEvents: "none", |
| 33 | + zIndex: 10, |
| 34 | + transition: "opacity 0.3s ease" |
| 35 | + }, |
| 36 | + topIndicator: { |
| 37 | + top: 0, |
| 38 | + background: "linear-gradient(to bottom, rgba(34, 34, 34, 0.9) 0%, rgba(34, 34, 34, 0.7) 70%, transparent 100%)" |
| 39 | + }, |
| 40 | + bottomIndicator: { |
| 41 | + bottom: 0, |
| 42 | + background: "linear-gradient(to top, rgba(34, 34, 34, 0.9) 0%, rgba(34, 34, 34, 0.7) 70%, transparent 100%)" |
| 43 | + }, |
| 44 | + hidden: { |
| 45 | + opacity: 0 |
| 46 | + }, |
| 47 | + visible: { |
| 48 | + opacity: 1 |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | +class ScrollableMenuWrapper extends Component { |
| 53 | + static propTypes = { |
| 54 | + classes: PropTypes.object.isRequired, |
| 55 | + children: PropTypes.node.isRequired |
| 56 | + }; |
| 57 | + |
| 58 | + constructor(props) { |
| 59 | + super(props); |
| 60 | + this.state = { |
| 61 | + showTopIndicator: false, |
| 62 | + showBottomIndicator: false |
| 63 | + }; |
| 64 | + this.scrollRef = React.createRef(); |
| 65 | + } |
| 66 | + |
| 67 | + componentDidMount() { |
| 68 | + this.checkScrollIndicators(); |
| 69 | + window.addEventListener("resize", this.checkScrollIndicators); |
| 70 | + } |
| 71 | + |
| 72 | + componentWillUnmount() { |
| 73 | + window.removeEventListener("resize", this.checkScrollIndicators); |
| 74 | + } |
| 75 | + |
| 76 | + checkScrollIndicators = () => { |
| 77 | + const element = this.scrollRef.current; |
| 78 | + if (!element) return; |
| 79 | + |
| 80 | + const { scrollTop, scrollHeight, clientHeight } = element; |
| 81 | + const showTopIndicator = scrollTop > 10; |
| 82 | + const showBottomIndicator = scrollTop + clientHeight < scrollHeight - 10; |
| 83 | + |
| 84 | + this.setState({ |
| 85 | + showTopIndicator, |
| 86 | + showBottomIndicator |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + render() { |
| 91 | + const { classes, children } = this.props; |
| 92 | + const { showTopIndicator, showBottomIndicator } = this.state; |
| 93 | + |
| 94 | + return ( |
| 95 | + <div className={classes.scrollWrapper}> |
| 96 | + <div ref={this.scrollRef} className={classes.scrollContent} onScroll={this.checkScrollIndicators}> |
| 97 | + {children} |
| 98 | + </div> |
| 99 | + <div |
| 100 | + className={`${classes.scrollIndicator} ${classes.topIndicator} ${ |
| 101 | + showTopIndicator ? classes.visible : classes.hidden |
| 102 | + }`} |
| 103 | + > |
| 104 | + <KeyboardArrowUpIcon /> |
| 105 | + </div> |
| 106 | + <div |
| 107 | + className={`${classes.scrollIndicator} ${classes.bottomIndicator} ${ |
| 108 | + showBottomIndicator ? classes.visible : classes.hidden |
| 109 | + }`} |
| 110 | + > |
| 111 | + <KeyboardArrowDownIcon /> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export default withStyles(styles)(ScrollableMenuWrapper); |
0 commit comments