Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | 'use client'; |
| 2 | |
| 3 | import { usePathname } from 'next/navigation'; |
| 4 | import { useEffect, useRef } from 'react'; |
| 5 | |
| 6 | /** |
| 7 | * Force scroll-to-top on every pathname change. |
| 8 | * |
| 9 | * Next.js App Router skips its built-in scroll reset when the new |
| 10 | * route resolves to the same `page.tsx` segment as the current one |
| 11 | * (e.g. navigating between two pages handled by |
| 12 | * `app/(home)/docs/[[...slug]]/page.tsx`). The welcome page links to |
| 13 | * many such siblings, so users would land mid-page on the new route. |
| 14 | */ |
| 15 | export function ScrollReset() { |
| 16 | const pathname = usePathname(); |
| 17 | const firstRender = useRef(true); |
| 18 | |
| 19 | useEffect(() => { |
| 20 | if (firstRender.current) { |
| 21 | firstRender.current = false; |
| 22 | return; |
| 23 | } |
| 24 | if (window.location.hash) return; |
| 25 | window.scrollTo({ top: 0, left: 0, behavior: 'instant' }); |
| 26 | }, [pathname]); |
| 27 | |
| 28 | return null; |
| 29 | } |