React Smooth Scroll Hook
A React Hook for using smooth scroll in React Component
Install / Use
npx skills add ron0115/react-smooth-scroll-hookInstalls into whichever agent you are using.
README
react-smooth-scroll-hook
Powered By GE-COMPONENTS From YY GFE TEAM
English | 简体中文
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 <a target="_blank" href="https://ron0115.github.io/react-smooth-scroll-hook/?path=/docs/main-usesmoothscroll--docs" >Here</a>.
Feature
-
🚀 You don't need to warn about compatibility, it use
requsetAnimationFrameapi to finish smooth scroll behaviour. -
👉 Provide
directionoption ,you can setxfor horizontal,yfor 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 asoverflow: scroll, if scroll whole document, passref = useRef(document.documentElement)oruseRef(document.body). - speed: Distance in one frame to move in
requestAnimationFramemode, defaults to100, if not provide, speed depends on native APIscrollTo. - direction: Scroll direction,
xfor horizontal oryfor vertical. - threshold: an error range distance for status of scrolling finished, .defaults to
1, unit ofpx.
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 todocument.querySelector, e.g.scrollTo('#your-dom-id')
- Pass
-
reachedTop
boolean: Whether it has reached the top of refContainer -
reachedBottom
boolean: Whether it has reached the bottom of refContainer
Demo
- <a target="_blank" href="https://codesandbox.io/s/usesmoothscroll-2zt20?file=/Body.stories.tsx" >CodeSandbox</a>
- <a target="_blank" href="https://ron0115.github.io/react-smooth-scroll-hook/?path=/docs/main-usesmoothscroll--docs" >Storybook</a>
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}):hrefis elemet selector string, which passing toquerySelector, 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
- <a target="_blank" href="https://codesandbox.io/s/gifted-field-5b3ui?file=/UseScrollWatch.stories.tsx:9-24" >CodeSandbox</a>
- <a target="_blank" href="https://ron0115.github.io/react-smooth-scroll-hook/?path=/docs/more-usescrollwatch--docs" >Storybook</a>
Related Skills
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
imsg
385.5kUse the imsg CLI from OpenClaw agents for iMessage/SMS DMs, groups, replies, reactions, polls, watching, and private-API actions.
Agent Development
140.7kThis skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or…
review-duplication
106.4kUse this skill during code reviews to proactively investigate the codebase for duplicated functionality, reinvented wheels, or failure to reuse existing project best practices and shared utilities.
