Giter Club home page Giter Club logo

use-scroll-restoration's Introduction

React custom hook for scroll restoration

This is a custom React hook that facilitates scroll restoration for a specific element. It allows you to save and restore the scroll position of an element, optionally persisting it to localStorage or sessionStorage. This can be useful in scenarios where you want to maintain scroll positions across navigation or page refreshes.
It differs from ScrollRestoration in that it can be attached to a specific dom element.

Demo

https://codesandbox.io/p/sandbox/aged-tree-kr7y6w

Installation

npm install use-scroll-restoration

Usage

Here's how you can use the useScrollRestoration hook in your React components:

import React from 'react';
import { useScrollRestoration } from 'use-scroll-restoration';

function ScrollRestorationExample() {
  const { ref, setScroll } = useScrollRestoration('exampleKey', {
    debounceTime: 200,
    persist: 'localStorage',
  });

  return (
    <div ref={ref} style={{ height: '500px', overflow: 'auto' }}>
      {/* Your scrollable content */}
    </div>
  );
}

API

useScrollRestoration(key: string, options?: ScrollRestorationOptions)

  • key (required): A unique key to identify the scroll restoration context.
  • options (optional): An object containing additional options:
    • debounceTime (default: 100): The debounce time (in milliseconds) before saving scroll position.
    • persist (default: false): Specifies where to persist scroll position. Can be false, 'localStorage', or 'sessionStorage'.

Returns an object with the following properties:

  • ref: A callback that sets the DOM element reference. Attach this to the ref prop of the scrollable element you want to manage.
  • setScroll({ x, y }): A function to manually set the scroll position, where x and y are optional scroll coordinates.

use-scroll-restoration's People

Contributors

wildcatco avatar

Stargazers

Richard Griffiths avatar Chris Laplante avatar Hayden Chen avatar wnhlee avatar  avatar

Watchers

 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.