├── .github
└── workflows
│ ├── release.yml
│ └── storybook.yml
├── .gitignore
├── .storybook
├── main.js
└── preview.js
├── CHANGELOG.md
├── GUILD.md
├── LICENSE
├── README-CN.md
├── README.md
├── example
├── .npmignore
├── Body.stories.tsx
├── Demo.stories.tsx
├── DirectionX.stories.tsx
├── UseScrollWatch.stories.tsx
├── index.html
├── index.tsx
├── package.json
├── tsconfig.json
└── yarn.lock
├── jest-puppeteer.config.js
├── jest.e2e.config.js
├── jest.unit.config.js
├── package.json
├── src
├── index.tsx
├── useScrollWatch.ts
├── useSmoothScroll.tsx
└── utils.ts
├── stories
├── index.css
├── index.stories.tsx
├── useScrollWath.stories.tsx
└── useSmoothScroll.stories.tsx
├── test
├── e2e
│ └── useSmoothScroll.test.tsx
└── specs
│ └── utils.test.tsx
├── tsconfig.json
└── yarn.lock
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release
2 | on:
3 | push:
4 | branches:
5 | - master
6 | paths:
7 | - 'src/**'
8 |
9 | pull_request:
10 | branches:
11 | - master
12 | paths:
13 | - 'src/**'
14 |
15 | jobs:
16 | build:
17 | runs-on: ubuntu-latest
18 |
19 | steps:
20 | - name: Begin CI...
21 | uses: actions/checkout@v2
22 |
23 | - name: Use Node 12
24 | uses: actions/setup-node@v1
25 | with:
26 | node-version: 12.x
27 |
28 | - name: Use cached node_modules
29 | uses: actions/cache@v1
30 | with:
31 | path: node_modules
32 | key: nodeModules-${{ hashFiles('**/yarn.lock') }}
33 | restore-keys: |
34 | nodeModules-
35 |
36 | - name: Install dependencies
37 | run: yarn install --frozen-lockfile
38 | env:
39 | CI: true
40 |
41 | # - name: Lint
42 | # run: yarn lint
43 | # env:
44 | # CI: true
45 |
46 | - name: Test
47 | run: |
48 | yarn test --ci --coverage --maxWorkers=2
49 | yarn test:e2e --ci --coverage --maxWorkers=2
50 | env:
51 | CI: true
52 |
53 | - name: Release
54 | run: |
55 | yarn build
56 | npm run release
57 | npm run deploy-storybook -- --ci
58 | env:
59 | GH_TOKEN: ${{ secrets.GH_TOKEN }}
60 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
61 |
--------------------------------------------------------------------------------
/.github/workflows/storybook.yml:
--------------------------------------------------------------------------------
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 }}
61 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.log
2 | .DS_Store
3 | node_modules
4 | .cache
5 | dist
6 | .history
7 | coverage
--------------------------------------------------------------------------------
/.storybook/main.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const basePath = path.resolve(__dirname, '../', 'src');
3 |
4 | module.exports = {
5 | stories: ['../stories/**/*.stories.@(jsx|tsx)'],
6 | addons: [
7 | '@storybook/addon-actions',
8 | '@storybook/addon-links',
9 | '@storybook/addon-docs',
10 | ],
11 | webpackFinal: async config => {
12 | config.module.rules.push({
13 | test: /\.(ts|tsx)$/,
14 | use: [
15 | {
16 | loader: require.resolve('ts-loader'),
17 | options: {
18 | transpileOnly: true,
19 | },
20 | },
21 | {
22 | loader: require.resolve('react-docgen-typescript-loader'),
23 | },
24 | ],
25 | });
26 | config.resolve.alias = {
27 | 'react-smooth-scroll-hook': basePath,
28 | };
29 | config.resolve.extensions.push('.ts', '.tsx');
30 |
31 | return config;
32 | },
33 | };
34 |
--------------------------------------------------------------------------------
/.storybook/preview.js:
--------------------------------------------------------------------------------
1 | export const parameters = {
2 | options: {
3 | storySort: { order: ['Introduction', 'Main', 'More'] },
4 | viewMode: 'docs',
5 | },
6 | };
7 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## [1.3.4](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.3...v1.3.4) (2020-10-27)
2 |
3 |
4 | ### Bug Fixes
5 |
6 | * **useScrollWatch:** do not use ! optional chain ([b931a6d](https://github.com/ron0115/react-smooth-scroll-hook/commit/b931a6dbc7e376de5c78fc60bf2b13cd10f7f507))
7 |
8 | ## [1.3.3](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.2...v1.3.3) (2020-08-29)
9 |
10 |
11 | ### Features
12 |
13 | * **API:** deprecated scrollToPage ([be27bde](https://github.com/ron0115/react-smooth-scroll-hook/commit/be27bdeea3af88e972ed29883680218d04ad9f31))
14 |
15 | ## [1.3.2](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.1...v1.3.2) (2020-08-28)
16 |
17 |
18 | ### Bug Fixes
19 |
20 | * detect some event when dom change ([5dc4ef8](https://github.com/ron0115/react-smooth-scroll-hook/commit/5dc4ef83234550c22a005ebff1dc309b860697b3))
21 |
22 | ## [1.3.1](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.3.0...v1.3.1) (2020-08-28)
23 |
24 |
25 | ### Features
26 |
27 | * useScrollWatch hook to detect scroll event ([7c0a42c](https://github.com/ron0115/react-smooth-scroll-hook/commit/7c0a42cf9ac47fb475eff980f2b6b9da24cb303f))
28 |
29 | # [1.3.0](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.2.0...v1.3.0) (2020-08-27)
30 |
31 |
32 | ### Features
33 |
34 | * support documentParent mode ([0deb1bd](https://github.com/ron0115/react-smooth-scroll-hook/commit/0deb1bdaa5ee0824426cc0df320c276c79923b50))
35 |
36 | # [1.2.0](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.1.0...v1.2.0) (2020-08-25)
37 |
38 |
39 | ### Bug Fixes
40 |
41 | * default to use native scrollTo ([de97546](https://github.com/ron0115/react-smooth-scroll-hook/commit/de9754651a2e33cb2d013df97a8350921748337d))
42 |
43 |
44 | ### Features
45 |
46 | * support some state return from hook ([e884422](https://github.com/ron0115/react-smooth-scroll-hook/commit/e88442297d04d8f17d11547736b7863b9768afdc))
47 |
48 | # [1.1.0](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.0.1...v1.1.0) (2020-08-25)
49 |
50 |
51 | ### Features
52 |
53 | * support some state return from hook ([e15e50d](https://github.com/ron0115/react-smooth-scroll-hook/commit/e15e50d536a283a55b19c579addf38590cf06be7))
54 |
55 | ## [1.0.1](https://github.com/ron0115/react-smooth-scroll-hook/compare/v1.0.0...v1.0.1) (2020-08-22)
56 |
57 |
58 | ### Bug Fixes
59 |
60 | * default to use native scrollTo ([2b0b244](https://github.com/ron0115/react-smooth-scroll-hook/commit/2b0b244b6d3607907a0df6d42546a27a22c67544))
61 |
62 | # 1.0.0 (2020-08-21)
63 |
64 |
65 | ### Features
66 |
67 | * initial first version ([e3f882b](https://github.com/ron0115/react-smooth-scroll-hook/commit/e3f882b8e9a1109743fac8e45b42bcc4b4244a13))
68 |
--------------------------------------------------------------------------------
/GUILD.md:
--------------------------------------------------------------------------------
1 | # TSDX React User Guide
2 |
3 | Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.
4 |
5 | > This TSDX setup is meant for developing React components (not apps!) that can be published to NPM. If you’re looking to build an app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
6 |
7 | > If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
8 |
9 | ## Commands
10 |
11 | TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`.
12 |
13 | The recommended workflow is to run TSDX in one terminal:
14 |
15 | ```
16 | npm start # or yarn start
17 | ```
18 |
19 | This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`.
20 |
21 | Then run either example playground or storybook:
22 |
23 | ### Storybook
24 |
25 | Run inside another terminal:
26 |
27 | ```
28 | yarn storybook
29 | ```
30 |
31 | This loads the stories from `./stories`.
32 |
33 | > NOTE: Stories should reference the components as if using the library, similar to the example playground. This means importing from the root project directory. This has been aliased in the tsconfig and the storybook webpack config as a helper.
34 |
35 | ### Example
36 |
37 | Then run the example inside another:
38 |
39 | ```
40 | cd example
41 | npm i # or yarn to install dependencies
42 | npm start # or yarn start
43 | ```
44 |
45 | The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**, [we use Parcel's aliasing](https://github.com/palmerhq/tsdx/pull/88/files).
46 |
47 | To do a one-off build, use `npm run build` or `yarn build`.
48 |
49 | To run tests, use `npm test` or `yarn test`.
50 |
51 | ## Configuration
52 |
53 | Code quality is [set up for you](https://github.com/palmerhq/tsdx/pull/45/files) with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
54 |
55 | ### Jest
56 |
57 | Jest tests are set up to run with `npm test` or `yarn test`. This runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.
58 |
59 | #### Setup Files
60 |
61 | This is the folder structure we set up for you:
62 |
63 | ```
64 | /example
65 | index.html
66 | index.tsx # test your component here in a demo app
67 | package.json
68 | tsconfig.json
69 | /src
70 | index.tsx # EDIT THIS
71 | /test
72 | blah.test.tsx # EDIT THIS
73 | .gitignore
74 | package.json
75 | README.md # EDIT THIS
76 | tsconfig.json
77 | ```
78 |
79 | #### React Testing Library
80 |
81 | We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
82 |
83 | ### Rollup
84 |
85 | TSDX uses [Rollup v1.x](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
86 |
87 | ### TypeScript
88 |
89 | `tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
90 |
91 | ## Continuous Integration
92 |
93 | ### Travis
94 |
95 | _to be completed_
96 |
97 | ### Circle
98 |
99 | _to be completed_
100 |
101 | ## Optimizations
102 |
103 | Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:
104 |
105 | ```js
106 | // ./types/index.d.ts
107 | declare var __DEV__: boolean;
108 |
109 | // inside your code...
110 | if (__DEV__) {
111 | console.log('foo');
112 | }
113 | ```
114 |
115 | You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
116 |
117 | ## Module Formats
118 |
119 | CJS, ESModules, and UMD module formats are supported.
120 |
121 | The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
122 |
123 | ## Using the Playground
124 |
125 | ```
126 | cd example
127 | npm i # or yarn to install dependencies
128 | npm start # or yarn start
129 | ```
130 |
131 | The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**!
132 |
133 | ## Deploying the Playground
134 |
135 | The Playground is just a simple [Parcel](https://parceljs.org) app, you can deploy it anywhere you would normally deploy that. Here are some guidelines for **manually** deploying with the Netlify CLI (`npm i -g netlify-cli`):
136 |
137 | ```bash
138 | cd example # if not already in the example folder
139 | npm run build # builds to dist
140 | netlify deploy # deploy the dist folder
141 | ```
142 |
143 | Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:
144 |
145 | ```bash
146 | netlify init
147 | # build command: yarn build && cd example && yarn && yarn build
148 | # directory to deploy: example/dist
149 | # pick yes for netlify.toml
150 | ```
151 |
152 | ## Named Exports
153 |
154 | Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
155 |
156 | ## Including Styles
157 |
158 | There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
159 |
160 | For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
161 |
162 | ## Publishing to NPM
163 |
164 | We recommend using https://github.com/sindresorhus/np.
165 |
166 | ## Usage with Lerna
167 |
168 | When creating a new package with TSDX within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_.
169 |
170 | The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.
171 |
172 | Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.
173 |
174 | ```diff
175 | "alias": {
176 | - "react": "../node_modules/react",
177 | - "react-dom": "../node_modules/react-dom"
178 | + "react": "../../../node_modules/react",
179 | + "react-dom": "../../../node_modules/react-dom"
180 | },
181 | ```
182 |
183 | An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/palmerhq/tsdx/issues/64)
184 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 liangzhirong
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README-CN.md:
--------------------------------------------------------------------------------
1 | # [react-smooth-scroll-hook](https://github.com/ron0115/react-smooth-scroll-hook)
2 |
3 | [](https://github.com/ron0115/react-smooth-scroll-hook/blob/master/LICENSE)
4 | [](https://npmjs.org/package/react-smooth-scroll-hook)
5 | [](https://github.com/ron0115/react-smooth-scroll-hook/stargazers)
6 |
7 | > Powered By GE-COMPONENTS From YY GFE TEAM
8 |
9 | 简体中文 | [Englist](./README.md)
10 |
11 | 提供 `useSmoothScroll` hook 完成在 react 项目中的平滑滚动, 同时, `useScrollWatch` 会返回一些滚动过程中的有用信息。
12 |
13 | 一个无痛的方式替换原生 `scrollTo` api.
14 |
15 | > **Storybook 文档 点击这里.**
16 |
17 | ## Feature
18 |
19 | - 🚀 不用担心兼容性, 使用`requsetAnimationFrame` api 实现平滑滚动.
20 |
21 | - 👉 提供 `direction` 选项 ,设置为`x` / `y`,同时支持水平/垂直滚动.
22 |
23 | - 💧 不依赖第三方工具,纯净且轻量.
24 |
25 | ## Installation
26 |
27 | ```sh
28 | npm install react-smooth-scroll-hook
29 | ```
30 |
31 | ## useSmoothScroll
32 |
33 | ### 快速开始
34 |
35 | ```tsx
36 | import React, { useRef } from 'react';
37 | import useSmoothScroll from 'react-smooth-scroll-hook';
38 | export const Demo = () => {
39 | // A custom scroll container
40 | const ref = useRef(null);
41 |
42 | // Also support document.body / document.documentElement, and you don't need to set a ref passing to jsx
43 | const ref = useRef(document.body);
44 |
45 | const { scrollTo } = useSmoothScroll({
46 | ref,
47 | speed: 100,
48 | direction: 'y',
49 | });
50 |
51 | return (
52 | <>
53 |
54 |