-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from MastanSayyad/main
Re-designed Scroll-to-Top button
- Loading branch information
Showing
1 changed file
with
41 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,57 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import styled from "styled-components"; | ||
import { FaArrowUp } from "react-icons/fa"; | ||
|
||
const ScrollToTopButton = () => { | ||
const GoToTop = () => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const toggleVisibility = () => { | ||
if (window.pageYOffset > 300) { | ||
setIsVisible(true); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
}; | ||
useEffect(() => { | ||
const handleScroll = () => { | ||
const currentScrollY = window.scrollY; | ||
|
||
const scrollToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: "smooth", | ||
}); | ||
}; | ||
setIsVisible(currentScrollY > 200); // Change 200 to your desired scroll distance | ||
}; | ||
|
||
window.addEventListener("scroll", handleScroll); | ||
|
||
useEffect(() => { | ||
window.addEventListener("scroll", toggleVisibility); | ||
return () => { | ||
window.removeEventListener("scroll", toggleVisibility); | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}, []); | ||
|
||
const goToTop = () => { | ||
window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); | ||
}; | ||
|
||
return ( | ||
<div className="fixed bottom-8 right-8"> | ||
<> | ||
{isVisible && ( | ||
<button | ||
onClick={scrollToTop} | ||
className="p-4 bg-green-500 text-white rounded-full shadow-lg hover:bg-green-600 focus:outline-none" | ||
style={{ fontSize: '24px', width: '60px', height: '60px' }} | ||
> | ||
↑ | ||
</button> | ||
<Wrapper onClick={goToTop}> | ||
<FaArrowUp className="top-btn--icon" /> | ||
</Wrapper> | ||
)} | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default ScrollToTopButton; | ||
const Wrapper = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
position: fixed; | ||
bottom: 95px; | ||
right: 20px; | ||
color: white; | ||
background-color: rgb(22 163 74); | ||
width: 55px; | ||
height: 55px; | ||
border-radius: 50%; | ||
cursor: pointer; | ||
transition: background-color 0.3s ease, box-shadow 0.3s ease; | ||
&:hover { | ||
background-color: #58D68D ; | ||
} | ||
`; | ||
|
||
export default GoToTop; |