1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/components/ui/ScrollButton/ScrollButton.tsx
Kevin Heis 29ebc555cb Add btn-primary-matte, ScrollButton, TruncateLines to storybook (#21611)
* Update create-processor.js

* Use built in semibold

* Preparing articlelist

* Add blue button to storybook

* Scope fade styles

* Add truncate component

* Add scroll button to storybook

* Update storybook.tsx

* Update DefaultLayout.tsx
2021-09-20 23:21:29 +00:00

49 lines
1.3 KiB
TypeScript

import { useState, useEffect } from 'react'
import cx from 'classnames'
import { ChevronUpIcon } from '@primer/octicons-react'
export type ScrollButtonPropsT = {
className?: string
ariaLabel?: string
}
export const ScrollButton = ({ className, ariaLabel }: ScrollButtonPropsT) => {
const [show, setShow] = useState(false)
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(className, '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={ariaLabel}
>
<ChevronUpIcon />
</button>
</div>
)
}