├── .all-contributorsrc
├── .babelrc
├── .changeset
├── README.md
└── config.json
├── .github
└── workflows
│ └── release.yml
├── .gitignore
├── .huskyrc.js
├── .npmrc
├── .prettierignore
├── .prettierrc.js
├── CHANGELOG.md
├── LICENSE
├── README.md
├── docs
└── spin-delay.gif
├── logo.svg
├── package-lock.json
├── package.json
├── src
├── index.test.js
├── index.ts
└── tsconfig.json
└── types
└── index.d.ts
/.all-contributorsrc:
--------------------------------------------------------------------------------
1 | {
2 | "projectName": "spin-delay",
3 | "projectOwner": "smeijer",
4 | "repoType": "github",
5 | "repoHost": "https://github.com",
6 | "files": [
7 | "README.md"
8 | ],
9 | "imageSize": 100,
10 | "commit": true,
11 | "commitConvention": "angular",
12 | "contributors": [
13 | {
14 | "login": "smeijer",
15 | "name": "Stephan Meijer",
16 | "avatar_url": "https://avatars1.githubusercontent.com/u/1196524?v=4",
17 | "profile": "https://github.com/smeijer",
18 | "contributions": [
19 | "ideas",
20 | "code",
21 | "infra",
22 | "maintenance",
23 | "test"
24 | ]
25 | },
26 | {
27 | "login": "Aprillion",
28 | "name": "Peter Hozák",
29 | "avatar_url": "https://avatars0.githubusercontent.com/u/1087670?v=4",
30 | "profile": "http://peter.hozak.info/",
31 | "contributions": [
32 | "ideas",
33 | "test"
34 | ]
35 | },
36 | {
37 | "login": "erichosick",
38 | "name": "Eric Hosick",
39 | "avatar_url": "https://avatars.githubusercontent.com/u/295228?v=4",
40 | "profile": "http://www.erichosick.com/",
41 | "contributions": [
42 | "doc"
43 | ]
44 | },
45 | {
46 | "login": "supachaidev",
47 | "name": "Supachai Dev",
48 | "avatar_url": "https://avatars.githubusercontent.com/u/88824768?v=4",
49 | "profile": "https://github.com/supachaidev",
50 | "contributions": [
51 | "code"
52 | ]
53 | },
54 | {
55 | "login": "kentcdodds",
56 | "name": "Kent C. Dodds",
57 | "avatar_url": "https://avatars.githubusercontent.com/u/1500684?v=4",
58 | "profile": "https://kentcdodds.com/",
59 | "contributions": [
60 | "code"
61 | ]
62 | },
63 | {
64 | "login": "chucamphong",
65 | "name": "Phong Chu",
66 | "avatar_url": "https://avatars.githubusercontent.com/u/58473133?v=4",
67 | "profile": "https://github.com/chucamphong",
68 | "contributions": [
69 | "code"
70 | ]
71 | },
72 | {
73 | "login": "joeporpeglia",
74 | "name": "Joe Porpeglia",
75 | "avatar_url": "https://avatars.githubusercontent.com/u/1399969?v=4",
76 | "profile": "https://github.com/joeporpeglia",
77 | "contributions": [
78 | "code"
79 | ]
80 | }
81 | ],
82 | "contributorsPerLine": 7,
83 | "commitType": "docs"
84 | }
85 |
--------------------------------------------------------------------------------
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "@babel/preset-env",
4 | "@babel/preset-react",
5 | "@babel/preset-typescript"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/.changeset/README.md:
--------------------------------------------------------------------------------
1 | # Changesets
2 |
3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4 | with multi-package repos, or single-package repos to help you version and publish your code. You can
5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6 |
7 | We have a quick list of common questions to get you started engaging with this project in
8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
9 |
--------------------------------------------------------------------------------
/.changeset/config.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://unpkg.com/@changesets/config@2.1.1/schema.json",
3 | "changelog": [
4 | "@changesets/changelog-github",
5 | { "repo": "smeijer/spin-delay" }
6 | ],
7 | "commit": false,
8 | "fixed": [],
9 | "linked": [],
10 | "access": "public",
11 | "baseBranch": "main",
12 | "updateInternalDependencies": "patch",
13 | "ignore": []
14 | }
15 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | name: Release
2 | on:
3 | push:
4 | branches:
5 | - master
6 | - main
7 | - next
8 |
9 | concurrency: ${{ github.workflow }}-${{ github.ref }}
10 |
11 | jobs:
12 | release:
13 | runs-on: ubuntu-latest
14 | steps:
15 | - uses: actions/checkout@v4
16 | with:
17 | token: ${{ secrets.MY_GITHUB_TOKEN }}
18 | - uses: bahmutov/npm-install@v1
19 |
20 | - name: Create Release Pull Request or Publish to npm
21 | uses: changesets/action@v1
22 | with:
23 | publish: npm run changeset:release
24 | commit: 'chore: version packages'
25 | title: 'next release'
26 | env:
27 | GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
28 | NPM_TOKEN: ${{ secrets.MY_NPM_TOKEN }}
29 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist/
2 | node_modules/
3 |
--------------------------------------------------------------------------------
/.huskyrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | hooks: {
3 | 'pre-commit': 'pretty-quick --staged',
4 | },
5 | };
6 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | legacy-peer-deps=true
2 |
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | dist/
2 | node_modules/
3 |
--------------------------------------------------------------------------------
/.prettierrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | semi: true,
3 | trailingComma: 'all',
4 | singleQuote: true,
5 | printWidth: 80,
6 | tabWidth: 2,
7 | };
8 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # spin-delay
2 |
3 | ## 2.0.1
4 |
5 | ### Patch Changes
6 |
7 | - [`f380792`](https://github.com/smeijer/spin-delay/commit/f380792a23c4331c4e389ebeeca03945a49c4848) Thanks [@smeijer](https://github.com/smeijer)! - Fixes the initial delay used when `options.ssr` is `false`.
8 |
9 | ## 2.0.0
10 |
11 | ### Major Changes
12 |
13 | - [#8](https://github.com/smeijer/spin-delay/pull/8) [`1b81585`](https://github.com/smeijer/spin-delay/commit/1b815854e454e2d10357f2dd586370ef9de44b4d) Thanks [@smeijer](https://github.com/smeijer)! - We now support spinner initialization from the server (SSR). When the `loading` prop is `true` due to server-side rendering, the spinner will be shown immediately. You can opt-out of this behavior by setting the `ssr` option to `false`.
14 |
15 | ```tsx
16 | import { useSpinDelay } from 'spin-delay';
17 |
18 | const spin = useSpinDelay(loading, {
19 | ssr: false, // defaults to true
20 | });
21 | ```
22 |
23 | - [#6](https://github.com/smeijer/spin-delay/pull/6) [`3d4f4d5`](https://github.com/smeijer/spin-delay/commit/3d4f4d51db5c3e0b9a301ff5ada5e9efbe5fd35a) Thanks [@smeijer](https://github.com/smeijer)! - We've to removed the default export. Please update your code to use the named
24 | export instead. This is a breaking change, but it's a minor one. Chances are
25 | that you're already using the named export, and you don't have to do anything.
26 |
27 | ```diff
28 | - import useSpinDelay from 'spin-delay';
29 | + import { useSpinDelay } from 'spin-delay';
30 | ```
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Stephan Meijer
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spin-delay
2 |
3 |
4 | [](#contributors-)
5 |
6 |
7 | **Smart Spinner Helper for React**
8 |
9 | 
10 |
11 | There are a few annoyances when working with spinners. Network request can be so
12 | fast that rendering a spinner does more harm than good. Why render a spinner
13 | when loading the data only takes like 50ms?
14 |
15 | This can be fixed by adding a delay. Only show the spinner when the request takes
16 | longer than 200ms for example. And what happens when the request takes 210ms? Right,
17 | we see a spinner for 10ms. This flicker can be annoying.
18 |
19 | `spin-delay` solves these issues by wrapping your booleans, and only returning
20 | true after the `delay`, and for a minimum time of `minDuration`. This way
21 | you're sure that you don't show unnecessary or very short living spinners.
22 |
23 | ## Demo
24 |
25 | Sandbox -> https://codesandbox.io/s/spin-delay-jlp2c
26 |
27 | ## Installation
28 |
29 | With npm:
30 |
31 | ```sh
32 | npm install --save spin-delay
33 | ```
34 |
35 | With yarn:
36 |
37 | ```sh
38 | yarn add spin-delay
39 | ```
40 |
41 | ## API
42 |
43 | The examples below use the following data object:
44 |
45 | ```jsx
46 | import { useSpinDelay } from 'spin-delay';
47 |
48 | function MyComponent() {
49 | const [{ loading }] = useFetch('http://example.com');
50 |
51 | // options are optional, and default to these values
52 | const showSpinner = useSpinDelay(loading, { delay: 500, minDuration: 200 });
53 |
54 | if (showSpinner) {
55 | return
Stephan Meijer 🤔 💻 🚇 🚧 ⚠️ |
73 | Peter Hozák 🤔 ⚠️ |
74 | Eric Hosick 📖 |
75 | Supachai Dev 💻 |
76 | Kent C. Dodds 💻 |
77 | Phong Chu 💻 |
78 | Joe Porpeglia 💻 |
79 |