react-usespinner 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -0
- package/README.DEV.md +6 -0
- package/README.md +75 -0
- package/dist/index.ts +5 -0
- package/dist/usespinner.tsx +64 -0
- package/package.json +77 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 William Cairns
|
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.
|
package/README.DEV.md
ADDED
package/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# useSpinner
|
2
|
+
|
3
|
+
Wraps action calls that can be grouped together to display a spinner while 1 or more is active. By default an action will be marked as complete after 10 seconds.
|
4
|
+
|
5
|
+
Any item that may require an indication of processing may be considered an action. For example a fetch call, or a long running process.
|
6
|
+
|
7
|
+
Typically before a fetch is started, an action would be started for the fetch. Once the fetch rresolves or is in error then the action can be ended
|
8
|
+
|
9
|
+
```javascript
|
10
|
+
start("slowapi") // start action
|
11
|
+
console.log("Starting slowapi action")
|
12
|
+
fetch("https://flash-the-slow-api.herokuapp.com/delay/3000")
|
13
|
+
.then(res => res.text())
|
14
|
+
.then(data => console.log(data))
|
15
|
+
.finally(() => {
|
16
|
+
console.log("Ending slowapi action")
|
17
|
+
end("slowapi") // finally end action
|
18
|
+
})
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```javascript
|
24
|
+
import useSpinner from './hooks/usespinner';
|
25
|
+
|
26
|
+
const { start, end, clear, busy, SpinnerContainer } = useSpinner();
|
27
|
+
|
28
|
+
// To start a new action
|
29
|
+
start("10 Second timer")
|
30
|
+
|
31
|
+
// To end an existing action
|
32
|
+
end("10 Second timer")
|
33
|
+
|
34
|
+
// clear all actions
|
35
|
+
clear();
|
36
|
+
|
37
|
+
// Check if any actions currently running
|
38
|
+
if (busy()) {
|
39
|
+
console.log("There are actions running")
|
40
|
+
}
|
41
|
+
|
42
|
+
// Display a spinner (if any actions are running)
|
43
|
+
<SpinnerContainer>
|
44
|
+
LOADING
|
45
|
+
</SpinnerContainer>
|
46
|
+
```
|
47
|
+
|
48
|
+
## Configuration
|
49
|
+
|
50
|
+
A global timeout can be configured, by default this is 10 seconds
|
51
|
+
|
52
|
+
```javascript
|
53
|
+
// to set a global delay of 1 second
|
54
|
+
const { start, end, clear, SpinnerContainer } = useSpinner({ delay: 1000 });
|
55
|
+
```
|
56
|
+
|
57
|
+
Each action can also be configurred with a configurable timeout
|
58
|
+
```javascript
|
59
|
+
// To start a new action that may take up to 60 seconds
|
60
|
+
start("10 Second timer", { delay: 60000 })
|
61
|
+
```
|
62
|
+
|
63
|
+
The action will be kept active based on (in order), action delay, global delay, 10 seconds
|
64
|
+
|
65
|
+
## ending actions
|
66
|
+
|
67
|
+
calling the end action method will immediatly close all actions with the given name, irrespective to when they were started, if mutiple actions with the same name are to be started each must be uniquely identified by name
|
68
|
+
|
69
|
+
```javascript
|
70
|
+
start("genericname", { delay: 10000})
|
71
|
+
start("genericname", { delay: 20000})
|
72
|
+
start("genericname", { delay: 30000})
|
73
|
+
|
74
|
+
end("genericname"); // will mark all three actions as complete as they have the same name
|
75
|
+
```
|
package/dist/index.ts
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
import React, { useState, useEffect, useRef } from "react";
|
2
|
+
|
3
|
+
interface Options {
|
4
|
+
delay: number;
|
5
|
+
}
|
6
|
+
interface SpinnerProps {
|
7
|
+
children: React.ReactNode;
|
8
|
+
}
|
9
|
+
type Action = {
|
10
|
+
name: string;
|
11
|
+
endtime: number;
|
12
|
+
};
|
13
|
+
|
14
|
+
const useSpinner = (globalOptions?: Options) => {
|
15
|
+
// store a list of actions
|
16
|
+
const [actions, setActions] = useState<Action[]>([]);
|
17
|
+
|
18
|
+
// set a timer that every second checks if any actions can be closed
|
19
|
+
useEffect(() => {
|
20
|
+
const timer = setTimeout(() => {
|
21
|
+
if (actions) {
|
22
|
+
setActions((prev) => prev.filter((item) => item.endtime > Date.now()));
|
23
|
+
}
|
24
|
+
}, 1000);
|
25
|
+
return () => clearTimeout(timer);
|
26
|
+
}, [actions]);
|
27
|
+
|
28
|
+
// Allow the start of a new Action to be tracked
|
29
|
+
const start = (name: string, options?: Options) => {
|
30
|
+
const delay: number = options?.delay || globalOptions?.delay || 10000;
|
31
|
+
const currentAction: Action = { name: name, endtime: Date.now() + delay };
|
32
|
+
setActions((prev) => [...prev, currentAction]);
|
33
|
+
};
|
34
|
+
|
35
|
+
// remove any action that has the selected name
|
36
|
+
const end = (name: string) => {
|
37
|
+
if (actions) {
|
38
|
+
setActions((prev) => prev.filter((item) => item.name !== name));
|
39
|
+
}
|
40
|
+
};
|
41
|
+
// clear all current actions
|
42
|
+
const clear = () => {
|
43
|
+
setActions([]);
|
44
|
+
};
|
45
|
+
|
46
|
+
const busy = () => {
|
47
|
+
return actions && actions.length > 0
|
48
|
+
}
|
49
|
+
|
50
|
+
// JSX component used to wrap spinner of choice
|
51
|
+
const SpinnerContainer = ({ children }: SpinnerProps) => {
|
52
|
+
// If no active Actions then dont display anything
|
53
|
+
if (actions && actions.length === 0) {
|
54
|
+
return null;
|
55
|
+
}
|
56
|
+
|
57
|
+
return <div className="useSpinner">{children}</div>;
|
58
|
+
};
|
59
|
+
|
60
|
+
// return hook values
|
61
|
+
return { start, end, clear, busy, SpinnerContainer };
|
62
|
+
};
|
63
|
+
|
64
|
+
export default useSpinner;
|
package/package.json
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
{
|
2
|
+
"name": "react-usespinner",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "track actions in progress to know if a spinner should be display. Actions expire within 10 seconds if not expired in code",
|
5
|
+
"private": false,
|
6
|
+
"license": "MIT",
|
7
|
+
"main": "dist/index.js",
|
8
|
+
"author": "cairnswm",
|
9
|
+
"repository": {
|
10
|
+
"type": "git",
|
11
|
+
"url": "https://github.com/cairnswm/usespinner.git"
|
12
|
+
},
|
13
|
+
"peerDependencies": {
|
14
|
+
"bootstrap": "^5.1.3",
|
15
|
+
"react": "17-18",
|
16
|
+
"react-bootstrap": "^2.0.4",
|
17
|
+
"react-dom": "17-18",
|
18
|
+
"react-scripts": "4-5"
|
19
|
+
},
|
20
|
+
"devDependencies": {
|
21
|
+
"@testing-library/jest-dom": "^5.16.5",
|
22
|
+
"@testing-library/react": "^13.4.0",
|
23
|
+
"@testing-library/user-event": "^13.5.0",
|
24
|
+
"@types/jest": "^27.5.2",
|
25
|
+
"@types/node": "^16.18.3",
|
26
|
+
"@types/react": "^18.0.25",
|
27
|
+
"@types/react-dom": "^18.0.9",
|
28
|
+
"cross-env": "^7.0.3",
|
29
|
+
"react": "17-18",
|
30
|
+
"react-dom": "17-18",
|
31
|
+
"react-scripts": "4-5",
|
32
|
+
"typescript": "^4.9.3",
|
33
|
+
"web-vitals": "^2.1.4"
|
34
|
+
},
|
35
|
+
"keywords": [
|
36
|
+
"react",
|
37
|
+
"react hooks",
|
38
|
+
"fetch",
|
39
|
+
"spinner",
|
40
|
+
"actions"
|
41
|
+
],
|
42
|
+
"files": [
|
43
|
+
"dist",
|
44
|
+
"README.md"
|
45
|
+
],
|
46
|
+
"scripts": {
|
47
|
+
"start": "react-scripts start",
|
48
|
+
"build": "react-scripts build",
|
49
|
+
"test": "react-scripts test",
|
50
|
+
"eject": "react-scripts eject",
|
51
|
+
"clean": "rimraf dist",
|
52
|
+
"compile": "npm run clean && cross-env NODE_ENV=production babel src/hooks --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,stories.js,__snapshots__"
|
53
|
+
},
|
54
|
+
"eslintConfig": {
|
55
|
+
"extends": [
|
56
|
+
"react-app",
|
57
|
+
"react-app/jest"
|
58
|
+
]
|
59
|
+
},
|
60
|
+
"browserslist": {
|
61
|
+
"production": [
|
62
|
+
">0.2%",
|
63
|
+
"not dead",
|
64
|
+
"not op_mini all"
|
65
|
+
],
|
66
|
+
"development": [
|
67
|
+
"last 1 chrome version",
|
68
|
+
"last 1 firefox version",
|
69
|
+
"last 1 safari version"
|
70
|
+
]
|
71
|
+
},
|
72
|
+
"dependencies": {
|
73
|
+
"@babel/cli": "^7.19.3",
|
74
|
+
"@babel/preset-env": "^7.20.2",
|
75
|
+
"@babel/preset-react": "^7.18.6"
|
76
|
+
}
|
77
|
+
}
|