2022-11-16 05:16:56 -05:00
|
|
|
import Button from "@components/button"
|
2022-11-12 21:39:03 -05:00
|
|
|
import Tooltip from "@components/tooltip"
|
2022-11-16 05:16:56 -05:00
|
|
|
import ChevronUp from "@geist-ui/icons/chevronUp"
|
2022-04-09 20:48:19 -04:00
|
|
|
import { useEffect, useState } from "react"
|
|
|
|
import styles from "./scroll.module.css"
|
2022-03-25 16:29:49 -04:00
|
|
|
|
|
|
|
const ScrollToTop = () => {
|
2022-04-09 20:48:19 -04:00
|
|
|
const [shouldShow, setShouldShow] = useState(false)
|
|
|
|
useEffect(() => {
|
|
|
|
// if user is scrolled, set visible
|
|
|
|
const handleScroll = () => {
|
|
|
|
setShouldShow(window.scrollY > 100)
|
|
|
|
}
|
|
|
|
window.addEventListener("scroll", handleScroll)
|
|
|
|
return () => window.removeEventListener("scroll", handleScroll)
|
|
|
|
}, [])
|
2022-03-25 16:29:49 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const isReducedMotion =
|
|
|
|
typeof window !== "undefined"
|
|
|
|
? window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
|
|
: false
|
|
|
|
const onClick = async (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
e.currentTarget.blur()
|
2022-04-03 16:09:04 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
window.scrollTo({ top: 0, behavior: isReducedMotion ? "auto" : "smooth" })
|
|
|
|
}
|
2022-04-03 16:09:04 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
2022-11-16 05:16:56 -05:00
|
|
|
<div className={styles.root}>
|
2022-04-09 20:48:19 -04:00
|
|
|
<Tooltip
|
2022-11-12 21:39:03 -05:00
|
|
|
content="Scroll to Top"
|
2022-04-09 20:48:19 -04:00
|
|
|
className={`${styles["scroll-up"]} ${
|
|
|
|
shouldShow ? styles["scroll-up-shown"] : ""
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
aria-label="Scroll to Top"
|
|
|
|
onClick={onClick}
|
2022-11-16 05:16:56 -05:00
|
|
|
iconLeft={<ChevronUp />}
|
|
|
|
/>
|
2022-04-09 20:48:19 -04:00
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-25 16:29:49 -04:00
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
export default ScrollToTop
|