2022-03-25 16:29:49 -04:00
|
|
|
import { Tooltip, Button, Spacer } from '@geist-ui/core'
|
|
|
|
import ChevronUp from '@geist-ui/icons/chevronUpCircleFill'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import styles from './scroll.module.css'
|
|
|
|
|
|
|
|
const ScrollToTop = () => {
|
|
|
|
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 17:43:39 -04:00
|
|
|
const isReducedMotion = typeof window !== 'undefined' ? window.matchMedia('(prefers-reduced-motion: reduce)').matches : false
|
2022-04-03 16:09:04 -04:00
|
|
|
const onClick = async (e: React.MouseEvent<HTMLButtonElement>) => {
|
2022-03-25 17:31:10 -04:00
|
|
|
e.currentTarget.blur()
|
2022-04-03 16:09:04 -04:00
|
|
|
|
2022-03-25 17:43:39 -04:00
|
|
|
window.scrollTo({ top: 0, behavior: isReducedMotion ? 'auto' : 'smooth' })
|
2022-04-03 16:09:04 -04:00
|
|
|
|
2022-03-25 16:29:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'row', width: '100%', height: 24, justifyContent: 'flex-end' }}>
|
2022-03-25 17:31:10 -04:00
|
|
|
<Tooltip hideArrow text="Scroll to Top" className={`${styles['scroll-up']} ${shouldShow ? styles['scroll-up-shown'] : ''}`}>
|
2022-03-25 16:29:49 -04:00
|
|
|
<Button aria-label='Scroll to Top' onClick={onClick} style={{ background: 'var(--light-gray)' }} auto >
|
|
|
|
<Spacer height={2 / 3} inline width={0} />
|
|
|
|
<ChevronUp />
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ScrollToTop
|