Home

Awesome

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({
  addNavigationListener,
  stateStorage,
  getCurrentLocation,
  /* shouldUpdateScroll, */
});

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

Guide

Installation

$ npm i -S scroll-behavior

Basic usage

Create a ScrollBehavior object with the following arguments:

This object will keep track of the scroll position. Call the updateScroll method on this object after navigation 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:

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.