Skip to content

Commit be27bde

Browse files
committed
feat(API): deprecated scrollToPage
1 parent 4d2a628 commit be27bde

File tree

6 files changed

+120
-35
lines changed

6 files changed

+120
-35
lines changed

README.md

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `useSmoothScroll` hook finish smooth scroll behaviour in react component by `requestAnimationFrame`.
44

5-
**Examples are <a target="_blank" href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/usesmoothscroll--docs#basic" >Here</a>.**(Storybook)
5+
**Examples are <a target="_blank" href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/hook-usesmoothscroll--docs" >Here</a>.**(Storybook)
66

77
**Live demo is <a target="_blank" href="https://codesandbox.io/s/reverent-cerf-ks4xh?file=/index.tsx" >Here</a>.**(Codesandbox)
88

@@ -20,7 +20,9 @@ The `useSmoothScroll` hook finish smooth scroll behaviour in react component by
2020
npm install react-smooth-scroll-hook
2121
```
2222

23-
## Basic Usage
23+
## useSmoothScroll
24+
25+
### Basic Usage
2426

2527
```tsx
2628
import React, { useRef } from 'react';
@@ -62,27 +64,96 @@ export const Demo = () => {
6264
};
6365
```
6466

65-
## Props
67+
### Props
6668

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

72-
### Returns of Hook
74+
#### Returns of Hook
7375

7476
- **scrollTo** `(string|number) => void`
7577

7678
- Pass `number`: the distance to scroll, e.g. `scrollTo(400)`
7779
- Pass `string`: the element seletor you want to scrollTo, meanwhile passing to `document.querySelector`, e.g. `scrollTo('#your-dom-id')`
7880

79-
- **reachTop** `boolean`: Whether it has reached the top of refContainer
81+
- **reachedTop** `boolean`: Whether it has reached the top of refContainer
8082

81-
- **reachBottom** `boolean`: Whether it has reached the bottom of refContainer
83+
- **reachedBottom** `boolean`: Whether it has reached the bottom of refContainer
8284

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

8688
- **refreshState** `() => void`: Manually refresh the state of `reachTop` and `reachBottom`, possibly useful in some situation.
8789

8890
- **refreshSize** `() => void`: Manually refresh the size of ref container, possibly useful in some situation.
91+
92+
## useScrollWatch
93+
94+
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`.
95+
96+
```tsx
97+
export const ScrollConatainerMode = () => {
98+
const ref = useRef<HTMLDivElement>(null);
99+
const { scrollTop, curIndex, curItem } = useScrollWatch({
100+
ref,
101+
list: [
102+
{
103+
href: '#item-0',
104+
},
105+
{
106+
href: '#item-10',
107+
},
108+
{
109+
href: '#item-20',
110+
},
111+
],
112+
});
113+
return (
114+
<>
115+
<h2>Scroll Container Mode</h2>
116+
<div>
117+
<p>
118+
<strong>scrollTop:</strong> {scrollTop}
119+
</p>
120+
<p>
121+
<strong>curIndex:</strong> {curIndex}
122+
</p>
123+
<p>
124+
<strong>curHref:</strong> {curItem?.href}
125+
</p>
126+
</div>
127+
<div
128+
style={{
129+
padding: '10px',
130+
maxHeight: '200px',
131+
overflowY: 'scroll',
132+
}}
133+
ref={ref}
134+
>
135+
{Array(100)
136+
.fill(null)
137+
.map((_item, i) => (
138+
<div key={i} id={`item-${i}`}>
139+
item-{i}
140+
</div>
141+
))}
142+
</div>
143+
</>
144+
);
145+
};
146+
```
147+
148+
> Click <a href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/hook-usescrollwatch--docs" target="_blank">Here</a> to know more and see Demo.
149+
150+
### Props
151+
152+
- **list** `Array({href, offset})`: `href` is elemet selector string, which passing to `querySelector`, such as `#element-id`
153+
- **ref**: the same as ref of `useSmoothScroll`
154+
155+
### Returns of Hook
156+
157+
- **scrollTop** `number`: current scrollTop of scroll container.
158+
- **curIndex** `number`: current Index of list
159+
- **curItem** `{href, offset}`: current Item of list

example/DirectionX.stories.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import useSmoothScroll from 'react-smooth-scroll-hook';
44
export const DirectionX = () => {
55
const [speed, setSpeed] = useState(200);
66
const ref = useRef<HTMLDivElement>(null);
7-
const { scrollTo, reachTop, reachBottom, scrollToPage } = useSmoothScroll({
7+
const {
8+
scrollTo,
9+
reachedTop,
10+
reachedBottom,
11+
containerSize,
12+
} = useSmoothScroll({
813
ref,
914
direction: 'x',
1015
speed,
@@ -50,18 +55,21 @@ export const DirectionX = () => {
5055
<br />
5156
<strong>
5257
Scroll to Page:
53-
<button disabled={reachBottom} onClick={() => scrollToPage(1)}>
54-
scrollToPage(1)
58+
<button
59+
disabled={reachedBottom}
60+
onClick={() => scrollTo(containerSize)}
61+
>
62+
scrollTo(containerSize)
5563
</button>
56-
<button disabled={reachTop} onClick={() => scrollToPage(-1)}>
57-
scrollToPage(-1)
64+
<button disabled={reachedTop} onClick={() => scrollTo(-containerSize)}>
65+
scrollTo(-containerSize)
5866
</button>
5967
</strong>
6068
<br />
6169
<br />
62-
reachTop: {String(reachTop)}
70+
reachedTop: {String(reachedTop)}
6371
<br />
64-
reachBottom: {String(reachBottom)}
72+
reachedBottom: {String(reachedBottom)}
6573
<br />
6674
<div
6775
ref={ref}

src/useScrollWatch.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import {
66
isWindowScrollParent,
77
} from './utils';
88

9-
type ListType = {
10-
href: string;
11-
offset?: number;
12-
}[];
13-
149
export type useScrollWathType = {
15-
/** the container dom RefObject which use `overflow:scroll` */
10+
/** the container dom RefObject which use `overflow:scroll`,if scroll whole document, pass `ref = useRef(document.documentElement)` or `useRef(document.body)`. */
1611
ref: React.RefObject<HTMLElement>;
17-
list: ListType;
12+
list: {
13+
/** dom id of Element */
14+
href: string;
15+
/** the scroll position judge preset of each Element */
16+
offset?: number;
17+
}[];
18+
/** global offset for every Element of list */
1819
offset?: number;
20+
/** scroll axis, x for horizontal, y for vertical */
1921
direction?: DirectionType;
2022
};
2123

@@ -74,7 +76,7 @@ export const useScrollWatch = (props: useScrollWathType) => {
7476

7577
useEffect(() => {
7678
refresh();
77-
}, [ref]);
79+
}, [ref, refresh]);
7880

7981
const curIndex = getCurIndex(scrollTop, posList);
8082

@@ -91,7 +93,7 @@ export const useScrollWatch = (props: useScrollWathType) => {
9193
observer.disconnect();
9294
elm && elm.removeEventListener('scroll', refresh);
9395
};
94-
}, [ref]);
96+
}, [ref, refresh]);
9597

9698
return {
9799
curIndex,

src/useSmoothScroll.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import {
1010
} from './utils';
1111

1212
export type UseSmoothScrollType = {
13-
/** the container dom RefObject which use `overflow:scroll` */
13+
/** the container dom RefObject which use `overflow:scroll`, if scroll whole document, pass `ref = useRef(document.documentElement)` or `useRef(document.body)`. */
1414
ref: React.RefObject<HTMLElement>;
1515
/** distance per frame, reflects to speed while scrolling */
1616
speed?: number;
1717
/** scroll direction, you can use 'x` for vertical, 'y` for horizontal */
1818
direction?: DirectionType;
19+
/** allowable distance beteween nowable state the judgement edge */
1920
threshold?: number;
2021
};
2122

@@ -56,8 +57,8 @@ export const useSmoothScroll = ({
5657
}: UseSmoothScrollType) => {
5758
const attrMap = getAttrMap(direction);
5859

59-
const [reachTop, setReachTop] = useState(true);
60-
const [reachBottom, setReachBottom] = useState(true);
60+
const [reachedTop, setReachedTop] = useState(true);
61+
const [reachedBottom, setReachedBottom] = useState(true);
6162
const [size, setSize] = useState(0);
6263

6364
const isTopEdge = () => {
@@ -86,8 +87,8 @@ export const useSmoothScroll = ({
8687
});
8788

8889
const refreshState = debounce((_evt: Event) => {
89-
isTopEdge() ? setReachTop(true) : setReachTop(false);
90-
isBottomEdge() ? setReachBottom(true) : setReachBottom(false);
90+
isTopEdge() ? setReachedTop(true) : setReachedTop(false);
91+
isBottomEdge() ? setReachedBottom(true) : setReachedBottom(false);
9192
});
9293

9394
const scrollTo = (target?: number | string, offset?: number) => {
@@ -194,18 +195,21 @@ export const useSmoothScroll = ({
194195
observer.disconnect();
195196
elm && elm.removeEventListener('scroll', refreshState);
196197
};
197-
}, [ref]);
198+
}, [ref, refreshState]);
198199

199200
return {
200-
reachTop,
201-
reachBottom,
201+
reachedTop,
202+
reachedBottom,
203+
containerSize: size,
202204
scrollTo,
205+
/** @deprecated replace with scrollTo(n * containerSize) */
203206
scrollToPage: (page: number) => {
204207
scrollTo(page * size);
205208
},
209+
/** @deprecated */
206210
refreshState,
211+
/** @deprecated */
207212
refreshSize,
208213
};
209214
};
210-
211215
export default useSmoothScroll;

stories/useScrollWath.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useScrollWatch } from '../src/index';
33
import './index.css';
44

55
export default {
6-
title: 'hook/useScrollWatch',
6+
title: 'More/useScrollWatch',
77
component: useScrollWatch,
88
};
99

@@ -19,7 +19,7 @@ export const Docs = () => {
1919
export { WindowMode, ScrollConatainerMode, DirectionX };
2020

2121
// @ts-ignore
22-
WindowMode.storyName = 'Body Parent';
22+
WindowMode.storyName = 'Window Parent Mode';
2323
// @ts-ignore
2424
ScrollConatainerMode.storyName = 'ScrollConatainer Mode';
2525
// @ts-ignore

stories/useSmoothScroll.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import useSmoothScroll from '../src/index';
44
import './index.css';
55

66
export default {
7-
title: 'hook/useSmoothScroll',
7+
title: 'Main/useSmoothScroll',
88
component: useSmoothScroll,
99
};
1010
import { Demo } from '../example/Demo.stories';

0 commit comments

Comments
 (0)