Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ScrollButton.tsx 1.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. import { useState, useEffect } from 'react'
  2. import cx from 'classnames'
  3. import { ChevronUpIcon } from '@primer/octicons-react'
  4. import { useTranslation } from './hooks/useTranslation'
  5. export const ScrollButton = () => {
  6. const [show, setShow] = useState(false)
  7. const { t } = useTranslation('scroll_button')
  8. useEffect(() => {
  9. // show scroll button only when view is scrolled down
  10. const onScroll = function () {
  11. const y = document.documentElement.scrollTop // get the height from page top
  12. if (y < 100) {
  13. setShow(false)
  14. } else if (y >= 100) {
  15. setShow(true)
  16. }
  17. }
  18. window.addEventListener('scroll', onScroll)
  19. return () => {
  20. window.removeEventListener('scroll', onScroll)
  21. }
  22. }, [])
  23. const onClick = () => {
  24. window.scrollTo({ top: 0, behavior: 'smooth' })
  25. }
  26. return (
  27. <div
  28. className={cx(
  29. 'position-fixed bottom-3 right-3 transition-200',
  30. show ? 'opacity-100' : 'opacity-0'
  31. )}
  32. >
  33. <button
  34. onClick={onClick}
  35. className={cx(
  36. 'tooltipped tooltipped-n tooltipped-no-delay color-bg-info-inverse color-text-inverse circle border-0'
  37. )}
  38. style={{ width: 40, height: 40 }}
  39. aria-label={t('scroll_to_top')}
  40. >
  41. <ChevronUpIcon />
  42. </button>
  43. </div>
  44. )
  45. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...