Code Monkey home page Code Monkey logo

found-scroll's Introduction

Found Scroll npm

Scroll management for Found.

Usage

import { createBrowserRouter, createRender } from 'found';
import { ScrollManager } from 'found-scroll';

/* ... */

const render = createRender({ renderError });

const BrowserRouter = createBrowserRouter({
  routeConfig,

  render: (renderArgs) => (
    <ScrollManager renderArgs={renderArgs}>{render(renderArgs)}</ScrollManager>
  ),
});

Guide

Installation

$ npm i -S react found
$ npm i -S found-scroll

Basic usage

When constructing a router, in the render method, wrap the rendered element with <ScrollManager>, and pass in renderArgs as a prop, as in the above example.

Scrollable Containers

Generally only the window scroll position is restored for a location. For cases where you also want to restore alternative scroll container there is useScrollContainer

import { useScrollContainer } from 'found-scroll';

function MyScrollView() {
  const scrollRef = useScrollContainer('my-scroll-view');

  return <div ref={scrollRef} />;
}

Scroll containers are identified with a 'scrollKey'. There should only be one element associated with a given key for any given location. Think of it as similar to React's key prop, in that it provides a stable identity for an element across renders.

Custom scroll behavior

You can provide a custom shouldUpdateScroll callback as a prop to <ScrollManager>. This callback receives the previous and the current renderArgs.

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
const shouldUpdateScrollByPathname = (prevRenderArgs, { location }) =>
  !prevRenderArgs || location.pathname !== prevRenderArgs.location.pathname;

const shouldUpdateScrollByRoute = (prevRenderArgs, { routes }) => {
  if (routes.some((route) => route.ignoreScrollBehavior)) {
    return false;
  }

  if (routes.some((route) => route.scrollToTop)) {
    return [0, 0];
  }

  return true;
};

const render = (renderArgs) => (
  <ScrollManager
    shouldUpdateScroll={shouldUpdateScrollByPathname}
    renderArgs={renderArgs}
  >
    {/* ... */}
  </ScrollManager>
);

You can customize <ScrollManager> even further by providing a createScrollBehavior callback that creates the scroll behavior object. This allows using a custom subclass of ScrollBehavior from scroll-behavior with custom logic. When using a custom createScrollBehavior callback, you can continue to specify the shouldUpdateScroll callback as above.

const render = (renderArgs) => (
  <ScrollManager
    createScrollBehavior={(config) => new MyScrollBehavior(config)}
    renderArgs={renderArgs}
  >
    {/* ... */}
  </ScrollManager>
);

found-scroll's People

Contributors

chentsulin avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar ethanve avatar itajaja avatar jquense avatar renovate-bot avatar renovate[bot] avatar taion avatar ykiu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.