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

@mapbox/scroll-restorer

v1.1.0

Published

Preserve scroll state while navigating client-side routes

Downloads

252

Readme

@mapbox/scroll-restorer

Preserve the scroll state of document.scrollingElement while navigating programmatically through history states — that is, client-side routing.

Installation

npm install @mapbox/scroll-restorer

Usage

  1. Invoke the start method when you would like to begin storing the browser's scroll position in history states, likely when your application mounts.
  2. Invoke the stop method when you would like to stop the restoration of scroll position, likely when your application unmounts.

Have a look at an example using React Router.

API

start

scrollRestorer.start([options])

Starts tracking the user's scroll position.

options

autoRestore

Type: boolean. Default: true.

When true, scroll-restorer will attempt to restore the scroll position on each popstate event. Set this to false if you want to manually control the scroll position restoration (e.g. you may wish to call restoreScroll at a very specific point in your application's rendering lifecycle).

captureScrollDebounce

Type: number. Default: 50.

The number of milliseconds by which the scroll-capturing function should be debounced.

end

scrollRestorer.end()

End scroll-restorer's behavior.

getSavedScroll

scrollRestorer.getSavedScroll([input])

Returns the scroll position retrieved from window.history or an optional popstate event. When the autoRestore option of start is true, this method will be invoked automatically, so you only want to use it manually if you've set autoRestore to false.

input

Type: { state: Object }, either window.history or a popstate event. Default: window.history.

restoreScroll

scrollRestorer.restoreScroll([input, attempts])

Manually restore the scroll position for a given history state or popstate event.

When the autoRestore option of start is true, this method will be invoked automatically, so you only want to use it manually if you've set autoRestore to false.

input

Type: { state: Object }, either window.history or a popstate event. Default: window.history.

attempts

Type: number. Default: 5.

The number of times to attempt adjusting the scroll position. scroll-restorer might need to make multiple attempts if the saved scroll state refers to a position that is not yet available because content is still loading.

React & React Router Example

import React, {Component} from 'react';
import {HashRouter, Link, Route} from 'react-router-dom';
import scrollRestorer from '@mapbox/scroll-restorer';

class ScrollRestorerExample extends Component {
  componentDidMount() {
    // Initiate the scroll position restoration behavior.
    scrollRestorer.start();
  }

  componentWillUnmount() {
    // Cease the scroll position restoration behavior.
    scrollRestorer.stop();
  }

  getContent(numParagraphs) {
    const paragraphArr = new Array(numParagraphs).fill();

    return (
      <div>
        <h1>Here are {numParagraphs} paragraph elements, enjoy!</h1>
        {paragraphArr.map((_, index) => <p key={index}>Paragraph #{index}.</p>)}
      </div>
    );
  }

  render() {
    return (
      <HashRouter>
        <div>
          <nav style={{background: '#fff', position: 'fixed', top: 0, left: 0, width: '100%'}}>
            <Link to="/">Home</Link> &nbsp;
            <Link to="/page-a">Page A</Link> &nbsp;
            <Link to="/page-b">Page B</Link> &nbsp;
            <Link to="/page-c">Page C</Link>
          </nav>
          <main style={{marginTop: '40px'}}>
            <Route exact path="/" render={() => <h1>This is the home page.</h1>} />
            <Route exact path="/page-a" render={() => this.getContent(5)} />
            <Route exact path="/page-b" render={() => this.getContent(20)} />
            <Route exact path="/page-c" render={() => this.getContent(100)} />
          </main>
        </div>
      </HashRouter>
    );
  }
}

export default ScrollRestorerExample;

Influences