"use client"; import { Header } from "@/components/Header"; import { Navigation } from "@/components/Navigation"; import { Dialog, Transition } from "@headlessui/react"; import { motion } from "framer-motion"; import { usePathname, useSearchParams } from "next/navigation"; import { Fragment, Suspense, createContext, useContext, useEffect, useRef } from "react"; import { create } from "zustand"; const MenuIcon = (props: React.ComponentPropsWithoutRef<"svg">) => { return ( ); }; const XIcon = (props: React.ComponentPropsWithoutRef<"svg">) => { return ( ); }; const IsInsideMobileNavigationContext = createContext(false); const MobileNavigationDialog = ({ isOpen, close }: { isOpen: boolean; close: () => void }) => { let pathname = usePathname(); let searchParams = useSearchParams(); let initialPathname = useRef(pathname).current; let initialSearchParams = useRef(searchParams).current; useEffect(() => { if (pathname !== initialPathname || searchParams !== initialSearchParams) { close(); } }, [pathname, searchParams, close, initialPathname, initialSearchParams]); const onClickDialog = (event: React.MouseEvent) => { if (!(event.target instanceof HTMLElement)) { return; } let link = event.target.closest("a"); if ( link && link.pathname + link.search + link.hash === window.location.pathname + window.location.search + window.location.hash ) { close(); } }; return (
); }; export const useIsInsideMobileNavigation = () => { return useContext(IsInsideMobileNavigationContext); }; export const useMobileNavigationStore = create<{ isOpen: boolean; open: () => void; close: () => void; toggle: () => void; }>()((set) => ({ isOpen: false, open: () => set({ isOpen: true }), close: () => set({ isOpen: false }), toggle: () => set((state) => ({ isOpen: !state.isOpen })), })); export const MobileNavigation = () => { let isInsideMobileNavigation = useIsInsideMobileNavigation(); let { isOpen, toggle, close } = useMobileNavigationStore(); let ToggleIcon = isOpen ? XIcon : MenuIcon; return ( {!isInsideMobileNavigation && ( )} ); };