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

react-scroll-manager

v1.0.3

Published

Scroll position manager for React applications

Downloads

417

Readme

react-scroll-manager

Build Status npm version

Overview

In a single page application (SPA), the application manipulates the browser history and DOM to simulate navigation. Because navigation is simulated and rendering is dynamic, the usual browser behavior of restoring scroll position when navigating back and forth through the history is not generally functional. While some browsers (particularly Chrome) attempt to support automatic scroll restoration in response to history navigation and asynchronous page rendering, this support is still incomplete and inconsistent. Similarly, SPA router libraries provide varying but incomplete levels of scroll restoration. For example, the current version of React Router does not provide scroll management, and older versions did not provide support for all cases.

This library attempts to provide this missing functionality to React applications in a flexible and mostly router-agnostic way. It supports saving per-location window and element scroll positions to session storage and automatically restoring them during navigation. It also provides support for the related problem of navigating to hash links that reference dynamically rendered elements.

Requirements

This library has the following requirements:

The following features of newer browsers are supported with fallbacks for older browsers:

Installation

npm install react-scroll-manager

Example

The following example demonstrates usage of this library with React Router v4. It includes scroll restoration for both the main content window and a fixed navigation panel.

import React from 'react';
import { Router } from 'react-router-dom';
import { ScrollManager, WindowScroller, ElementScroller } from 'react-scroll-manager';
import { createBrowserHistory as createHistory } from 'history';

class App extends React.Component {
  constructor() {
    super();
    this.history = createHistory();
  }
  render() {
    return (
      <ScrollManager history={this.history}>
        <Router history={this.history}>
          <WindowScroller>
            <ElementScroller scrollKey="nav">
              <div className="nav">
                ...
              </div>
            </ElementScroller>
            <div className="content">
              ...
            </div>
          </WindowScroller>
        </Router>
      </ScrollManager>
    );
  }
}

API

ScrollManager

The ScrollManager component goes outside of your router component. It enables manual scroll restoration, reads and writes scroll positions from/to session storage, saves positions before navigation events, handles scrolling of nested components like WindowScroller and ElementScroller, and performs delayed scrolling to hash links anywhere within the document. It has the following properties:

| Name | Type | Required | Description | |------|------|----------|-------------| | history | object | yes | A history object, as returned by createBrowserHistory or createMemoryHistory. | | sessionKey | string | no | The key under which session state is stored. Defaults to ScrollManager. | | timeout | number | no | The maximum number of milliseconds to wait for rendering to complete. Defaults to 3000. |

WindowScroller

The WindowScroller component goes immediately inside your router component. It handles scrolling the window position after navigation. If your window position never changes (e.g. your layout is fixed and all scrolling occurs within elements), it need not be used. It has no properties, but must be nested within a ScrollManager.

ElementScroller

The ElementScroller component goes immediately outside of a scrollable component (e.g. with overflow: auto style) for which you would like to save and restore the scroll position. It must be nested within a ScrollManager and has the following required property:

| Name | Type | Required | Description | |------|------|----------|-------------| | scrollKey | string | yes | The key within the session state under which the element scroll position is stored. |

Tips

Use router link elements for hash links within a page

Always be sure to use your router library's link component rather than <a> tags when navigating to hash links. While a link like <a href="#id"> will navigate to the given element on the current page, it bypasses the usual call to history.pushState, which assigns a unique key to the history location. Without a location key, the library has no way to associate the position with the location, and scroll restoration won't work for those locations.

  <Link to="#id">...</Link> <!-- right way -->
  <a href="#id">...</a>     <!-- wrong way -->

Acknowledgments

License

react-scroll-manager is available under the ISC license.