Skip to content

Commit 2b0b244

Browse files
committed
fix: default to use native scrollTo
1 parent f249b73 commit 2b0b244

File tree

5 files changed

+56
-437
lines changed

5 files changed

+56
-437
lines changed

README.md

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

33
This is a hook to control your scrollbar in react component!
44

5-
**Live demo is [here](https://ron0115.github.io/react-smooth-scroll-hook/?path=/docs/hooks-usesmoothscroll--docs).**
5+
**Live demo is <a target="_blank" href="https://codesandbox.io/s/react-smooth-scroll-hook-vhudw?file=/src/App.js" >Here</a>.**
66

77
Basically; `useSmoothScroll` hook checks the `HTMLElement`'s API `scrollTo`, otherwise, use `requestAnimationFrame` to finish smooth scroll behaviour!
88

99
If you want to control the `speed` of scroll behaviour, it defaults to use `requestAnimationFrame` mode.
1010

1111
## Feature
1212

13-
- Provide `scrollTo`, you don't need to warn about compatibility, it use rAF api on low version browsers.
14-
- Provide `speed` on your demand.
15-
- Provide direction option ,you can set `x` for vertical, `y` for horizontal.
13+
- You don't need to warn about compatibility, it use rAF api on low version browsers.
14+
- Provide `speed` on your demand, otherwise it would use the native API `element.scrollTo`.
15+
- Provide `direction` option ,you can set `x` for vertical, `y` for horizontal.
1616

1717
## Installation
1818

@@ -33,7 +33,7 @@ export const Demo = () => {
3333

3434
return (
3535
<>
36-
<button onClick={() => scrollTo('#y-20')}>scrollTo('#y-20')</button>
36+
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
3737
<div
3838
ref={ref}
3939
style={{
@@ -45,7 +45,7 @@ export const Demo = () => {
4545
{Array(100)
4646
.fill(null)
4747
.map((_item, i) => (
48-
<div key={i} id={`y-${i}`}>
48+
<div key={i} id={`item-${i}`}>
4949
y-{i}
5050
</div>
5151
))}
@@ -59,13 +59,13 @@ export const Demo = () => {
5959
## Props
6060

6161
- **ref:** `RefObject<HTMLElement>`, container which set as `overflow: scroll`.
62-
- **speed:** Distance in one frame to move, useful in `requestAnimationFrame` mode.
62+
- **speed:** Distance in one frame to move in `requestAnimationFrame` mode, defaults to `100`, if not provide, speed depends on native API `scrollTo`.
6363
- **direction:** Scroll direction
6464
- **threshold:** Judge scroll is finished has an error range, .defaults to `1`.
6565

66-
### Props of scrollTo
66+
### Returns of Hook
6767

68-
`scrollTo(string | number)`
68+
- `scrollTo(string | number)`
6969

70-
- Pass `number`: the distance to scroll
71-
- Pass `string`: the element seletor you want to scrollTo, passing to `document.querySelector`, such as `#id`
70+
- Pass `number`: the distance to scroll, e.g. `scrollTo(400)`
71+
- Pass `string`: the element seletor you want to scrollTo, passing to `document.querySelector`, e.g. `scrollTo('#your-dom-id')`

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"dist",
88
"src"
99
],
10+
"homepage": "https://github.com/ron0115/react-smooth-scroll-hook#readme",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/ron0115/react-smooth-scroll-hook.git"
14+
},
1015
"engines": {
1116
"node": ">=10"
1217
},
@@ -48,7 +53,6 @@
4853
"@semantic-release/release-notes-generator": "^9.0.1",
4954
"@storybook/addon-actions": "^6.0.16",
5055
"@storybook/addon-docs": "^6.0.16",
51-
"@storybook/addon-info": "^5.3.19",
5256
"@storybook/addon-links": "^6.0.16",
5357
"@storybook/addons": "^6.0.16",
5458
"@storybook/react": "^6.0.16",

src/index.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export enum Direction {
55
}
66

77
export type UseSmoothScrollType = {
8-
/** the container RefObject which use `overflow:scroll` */
8+
/** the container dom RefObject which use `overflow:scroll` */
99
ref: React.RefObject<HTMLElement>;
1010
/** distance per frame, reflects to speed while scrolling */
1111
speed?: number;
@@ -28,10 +28,13 @@ const getRelativeDistance = (
2828
parent: HTMLElement,
2929
attrMap: AttrMapType
3030
) => {
31-
if (!target) return 0;
3231
if (typeof target === 'number') return target;
3332
if (typeof target === 'string') {
34-
const elm = document.querySelector(target) as HTMLElement;
33+
const elm = document.querySelector(target);
34+
if (!elm) {
35+
console.warn('Please pass correct selector string for scrollTo()!');
36+
return 0;
37+
}
3538
const dis =
3639
elm.getBoundingClientRect()[attrMap.leftTop] -
3740
parent.getBoundingClientRect()[attrMap.leftTop];
@@ -42,7 +45,7 @@ const getRelativeDistance = (
4245

4346
export const useSmoothScroll = ({
4447
ref,
45-
speed = 40,
48+
speed,
4649
direction = Direction.Y,
4750
threshold = 1,
4851
}: UseSmoothScrollType) => {
@@ -59,8 +62,19 @@ export const useSmoothScroll = ({
5962
};
6063

6164
const scrollTo = (target: number | string) => {
65+
if (!ref || !ref.current) {
66+
console.warn(
67+
'Please pass `ref` property for your scroll container! \n Get more info at https://github.com/ron0115/react-smooth-scroll-hook'
68+
);
69+
return;
70+
}
6271
const elm = ref.current;
6372
if (!elm) return;
73+
if (!target) {
74+
console.warn(
75+
'Please pass a valid property for `scrollTo()`! \n Get more info at https://github.com/ron0115/react-smooth-scroll-hook'
76+
);
77+
}
6478
const initScrollLeftTop = elm[attrMap.scrollLeftTop];
6579
const distance = getRelativeDistance(target, elm, attrMap);
6680

@@ -72,7 +86,7 @@ export const useSmoothScroll = ({
7286
behavior: 'smooth',
7387
});
7488
} else {
75-
let _speed = speed;
89+
let _speed = speed || 100;
7690
const cb = () => {
7791
// scroll to edge
7892
if (
@@ -86,11 +100,11 @@ export const useSmoothScroll = ({
86100
const gone = () =>
87101
Math.abs(elm[attrMap.scrollLeftTop] - initScrollLeftTop);
88102

89-
if (Math.abs(distance) - gone() < speed) {
103+
if (Math.abs(distance) - gone() < _speed) {
90104
_speed = Math.abs(distance) - gone();
91105
}
92106

93-
// 每个帧,通常为1/60s内滚动条移动的距离
107+
// distance to run every frame,always 1/60s
94108
elm[attrMap.scrollLeftTop] += _speed * (distance > 0 ? 1 : -1);
95109

96110
// reach destination, threshold defaults to 1

stories/Demo.stories.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export const DirectionX = () => {
6767
<button onClick={() => scrollTo(Infinity)}>
6868
scrollTo Edge - scrollTo(Infinity)
6969
</button>
70+
<button onClick={() => scrollTo(-Infinity)}>
71+
scrollTo Edge - scrollTo(-Infinity)
72+
</button>
7073
<button onClick={() => scrollTo(100)}>scrollTo(100)</button>
7174
<button onClick={() => scrollTo(-100)}>scrollTo(-100)</button>
7275
<br />

0 commit comments

Comments
 (0)