You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+78-7Lines changed: 78 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
The `useSmoothScroll` hook finish smooth scroll behaviour in react component by `requestAnimationFrame`.
4
4
5
-
**Examples are <atarget="_blank"href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/usesmoothscroll--docs#basic" >Here</a>.**(Storybook)
5
+
**Examples are <atarget="_blank"href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/hook-usesmoothscroll--docs" >Here</a>.**(Storybook)
6
6
7
7
**Live demo is <atarget="_blank"href="https://codesandbox.io/s/reverent-cerf-ks4xh?file=/index.tsx" >Here</a>.**(Codesandbox)
8
8
@@ -20,7 +20,9 @@ The `useSmoothScroll` hook finish smooth scroll behaviour in react component by
20
20
npm install react-smooth-scroll-hook
21
21
```
22
22
23
-
## Basic Usage
23
+
## useSmoothScroll
24
+
25
+
### Basic Usage
24
26
25
27
```tsx
26
28
importReact, { useRef } from'react';
@@ -62,27 +64,96 @@ export const Demo = () => {
62
64
};
63
65
```
64
66
65
-
## Props
67
+
###Props
66
68
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)`.
68
70
-**speed:** Distance in one frame to move in `requestAnimationFrame` mode, defaults to `100`, if not provide, speed depends on native API `scrollTo`.
69
71
-**direction:** Scroll direction, `x` for horizontal or `y` for vertical.
70
72
-**threshold:** Judge scroll is finished has an error range, .defaults to `1`.
71
73
72
-
### Returns of Hook
74
+
####Returns of Hook
73
75
74
76
-**scrollTo**`(string|number) => void`
75
77
76
78
- Pass `number`: the distance to scroll, e.g. `scrollTo(400)`
77
79
- Pass `string`: the element seletor you want to scrollTo, meanwhile passing to `document.querySelector`, e.g. `scrollTo('#your-dom-id')`
78
80
79
-
-**reachTop**`boolean`: Whether it has reached the top of refContainer
81
+
-**reachedTop**`boolean`: Whether it has reached the top of refContainer
80
82
81
-
-**reachBottom**`boolean`: Whether it has reached the bottom of refContainer
83
+
-**reachedBottom**`boolean`: Whether it has reached the bottom of refContainer
82
84
83
85
-**scrollToPage**`(number) => void`: Pass page(`number`), which scroll to a distance as multiples of container size(`offsetWidth`/`offsetHeight`)
84
86
.e.g `scrollToPage(1)`,`scrollToPage(-1)`
85
87
86
88
-**refreshState**`() => void`: Manually refresh the state of `reachTop` and `reachBottom`, possibly useful in some situation.
87
89
88
90
-**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`.
> Click <ahref="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
Copy file name to clipboardExpand all lines: src/useScrollWatch.ts
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,18 @@ import {
6
6
isWindowScrollParent,
7
7
}from'./utils';
8
8
9
-
typeListType={
10
-
href: string;
11
-
offset?: number;
12
-
}[];
13
-
14
9
exporttypeuseScrollWathType={
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)`. */
16
11
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 */
18
19
offset?: number;
20
+
/** scroll axis, x for horizontal, y for vertical */
Copy file name to clipboardExpand all lines: src/useSmoothScroll.tsx
+13-9Lines changed: 13 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -10,12 +10,13 @@ import {
10
10
}from'./utils';
11
11
12
12
exporttypeUseSmoothScrollType={
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)`. */
14
14
ref: React.RefObject<HTMLElement>;
15
15
/** distance per frame, reflects to speed while scrolling */
16
16
speed?: number;
17
17
/** scroll direction, you can use 'x` for vertical, 'y` for horizontal */
18
18
direction?: DirectionType;
19
+
/** allowable distance beteween nowable state the judgement edge */
0 commit comments