"use client"; import { Header } from "@/components/docs/Header"; 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"; import { Navigation } from "./Navigation"; function MenuIcon(props: React.ComponentPropsWithoutRef<"svg">) { return ( ); } function XIcon(props: React.ComponentPropsWithoutRef<"svg">) { return ( ); } const IsInsideMobileNavigationContext = createContext(false); function 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]); function 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 function 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 function MobileNavigation() { let isInsideMobileNavigation = useIsInsideMobileNavigation(); let { isOpen, toggle, close } = useMobileNavigationStore(); let ToggleIcon = isOpen ? XIcon : MenuIcon; return ( {!isInsideMobileNavigation && ( )} ); }