mirror of https://github.com/MaxLeiter/Drift
client: add scroll to top button on post view
parent
1ace04985c
commit
ff8d5aab5c
@ -0,0 +1,33 @@
|
||||
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)
|
||||
}, [])
|
||||
|
||||
const onClick = () => {
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{ display: 'flex', flexDirection: 'row', width: '100%', height: 24, justifyContent: 'flex-end' }}>
|
||||
<Tooltip text="Scroll to Top" className={`${styles['scroll-up']} ${shouldShow ? styles['scroll-up-shown'] : ''}`}>
|
||||
<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
|
||||
@ -0,0 +1,17 @@
|
||||
.scroll-up {
|
||||
position: fixed;
|
||||
z-index: 2;
|
||||
pointer-events: none;
|
||||
opacity: 0;
|
||||
transform: translateY(16px);
|
||||
transition: transform 0.2s, opacity 0.2s;
|
||||
cursor: pointer;
|
||||
bottom: var(--gap-double);
|
||||
will-change: transform, opacity;
|
||||
}
|
||||
|
||||
.scroll-up-shown {
|
||||
opacity: 0.8;
|
||||
transform: none;
|
||||
pointer-events: auto;
|
||||
}
|
||||
Loading…
Reference in New Issue