blakeblackshear.frigate/web/src/components/AppBar.jsx

70 lines
2.2 KiB
React
Raw Normal View History

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';
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;
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);
const { setShowDrawer } = useDrawer();
2021-01-31 15:24:04 +01:00
const scrollListener = useCallback(() => {
const scrollY = window.scrollY;
2021-02-02 05:28:25 +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);
};
}, [scrollListener]);
2021-02-02 05:28:25 +01:00
const handleShowDrawer = useCallback(() => {
setShowDrawer(true);
}, [setShowDrawer]);
2021-02-02 05:28:25 +01:00
return (
<div
id="appbar"
className={`w-full border-b border-gray-200 dark:border-gray-700 flex items-center align-middle p-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'
} ${!atZero ? 'shadow-sm' : ''}`}
data-testid="appbar"
2021-02-02 05:28:25 +01:00
>
<div className="lg:hidden">
2021-08-20 13:04:47 +02:00
<Button color="black" className="rounded-full w-10 h-10" onClick={handleShowDrawer} type="text">
<MenuIcon className="w-10 h-10" />
2021-02-02 05:28:25 +01:00
</Button>
</div>
<Title />
2021-01-31 15:24:04 +01:00
<div className="flex-grow-1 flex justify-end w-full">
{overflowRef && onOverflowClick ? (
<div className="w-auto" ref={overflowRef}>
<Button
aria-label="More options"
color="black"
2021-08-20 13:04:47 +02:00
className="rounded-full w-9 h-9"
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>
);
}