forked from MonalikaPatnaik/SheEARNS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created reusable back to top react component
- Loading branch information
1 parent
6b924c4
commit ec3ec36
Showing
3 changed files
with
46 additions
and
40 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
const BackToTopButton = () => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
// Scroll event listener to show/hide the button | ||
const handleScroll = () => { | ||
const scrollY = window.scrollY; | ||
const triggerPoint = 300; | ||
|
||
if (scrollY > triggerPoint) { | ||
setIsVisible(true); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
}; | ||
|
||
// Add scroll event listener when the component mounts | ||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
|
||
// Remove the event listener when the component unmounts | ||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, []); | ||
|
||
// Scroll to the top of the page | ||
const scrollToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth', | ||
}); | ||
}; | ||
|
||
return ( | ||
<button | ||
className={`btn btn-primary back-to-top ${isVisible ? 'visible' : 'hidden'}`} | ||
onClick={scrollToTop} | ||
> | ||
Back to Top | ||
</button> | ||
); | ||
}; | ||
|
||
export default BackToTopButton; |