Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

45 lignes
1.1 KiB

  1. import React from 'react';
  2. import * as Icon from 'react-feather';
  3. const GoTop = ({scrollStepInPx, delayInMs}) => {
  4. const [thePosition, setThePosition] = React.useState(false);
  5. const timeoutRef = React.useRef(null);
  6. React.useEffect(() => {
  7. document.addEventListener("scroll", () => {
  8. if (window.scrollY > 170) {
  9. setThePosition(true)
  10. } else {
  11. setThePosition(false);
  12. }
  13. });
  14. }, [])
  15. const onScrollStep = () => {
  16. if (window.pageYOffset === 0){
  17. clearInterval(timeoutRef.current);
  18. }
  19. window.scroll(0, window.pageYOffset - scrollStepInPx);
  20. }
  21. const scrollToTop = () => {
  22. timeoutRef.current = setInterval(onScrollStep, delayInMs);
  23. }
  24. const renderGoTopIcon = () => {
  25. return (
  26. <div className={`go-top ${thePosition ? 'active' : ''}`} onClick={scrollToTop}>
  27. <Icon.ArrowUp />
  28. </div>
  29. )
  30. }
  31. return (
  32. <React.Fragment>
  33. {renderGoTopIcon()}
  34. </React.Fragment>
  35. )
  36. }
  37. export default GoTop;