import { h } from 'preact'; import Button from './Button'; import LinkedLogo from './LinkedLogo'; import MenuIcon from '../icons/Menu'; import { useLayoutEffect, useCallback, useState } from 'preact/hooks'; // We would typically preserve these in component state // But need to avoid too many re-renders let ticking = false; let lastScrollY = window.scrollY; export default function AppBar({ title }) { const [show, setShow] = useState(true); const [atZero, setAtZero] = useState(window.scrollY === 0); const [sidebarVisible, setSidebarVisible] = useState(true); const scrollListener = useCallback( (event) => { const scrollY = window.scrollY; // if (!ticking) { window.requestAnimationFrame(() => { setShow(scrollY <= 0 || lastScrollY > scrollY); setAtZero(scrollY === 0); ticking = false; lastScrollY = scrollY; }); ticking = true; // } }, [setShow] ); useLayoutEffect(() => { document.addEventListener('scroll', scrollListener); return () => { document.removeEventListener('scroll', scrollListener); }; }, []); return (