()
| 15 | }; |
| 16 | |
| 17 | export default function ScrollToTopButton(): ReactElement | null { |
| 18 | const [show, setShow] = useState(false); |
| 19 | const [hasShown, setHasShown] = useState(false); |
| 20 | const isLaptop = useViewSize(ViewSize.Laptop); |
| 21 | const isTablet = useViewSize(ViewSize.Tablet); |
| 22 | const { showFeedbackButton } = useSettingsContext(); |
| 23 | const isLaptopLayout = useLaptopLayout(); |
| 24 | const size = (() => { |
| 25 | if (isLaptop) { |
| 26 | return ButtonSize.Large; |
| 27 | } |
| 28 | |
| 29 | return isTablet ? ButtonSize.Medium : ButtonSize.Small; |
| 30 | })(); |
| 31 | |
| 32 | useEffect(() => { |
| 33 | const callback = () => { |
| 34 | const shouldShow = |
| 35 | document.documentElement.scrollTop >= window.innerHeight / 2; |
| 36 | setShow(shouldShow); |
| 37 | if (shouldShow) { |
| 38 | setHasShown(true); |
| 39 | } |
| 40 | }; |
| 41 | callback(); |
| 42 | window.addEventListener('scroll', callback, { passive: true }); |
| 43 | return () => window.removeEventListener('scroll', callback); |
| 44 | }, []); |
| 45 | |
| 46 | if (!hasShown) { |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | const style: CSSProperties = { |
| 51 | ...baseStyle, |
| 52 | transform: show ? undefined : 'translateY(1rem)', |
| 53 | opacity: show ? undefined : 0, |
| 54 | pointerEvents: show ? undefined : 'none', |
| 55 | }; |
| 56 | |
| 57 | const onClick = () => window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 58 | |
| 59 | const props: ButtonProps<'button'> = { |
| 60 | icon: <ArrowIcon />, |
| 61 | onClick, |
| 62 | }; |
| 63 | |
| 64 | if (isLaptopLayout) { |
| 65 | // The rail layout wears the phone shell's back-to-top material |
| 66 | // (ShellTopButton): the floating material, 38px tall with 14px corners |
| 67 | // and the small rotated arrow, so both layouts share one look. Desktop |
| 68 | // has room for a label, so "Back to top" sits beside the arrow. |
| 69 | // The card floats 0.75rem in from the window and the feedback pill |
| 70 | // lives in the rail, so the button sits 1rem inside the card's corner: |
| 71 | // 0.75rem + 1rem from the right, and 1rem above the sponsor strip when |
| 72 | // one is docked (the card drops its bottom gutter then) or |
| 73 | // 0.75rem + 1rem from the bottom when not, which is what the |
| 74 | // property's fallback adds back. |
nothing calls this directly
no test coverage detected