All Projects → ron0115 → React Smooth Scroll Hook

ron0115 / React Smooth Scroll Hook

Licence: mit
A React Hook for using smooth scroll in React Component

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Smooth Scroll Hook

react-cool-virtual
😎 ♻️ A tiny React hook for rendering large datasets like a breeze.
Stars: ✭ 1,031 (+804.39%)
Mutual labels:  hook, smooth-scrolling
Fbhookfork
从 fb 的 profilo 项目里提取出来的hook 库,自己用
Stars: ✭ 98 (-14.04%)
Mutual labels:  hook
Scroll Behavior Polyfill
A polyfill for the 'scroll-behavior' CSS-property
Stars: ✭ 76 (-33.33%)
Mutual labels:  smooth-scrolling
Neatinput
A .NET standard project which aims to make keyboard and mouse input monitoring easy on Windows and eventually Linux.
Stars: ✭ 89 (-21.93%)
Mutual labels:  hook
Jjexception
Protect the objective-c application(保护App不闪退)
Stars: ✭ 1,216 (+966.67%)
Mutual labels:  hook
Gohook
GoHook, Go global keyboard and mouse listener hook
Stars: ✭ 94 (-17.54%)
Mutual labels:  hook
React Hook Mighty Mouse
🐭 React hook that tracks mouse events on selected element - zero dependencies
Stars: ✭ 75 (-34.21%)
Mutual labels:  hook
Antdfront
using next generation data manager and hook、pure function component 、webpack to build antd design pro microfrontend project without umi, cra,dva or rematch
Stars: ✭ 105 (-7.89%)
Mutual labels:  hook
React Swipeable
React swipe event handler hook
Stars: ✭ 1,348 (+1082.46%)
Mutual labels:  hook
Commit Msg Linter
git commit message linter hook
Stars: ✭ 87 (-23.68%)
Mutual labels:  hook
Hookmsrbysvm
hook msr by amd svm
Stars: ✭ 86 (-24.56%)
Mutual labels:  hook
Lottie React
A lightweight React library for rendering complex After Effects animations in real time using Lottie.
Stars: ✭ 83 (-27.19%)
Mutual labels:  hook
Swifthook
A library to hook methods in Swift and Objective-C.
Stars: ✭ 93 (-18.42%)
Mutual labels:  hook
Ysf
YSF Server Functions
Stars: ✭ 77 (-32.46%)
Mutual labels:  hook
Homebase React
The React state management library for write-heavy applications
Stars: ✭ 101 (-11.4%)
Mutual labels:  hook
Dynamicoc
深入理解 iOS 热修复原理
Stars: ✭ 76 (-33.33%)
Mutual labels:  hook
Delphihookutils
Delphi Hooking Library by Lsuper
Stars: ✭ 85 (-25.44%)
Mutual labels:  hook
React Hook Thunk Reducer
📡 A React useReducer() hook whose dispatcher supports thunks à la redux-thunk.
Stars: ✭ 91 (-20.18%)
Mutual labels:  hook
Rolly
Custom scroll with inertia, smooth parallax and scenes manager
Stars: ✭ 109 (-4.39%)
Mutual labels:  smooth-scrolling
Icmethoddigger
An easy way to print almost methods including private methods (supported arm64 architecture devices).
Stars: ✭ 103 (-9.65%)
Mutual labels:  hook

react-smooth-scroll-hook

GitHub license npm version GitHub stars

It provided useSmoothScroll hook for finishing smooth scroll behaviour in react component, and useScrollWatch to return some information in scroll container.

It 's a more convenient way to replace native scrollTo api.

Storybook Docs are Here.

Feature

  • 🚀 You don't need to warn about compatibility, it use requsetAnimationFrame api to finish smooth scroll behaviour.

  • 👉 Provide direction option ,you can set x for horizontal, y for vertical.

  • 💧 No Third Party dependencies, light and pure.

Installation

npm install react-smooth-scroll-hook

useSmoothScroll

Quick Start

import React, { useRef } from 'react';
import useSmoothScroll from 'react-smooth-scroll-hook';
export const Demo = () => {
  // A custom scroll container
  const ref = useRef(null);

  // Also support document.body / document.documentElement, and you don't need to set a ref passing to jsx
  const ref = useRef(document.body);

  const { scrollTo } = useSmoothScroll({
    ref,
    speed: 100,
    direction: 'y',
  });

  return (
    <>
      <button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
      <div
        // if use custom scroll container, pass ref
        ref={ref}
        style={{
          overflowY: 'scroll',
          maxHeight: '200px',
        }}
      >
        {Array(100)
          .fill(null)
          .map((_item, i) => (
            <div key={i} id={`item-${i}`}>
              item-{i}
            </div>
          ))}
      </div>
    </>
  );
};

Props

  • ref: RefObject<HTMLElement>, container which set as overflow: scroll, if scroll whole document, pass ref = useRef(document.documentElement) or useRef(document.body).
  • speed: Distance in one frame to move in requestAnimationFrame mode, defaults to 100, if not provide, speed depends on native API scrollTo.
  • direction: Scroll direction, x for horizontal or y for vertical.
  • threshold: Judge scroll is finished has an error range, .defaults to 1.

Returns of Hook

  • scrollTo (string|number) => void

    • Pass number: the distance to scroll, e.g. scrollTo(400)
    • Pass string: the element seletor you want to scrollTo, meanwhile passing to document.querySelector, e.g. scrollTo('#your-dom-id')
  • reachedTop boolean: Whether it has reached the top of refContainer

  • reachedBottom boolean: Whether it has reached the bottom of refContainer

  • scrollToPage (number) => void: Pass page(number), which scroll to a distance as multiples of container size(offsetWidth/offsetHeight) .e.g scrollToPage(1),scrollToPage(-1)

  • refreshState () => void: Manually refresh the state of reachTop and reachBottom, possibly useful in some situation.

  • refreshSize () => void: Manually refresh the size of ref container, possibly useful in some situation.

Demo

useScrollWatch

Proviede a list of dom like below, and pass the parent container ref to hook, it return the scrollbar current state of scrollTop, curIndex, curItem.

Quick Start

import React, { useRef } from 'react';
import { useScrollWatch } from 'react-smooth-scroll-hook';
export const ScrollConatainerMode = () => {
  const ref = useRef(null);
  const { scrollTop, curIndex, curItem } = useScrollWatch({
    ref,
    list: [
      {
        href: '#item-0',
      },
      {
        href: '#item-10',
      },
      {
        href: '#item-20',
      },
    ],
  });
  return (
    <>
      <h2>Scroll Container Mode</h2>
      <div>
        <p>
          <strong>scrollTop:</strong> {scrollTop}
        </p>
        <p>
          <strong>curIndex:</strong> {curIndex}
        </p>
        <p>
          <strong>curHref:</strong> {curItem?.href}
        </p>
      </div>
      <div
        style={{
          padding: '10px',
          maxHeight: '200px',
          overflowY: 'scroll',
        }}
        ref={ref}
      >
        {Array(100)
          .fill(null)
          .map((_item, i) => (
            <div key={i} id={`item-${i}`}>
              item-{i}
            </div>
          ))}
      </div>
    </>
  );
};

Props

  • list Array({href, offset}): href is elemet selector string, which passing to querySelector, such as #element-id
  • ref: the same as ref of useSmoothScroll

Returns of Hook

  • scrollTop number: current scrollTop of scroll container.
  • curIndex number: current Index of list
  • curItem {href, offset}: current Item of list

Demo

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].