()
| 3 | import React from 'react'; |
| 4 | |
| 5 | const ScrollUpButton = () => { |
| 6 | const [state, setState] = React.useState({display: 'none', opacity: 0}); |
| 7 | React.useEffect(() => { |
| 8 | const scrollHandler = () => { |
| 9 | const currentScrollPos = Math.max(window.pageYOffset - 1000, 0); |
| 10 | const opacity = Math.min(currentScrollPos / 1000, 1); |
| 11 | const nextState = {display: currentScrollPos > 0 ? 'inherit' : 'none', opacity}; |
| 12 | if (state.display !== nextState.display || state.opacity !== nextState.opacity) { |
| 13 | setState(nextState); |
| 14 | } |
| 15 | }; |
| 16 | window.addEventListener('scroll', scrollHandler); |
| 17 | return () => window.removeEventListener('scroll', scrollHandler); |
| 18 | }, []); |
| 19 | |
| 20 | return ( |
| 21 | <Fab |
| 22 | color="primary" |
| 23 | style={{ |
| 24 | position: 'fixed', |
| 25 | bottom: '30px', |
| 26 | right: '30px', |
| 27 | zIndex: 100000, |
| 28 | display: state.display, |
| 29 | opacity: state.opacity, |
| 30 | }} |
| 31 | onClick={() => window.scrollTo(0, 0)}> |
| 32 | <KeyboardArrowUp /> |
| 33 | </Fab> |
| 34 | ); |
| 35 | }; |
| 36 | |
| 37 | export default ScrollUpButton; |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…