Skip to content

Commit e3f882b

Browse files
committed
feat: initial first version
1 parent 88df894 commit e3f882b

File tree

6 files changed

+2872
-102
lines changed

6 files changed

+2872
-102
lines changed

.github/workflows/main.yml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
name: CI
2-
on: [push]
1+
name: Deploy
2+
on:
3+
push:
4+
branches:
5+
- master
36
jobs:
47
build:
58
runs-on: ubuntu-latest
@@ -31,12 +34,16 @@ jobs:
3134
env:
3235
CI: true
3336

34-
- name: Test
35-
run: yarn test --ci --coverage --maxWorkers=2
36-
env:
37-
CI: true
37+
# - name: Test
38+
# run: yarn test --ci --coverage --maxWorkers=2
39+
# env:
40+
# CI: true
3841

39-
- name: Build
40-
run: yarn build
42+
- name: Release
43+
run: |
44+
yarn build
45+
npm run release
46+
npm run deploy-storybook -- --ci
4147
env:
42-
CI: true
48+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
49+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

README.md

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

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

5-
**Live demo is [here]().**
5+
**Live demo is [here](https://ron0115.github.io/react-smooth-scroll-hook/?path=/docs/hooks-usesmoothscroll--docs).**
66

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

@@ -22,7 +22,38 @@ npm install react-smotth-scroll-hook
2222

2323
## Basic Usage
2424

25-
```javascript
25+
```tsx
26+
import React, { useRef } from 'react';
27+
import useSmoothScroll from 'react-smooth-scroll-hook';
28+
export const Demo = () => {
29+
const ref = useRef<HTMLDivElement>(null);
30+
const { scrollTo } = useSmoothScroll({
31+
ref,
32+
});
33+
34+
return (
35+
<>
36+
<button onClick={() => scrollTo('#y-20')}>scrollTo('#y-20')</button>
37+
<div
38+
ref={ref}
39+
style={{
40+
overflowY: 'scroll',
41+
maxHeight: '200px',
42+
}}
43+
>
44+
<div>
45+
{Array(100)
46+
.fill(null)
47+
.map((_item, i) => (
48+
<div key={i} id={`y-${i}`}>
49+
y-{i}
50+
</div>
51+
))}
52+
</div>
53+
</div>
54+
</>
55+
);
56+
};
2657
```
2758

2859
## Props
@@ -31,3 +62,10 @@ npm install react-smotth-scroll-hook
3162
- **speed:** Distance in one frame to move, useful in `requestAnimationFrame` mode.
3263
- **direction:** Scroll direction
3364
- **threshold:** Judge scroll is finished has an error range, .defaults to `1`.
65+
66+
### Props of scrollTo
67+
68+
`scrollTo(string | number)`
69+
70+
- Pass `number`: the distance to scroll
71+
- Pass `string`: the element seletor you want to scrollTo, passing to `document.querySelector`, such as `#id`

package.json

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.0",
2+
"version": "0.1.1",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",
@@ -17,10 +17,13 @@
1717
"lint": "tsdx lint",
1818
"prepare": "tsdx build",
1919
"storybook": "start-storybook -p 6006",
20-
"build-storybook": "build-storybook"
20+
"build-storybook": "build-storybook --docs",
21+
"deploy-storybook": "storybook-to-ghpages",
22+
"release": "semantic-release"
2123
},
2224
"peerDependencies": {
23-
"react": ">=16"
25+
"react": ">=16.8.0",
26+
"react-dom": ">=16.8.0"
2427
},
2528
"husky": {
2629
"hooks": {
@@ -38,12 +41,18 @@
3841
"module": "dist/react-smooth-scroll-hook.esm.js",
3942
"devDependencies": {
4043
"@babel/core": "^7.11.4",
44+
"@semantic-release/changelog": "^5.0.1",
45+
"@semantic-release/commit-analyzer": "^8.0.1",
46+
"@semantic-release/git": "^9.0.0",
47+
"@semantic-release/npm": "^7.0.5",
48+
"@semantic-release/release-notes-generator": "^9.0.1",
4149
"@storybook/addon-actions": "^6.0.16",
4250
"@storybook/addon-docs": "^6.0.16",
4351
"@storybook/addon-info": "^5.3.19",
4452
"@storybook/addon-links": "^6.0.16",
4553
"@storybook/addons": "^6.0.16",
4654
"@storybook/react": "^6.0.16",
55+
"@storybook/storybook-deployer": "^2.8.6",
4756
"@types/react": "^16.9.46",
4857
"@types/react-dom": "^16.9.8",
4958
"babel-loader": "^8.1.0",
@@ -52,9 +61,24 @@
5261
"react-docgen-typescript-loader": "^3.7.2",
5362
"react-dom": "^16.13.1",
5463
"react-is": "^16.13.1",
64+
"semantic-release": "^17.1.1",
5565
"ts-loader": "^8.0.2",
5666
"tsdx": "^0.13.2",
5767
"tslib": "^2.0.1",
5868
"typescript": "^4.0.2"
69+
},
70+
"publishConfig": {
71+
"access": "public"
72+
},
73+
"release": {
74+
"plugins": [
75+
"@semantic-release/commit-analyzer",
76+
"@semantic-release/release-notes-generator",
77+
"@semantic-release/changelog",
78+
"@semantic-release/npm",
79+
"@semantic-release/git",
80+
"@semantic-release/github"
81+
],
82+
"branch": "master"
5983
}
6084
}

src/index.tsx

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

77
export type UseSmoothScrollType = {
8-
/** 传入`overflow:scroll`容器的ref */
8+
/** the container RefObject which use `overflow:scroll` */
99
ref: React.RefObject<HTMLElement>;
10-
/** 每一帧滑动的路程,单位为1px,指定后会使用rAF模式 */
10+
/** distance per frame, reflects to speed while scrolling */
1111
speed?: number;
12-
/** 滚动方向 */
12+
/** scroll direction, you can use 'x` for vertical, 'y` for horizontal */
1313
direction?: Direction | 'x' | 'y';
1414
threshold?: number;
1515
};
@@ -22,7 +22,7 @@ export type AttrMapType = {
2222
offsetWidthHeight: 'offsetWidth' | 'offsetHeight';
2323
leftTop: 'top' | 'left';
2424
};
25-
// 获取元素与当前滚动条位置的相对距离
25+
// get the relative distance from destination
2626
const getRelativeDistance = (
2727
target: number | string,
2828
parent: HTMLElement,

stories/Demo.stories.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import React, { useRef, useState } from 'react';
22
import useSmoothScroll from '../src/index';
3+
import { Description } from '@storybook/addon-docs/dist/blocks';
34
import './index.css';
5+
46
export default {
57
title: '@Hooks/useSmoothScroll',
68
component: useSmoothScroll,
79
};
810

9-
export const Docs = () => <></>;
11+
export const Docs = () => (
12+
<>
13+
<Description markdown={require('../README.md').default} />{' '}
14+
</>
15+
);
1016

1117
export const Demo = () => {
1218
const ref = useRef<HTMLDivElement>(null);
@@ -29,20 +35,18 @@ export const Demo = () => {
2935
padding: '10px',
3036
}}
3137
>
32-
<div>
33-
{Array(100)
34-
.fill(null)
35-
.map((_item, i) => (
36-
<div key={i} id={`y-item-${i}`}>
37-
y-item-{i}
38-
</div>
39-
))}
40-
</div>
38+
{Array(100)
39+
.fill(null)
40+
.map((_item, i) => (
41+
<div key={i} id={`y-item-${i}`}>
42+
y-item-{i}
43+
</div>
44+
))}
4145
</div>
4246
</>
4347
);
4448
};
45-
Demo.storyName = '基本使用';
49+
Demo.storyName = 'Basic';
4650

4751
export const DirectionX = () => {
4852
const [speed, setSpeed] = useState(50);
@@ -61,7 +65,7 @@ export const DirectionX = () => {
6165
scrollTo('#x-item-40')
6266
</button>
6367
<button onClick={() => scrollTo(Infinity)}>
64-
滚到底部scrollTo(Infinity)
68+
scrollTo Edge - scrollTo(Infinity)
6569
</button>
6670
<button onClick={() => scrollTo(100)}>scrollTo(100)</button>
6771
<button onClick={() => scrollTo(-100)}>scrollTo(-100)</button>
@@ -109,4 +113,4 @@ export const DirectionX = () => {
109113
</>
110114
);
111115
};
112-
DirectionX.storyName = '横向滚动';
116+
DirectionX.storyName = 'More Use';

0 commit comments

Comments
 (0)