Skip to content

Commit 120aef4

Browse files
committed
meta: add docs and proper README
1 parent a4ef6ad commit 120aef4

File tree

12 files changed

+9274
-9152
lines changed

12 files changed

+9274
-9152
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
run: yarn lint
2727

2828
- name: Test
29-
run: yarn test --ci --coverage --maxWorkers=2
29+
run: yarn test --ci --maxWorkers=2
3030

3131
- name: Build
3232
run: yarn build

.github/workflows/publish.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Install and build
12+
uses: actions/checkout@v1
13+
uses: actions/setup-node@v1
14+
with:
15+
node-version: 12
16+
registry-url: https://registry.npmjs.org/
17+
run: |
18+
yarn install --frozen-lock-file
19+
yarn build
20+
21+
- name: Build demo page
22+
working-directory: example
23+
run: |
24+
yarn install --frozen-lock-file
25+
yarn build
26+
27+
- name: Deploy demo page
28+
uses: JamesIves/[email protected]
29+
working-directory: example
30+
with:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
BRANCH: gh-pages
33+
FOLDER: dist
34+
CLEAN: true
35+
36+
- name: Publish
37+
run: npm publish
38+
env:
39+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.vscode/launch.json

+16-14
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
// Hover to view descriptions of existing attributes.
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
6-
"configurations": [{
7-
"name": "Node.js: Debug TSDX Jest Tests",
8-
"type": "node",
9-
"request": "launch",
10-
"runtimeArgs": [
11-
"--inspect-brk",
12-
"${workspaceRoot}/node_modules/tsdx/dist/index.js",
13-
"test",
14-
"--runInBand"
15-
],
16-
"console": "integratedTerminal",
17-
"internalConsoleOptions": "neverOpen",
18-
"port": 9229
19-
}]
6+
"configurations": [
7+
{
8+
"name": "Node.js: Debug TSDX Jest Tests",
9+
"type": "node",
10+
"request": "launch",
11+
"runtimeArgs": [
12+
"--inspect-brk",
13+
"${workspaceRoot}/node_modules/tsdx/dist/index.js",
14+
"test",
15+
"--runInBand"
16+
],
17+
"console": "integratedTerminal",
18+
"internalConsoleOptions": "neverOpen",
19+
"port": 9229
20+
}
21+
]
2022
}

README.md

+52-132
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,80 @@
1-
# TSDX React User Guide
1+
<p align="center">
2+
<img src="logo.svg" width="200px" height="200px" alt="logo">
3+
<h2 align="center">useDebouncedLoader</h2>
4+
<p align="center">
5+
✨ Configurable hook to avoid flickering spinners
6+
<br>
7+
<br>
8+
<a href="https://frysztak.github.io/use-debounced-loader/">🎮 View Demo</a>
9+
</p>
10+
</p>
211

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.
12+
## Table of Contents
413

5-
> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
14+
- [📜 About](#-about)
15+
- [🏁 Getting started](#-getting-started)
16+
- [👨‍💻 Development](#-development)
617

7-
> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
18+
## 📜 About
819

9-
## Commands
20+
**useDebouncedLoader** is a tiny React hook designed to debounce loaders/spinners.
1021

11-
TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`.
22+
Using regular `debounce()` function (either from Lodash, many hookified versions or `debounceTime()` from RxJS) is a popular choice for
23+
debouncing spinners shown when your app is awaiting some API response. However, misadjusting its `delay` parameter can result in suboptimal UX. For instance:
1224

13-
The recommended workflow is to run TSDX in one terminal:
25+
- for relatively short debounce time (say, 100..300 ms) it can still cause a little bit of flickering. That's because `debounce()` propagates time on (time, when input stays high).
26+
To remedy this, you can increase debounce time, but then...
27+
- for relatively long debounce time (say, 600..800 ms) and relatively long response time, it can cause _lingering_. That's when spinner lingers on screen, even though
28+
request is already finished. This can turn out to be quite bad for User Experience, as your app will seem slower and less responsive.
1429

15-
```bash
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 the example inside another:
22-
23-
```bash
24-
cd example
25-
npm i # or yarn to install dependencies
26-
npm start # or yarn start
27-
```
28-
29-
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://parceljs.org/module_resolution.html#aliases).
30-
31-
To do a one-off build, use `npm run build` or `yarn build`.
32-
33-
To run tests, use `npm test` or `yarn test`.
34-
35-
## Configuration
36-
37-
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly.
30+
This library aims to avoid both flickering and lingering spinners. It does so by introducing another parameter called `minimalTimeOn`. That's the minimal time spinner will stay on screen.
3831

39-
### Jest
32+
For more details, see (a very short!) API [docs](https://github.com/frysztak/use-debounced-loader/docs). You can also check out [comparison](https://frysztak.github.io/use-debounced-loader/) with a popular [useDebounce](https://github.com/xnimorz/use-debounce) library.
4033

41-
Jest tests are set up to run with `npm test` or `yarn test`.
34+
## 🏁 Getting Started
4235

43-
### Bundle analysis
36+
It's very simple. Just run
4437

45-
Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`.
46-
47-
#### Setup Files
48-
49-
This is the folder structure we set up for you:
50-
51-
```txt
52-
/example
53-
index.html
54-
index.tsx # test your component here in a demo app
55-
package.json
56-
tsconfig.json
57-
/src
58-
index.tsx # EDIT THIS
59-
/test
60-
blah.test.tsx # EDIT THIS
61-
.gitignore
62-
package.json
63-
README.md # EDIT THIS
64-
tsconfig.json
38+
```sh
39+
yarn add use-debounced-loader
6540
```
6641

67-
#### React Testing Library
68-
69-
We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
70-
71-
### Rollup
72-
73-
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
74-
75-
### TypeScript
76-
77-
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
78-
79-
## Continuous Integration
80-
81-
### GitHub Actions
42+
or
8243

83-
Two actions are added by default:
84-
85-
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
86-
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit)
87-
88-
## Optimizations
44+
```sh
45+
npm install use-debounced-loader
46+
```
8947

90-
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:
48+
In your app, you might have code looking roughly like this:
9149

92-
```js
93-
// ./types/index.d.ts
94-
declare var __DEV__: boolean;
50+
```ts
51+
const { isLoading, data, ... } = useQuery(...);
9552

96-
// inside your code...
97-
if (__DEV__) {
98-
console.log('foo');
53+
if (isLoading) {
54+
return <Spinner />
9955
}
10056
```
10157

102-
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
103-
104-
## Module Formats
105-
106-
CJS, ESModules, and UMD module formats are supported.
107-
108-
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
109-
110-
## Deploying the Example Playground
111-
112-
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`):
113-
114-
```bash
115-
cd example # if not already in the example folder
116-
npm run build # builds to dist
117-
netlify deploy # deploy the dist folder
118-
```
58+
All you need to do, is to debounce `isLoading`:
11959

120-
Alternatively, if you already have a git repo connected, you can set up continuous deployment with Netlify:
60+
```ts
61+
const { isLoading, data, ... } = useQuery(...);
62+
const debouncedIsLoading = useDebouncedLoader(isLoading);
12163

122-
```bash
123-
netlify init
124-
# build command: yarn build && cd example && yarn && yarn build
125-
# directory to deploy: example/dist
126-
# pick yes for netlify.toml
64+
if (debouncedIsLoading) {
65+
return <Spinner />
66+
}
12767
```
12868

129-
## Named Exports
130-
131-
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.
132-
133-
## Including Styles
134-
135-
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
136-
137-
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.
69+
and that's it!
13870

139-
## Publishing to NPM
71+
## 👨‍💻 Development
14072

141-
We recommend using [np](https://github.com/sindresorhus/np).
73+
Pull requests are very welcome. This projects has been bootstrapped with awesome [tsdx](https://tsdx.io), so getting started is very easy. All regular commands apply here as well.
14274

143-
## Usage with Lerna
144-
145-
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_.
146-
147-
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.
148-
149-
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.
150-
151-
```diff
152-
"alias": {
153-
- "react": "../node_modules/react",
154-
- "react-dom": "../node_modules/react-dom"
155-
+ "react": "../../../node_modules/react",
156-
+ "react-dom": "../../../node_modules/react-dom"
157-
},
158-
```
75+
Demo app uses [Chakra-UI](https://chakra-ui.com/) and [react-timing-hooks](https://github.com/EricLambrecht/react-timing-hooks).
15976

160-
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)
77+
<br />
78+
<br />
79+
<br />
80+
Logo adapted from icon made by <a href="https://www.flaticon.com/authors/freepik" title="Freepik">Freepik</a> from <a href="https://www.flaticon.com/" title="Flaticon"> www.flaticon.com</a>.

docs/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
**[use-debounced-loader](README.md)**
2+
3+
# use-debounced-loader
4+
5+
## Index
6+
7+
### Modules
8+
9+
* ["useDebouncedLoader"](modules/_usedebouncedloader_.md)
10+
* ["useTimer"](modules/_usetimer_.md)

docs/modules/_usedebouncedloader_.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
**[use-debounced-loader](../README.md)**
2+
3+
# Module: "useDebouncedLoader"
4+
5+
## Index
6+
7+
### Functions
8+
9+
* [useDebouncedLoader](_usedebouncedloader_.md#usedebouncedloader)
10+
11+
## Functions
12+
13+
### useDebouncedLoader
14+
15+
**useDebouncedLoader**(`isLoading`: boolean, `initialDelay?`: number, `minimalTimeOn?`: number): boolean
16+
17+
*Defined in useDebouncedLoader.ts:48*
18+
19+
Debounces `isLoading` input. If `isLoading` remains `true` for at least `initialDelay` miliseconds, this hook
20+
will returns `true` for `minimalTimeOn` miliseconds.
21+
22+
#### Parameters:
23+
24+
Name | Type | Default value | Description |
25+
------ | ------ | ------ | ------ |
26+
`isLoading` | boolean | - | Input signal you wish to debounce (usually it connects to a spinner and indicates a pending operation) |
27+
`initialDelay` | number | 400 | Delay in miliseconds. Requests shorter than `initialDelay` will be ignored. |
28+
`minimalTimeOn` | number | 400 | Once spinner appears, it will stay on screen for a least `minimalTimeOn` miliseconds. |
29+
30+
**Returns:** boolean
31+
32+
Debounced `isLoading` signal

docs/modules/_usetimer_.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
**[use-debounced-loader](../README.md)**
2+
3+
# Module: "useTimer"
4+
5+
## Index
6+
7+
### Functions
8+
9+
* [useTimer](_usetimer_.md#usetimer)
10+
11+
## Functions
12+
13+
### useTimer
14+
15+
**useTimer**(): object
16+
17+
*Defined in useTimer.ts:12*
18+
19+
Hook-based wrapper for `setTimeout()`.
20+
21+
**Returns:** object
22+
23+
Name | Type | Description |
24+
------ | ------ | ------ |
25+
`done` | boolean | Indicates whether timer has finished. |
26+
`start` | (ms: number) => void | Function that starts the timer. Takes number of miliseconds to wait out. |

logo.svg

+3
Loading

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"lint": "tsdx lint",
1818
"prepare": "tsdx build",
1919
"size": "size-limit",
20-
"analyze": "size-limit --why"
20+
"analyze": "size-limit --why",
21+
"docs": "typedoc --plugin typedoc-plugin-markdown --excludeNotExported --readme none --hideBreadcrumbs --exclude '**/index.tsx'"
2122
},
2223
"peerDependencies": {
2324
"react": ">=16"
@@ -88,6 +89,8 @@
8889
"ts-jest": "^25.5.1",
8990
"tsdx": "^0.14.1",
9091
"tslib": "^2.0.3",
92+
"typedoc": "^0.19.2",
93+
"typedoc-plugin-markdown": "^3.0.11",
9194
"typescript": "^4.0.5"
9295
}
9396
}

0 commit comments

Comments
 (0)