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 🙏

© 2019 – Ryan Hefner

scroll-behavior

v0.9.10

Published

Pluggable browser scroll management

Downloads

1,123,824

Maintainers

taiontaion

Keywords

scroll

Readme

scroll-behavior Travis npm

Pluggable browser scroll management.

This library is not generally meant to be used directly by applications. Instead, it's meant to be used in integrations for routing libraries or frameworks. For examples of such integrations, see:

Codecov Discord

Usage

import ScrollBehavior from 'scroll-behavior';

/* ... */

const scrollBehavior = new ScrollBehavior({
  addTransitionHook,
  stateStorage,
  getCurrentLocation,
  /* shouldUpdateScroll, */
});

// After a transition:
scrollBehavior.updateScroll(/* prevContext, context */);

Guide

Installation

$ npm i -S scroll-behavior

Basic usage

Create a ScrollBehavior object with the following arguments:

  • addTransitionHook: this function should take a transition hook function and return an unregister function
    • The transition hook function should be called immediately before a transition updates the page
    • The unregister function should remove the transition hook when called
  • stateStorage: this object should implement read and save methods
    • The save method should take a location object, a nullable element key, and a truthy value; it should save that value for the duration of the page session
    • The read method should take a location object and a nullable element key; it should return the value that save was called with for that location and element key, or a falsy value if no saved value is available
  • getCurrentLocation: this function should return the current location object

This object will keep track of the scroll position. Call the updateScroll method on this object after transitions to emulate the default browser scroll behavior on page changes.

Call the stop method to tear down all listeners.

Custom scroll behavior

You can customize the scroll behavior by providing a shouldUpdateScroll callback when constructing the ScrollBehavior object. When you call updateScroll, you can pass in up to two additional context arguments, which will get passed to this callback.

The callback can return:

  • a falsy value to suppress updating the scroll position
  • a position array of x and y, such as [0, 100], to scroll to that position
  • a string with the id or name of an element, to scroll to that element
  • a truthy value to emulate the browser default scroll behavior

Assuming we call updateScroll with the previous and current location objects:

const scrollBehavior = new ScrollBehavior({
  ...options,
  shouldUpdateScroll: (prevLocation, location) => (
    // Don't scroll if the pathname is the same.
    !prevLocation || location.pathname !== prevLocation.pathname
  ),
});
const scrollBehavior = new ScrollBehavior({
  ...options,
  shouldUpdateScroll: (prevLocation, location) => (
    // Scroll to top when attempting to visit the current path.
    prevLocation && location.pathname === prevLocation.pathname ? [0, 0] : true
  ),
});

Scrolling elements other than window

Call the registerElement method to register an element other than window to have managed scroll behavior. Each of these elements needs to be given a unique key at registration time, and can be given an optional shouldUpdateScroll callback that behaves as above. This method should also be called with the current context per updateScroll above, if applicable, to set up the element's initial scroll position.

scrollBehavior.registerScrollElement(
  key, element, shouldUpdateScroll, context,
);

To unregister an element, call the unregisterElement method with the key used to register that element.

Further scroll behavior customization

If you need to further customize scrolling behavior, subclass the ScrollBehavior class, then override methods as needed. For example, with the appropriate polyfill, you can override scrollToTarget to use smooth scrolling for window.

class SmoothScrollBehavior extends ScrollBehavior {
  scrollToTarget(element, target) {
    if (element !== window) {
      super.scrollToTarget(element, target);
      return;
    }

    if (typeof target === 'string') {
      const targetElement = (
        document.getElementById(target) ||
        document.getElementsByName(target)[0]
      );
      if (targetElement) {
        targetElement.scrollIntoView({ behavior: 'smooth' });
        return;
      }

      // Fallback to scrolling to top when target fragment doesn't exist.
      target = [0, 0]; // eslint-disable-line no-param-reassign
    }

    const [left, top] = target;
    window.scrollTo({ left, top, behavior: 'smooth' });
  }
}

Integrations should accept a createScrollBehavior callback that can create an instance of a custom scroll behavior class.