Closes a layout-mounted overlay (modal, drawer, dropdown, side panel) automatically when the URL pathname changes, preventing stale body-level scroll/pointer-event locks on the new page. ## Key Components ### `useCloseOnNavigation(close, pathname)` | Parameter | Type | Description | |-----------|------|-------------| | `close` | `() => void` | Stable callback that triggers the overlay's close flow | | `pathname` | `string \| null` | Current route pathname; threading this makes the hook router-agnostic | **Behaviour:** - **Skip-first-render** — uses `initializedRef` to record the initial pathname without firing `close()`, preventing a spurious close on mount - **Real change only** — compares against `prevPathnameRef` and calls `close()` only when the pathname actually changes - `close` is intentionally excluded from the `useEffect` dependency array; callers must pass a stable (memoized or ref-stable) callback ## Usage Example ```typescript // Lib (router-agnostic, direct usage): import { useCloseOnNavigation } from '@/lib/hooks/use-close-on-navigation' function MyDrawer() { const [isOpen, setIsOpen] = useState(false) const pathname = usePathname() // or useLocation(), window.location.pathname, etc. useCloseOnNavigation(() => setIsOpen(false), pathname) return setIsOpen(false)} /> } ``` ```typescript // Hub wrapper (binds Next.js router once, re-exports a cleaner API): import { usePathname } from 'next/navigation' import { useCloseOnNavigation as libUseCloseOnNavigation } from '@lib/hooks/use-close-on-navigation' export function useCloseOnNavigation(close: () => void) { return libUseCloseOnNavigation(close, usePathname()) } ``` > **Layout-shell only** — do not call this from page-level components. Pages unmount on navigation naturally; this hook exists solely for overlays that persist in the root layout across route changes.