npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-scroll-restoration

v0.0.4

Published

A robust scroll restoration library for Next.js App Router that supports custom scrollable elements.

Downloads

825

Readme

Next Scroll Restoration

A robust scroll restoration library for Next.js App Router that supports custom scrollable elements.

Purpose

Next Scroll Restoration solves the problem of preserving scroll positions for custom scrollable elements in Next.js applications, which is not natively supported by Next.js's built-in scroll restoration feature. Check out our demo to see it in action.

Features

  • 🔄 Preserves scroll position for custom scrollable elements
  • 🚀 Works seamlessly with Next.js App Router and its built-in scroll restoration
  • 🖱️ Supports multiple scrollable areas within a single page
  • 🔧 Fine-grained control over scroll behavior via URL parameters
  • 🏎️ Performance-optimized with batched updates to sessionStorage
  • 📦 Minimal setup required
  • 🧠 Handles edge cases like same-page navigation, initial page load, hard reload, and more

Installation

npm install next-scroll-restoration

Quick Start

import React from 'react'
import { ScrollRestoration } from 'next-scroll-restoration'

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html>
      <body>
        {children}
        <ScrollRestoration />
      </body>
    </html>
  )
}

Usage

  1. Add the ScrollRestoration component to your root layout.
  2. Mark scrollable elements with the data-scroll-restoration-id attribute:
<div
  data-scroll-restoration-id="container"
  style={{
    height: '100dvh',
    overflowY: 'scroll',
  }}
>
  Your scrollable content
</div>
  1. Use URL parameters to control scroll behavior:
    • ?scroll=false or ?scroll=0: Disable scroll restoration
    • ?scroll=true or ?scroll=1: Force scroll to top (useful for same-page navigation)

How It Works

  • Designed to work alongside Next.js's built-in scroll restoration feature
  • Handles scroll restoration for elements explicitly marked with data-scroll-restoration-id attribute
  • Uses sessionStorage to persist scroll positions across page reloads
  • Utilizes React hooks and Next.js routing for efficient state management
  • Handles various navigation scenarios including history push/forward/reload
  • Handles Next.js App Router features like async Components, nested Suspense boundaries, and SSG correctly
  • Implements a pre-hydration script to eliminate flickering (experimental: optional)

Important Considerations

  1. Same-page navigation: To ensure scroll-to-top behavior, use:

    <Link href="/same-page?scroll=true">Link</Link>
  2. Page navigation with scroll: false: When using Next.js scroll options, also specify in the URL:

    <Link href="/another-page?scroll=false" scroll={false}>
      Link
    </Link>
    router.push('/another-page?scroll=false', { scroll: false })
  3. Search query cleanup: The scroll search query is automatically removed after scroll restoration.

Advanced Usage: Scroll Restoration Before Hydration

Experimental (React 19+ Only)

For cases where you notice flickering on initial page load or first navigation, you can optionally use the ScrollRestorationBeforeHydration component:

import React from 'react'
import {
  ScrollRestoration,
  experimental_ScrollRestorationBeforeHydration as ScrollRestorationBeforeHydration,
} from 'next-scroll-restoration'

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html>
      <body>
        {children}
        <ScrollRestoration>
          <ScrollRestorationBeforeHydration />
        </ScrollRestoration>
      </body>
    </html>
  )
}

This component injects a script that runs before React hydration, restoring scroll position immediately after content becomes readable.

Browser Support

Compatible with all modern browsers that support sessionStorage and ResizeObserver.