1
0
mirror of synced 2025-12-22 19:34:15 -05:00
Files
docs/components/ui/ScrollButton/ScrollButton.tsx
Grace Park 47de433865 Accessibility Search updates and additional minor changes (#24136)
* update to use heading markup

* updating to h3 for accessibility

* increase opactiy to meet 5.5:1 color contrast ratio

* update opacity to meet color contrast ratio

* update styling to match original but use heading markup

* remove aria-hidden for accessibility

* add h1 title search results and update scroll tip colors

* add number of matches

* remove no results since we are showing 0 results

* add overflow to fix border bottom on version search

* Update components/Search.tsx

Co-authored-by: Peter Bengtsson <peterbe@github.com>

* Update components/Search.tsx

Co-authored-by: Peter Bengtsson <peterbe@github.com>

* incorporating feedback and update padding

Co-authored-by: Peter Bengtsson <peterbe@github.com>
2022-01-07 21:29:49 +00:00

55 lines
1.5 KiB
TypeScript

import { useState, useEffect } from 'react'
import cx from 'classnames'
import { ChevronUpIcon } from '@primer/octicons-react'
import { useTranslation } from '../../hooks/useTranslation'
export type ScrollButtonPropsT = {
className?: string
ariaLabel?: string
}
export const ScrollButton = ({ className, ariaLabel }: ScrollButtonPropsT) => {
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(className, 'transition-200', show ? 'opacity-100' : 'opacity-0')}>
<button
onClick={onClick}
className="color-bg-default color-fg-default border-0 d-inline-block mr-2 f6"
>
{t('scroll_to_top')}
</button>
<button
onClick={onClick}
className={cx('color-bg-accent-emphasis color-fg-on-emphasis circle border-0')}
style={{ width: 40, height: 40 }}
aria-label={ariaLabel}
>
<ChevronUpIcon />
</button>
</div>
)
}