Skip to content

Commit 4a815f8

Browse files
committed
Merge branch 'master' into dev
2 parents d277f72 + 72100ce commit 4a815f8

23 files changed

+6362
-480
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy
1+
name: Release
22
on:
33
push:
44
branches:

.github/workflows/storybook.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Storybook
2+
on:
3+
push:
4+
branches:
5+
- master
6+
paths:
7+
- 'stories/**'
8+
- 'example/**'
9+
- README.md
10+
11+
pull_request:
12+
branches:
13+
- master
14+
paths:
15+
- 'stories/**'
16+
- 'example/**'
17+
- README.md
18+
19+
jobs:
20+
deploy:
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Begin CI...
25+
uses: actions/checkout@v2
26+
27+
- name: Use Node 12
28+
uses: actions/setup-node@v1
29+
with:
30+
node-version: 12.x
31+
32+
- name: Use cached node_modules
33+
uses: actions/cache@v1
34+
with:
35+
path: node_modules
36+
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
37+
restore-keys: |
38+
nodeModules-
39+
40+
- name: Install dependencies
41+
run: yarn install --frozen-lockfile
42+
env:
43+
CI: true
44+
45+
# - name: Lint
46+
# run: yarn lint
47+
# env:
48+
# CI: true
49+
50+
# - name: Test
51+
# run: yarn test --ci --coverage --maxWorkers=2
52+
# env:
53+
# CI: true
54+
55+
- name: Deploy
56+
run: |
57+
npm run deploy-storybook -- --ci
58+
env:
59+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
60+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const basePath = path.resolve(__dirname, '../', 'src');
33

44
module.exports = {
5-
stories: ['../(stories|example)/**/*.stories.(ts|tsx)'],
5+
stories: ['../@(stories|example)/*.stories.@(ts|tsx)'],
66
addons: [
77
'@storybook/addon-actions',
88
'@storybook/addon-links',

.storybook/preview.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const parameters = {
2+
options: {
3+
storySort: { order: ['Introduction', 'Main', 'More'] },
4+
viewMode: 'docs',
5+
},
6+
};

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
## [1.3.3](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.2...v1.3.3) (2020-08-29)
2+
3+
4+
### Features
5+
6+
* **API:** deprecated scrollToPage ([be27bde](https://github.com/ron0115/react-smooth-scroll-hook/commit/be27bdeea3af88e972ed29883680218d04ad9f31))
7+
8+
## [1.3.2](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.1...v1.3.2) (2020-08-28)
9+
10+
11+
### Bug Fixes
12+
13+
* detect some event when dom change ([5dc4ef8](https://github.com/ron0115/react-smooth-scroll-hook/commit/5dc4ef83234550c22a005ebff1dc309b860697b3))
14+
15+
## [1.3.1](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.0...v1.3.1) (2020-08-28)
16+
17+
18+
### Features
19+
20+
* useScrollWatch hook to detect scroll event ([7c0a42c](https://github.com/ron0115/react-smooth-scroll-hook/commit/7c0a42cf9ac47fb475eff980f2b6b9da24cb303f))
21+
22+
# [1.3.0](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.2.0...v1.3.0) (2020-08-27)
23+
24+
25+
### Features
26+
27+
* support documentParent mode ([0deb1bd](https://github.com/ron0115/react-smooth-scroll-hook/commit/0deb1bdaa5ee0824426cc0df320c276c79923b50))
28+
129
# [1.2.0](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.1.0...v1.2.0) (2020-08-25)
230

331

README.md

Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
# react-smooth-scroll-hook
1+
# [react-smooth-scroll-hook](https://github.com/ron0115/react-smooth-scroll-hook)
22

3-
This is a hook to control your scrollbar in react component!
3+
[![GitHub license](https://img.shields.io/github/license/ron0115/react-smooth-scroll-hook?style=flat)](https://github.com/ron0115/react-smooth-scroll-hook/blob/master/LICENSE)
4+
[![npm version](http://img.shields.io/npm/v/react-smooth-scroll-hook.svg?style=flat)](https://npmjs.org/package/react-smooth-scroll-hook)
5+
[![GitHub stars](https://img.shields.io/github/stars/ron0115/react-smooth-scroll-hook?style=flat)](https://github.com/ron0115/react-smooth-scroll-hook/stargazers)
46

5-
Basically; `useSmoothScroll` hook use `requestAnimationFrame` to finish smooth scroll behaviour!
7+
The `useSmoothScroll` hook finish smooth scroll behaviour in react component by `requestAnimationFrame`.
68

7-
If you want to control the `speed` of scroll behaviour, it defaults to use `requestAnimationFrame` mode.
8-
9-
**Examples are <a target="_blank" href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/usesmoothscroll--docs#basic" >Here</a>.**(Storybook)
10-
11-
**Live demo is <a target="_blank" href="https://codesandbox.io/s/reverent-cerf-ks4xh?file=/index.tsx" >Here</a>.**(Codesandbox)
9+
> **Storybook Docs are <a target="_blank" href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/main-usesmoothscroll--docs" >Here</a>.**
1210
1311
## Feature
1412

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

17-
- 👉 Provide `direction` option ,you can set `x` for vertical, `y` for horizontal.
15+
- 👉 Provide `direction` option ,you can set `x` for horizontal, `y` for vertical.
1816

1917
- 💧 No Third Party dependencies, light and pure.
2018

@@ -24,63 +22,152 @@ If you want to control the `speed` of scroll behaviour, it defaults to use `requ
2422
npm install react-smooth-scroll-hook
2523
```
2624

27-
## Basic Usage
25+
## useSmoothScroll
26+
27+
### Quick Start
2828

2929
```tsx
3030
import React, { useRef } from 'react';
3131
import useSmoothScroll from 'react-smooth-scroll-hook';
3232
export const Demo = () => {
33-
const ref = useRef<HTMLDivElement>(null);
33+
// A custom scroll container
34+
const ref = useRef(null);
35+
36+
// Also support document.body / document.documentElement, and you don't need to set a ref passing to jsx
37+
const ref = useRef(document.body);
38+
3439
const { scrollTo } = useSmoothScroll({
3540
ref,
41+
speed: 100,
42+
direction: 'y',
3643
});
3744

3845
return (
3946
<>
4047
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
4148
<div
49+
// if use custom scroll container, pass ref
4250
ref={ref}
4351
style={{
4452
overflowY: 'scroll',
4553
maxHeight: '200px',
4654
}}
4755
>
48-
<div>
49-
{Array(100)
50-
.fill(null)
51-
.map((_item, i) => (
52-
<div key={i} id={`item-${i}`}>
53-
y-{i}
54-
</div>
55-
))}
56-
</div>
56+
{Array(100)
57+
.fill(null)
58+
.map((_item, i) => (
59+
<div key={i} id={`item-${i}`}>
60+
item-{i}
61+
</div>
62+
))}
5763
</div>
5864
</>
5965
);
6066
};
6167
```
6268

63-
## Props
69+
### Props
6470

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

70-
### Returns of Hook
76+
#### Returns of Hook
7177

7278
- **scrollTo** `(string|number) => void`
7379

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

77-
- **reachTop** `boolean`: Whether it has reached the top of scrollContainer
83+
- **reachedTop** `boolean`: Whether it has reached the top of refContainer
7884

79-
- **reachBottom** `boolean`: Whether it has reached the bottom of scrollContainer
85+
- **reachedBottom** `boolean`: Whether it has reached the bottom of refContainer
8086

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

84-
- **refreshState** `() => void`: Manually refresh the state of `reachTop` and `reachBottom`, just an API as you need, and possibly useful in some situation.
90+
- **refreshState** `() => void`: Manually refresh the state of `reachTop` and `reachBottom`, possibly useful in some situation.
91+
92+
- **refreshSize** `() => void`: Manually refresh the size of ref container, possibly useful in some situation.
93+
94+
### Demo
95+
96+
- **<a target="_blank" href="https://codesandbox.io/s/usesmoothscroll-2zt20?file=/Body.stories.tsx" >CodeSandbox</a>**
97+
- **<a target="_blank" href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/main-usesmoothscroll--docs" >Storybook</a>**
98+
99+
## useScrollWatch
100+
101+
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`.
102+
103+
### Quick Start
104+
105+
```tsx
106+
import React, { useRef } from 'react';
107+
import { useScrollWatch } from 'react-smooth-scroll-hook';
108+
export const ScrollConatainerMode = () => {
109+
const ref = useRef(null);
110+
const { scrollTop, curIndex, curItem } = useScrollWatch({
111+
ref,
112+
list: [
113+
{
114+
href: '#item-0',
115+
},
116+
{
117+
href: '#item-10',
118+
},
119+
{
120+
href: '#item-20',
121+
},
122+
],
123+
});
124+
return (
125+
<>
126+
<h2>Scroll Container Mode</h2>
127+
<div>
128+
<p>
129+
<strong>scrollTop:</strong> {scrollTop}
130+
</p>
131+
<p>
132+
<strong>curIndex:</strong> {curIndex}
133+
</p>
134+
<p>
135+
<strong>curHref:</strong> {curItem?.href}
136+
</p>
137+
</div>
138+
<div
139+
style={{
140+
padding: '10px',
141+
maxHeight: '200px',
142+
overflowY: 'scroll',
143+
}}
144+
ref={ref}
145+
>
146+
{Array(100)
147+
.fill(null)
148+
.map((_item, i) => (
149+
<div key={i} id={`item-${i}`}>
150+
item-{i}
151+
</div>
152+
))}
153+
</div>
154+
</>
155+
);
156+
};
157+
```
158+
159+
### Props
160+
161+
- **list** `Array({href, offset})`: `href` is elemet selector string, which passing to `querySelector`, such as `#element-id`
162+
- **ref**: the same as ref of `useSmoothScroll`
163+
164+
### Returns of Hook
165+
166+
- **scrollTop** `number`: current scrollTop of scroll container.
167+
- **curIndex** `number`: current Index of list
168+
- **curItem** `{href, offset}`: current Item of list
169+
170+
### Demo
85171

86-
- **refreshSize** `() => void`: Manually refresh the size of ref container, just an API as you need, and possibly useful in some situation.
172+
- **<a target="_blank" href="https://codesandbox.io/s/gifted-field-5b3ui?file=/UseScrollWatch.stories.tsx:9-24" >CodeSandbox</a>**
173+
- **<a target="_blank" href="https://ron0115.best/react-smooth-scroll-hook/?path=/docs/more-usescrollwatch--docs" >Storybook</a>**

example/Body.stories.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import useSmoothScroll from 'react-smooth-scroll-hook';
2+
import React, { useRef } from 'react';
3+
4+
export const Body = () => {
5+
const ref = useRef<HTMLElement>(document.documentElement);
6+
const { scrollTo } = useSmoothScroll({
7+
ref,
8+
});
9+
10+
return (
11+
<>
12+
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
13+
<button onClick={() => scrollTo(400)}>scrollTo(400)</button>
14+
<br />
15+
<div
16+
style={{
17+
padding: '10px',
18+
}}
19+
>
20+
{Array(100)
21+
.fill(null)
22+
.map((_item, i) => (
23+
<div key={i} id={`item-${i}`}>
24+
item-{i}
25+
</div>
26+
))}
27+
</div>
28+
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
29+
</>
30+
);
31+
};

0 commit comments

Comments
 (0)