2023-04-17 23:00:47 -04:00
|
|
|
'use client';
|
|
|
|
|
2023-06-21 17:13:58 -05:00
|
|
|
import { Dialog, Transition } from '@headlessui/react';
|
2023-04-17 23:00:47 -04:00
|
|
|
import Link from 'next/link';
|
|
|
|
import { usePathname, useSearchParams } from 'next/navigation';
|
2023-06-21 17:13:58 -05:00
|
|
|
import { Fragment, useEffect, useState } from 'react';
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2023-07-24 21:40:29 -05:00
|
|
|
import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline';
|
2023-04-17 23:00:47 -04:00
|
|
|
import { Menu } from 'lib/shopify/types';
|
|
|
|
import Search from './search';
|
|
|
|
|
|
|
|
export default function MobileMenu({ menu }: { menu: Menu[] }) {
|
|
|
|
const pathname = usePathname();
|
|
|
|
const searchParams = useSearchParams();
|
2023-06-21 17:13:58 -05:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
const openMobileMenu = () => setIsOpen(true);
|
|
|
|
const closeMobileMenu = () => setIsOpen(false);
|
2023-04-17 23:00:47 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleResize = () => {
|
|
|
|
if (window.innerWidth > 768) {
|
2023-06-21 17:13:58 -05:00
|
|
|
setIsOpen(false);
|
2023-04-17 23:00:47 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
window.addEventListener('resize', handleResize);
|
|
|
|
return () => window.removeEventListener('resize', handleResize);
|
2023-06-21 17:13:58 -05:00
|
|
|
}, [isOpen]);
|
2023-04-17 23:00:47 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-21 17:13:58 -05:00
|
|
|
setIsOpen(false);
|
2023-04-17 23:00:47 -04:00
|
|
|
}, [pathname, searchParams]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-07-31 09:09:56 -05:00
|
|
|
<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"
|
|
|
|
>
|
|
|
|
<Bars3Icon className="h-4" />
|
2023-04-17 23:00:47 -04:00
|
|
|
</button>
|
2023-06-21 17:13:58 -05:00
|
|
|
<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"
|
2023-04-17 23:00:47 -04:00
|
|
|
>
|
2023-06-21 17:13:58 -05:00
|
|
|
<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">
|
2023-07-31 09:09:56 -05:00
|
|
|
<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"
|
|
|
|
>
|
2023-07-24 21:40:29 -05:00
|
|
|
<XMarkIcon className="h-6" />
|
2023-06-21 17:13:58 -05:00
|
|
|
</button>
|
2023-04-17 23:00:47 -04:00
|
|
|
|
2023-06-21 17:13:58 -05:00
|
|
|
<div className="mb-4 w-full">
|
|
|
|
<Search />
|
2023-05-11 12:53:04 -07:00
|
|
|
</div>
|
2023-06-21 17:13:58 -05:00
|
|
|
{menu.length ? (
|
2023-07-31 09:09:56 -05:00
|
|
|
<ul className="flex w-full flex-col">
|
2023-06-21 17:13:58 -05:00
|
|
|
{menu.map((item: Menu) => (
|
2023-07-31 09:09:56 -05:00
|
|
|
<li
|
|
|
|
className="py-2 text-xl text-black transition-colors hover:text-neutral-500 dark:text-white"
|
|
|
|
key={item.title}
|
|
|
|
>
|
|
|
|
<Link href={item.path} onClick={closeMobileMenu}>
|
2023-06-21 17:13:58 -05:00
|
|
|
{item.title}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
</Dialog.Panel>
|
|
|
|
</Transition.Child>
|
|
|
|
</Dialog>
|
|
|
|
</Transition>
|
2023-04-17 23:00:47 -04:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|