forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollButton.tsx
50 lines (45 loc) · 1.3 KB
/
ScrollButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useState, useEffect } from 'react'
import cx from 'classnames'
import { ChevronUpIcon } from '@primer/octicons-react'
import { useTranslation } from './hooks/useTranslation'
export const ScrollButton = () => {
const [show, setShow] = useState(false)
const { t } = useTranslation('scroll_button')
useEffect(() => {
// show scroll button only when view is scrolled down
const onScroll = function () {
const y = document.documentElement.scrollTop // get the height from page top
if (y < 100) {
setShow(false)
} else if (y >= 100) {
setShow(true)
}
}
window.addEventListener('scroll', onScroll)
return () => {
window.removeEventListener('scroll', onScroll)
}
}, [])
const onClick = () => {
window.scrollTo({ top: 0, behavior: 'smooth' })
}
return (
<div
className={cx(
'position-fixed bottom-3 right-3 transition-200',
show ? 'opacity-100' : 'opacity-0'
)}
>
<button
onClick={onClick}
className={cx(
'tooltipped tooltipped-n tooltipped-no-delay color-bg-info-inverse color-text-inverse circle border-0'
)}
style={{ width: 40, height: 40 }}
aria-label={t('scroll_to_top')}
>
<ChevronUpIcon />
</button>
</div>
)
}