import { h } from 'preact'; import Button from './Button'; import MenuIcon from '../icons/Menu'; import MoreIcon from '../icons/More'; import { useDrawer } from '../context'; import { useLayoutEffect, useCallback, useState } from 'preact/hooks'; // We would typically preserve these in component state // But need to avoid too many re-renders let lastScrollY = window.scrollY; export default function AppBar({ title: Title, overflowRef, onOverflowClick }) { const [show, setShow] = useState(true); const [atZero, setAtZero] = useState(window.scrollY === 0); const { setShowDrawer } = useDrawer(); const scrollListener = useCallback(() => { const scrollY = window.scrollY; window.requestAnimationFrame(() => { setShow(scrollY <= 0 || lastScrollY > scrollY); setAtZero(scrollY === 0); lastScrollY = scrollY; }); }, [setShow]); useLayoutEffect(() => { document.addEventListener('scroll', scrollListener); return () => { document.removeEventListener('scroll', scrollListener); }; }, [scrollListener]); const handleShowDrawer = useCallback(() => { setShowDrawer(true); }, [setShowDrawer]); return (