Skip to content

Commit

Permalink
Fixed type error caused by scroll to top button smaranjitghose#543
Browse files Browse the repository at this point in the history
  • Loading branch information
niteshsinha17 committed Feb 22, 2021
1 parent 58ea1ee commit 3218255
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/components/ScrollToTopButton/ScrollToTopButton.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React, { useEffect, useRef } from "react";
import React, { useEffect, useRef, useState } from "react";
import Button from "react-bootstrap/Button";
import "bootstrap/dist/css/bootstrap.min.css";
import { AiOutlineArrowUp } from "react-icons/ai";
import styles from "./style.module.css";

const ScrollToTop = () => {
let toTopButtonObj = useRef(null);
const [buttonState, changeButtonState] = useState({
visible: false,
lastHeight: 0,
});
useEffect(() => {
scrollFunction();
});
Expand All @@ -14,10 +18,23 @@ const ScrollToTop = () => {
};

function scrollFunction() {
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
toTopButtonObj.current.style.display = "block";
if (
document.body.scrollTop > 50 ||
document.documentElement.scrollTop > 50
) {
if (!buttonState.visible) {
changeButtonState({
visible: true,
lastHeight: document.body.scrollTop,
});
}
} else {
toTopButtonObj.current.style.display = "none";
if (buttonState.visible) {
changeButtonState({
visible: false,
lastHeight: document.body.scrollTop,
});
}
}
}

Expand All @@ -26,7 +43,7 @@ const ScrollToTop = () => {
document.documentElement.scrollTop = 0;
}

return (
const button = buttonState.visible ? (
<Button
ref={toTopButtonObj}
className={styles.toTopButton}
Expand All @@ -36,7 +53,9 @@ const ScrollToTop = () => {
>
<AiOutlineArrowUp />
</Button>
);
) : null;

return button;
};

export default ScrollToTop;

0 comments on commit 3218255

Please sign in to comment.