2021-02-02 05:28:25 +01:00
|
|
|
import { h } from 'preact';
|
|
|
|
import Button from './Button';
|
|
|
|
import LinkedLogo from './LinkedLogo';
|
2021-02-04 01:00:54 +01:00
|
|
|
import Menu, { MenuItem, MenuSeparator } from './Menu';
|
2021-02-02 05:28:25 +01:00
|
|
|
import MenuIcon from '../icons/Menu';
|
2021-01-31 15:24:04 +01:00
|
|
|
import MoreIcon from '../icons/More';
|
2021-02-04 01:00:54 +01:00
|
|
|
import AutoAwesomeIcon from '../icons/AutoAwesome';
|
|
|
|
import LightModeIcon from '../icons/LightMode';
|
|
|
|
import DarkModeIcon from '../icons/DarkMode';
|
2021-02-04 19:13:47 +01:00
|
|
|
import { useDarkMode, useDrawer } from '../context';
|
2021-01-31 15:24:04 +01:00
|
|
|
import { useLayoutEffect, useCallback, useRef, 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 ticking = false;
|
|
|
|
let lastScrollY = window.scrollY;
|
|
|
|
|
|
|
|
export default function AppBar({ title }) {
|
|
|
|
const [show, setShow] = useState(true);
|
|
|
|
const [atZero, setAtZero] = useState(window.scrollY === 0);
|
2021-02-04 19:13:47 +01:00
|
|
|
const [_, setDrawerVisible] = useState(true);
|
2021-01-31 15:24:04 +01:00
|
|
|
const [showMoreMenu, setShowMoreMenu] = useState(false);
|
|
|
|
const { currentMode, persistedMode, setDarkMode } = useDarkMode();
|
2021-02-04 19:13:47 +01:00
|
|
|
const { showDrawer, setShowDrawer } = useDrawer();
|
2021-01-31 15:24:04 +01:00
|
|
|
|
|
|
|
const handleSelectDarkMode = useCallback(
|
|
|
|
(value, label) => {
|
|
|
|
setDarkMode(value);
|
|
|
|
setShowMoreMenu(false);
|
|
|
|
},
|
|
|
|
[setDarkMode, setShowMoreMenu]
|
|
|
|
);
|
|
|
|
|
|
|
|
const moreRef = useRef(null);
|
2021-02-02 05:28:25 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2021-01-31 15:24:04 +01:00
|
|
|
const handleShowMenu = useCallback(() => {
|
|
|
|
setShowMoreMenu(true);
|
|
|
|
}, [setShowMoreMenu]);
|
|
|
|
|
|
|
|
const handleDismissMoreMenu = useCallback(() => {
|
|
|
|
setShowMoreMenu(false);
|
|
|
|
}, [setShowMoreMenu]);
|
|
|
|
|
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-04 00:43:24 +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 translate-y-0 ${
|
2021-02-02 05:28:25 +01:00
|
|
|
!show ? '-translate-y-full' : ''
|
|
|
|
} ${!atZero ? 'shadow' : ''}`}
|
|
|
|
>
|
|
|
|
<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-02 05:28:25 +01:00
|
|
|
<MenuIcon />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
<LinkedLogo />
|
2021-01-31 15:24:04 +01:00
|
|
|
<div className="flex-grow-1 flex justify-end w-full">
|
|
|
|
<div ref={moreRef}>
|
2021-02-04 00:43:24 +01:00
|
|
|
<Button color="black" className="rounded-full w-12 h-12" onClick={handleShowMenu} type="text">
|
2021-01-31 15:24:04 +01:00
|
|
|
<MoreIcon />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{showMoreMenu ? (
|
|
|
|
<Menu onDismiss={handleDismissMoreMenu} relativeTo={moreRef}>
|
2021-02-04 01:00:54 +01:00
|
|
|
<MenuItem icon={AutoAwesomeIcon} label="Auto dark mode" value="media" onSelect={handleSelectDarkMode} />
|
|
|
|
<MenuSeparator />
|
|
|
|
<MenuItem icon={LightModeIcon} label="Light" value="light" onSelect={handleSelectDarkMode} />
|
|
|
|
<MenuItem icon={DarkModeIcon} label="Dark" value="dark" onSelect={handleSelectDarkMode} />
|
2021-01-31 15:24:04 +01:00
|
|
|
</Menu>
|
|
|
|
) : null}
|
2021-02-02 05:28:25 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|