-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScrollButton.js
More file actions
36 lines (30 loc) · 837 Bytes
/
ScrollButton.js
File metadata and controls
36 lines (30 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useState, useEffect } from "react";
import { FaArrowCircleUp } from "react-icons/fa";
import { Button } from "./Styles";
const ScrollButton = ({ scrollPoint }) => {
const [visible, setVisible] = useState(false);
useEffect(() => {
window.addEventListener("scroll", toggleVisible);
return () => window.removeEventListener("scroll", toggleVisible);
});
const toggleVisible = () => {
const scrolled = document.documentElement.scrollTop;
if (scrolled > scrollPoint) {
setVisible(true);
} else if (scrolled <= scrollPoint) {
setVisible(false);
}
};
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};
return (
<Button>
<FaArrowCircleUp onClick={scrollToTop} style={{ display: visible ? "inline" : "none" }} />
</Button>
);
};
export default ScrollButton;