Browse Source

feat: kb navigation

pull/2/head
Muthu Kumar 12 months ago
parent
commit
8ec1c4146b
Failed to extract signature
  1. 126
      src/components/Container.tsx
  2. 16
      src/components/Menu.tsx
  3. 2
      src/pages/main/Contact.tsx
  4. 2
      src/pages/main/Exp.tsx
  5. 2
      src/pages/main/Home.tsx
  6. 2
      src/pages/main/Projects.tsx

126
src/components/Container.tsx

@ -4,8 +4,8 @@ import useLocation from "wouter/use-location";
import { ReactComponent as Logo } from "../assets/logo.svg"; import { ReactComponent as Logo } from "../assets/logo.svg";
import { ReactComponent as Right } from "../assets/arrow-right.svg"; import { ReactComponent as Right } from "../assets/arrow-right.svg";
import { getTimeout } from "../util"; import { get, getTimeout } from "../util";
import Menu from "./Menu"; import Menu, { MenuEntries } from "./Menu";
import useMediaQuery from "../util/useMediaQuery"; import useMediaQuery from "../util/useMediaQuery";
const [timer, clear] = getTimeout(); const [timer, clear] = getTimeout();
@ -17,18 +17,9 @@ const Container: React.FC<{
| React.ReactElement | React.ReactElement
)[]; )[];
hideNav?: boolean; hideNav?: boolean;
end?: boolean;
next?: string;
className?: string; className?: string;
}> = ({ }> = ({ children: _children, hideNav = false, className, ...props }) => {
children: _children, const [location, navigate] = useLocation();
hideNav = false,
end = false,
next,
className,
...props
}) => {
const [, navigate] = useLocation();
const mobile = useMediaQuery("(max-width: 50rem)"); const mobile = useMediaQuery("(max-width: 50rem)");
@ -116,10 +107,9 @@ const Container: React.FC<{
return () => window.removeEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize);
}, []); }, []);
// on first render type MouseKb = React.MouseEvent | React.KeyboardEvent | KeyboardEvent;
useLayoutEffect(handleResize, []);
const handleNext: React.MouseEventHandler<HTMLButtonElement> = e => { const animateArrow = (e: MouseKb) => {
if (containerChild.current) { if (containerChild.current) {
( (
[...containerChild.current.children] as (HTMLElement | SVGElement)[] [...containerChild.current.children] as (HTMLElement | SVGElement)[]
@ -130,10 +120,46 @@ const Container: React.FC<{
} }
document.body.style.maxHeight = "100vh"; document.body.style.maxHeight = "100vh";
document.body.style.overflow = "hidden"; document.body.style.overflow = "hidden";
e.currentTarget.style.width = "0"; try {
const target = e.currentTarget! as HTMLButtonElement;
target.style.width = "0";
} catch {}
};
const current = MenuEntries.findIndex(([, path]) => location === path);
const next = get.next(MenuEntries, current)[1];
const prev = get.prev(MenuEntries, current)[1];
const end = current === MenuEntries.length - 1;
const handlePrev = (e: MouseKb) => {
animateArrow(e);
timer(() => prev && navigate(prev), 300);
};
const handleNext = (e: MouseKb) => {
animateArrow(e);
timer(() => next && navigate(next), 300); timer(() => next && navigate(next), 300);
}; };
function kbnav(e: KeyboardEvent) {
switch (e.key) {
case "ArrowLeft":
return handlePrev(e);
case "ArrowRight":
return handleNext(e);
}
}
useEffect(() => {
window.addEventListener("keydown", kbnav);
// cleanup
return () => window.removeEventListener("keydown", kbnav);
}, []);
// on first render
useLayoutEffect(handleResize, []);
return ( return (
<div <div
className={css` className={css`
@ -247,41 +273,39 @@ const Container: React.FC<{
<Menu show={showMenu} setShowMenu={setShowMenu} /> <Menu show={showMenu} setShowMenu={setShowMenu} />
</span> </span>
)} )}
{next && ( <button
<button onClick={handleNext}
onClick={handleNext} ref={nextBtn}
ref={nextBtn} title={end ? "Back to start" : "Next page"}
title={end ? "Back to start" : "Next page"} className={css`
position: fixed;
right: 14vw;
bottom: 10vh;
z-index: 500;
background: none;
padding: 0;
font-weight: 500;
cursor: pointer;
letter-spacing: 0.2rem;
border: none;
overflow: hidden;
width: 0;
transition: all 300ms;
overflow: hidden;
${end ? "rotate: 180deg;" : ""}
&:hover * {
fill: var(--primary-colour);
}
`}>
<Right
className={css` className={css`
position: fixed; height: "2rem";
right: 14vw; width: "2rem";
bottom: 10vh; `}
z-index: 500; />
background: none; </button>
padding: 0;
font-weight: 500;
cursor: pointer;
letter-spacing: 0.2rem;
border: none;
overflow: hidden;
width: 0;
transition: all 300ms;
overflow: hidden;
${end ? "rotate: 180deg;" : ""}
&:hover * {
fill: var(--primary-colour);
}
`}>
<Right
className={css`
height: "2rem";
width: "2rem";
`}
/>
</button>
)}
<div <div
className={cx( className={cx(
css` css`

16
src/components/Menu.tsx

@ -5,12 +5,14 @@ import RevealChildren from "./RevealChildren";
import useMediaQuery from "../util/useMediaQuery"; import useMediaQuery from "../util/useMediaQuery";
import { useNav } from "../util"; import { useNav } from "../util";
const menu = [ export const MENU = {
{ name: "Home", link: "/" }, Home: "/",
{ name: "Experience", link: "/experience" }, Experience: "/experience",
{ name: "Projects", link: "/projects" }, Projects: "/projects",
{ name: "Contact", link: "/contact" }, Contact: "/contact",
]; } as const;
export const MenuEntries = Object.entries(MENU);
const desktopNav = css` const desktopNav = css`
float: right; float: right;
@ -71,7 +73,7 @@ const Menu: React.FC<{
const mobile = useMediaQuery("(max-width: 50rem)"); const mobile = useMediaQuery("(max-width: 50rem)");
const notmobile = !mobile; const notmobile = !mobile;
const menuItems = menu.map(({ link, name }) => ( const menuItems = Object.entries(MENU).map(([name, link]) => (
<a key={link} onClick={navigate(link)} href={link}> <a key={link} onClick={navigate(link)} href={link}>
{name} {name}
</a> </a>

2
src/pages/main/Contact.tsx

@ -77,8 +77,6 @@ const Home: React.FC = () => {
return ( return (
<Container <Container
end
next="/"
className={css` className={css`
min-height: 50vh; min-height: 50vh;
display: flex; display: flex;

2
src/pages/main/Exp.tsx

@ -159,7 +159,7 @@ const age = getAge("27 May 1995");
const Exp: React.FC = () => { const Exp: React.FC = () => {
return ( return (
<Container next="/projects"> <Container>
<h2> <h2>
Im a {age} year old developer from Im a {age} year old developer from
<br /> <br />

2
src/pages/main/Home.tsx

@ -4,7 +4,7 @@ import Dashed from "../../components/Dashed";
const Home: React.FC = () => { const Home: React.FC = () => {
return ( return (
<Container next="/experience"> <Container>
<h1>MKRhere</h1> <h1>MKRhere</h1>
<p> <p>
Web home of <Dashed>designer</Dashed>, <Dashed>developer</Dashed>, and{" "} Web home of <Dashed>designer</Dashed>, <Dashed>developer</Dashed>, and{" "}

2
src/pages/main/Projects.tsx

@ -146,7 +146,7 @@ const ProjectUnit: React.FC<Project> = ({
const Exp: React.FC = () => { const Exp: React.FC = () => {
return ( return (
<Container next="/contact"> <Container>
<h2>What else have I built?</h2> <h2>What else have I built?</h2>
<p>Some tools, libraries, and apps over time:</p> <p>Some tools, libraries, and apps over time:</p>
<div <div

Loading…
Cancel
Save