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

@epic-web/restore-scroll

v1.1.1

Published

Restore scroll position of elements on page navigation

Downloads

4,390

Readme

npm install @epic-web/restore-scroll

Build Status MIT License Code of Conduct

The Problem

When a user navigates to a new page, the browser will scroll the page to the position it was at when the user left the page. This is a great feature, but it's not perfect. The browser only scrolls the <body> element. If the user scrolls a list, then navigates back and forth, the browser will scroll the page to the top, but the list will still be scrolled to the position it was at when the user left the page.

The Solution

This library provides a way to restore the scroll position of any element on the page you choose. It does this by storing the scroll position of the element in session storage and then restoring it when the user navigates back to the page (very similar to how Remix handles scroll restoration for the <body>).

This depends on React Router's useBeforeUnload, useNavigation, and useLocation hooks. It could probably be generalized to work with other routers. PRs welcome.

Usage

import { ElementScrollRestoration } from '@epic-web/restore-scroll'

return (
	<div>
		<ul id="christmas-gifts">
			<li>🎁</li>
			<li>🎂</li>
			<li>🎉</li>
			{/* ... */}
		</ul>
		<ElementScrollRestoration elementQuery="#christmas-gifts" />
	</div>
)

And that's it! Now when the user navigates away from the page and then back to it, the list will be scrolled to the position it was at when the user left the page.

You can also specify horizontal scroll for elements like carousels:

<ElementScrollRestoration elementQuery="#christmas-gifts" direction="horizontal" />

Tips:

  1. This requires an inline script, so you'll need to pass a nonce if you're using a Content Security Policy that requires this.
  2. Make certain to place the ElementScrollRestoration component after the element you want to restore the scroll position of. This is because the component will render a <script> tag immediately after the element, and that script will run immediately, so the element needs to be in the DOM before the script runs.
  3. If you're computing the id and that value can change between navigations, you may need to specify a key on ElementScrollRestoration to trigger the inline script to be evaluated again and set the scroll position correctly.
  4. You'll want one of these components for each scrollable element you want to restore the scroll position for.

License

MIT