mirror of
https://github.com/vercel/commerce.git
synced 2025-04-27 21:37:50 +00:00
128 lines
4.3 KiB
TypeScript
128 lines
4.3 KiB
TypeScript
"use client";
|
|
|
|
import { Dialog, Transition } from "@headlessui/react";
|
|
import Link from "next/link";
|
|
import { usePathname, useSearchParams } from "next/navigation";
|
|
import { Fragment, useEffect, useState } from "react";
|
|
|
|
import { Menu } from "@/lib/store/menu";
|
|
import Search from "./search";
|
|
|
|
interface MobileMenuProps {
|
|
menu?: Menu;
|
|
}
|
|
|
|
export default function MobileMenu({ menu }: MobileMenuProps) {
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const openMobileMenu = () => setIsOpen(true);
|
|
const closeMobileMenu = () => setIsOpen(false);
|
|
|
|
useEffect(() => {
|
|
const handleResize = () => {
|
|
if (window.innerWidth > 768) {
|
|
setIsOpen(false);
|
|
}
|
|
};
|
|
window.addEventListener("resize", handleResize);
|
|
return () => window.removeEventListener("resize", handleResize);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
setIsOpen(false);
|
|
}, [pathname, searchParams]);
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
onClick={openMobileMenu}
|
|
aria-label="Open mobile menu"
|
|
className="flex h-11 w-11 items-center justify-center rounded-md border border-neutral-200 text-black transition-colors dark:border-neutral-700 dark:text-white md:hidden"
|
|
>
|
|
<svg
|
|
className="h-4"
|
|
fill="none"
|
|
viewBox="0 0 24 24"
|
|
stroke="currentColor"
|
|
aria-hidden="true"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth="2"
|
|
d="M4 6h16M4 12h16M4 18h16"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
|
|
<Transition show={isOpen}>
|
|
<Dialog onClose={closeMobileMenu} className="relative z-50">
|
|
<Transition.Child
|
|
as={Fragment}
|
|
enter="transition-all ease-in-out duration-300"
|
|
enterFrom="opacity-0 backdrop-blur-none"
|
|
enterTo="opacity-100 backdrop-blur-[.5px]"
|
|
leave="transition-all ease-in-out duration-200"
|
|
leaveFrom="opacity-100 backdrop-blur-[.5px]"
|
|
leaveTo="opacity-0 backdrop-blur-none"
|
|
>
|
|
<div className="fixed inset-0 bg-black/30" aria-hidden="true" />
|
|
</Transition.Child>
|
|
<Transition.Child
|
|
as={Fragment}
|
|
enter="transition-all ease-in-out duration-300"
|
|
enterFrom="translate-x-[-100%]"
|
|
enterTo="translate-x-0"
|
|
leave="transition-all ease-in-out duration-200"
|
|
leaveFrom="translate-x-0"
|
|
leaveTo="translate-x-[-100%]"
|
|
>
|
|
<Dialog.Panel className="fixed bottom-0 left-0 right-0 top-0 flex h-full w-full flex-col bg-white pb-6 dark:bg-black">
|
|
<div className="p-4">
|
|
<button
|
|
className="mb-4 flex h-11 w-11 items-center justify-center rounded-md border border-neutral-200 text-black transition-colors dark:border-neutral-700 dark:text-white"
|
|
onClick={closeMobileMenu}
|
|
aria-label="Close mobile menu"
|
|
>
|
|
<svg
|
|
className="h-4"
|
|
fill="none"
|
|
viewBox="0 0 24 24"
|
|
stroke="currentColor"
|
|
aria-hidden="true"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth="2"
|
|
d="M6 18L18 6M6 6l12 12"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
<div className="mb-4 w-full">
|
|
<Search />
|
|
</div>
|
|
{menu?.items ? (
|
|
<ul className="flex w-full flex-col">
|
|
{menu.items.map((item) => (
|
|
<li
|
|
key={item.title}
|
|
className="py-2 text-xl text-black transition-colors hover:text-neutral-500 dark:text-white"
|
|
>
|
|
<Link href={item.path} onClick={closeMobileMenu}>
|
|
{item.title}
|
|
</Link>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
</div>
|
|
</Dialog.Panel>
|
|
</Transition.Child>
|
|
</Dialog>
|
|
</Transition>
|
|
</>
|
|
);
|
|
}
|