@zedtech/zedkit-ui

Reset scroll on route change

Scrolls to the top (or to the `#hash` anchor) when the route key changes, leaving back/forward restoration to the browser. Install with npm.

beta

At a glance

  • Who: Developers whose client-side navigation keeps the previous page's scroll position.
  • What: Scrolls to the top (or to the #hash anchor) when the route key changes, leaving back/forward restoration to the browser.
  • Use when: Once near the root of the app, fed the router pathname (usePathname() in Next.js).
  • Avoid when: For in-page anchor links or scroll spies — the browser already handles those.
  • See also: Render a route as a modal over the list

Install

pnpm add @zedtech/zedkit-ui

Latest: 1.6.2 (via changesets).

Example

Usage
"use client";
import { ScrollReset } from "@zedtech/zedkit-ui";
import { usePathname } from "next/navigation";
export function RouteScroll() {
  return <ScrollReset routeKey={usePathname()} />;
}
Inner scroll container
import { useScrollReset } from "@zedtech/zedkit-ui";
const getMain = useCallback(() => document.querySelector<HTMLElement>("main"), []);
useScrollReset(pathname, { target: getMain });

Wraps: (none)

Accessibility

Jumps instantly by default and always under prefers-reduced-motion: reduce; behavior="smooth" only applies when motion is allowed. Does not move focus. Back/forward (popstate) navigations are skipped so the browser restores the previous position.

On this page