From 8ec1c4146b73df0d573968db7e1580a8b12b0a3e Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 3 Oct 2023 02:23:51 +0530 Subject: [PATCH] feat: kb navigation --- src/components/Container.tsx | 126 +++++++++++++++++++++++++------------------ src/components/Menu.tsx | 16 +++--- src/pages/main/Contact.tsx | 2 - src/pages/main/Exp.tsx | 2 +- src/pages/main/Home.tsx | 2 +- src/pages/main/Projects.tsx | 2 +- 6 files changed, 87 insertions(+), 63 deletions(-) diff --git a/src/components/Container.tsx b/src/components/Container.tsx index 59b8961..46dd3c6 100644 --- a/src/components/Container.tsx +++ b/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 Right } from "../assets/arrow-right.svg"; -import { getTimeout } from "../util"; -import Menu from "./Menu"; +import { get, getTimeout } from "../util"; +import Menu, { MenuEntries } from "./Menu"; import useMediaQuery from "../util/useMediaQuery"; const [timer, clear] = getTimeout(); @@ -17,18 +17,9 @@ const Container: React.FC<{ | React.ReactElement )[]; hideNav?: boolean; - end?: boolean; - next?: string; className?: string; -}> = ({ - children: _children, - hideNav = false, - end = false, - next, - className, - ...props -}) => { - const [, navigate] = useLocation(); +}> = ({ children: _children, hideNav = false, className, ...props }) => { + const [location, navigate] = useLocation(); const mobile = useMediaQuery("(max-width: 50rem)"); @@ -116,10 +107,9 @@ const Container: React.FC<{ return () => window.removeEventListener("resize", handleResize); }, []); - // on first render - useLayoutEffect(handleResize, []); + type MouseKb = React.MouseEvent | React.KeyboardEvent | KeyboardEvent; - const handleNext: React.MouseEventHandler = e => { + const animateArrow = (e: MouseKb) => { if (containerChild.current) { ( [...containerChild.current.children] as (HTMLElement | SVGElement)[] @@ -130,10 +120,46 @@ const Container: React.FC<{ } document.body.style.maxHeight = "100vh"; 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); }; + 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 (
)} - {next && ( - - )} + height: "2rem"; + width: "2rem"; + `} + /> +
( + const menuItems = Object.entries(MENU).map(([name, link]) => ( {name} diff --git a/src/pages/main/Contact.tsx b/src/pages/main/Contact.tsx index 86a7958..116fae1 100644 --- a/src/pages/main/Contact.tsx +++ b/src/pages/main/Contact.tsx @@ -77,8 +77,6 @@ const Home: React.FC = () => { return ( { return ( - +

I’m a {age} year old developer from
diff --git a/src/pages/main/Home.tsx b/src/pages/main/Home.tsx index 50dc58a..0764506 100644 --- a/src/pages/main/Home.tsx +++ b/src/pages/main/Home.tsx @@ -4,7 +4,7 @@ import Dashed from "../../components/Dashed"; const Home: React.FC = () => { return ( - +

MKRhere

Web home of designer, developer, and{" "} diff --git a/src/pages/main/Projects.tsx b/src/pages/main/Projects.tsx index b606782..1805244 100644 --- a/src/pages/main/Projects.tsx +++ b/src/pages/main/Projects.tsx @@ -146,7 +146,7 @@ const ProjectUnit: React.FC = ({ const Exp: React.FC = () => { return ( - +

What else have I built?

Some tools, libraries, and apps over time: