-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scroll): Add scroll to top button (#77)
- Loading branch information
1 parent
28614c4
commit 2961e70
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 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,37 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { Fab } from '@material-ui/core'; | ||
import ArrowUpIcon from '@material-ui/icons/ArrowUpward'; | ||
import './ScrollToTopButton.scss'; | ||
|
||
const ScrollToTopButton = () => { | ||
const [showScroll, setShowScroll] = useState(false); | ||
|
||
const getShouldShowButton = () => { | ||
window.pageYOffset > 500 ? setShowScroll(true) : setShowScroll(false); | ||
}; | ||
|
||
const scrollToTop = () => { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', getShouldShowButton); | ||
return () => { | ||
window.removeEventListener('scroll', getShouldShowButton); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className="scroll-to-top" onClick={scrollToTop}> | ||
{showScroll && ( | ||
<div> | ||
<Fab aria-label="scroll back to top" color="primary"> | ||
<ArrowUpIcon fontSize="large" /> | ||
</Fab> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ScrollToTopButton; |
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,15 @@ | ||
.scroll-to-top{ | ||
position: fixed; | ||
width: 56px; | ||
height: 56px; | ||
bottom: 4%; | ||
right: 2%; | ||
z-index: 1000; | ||
} | ||
|
||
@media only screen and (max-width: 1000px) { | ||
.scroll-to-top{ | ||
bottom: 11%; | ||
right: 7.5%; | ||
} | ||
} |