2021-02-02 05:28:25 +01:00
|
|
|
import { h } from 'preact';
|
|
|
|
import Button from './Button';
|
|
|
|
import MenuIcon from '../icons/Menu';
|
2021-01-31 15:24:04 +01:00
|
|
|
import MoreIcon from '../icons/More';
|
2021-02-10 22:21:43 +01:00
|
|
|
import { useDrawer } from '../context';
|
|
|
|
import { useLayoutEffect, useCallback, useState } from 'preact/hooks';
|
2021-02-02 05:28:25 +01:00
|
|
|
|
|
|
|
// We would typically preserve these in component state
|
|
|
|
// But need to avoid too many re-renders
|
|
|
|
let lastScrollY = window.scrollY;
|
|
|
|
|
2021-02-10 22:21:43 +01:00
|
|
|
export default function AppBar({ title: Title, overflowRef, onOverflowClick }) {
|
2021-02-02 05:28:25 +01:00
|
|
|
const [show, setShow] = useState(true);
|
|
|
|
const [atZero, setAtZero] = useState(window.scrollY === 0);
|
2021-02-09 20:35:33 +01:00
|
|
|
const { setShowDrawer } = useDrawer();
|
2021-01-31 15:24:04 +01:00
|
|
|
|
2021-02-10 22:21:43 +01:00
|
|
|
const scrollListener = useCallback(() => {
|
|
|
|
const scrollY = window.scrollY;
|
2021-02-02 05:28:25 +01:00
|
|
|
|
2021-02-10 22:21:43 +01:00
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
setShow(scrollY <= 0 || lastScrollY > scrollY);
|
|
|
|
setAtZero(scrollY === 0);
|
|
|
|
lastScrollY = scrollY;
|
|
|
|
});
|
|
|
|
}, [setShow]);
|
2021-02-02 05:28:25 +01:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
document.addEventListener('scroll', scrollListener);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('scroll', scrollListener);
|
|
|
|
};
|
2021-02-09 20:35:33 +01:00
|
|
|
}, [scrollListener]);
|
2021-02-02 05:28:25 +01:00
|
|
|
|
2021-02-04 19:13:47 +01:00
|
|
|
const handleShowDrawer = useCallback(() => {
|
|
|
|
setShowDrawer(true);
|
|
|
|
}, [setShowDrawer]);
|
2021-02-04 00:15:27 +01:00
|
|
|
|
2021-02-02 05:28:25 +01:00
|
|
|
return (
|
|
|
|
<div
|
2021-02-10 22:21:43 +01:00
|
|
|
className={`w-full border-b border-gray-200 dark:border-gray-700 flex items-center align-middle p-4 space-x-2 fixed left-0 right-0 z-10 bg-white dark:bg-gray-900 transform transition-all duration-200 ${
|
|
|
|
!show ? '-translate-y-full' : 'translate-y-0'
|
2021-02-04 19:49:14 +01:00
|
|
|
} ${!atZero ? 'shadow-sm' : ''}`}
|
2021-02-10 22:21:43 +01:00
|
|
|
data-testid="appbar"
|
2021-02-02 05:28:25 +01:00
|
|
|
>
|
|
|
|
<div className="lg:hidden">
|
2021-02-04 19:13:47 +01:00
|
|
|
<Button color="black" className="rounded-full w-12 h-12" onClick={handleShowDrawer} type="text">
|
2021-02-08 22:35:41 +01:00
|
|
|
<MenuIcon className="w-10 h-10" />
|
2021-02-02 05:28:25 +01:00
|
|
|
</Button>
|
|
|
|
</div>
|
2021-02-10 22:21:43 +01:00
|
|
|
<Title />
|
2021-01-31 15:24:04 +01:00
|
|
|
<div className="flex-grow-1 flex justify-end w-full">
|
2021-02-10 22:21:43 +01:00
|
|
|
{overflowRef && onOverflowClick ? (
|
|
|
|
<div className="w-auto" ref={overflowRef}>
|
|
|
|
<Button
|
|
|
|
aria-label="More options"
|
|
|
|
color="black"
|
|
|
|
className="rounded-full w-12 h-12"
|
|
|
|
onClick={onOverflowClick}
|
|
|
|
type="text"
|
|
|
|
>
|
|
|
|
<MoreIcon className="w-10 h-10" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-01-31 15:24:04 +01:00
|
|
|
</div>
|
2021-02-02 05:28:25 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|