Skip to content

Commit 15d2ee0

Browse files
authored
Hide scroll to top button if window is too short (#51929)
1 parent 779ee05 commit 15d2ee0

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/frame/components/ui/ScrollButton/ScrollButton.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react'
1+
import { useState, useEffect, useLayoutEffect } from 'react'
22
import cx from 'classnames'
33
import { ChevronUpIcon } from '@primer/octicons-react'
44
import styles from './ScrollButton.module.scss'
@@ -12,6 +12,7 @@ export type ScrollButtonPropsT = {
1212

1313
export const ScrollButton = ({ className, ariaLabel }: ScrollButtonPropsT) => {
1414
const [show, setShow] = useState(false)
15+
const [isTallEnough, setIsTallEnough] = useState(false)
1516

1617
useEffect(() => {
1718
// We cannot determine document.documentElement.scrollTop height because we set the height: 100vh and set overflow to auto to keep the header sticky
@@ -33,13 +34,27 @@ export const ScrollButton = ({ className, ariaLabel }: ScrollButtonPropsT) => {
3334
}
3435
}, [])
3536

37+
// If the window isn't tall enough, the scroll button will hide some of the content
38+
// A11y issue 8822
39+
useLayoutEffect(() => {
40+
function updateDocumentSize() {
41+
setIsTallEnough(document.documentElement.clientHeight > 400)
42+
}
43+
updateDocumentSize()
44+
window.addEventListener('resize', updateDocumentSize)
45+
return () => window.removeEventListener('resize', updateDocumentSize)
46+
}, [])
47+
3648
const onClick = () => {
3749
document?.getElementById('github-logo')?.focus()
3850
document?.getElementById('main-content')?.scrollIntoView()
3951
}
4052

4153
return (
42-
<div role="tooltip" className={cx(className, transition200, show ? opacity100 : opacity0)}>
54+
<div
55+
role="tooltip"
56+
className={cx(className, transition200, show && isTallEnough ? opacity100 : opacity0)}
57+
>
4358
<button
4459
onClick={onClick}
4560
className={cx(

0 commit comments

Comments
 (0)