@rokku-x/react-hook-loading-spinner 0.2.0 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -21
- package/dist/index.cjs.js +6 -11
- package/dist/index.d.ts +0 -1
- package/dist/index.esm.js +112 -313
- package/dist/store/loadingStore.d.ts +1 -17
- package/package.json +2 -2
- package/dist/hooks/ssrLoading.d.ts +0 -9
package/README.md
CHANGED
|
@@ -20,7 +20,6 @@ npm install @rokku-x/react-hook-loading-spinner
|
|
|
20
20
|
- 📡 **Event System** - Subscribe to loading state changes
|
|
21
21
|
- ♿ **Accessibility** - Built-in inert attribute and scroll prevention
|
|
22
22
|
- 📱 **Zero Dependencies** - Only requires React and Zustand
|
|
23
|
-
- 🌐 **SSR-Compatible** - Hooks work in SSR; use `ssrLoading` helper in Server Components
|
|
24
23
|
|
|
25
24
|
## Bundle Size
|
|
26
25
|
|
|
@@ -397,28 +396,9 @@ function App() {
|
|
|
397
396
|
loadingComponentScale={1.5}
|
|
398
397
|
/>
|
|
399
398
|
</>
|
|
400
|
-
|
|
401
|
-
### SSR Usage
|
|
402
|
-
|
|
403
|
-
For Server Components, use the SSR-safe helper instead of the client hook:
|
|
404
|
-
|
|
405
|
-
```tsx
|
|
406
|
-
import { ssrLoading } from '@rokku-x/react-hook-loading-spinner';
|
|
407
|
-
|
|
408
|
-
export default function ServerComponent() {
|
|
409
|
-
const { startLoading, stopLoading, isLoading } = ssrLoading();
|
|
410
|
-
|
|
411
|
-
startLoading();
|
|
412
|
-
// ...run your server-side work
|
|
413
|
-
stopLoading();
|
|
414
|
-
|
|
415
|
-
return <div>{isLoading ? 'Loading…' : 'Done'}</div>;
|
|
416
|
-
}
|
|
417
|
-
```
|
|
418
|
-
|
|
419
|
-
For Client Components, continue using `useLoading` as shown above.
|
|
420
399
|
);
|
|
421
400
|
}
|
|
401
|
+
}
|
|
422
402
|
```
|
|
423
403
|
|
|
424
404
|
### Example 7: Fade Animation
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
|
|
3
|
-
[zustand devtools middleware] Unsupported __setState action format.
|
|
4
|
-
When using 'store' option in devtools(), the 'state' should have only one key, which is a value of 'store' that was passed in devtools(),
|
|
5
|
-
and value of this only key should be a state object. Example: { "type": "__setState", "state": { "abc123Store": { "foo": "bar" } } }
|
|
6
|
-
`);const S=l.state[a];if(S==null)return;JSON.stringify(o.getState())!==JSON.stringify(S)&&u(S);return}o.dispatchFromDevtools&&typeof o.dispatch=="function"&&o.dispatch(l)});case"DISPATCH":switch(d.payload.type){case"RESET":return u(b),a===void 0?r?.init(o.getState()):r?.init(w(v.name));case"COMMIT":if(a===void 0){r?.init(o.getState());return}return r?.init(w(v.name));case"ROLLBACK":return O(d.state,l=>{if(a===void 0){u(l),r?.init(o.getState());return}u(l[a]),r?.init(w(v.name))});case"JUMP_TO_STATE":case"JUMP_TO_ACTION":return O(d.state,l=>{if(a===void 0){u(l);return}JSON.stringify(o.getState())!==JSON.stringify(l[a])&&u(l[a])});case"IMPORT_STATE":{const{nextLiftedState:l}=d.payload,S=(p=l.computedStates.slice(-1)[0])==null?void 0:p.state;if(!S)return;u(a===void 0?S:S[a]),r?.send(null,l);return}case"PAUSE_RECORDING":return L=!L}return}}),b},G=q,O=(e,t)=>{let n;try{n=JSON.parse(e)}catch(i){console.error("[zustand devtools middleware] Could not parse the received json",i)}n!==void 0&&t(n)};class R extends EventTarget{constructor(){super(...arguments),this.controller=new AbortController}on(t,n){return this.addEventListener(t,i=>n(i.detail),{signal:this.controller.signal}),this}once(t,n){return this.addEventListener(t,i=>n(i.detail),{signal:this.controller.signal,once:!0}),this}emit(t,n){return this.dispatchEvent(new CustomEvent(t,{detail:n}))}removeAllListeners(){this.controller.abort(),this.controller=new AbortController}}const h=new R,_=$()(G((e,t)=>({loadingCount:0,overrideState:null,localCounters:{},isLoading(){return t().overrideState??t().loadingCount>0},actions:{isGlobalLoading(){return t().isLoading()},startLoading:n=>{const i=t().isLoading();e(s=>({loadingCount:s.loadingCount+1})),n&&e(s=>({localCounters:{...s.localCounters,[n]:(s.localCounters[n]??0)+1}}));const o=t().isLoading();o&&!i&&h.emit("start",null),h.emit("change",{isLoading:o,isOverrideState:t().overrideState!==null})},stopLoading:n=>{const i=t().isLoading(),o=n?t().localCounters[n]??0:0;n&&o>0&&e(f=>({loadingCount:Math.max(0,f.loadingCount-1),localCounters:{...f.localCounters,[n]:Math.max(0,f.localCounters[n]-1)}}));const s=t().isLoading();!s&&i&&h.emit("stop",null),h.emit("change",{isLoading:s,isOverrideState:t().overrideState!==null})},overrideLoading:n=>{const i=t().isLoading();e({overrideState:n});const o=t().isLoading();o&&!i?h.emit("start",null):!o&&i&&h.emit("stop",null),h.emit("change",{isLoading:o,isOverrideState:n!==null})},getLocalCounter:n=>t().localCounters[n]??0,isLocalLoading:n=>(t().localCounters[n]??0)>0}})));function A(){const e=y.useId(),{actions:t}=_(s=>s),n=()=>{t.startLoading(e)},i=()=>{t.isLocalLoading(e)&&t.stopLoading(e)},o=async s=>{n();try{return await s}finally{i()}};return{overrideLoading:t.overrideLoading,startLoading:n,stopLoading:i,get isLocalLoading(){return t.isLocalLoading(e)},asyncUseLoading:o,get isLoading(){return t.isGlobalLoading()}}}function V(){const e=N.randomUUID(),{actions:t}=_(s=>s),n=()=>{t.startLoading(e)},i=()=>{t.isLocalLoading(e)&&t.stopLoading(e)},o=async s=>{n();try{return await s}finally{i()}};return{overrideLoading:t.overrideLoading,startLoading:n,stopLoading:i,get isLocalLoading(){return t.isLocalLoading(e)},asyncUseLoading:o,get isLoading(){return t.isGlobalLoading()}}}const m={Spin:"spin",FadeInOut:"fadeInOut",None:"none"},B=({scale:e=1,animationType:t=m.Spin,animationDuration:n,children:i,style:o,className:s,id:f,prefix:a})=>{const v=t,g=t===m.Spin?1:t===m.FadeInOut?2:0,r=n||g,c=t===m.Spin?"linear":t===m.FadeInOut?"ease-in-out":"linear",L=t===m.None?"none":`${a}-${v} ${r}s ${c} infinite`;return C.jsx("div",{style:{animation:L,...e!==1?{zoom:e}:{},...o},className:s,id:f,children:i})},k=e=>C.jsx("div",{id:"loading-circle",style:{width:"90px",height:"90px",border:"15px solid #f3f3f3",borderTop:"15px solid #009b4bff",borderRadius:"50%",boxSizing:"border-box"},...e}),D=e=>C.jsx("div",{style:{padding:"20px",fontSize:"25px",color:"#333",fontFamily:"system-ui, sans-serif"},...e,children:"Please wait..."});function X({loadingComponent:e,loadingComponentScale:t=1,animationType:n=m.Spin,animationDuration:i,wrapperStyle:o,wrapperClassName:s,wrapperId:f,animationWrapperStyle:a,animationWrapperClassName:v,animationWrapperId:g}){e=e||(n===m.Spin?k:D);const r=y.useRef(Math.random().toString(36).substring(2,6).replace(/[0-9]/g,"")),{loadingCount:c,overrideState:L}=_(p=>p),u=L??c>0,b=y.useRef(null);y.useEffect(()=>{u?(b.current?.showModal(),document.body.setAttribute("inert","")):(b.current?.close(),document.body.removeAttribute("inert"))},[u]);const d=f||"loading-wrapper-"+r.current;return u?F.createPortal(C.jsxs(C.Fragment,{children:[C.jsx("style",{children:`
|
|
7
|
-
dialog#${d}[open] {
|
|
1
|
+
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react"),L=require("react/jsx-runtime"),O=require("react-dom"),v=e=>{let t;const n=new Set,o=(a,l)=>{const d=typeof a=="function"?a(t):a;if(!Object.is(d,t)){const m=t;t=l??(typeof d!="object"||d===null)?d:Object.assign({},t,d),n.forEach(p=>p(t,m))}},i=()=>t,f={setState:o,getState:i,getInitialState:()=>b,subscribe:a=>(n.add(a),()=>n.delete(a))},b=t=e(o,i,f);return f},A=(e=>e?v(e):v),R=e=>e;function $(e,t=R){const n=s.useSyncExternalStore(e.subscribe,s.useCallback(()=>t(e.getState()),[e,t]),s.useCallback(()=>t(e.getInitialState()),[e,t]));return s.useDebugValue(n),n}const x=e=>{const t=A(e),n=o=>$(t,o);return Object.assign(n,t),n},F=(e=>e?x(e):x);class y extends EventTarget{constructor(){super(...arguments),this.controller=new AbortController}on(t,n){return this.addEventListener(t,o=>n(o.detail),{signal:this.controller.signal}),this}once(t,n){return this.addEventListener(t,o=>n(o.detail),{signal:this.controller.signal,once:!0}),this}emit(t,n){return this.dispatchEvent(new CustomEvent(t,{detail:n}))}removeAllListeners(){this.controller.abort(),this.controller=new AbortController}}const g=new y,C=F()((e,t)=>({loadingCount:0,overrideState:null,localCounters:{},isLoading(){return t().overrideState??t().loadingCount>0},actions:{isGlobalLoading(){return t().isLoading()},startLoading:n=>{const o=t().isLoading();e(r=>({loadingCount:r.loadingCount+1})),n&&e(r=>({localCounters:{...r.localCounters,[n]:(r.localCounters[n]??0)+1}}));const i=t().isLoading();i&&!o&&g.emit("start",null),g.emit("change",{isLoading:i,isOverrideState:t().overrideState!==null})},stopLoading:n=>{const o=t().isLoading(),i=n?t().localCounters[n]??0:0;n&&i>0&&e(u=>({loadingCount:Math.max(0,u.loadingCount-1),localCounters:{...u.localCounters,[n]:Math.max(0,u.localCounters[n]-1)}}));const r=t().isLoading();!r&&o&&g.emit("stop",null),g.emit("change",{isLoading:r,isOverrideState:t().overrideState!==null})},overrideLoading:n=>{const o=t().isLoading();e({overrideState:n});const i=t().isLoading();i&&!o?g.emit("start",null):!i&&o&&g.emit("stop",null),g.emit("change",{isLoading:i,isOverrideState:n!==null})},getLocalCounter:n=>t().localCounters[n]??0,isLocalLoading:n=>(t().localCounters[n]??0)>0}}));function w(){const e=s.useId(),{actions:t}=C(r=>r),n=()=>{t.startLoading(e)},o=()=>{t.isLocalLoading(e)&&t.stopLoading(e)},i=async r=>{n();try{return await r}finally{o()}};return{overrideLoading:t.overrideLoading,startLoading:n,stopLoading:o,get isLocalLoading(){return t.isLocalLoading(e)},asyncUseLoading:i,get isLoading(){return t.isGlobalLoading()}}}const c={Spin:"spin",FadeInOut:"fadeInOut",None:"none"},k=({scale:e=1,animationType:t=c.Spin,animationDuration:n,children:o,style:i,className:r,id:u,prefix:f})=>{const b=t,a=t===c.Spin?1:t===c.FadeInOut?2:0,l=n||a,d=t===c.Spin?"linear":t===c.FadeInOut?"ease-in-out":"linear",m=t===c.None?"none":`${f}-${b} ${l}s ${d} infinite`;return L.jsx("div",{style:{animation:m,...e!==1?{zoom:e}:{},...i},className:r,id:u,children:o})},E=e=>L.jsx("div",{id:"loading-circle",style:{width:"90px",height:"90px",border:"15px solid #f3f3f3",borderTop:"15px solid #009b4bff",borderRadius:"50%",boxSizing:"border-box"},...e}),j=e=>L.jsx("div",{style:{padding:"20px",fontSize:"25px",color:"#333",fontFamily:"system-ui, sans-serif"},...e,children:"Please wait..."});function M({loadingComponent:e,loadingComponentScale:t=1,animationType:n=c.Spin,animationDuration:o,wrapperStyle:i,wrapperClassName:r,wrapperId:u,animationWrapperStyle:f,animationWrapperClassName:b,animationWrapperId:a}){e=e||(n===c.Spin?E:j);const l=s.useRef(Math.random().toString(36).substring(2,6).replace(/[0-9]/g,"")),{loadingCount:d,overrideState:m}=C(I=>I),p=m??d>0,S=s.useRef(null);s.useEffect(()=>{p?(S.current?.showModal(),document.body.setAttribute("inert","")):(S.current?.close(),document.body.removeAttribute("inert"))},[p]);const h=u||"loading-wrapper-"+l.current;return p?O.createPortal(L.jsxs(L.Fragment,{children:[L.jsx("style",{children:`
|
|
2
|
+
dialog#${h}[open] {
|
|
8
3
|
display: flex !important;
|
|
9
4
|
justify-content: center;
|
|
10
5
|
align-items: center;
|
|
@@ -13,18 +8,18 @@
|
|
|
13
8
|
max-width: 100%;
|
|
14
9
|
max-height: 100%;
|
|
15
10
|
}
|
|
16
|
-
@keyframes ${
|
|
11
|
+
@keyframes ${l.current}-spin {
|
|
17
12
|
0% { transform: rotate(0deg); }
|
|
18
13
|
100% { transform: rotate(360deg); }
|
|
19
14
|
}
|
|
20
|
-
@keyframes ${
|
|
15
|
+
@keyframes ${l.current}-fadeInOut {
|
|
21
16
|
0%, 100% { opacity: 0.2; }
|
|
22
17
|
50% { opacity: 1; }
|
|
23
18
|
}
|
|
24
|
-
body:has(dialog#${
|
|
19
|
+
body:has(dialog#${h}[open]) {
|
|
25
20
|
overflow: hidden;
|
|
26
21
|
}
|
|
27
22
|
body {
|
|
28
23
|
scrollbar-gutter: stable;
|
|
29
24
|
}
|
|
30
|
-
`}),
|
|
25
|
+
`}),L.jsx("dialog",{ref:S,style:{border:"none",padding:0,backgroundColor:"rgba(0, 0, 0, 0.5)",backdropFilter:"blur(2px)",...i},className:r,id:h,children:L.jsx(k,{scale:t,animationType:n,animationDuration:o,style:f,className:b,id:a,prefix:l.current,children:s.isValidElement(e)?e:s.createElement(e)})})]}),document.body):null}function N({isLoading:e=!1}){const{startLoading:t,stopLoading:n}=w();return s.useEffect(()=>(e?t():n(),()=>{e&&n()}),[e]),null}exports.AnimationType=c;exports.EventEmitter=y;exports.Loading=N;exports.LoadingCircle=E;exports.LoadingPleaseWait=j;exports.LoadingRenderer=M;exports.loadingEventTarget=g;exports.useLoading=w;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { default as useLoading } from './hooks/useLoading';
|
|
2
|
-
export { default as ssrLoading } from './hooks/ssrLoading';
|
|
3
2
|
export { LoadingCircle, LoadingPleaseWait, AnimationType } from './components/LoadingRenderer';
|
|
4
3
|
export type { AnimationType as AnimationTypeType } from './components/LoadingRenderer';
|
|
5
4
|
export { default as Loading } from './components/Loading';
|
package/dist/index.esm.js
CHANGED
|
@@ -1,233 +1,59 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
const T = (e) => {
|
|
2
|
+
import L, { useId as E, useRef as v, useEffect as C } from "react";
|
|
3
|
+
import { jsx as f, jsxs as O, Fragment as $ } from "react/jsx-runtime";
|
|
4
|
+
import { createPortal as j } from "react-dom";
|
|
5
|
+
const y = (n) => {
|
|
7
6
|
let t;
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
if (!Object.is(
|
|
11
|
-
const
|
|
12
|
-
t =
|
|
7
|
+
const e = /* @__PURE__ */ new Set(), o = (s, a) => {
|
|
8
|
+
const l = typeof s == "function" ? s(t) : s;
|
|
9
|
+
if (!Object.is(l, t)) {
|
|
10
|
+
const m = t;
|
|
11
|
+
t = a ?? (typeof l != "object" || l === null) ? l : Object.assign({}, t, l), e.forEach((p) => p(t, m));
|
|
13
12
|
}
|
|
14
|
-
},
|
|
15
|
-
return
|
|
16
|
-
},
|
|
17
|
-
function
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
}, i = () => t, g = { setState: o, getState: i, getInitialState: () => b, subscribe: (s) => (e.add(s), () => e.delete(s)) }, b = t = n(o, i, g);
|
|
14
|
+
return g;
|
|
15
|
+
}, A = ((n) => n ? y(n) : y), F = (n) => n;
|
|
16
|
+
function k(n, t = F) {
|
|
17
|
+
const e = L.useSyncExternalStore(
|
|
18
|
+
n.subscribe,
|
|
19
|
+
L.useCallback(() => t(n.getState()), [n, t]),
|
|
20
|
+
L.useCallback(() => t(n.getInitialState()), [n, t])
|
|
22
21
|
);
|
|
23
|
-
return
|
|
22
|
+
return L.useDebugValue(e), e;
|
|
24
23
|
}
|
|
25
|
-
const
|
|
26
|
-
const t =
|
|
27
|
-
return Object.assign(
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
return t ? Object.fromEntries(
|
|
31
|
-
Object.entries(t.stores).map(([n, i]) => [n, i.getState()])
|
|
32
|
-
) : {};
|
|
33
|
-
}, z = (e, t, n) => {
|
|
34
|
-
if (e === void 0)
|
|
35
|
-
return {
|
|
36
|
-
type: "untracked",
|
|
37
|
-
connection: t.connect(n)
|
|
38
|
-
};
|
|
39
|
-
const i = w.get(n.name);
|
|
40
|
-
if (i)
|
|
41
|
-
return { type: "tracked", store: e, ...i };
|
|
42
|
-
const o = {
|
|
43
|
-
connection: t.connect(n),
|
|
44
|
-
stores: {}
|
|
45
|
-
};
|
|
46
|
-
return w.set(n.name, o), { type: "tracked", store: e, ...o };
|
|
47
|
-
}, G = (e, t) => {
|
|
48
|
-
if (t === void 0) return;
|
|
49
|
-
const n = w.get(e);
|
|
50
|
-
n && (delete n.stores[t], Object.keys(n.stores).length === 0 && w.delete(e));
|
|
51
|
-
}, V = (e) => {
|
|
52
|
-
var t, n;
|
|
53
|
-
if (!e) return;
|
|
54
|
-
const i = e.split(`
|
|
55
|
-
`), o = i.findIndex(
|
|
56
|
-
(g) => g.includes("api.setState")
|
|
57
|
-
);
|
|
58
|
-
if (o < 0) return;
|
|
59
|
-
const s = ((t = i[o + 1]) == null ? void 0 : t.trim()) || "";
|
|
60
|
-
return (n = /.+ (.+) .+/.exec(s)) == null ? void 0 : n[1];
|
|
61
|
-
}, W = (e, t = {}) => (n, i, o) => {
|
|
62
|
-
const { enabled: s, anonymousActionType: g, store: a, ...v } = t;
|
|
63
|
-
let f;
|
|
64
|
-
try {
|
|
65
|
-
f = (s ?? (k ? "production" : void 0) !== "production") && window.__REDUX_DEVTOOLS_EXTENSION__;
|
|
66
|
-
} catch {
|
|
67
|
-
}
|
|
68
|
-
if (!f)
|
|
69
|
-
return e(n, i, o);
|
|
70
|
-
const { connection: r, ...c } = z(a, f, v);
|
|
71
|
-
let p = !0;
|
|
72
|
-
o.setState = ((d, S, l) => {
|
|
73
|
-
const L = n(d, S);
|
|
74
|
-
if (!p) return L;
|
|
75
|
-
const O = l === void 0 ? {
|
|
76
|
-
type: g || V(new Error().stack) || "anonymous"
|
|
77
|
-
} : typeof l == "string" ? { type: l } : l;
|
|
78
|
-
return a === void 0 ? (r?.send(O, i()), L) : (r?.send(
|
|
79
|
-
{
|
|
80
|
-
...O,
|
|
81
|
-
type: `${a}/${O.type}`
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
..._(v.name),
|
|
85
|
-
[a]: o.getState()
|
|
86
|
-
}
|
|
87
|
-
), L);
|
|
88
|
-
}), o.devtools = {
|
|
89
|
-
cleanup: () => {
|
|
90
|
-
r && typeof r.unsubscribe == "function" && r.unsubscribe(), G(v.name, a);
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
const u = (...d) => {
|
|
94
|
-
const S = p;
|
|
95
|
-
p = !1, n(...d), p = S;
|
|
96
|
-
}, m = e(o.setState, i, o);
|
|
97
|
-
if (c.type === "untracked" ? r?.init(m) : (c.stores[c.store] = o, r?.init(
|
|
98
|
-
Object.fromEntries(
|
|
99
|
-
Object.entries(c.stores).map(([d, S]) => [
|
|
100
|
-
d,
|
|
101
|
-
d === c.store ? m : S.getState()
|
|
102
|
-
])
|
|
103
|
-
)
|
|
104
|
-
)), o.dispatchFromDevtools && typeof o.dispatch == "function") {
|
|
105
|
-
let d = !1;
|
|
106
|
-
const S = o.dispatch;
|
|
107
|
-
o.dispatch = (...l) => {
|
|
108
|
-
(k ? "production" : void 0) !== "production" && l[0].type === "__setState" && !d && (console.warn(
|
|
109
|
-
'[zustand devtools middleware] "__setState" action type is reserved to set state from the devtools. Avoid using it.'
|
|
110
|
-
), d = !0), S(...l);
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
return r.subscribe((d) => {
|
|
114
|
-
var S;
|
|
115
|
-
switch (d.type) {
|
|
116
|
-
case "ACTION":
|
|
117
|
-
if (typeof d.payload != "string") {
|
|
118
|
-
console.error(
|
|
119
|
-
"[zustand devtools middleware] Unsupported action format"
|
|
120
|
-
);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
return E(
|
|
124
|
-
d.payload,
|
|
125
|
-
(l) => {
|
|
126
|
-
if (l.type === "__setState") {
|
|
127
|
-
if (a === void 0) {
|
|
128
|
-
u(l.state);
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
Object.keys(l.state).length !== 1 && console.error(
|
|
132
|
-
`
|
|
133
|
-
[zustand devtools middleware] Unsupported __setState action format.
|
|
134
|
-
When using 'store' option in devtools(), the 'state' should have only one key, which is a value of 'store' that was passed in devtools(),
|
|
135
|
-
and value of this only key should be a state object. Example: { "type": "__setState", "state": { "abc123Store": { "foo": "bar" } } }
|
|
136
|
-
`
|
|
137
|
-
);
|
|
138
|
-
const L = l.state[a];
|
|
139
|
-
if (L == null)
|
|
140
|
-
return;
|
|
141
|
-
JSON.stringify(o.getState()) !== JSON.stringify(L) && u(L);
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
o.dispatchFromDevtools && typeof o.dispatch == "function" && o.dispatch(l);
|
|
145
|
-
}
|
|
146
|
-
);
|
|
147
|
-
case "DISPATCH":
|
|
148
|
-
switch (d.payload.type) {
|
|
149
|
-
case "RESET":
|
|
150
|
-
return u(m), a === void 0 ? r?.init(o.getState()) : r?.init(_(v.name));
|
|
151
|
-
case "COMMIT":
|
|
152
|
-
if (a === void 0) {
|
|
153
|
-
r?.init(o.getState());
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
return r?.init(_(v.name));
|
|
157
|
-
case "ROLLBACK":
|
|
158
|
-
return E(d.state, (l) => {
|
|
159
|
-
if (a === void 0) {
|
|
160
|
-
u(l), r?.init(o.getState());
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
u(l[a]), r?.init(_(v.name));
|
|
164
|
-
});
|
|
165
|
-
case "JUMP_TO_STATE":
|
|
166
|
-
case "JUMP_TO_ACTION":
|
|
167
|
-
return E(d.state, (l) => {
|
|
168
|
-
if (a === void 0) {
|
|
169
|
-
u(l);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
JSON.stringify(o.getState()) !== JSON.stringify(l[a]) && u(l[a]);
|
|
173
|
-
});
|
|
174
|
-
case "IMPORT_STATE": {
|
|
175
|
-
const { nextLiftedState: l } = d.payload, L = (S = l.computedStates.slice(-1)[0]) == null ? void 0 : S.state;
|
|
176
|
-
if (!L) return;
|
|
177
|
-
u(a === void 0 ? L : L[a]), r?.send(
|
|
178
|
-
null,
|
|
179
|
-
// FIXME no-any
|
|
180
|
-
l
|
|
181
|
-
);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
case "PAUSE_RECORDING":
|
|
185
|
-
return p = !p;
|
|
186
|
-
}
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
}), m;
|
|
190
|
-
}, B = W, E = (e, t) => {
|
|
191
|
-
let n;
|
|
192
|
-
try {
|
|
193
|
-
n = JSON.parse(e);
|
|
194
|
-
} catch (i) {
|
|
195
|
-
console.error(
|
|
196
|
-
"[zustand devtools middleware] Could not parse the received json",
|
|
197
|
-
i
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
n !== void 0 && t(n);
|
|
201
|
-
};
|
|
202
|
-
class X extends EventTarget {
|
|
24
|
+
const x = (n) => {
|
|
25
|
+
const t = A(n), e = (o) => k(t, o);
|
|
26
|
+
return Object.assign(e, t), e;
|
|
27
|
+
}, N = ((n) => n ? x(n) : x);
|
|
28
|
+
class R extends EventTarget {
|
|
203
29
|
constructor() {
|
|
204
30
|
super(...arguments), this.controller = new AbortController();
|
|
205
31
|
}
|
|
206
|
-
on(t,
|
|
32
|
+
on(t, e) {
|
|
207
33
|
return this.addEventListener(
|
|
208
34
|
t,
|
|
209
|
-
(
|
|
35
|
+
(o) => e(o.detail),
|
|
210
36
|
{ signal: this.controller.signal }
|
|
211
37
|
), this;
|
|
212
38
|
}
|
|
213
|
-
once(t,
|
|
39
|
+
once(t, e) {
|
|
214
40
|
return this.addEventListener(
|
|
215
41
|
t,
|
|
216
|
-
(
|
|
42
|
+
(o) => e(o.detail),
|
|
217
43
|
{
|
|
218
44
|
signal: this.controller.signal,
|
|
219
45
|
once: !0
|
|
220
46
|
}
|
|
221
47
|
), this;
|
|
222
48
|
}
|
|
223
|
-
emit(t,
|
|
224
|
-
return this.dispatchEvent(new CustomEvent(t, { detail:
|
|
49
|
+
emit(t, e) {
|
|
50
|
+
return this.dispatchEvent(new CustomEvent(t, { detail: e }));
|
|
225
51
|
}
|
|
226
52
|
removeAllListeners() {
|
|
227
53
|
this.controller.abort(), this.controller = new AbortController();
|
|
228
54
|
}
|
|
229
55
|
}
|
|
230
|
-
const
|
|
56
|
+
const u = new R(), w = N()((n, t) => ({
|
|
231
57
|
loadingCount: 0,
|
|
232
58
|
overrideState: null,
|
|
233
59
|
localCounters: {},
|
|
@@ -238,121 +64,95 @@ const h = new X(), I = J()(B((e, t) => ({
|
|
|
238
64
|
isGlobalLoading() {
|
|
239
65
|
return t().isLoading();
|
|
240
66
|
},
|
|
241
|
-
startLoading: (
|
|
242
|
-
const
|
|
243
|
-
|
|
67
|
+
startLoading: (e) => {
|
|
68
|
+
const o = t().isLoading();
|
|
69
|
+
n((r) => ({ loadingCount: r.loadingCount + 1 })), e && n((r) => ({
|
|
244
70
|
localCounters: {
|
|
245
|
-
...
|
|
246
|
-
[
|
|
71
|
+
...r.localCounters,
|
|
72
|
+
[e]: (r.localCounters[e] ?? 0) + 1
|
|
247
73
|
}
|
|
248
74
|
}));
|
|
249
|
-
const
|
|
250
|
-
|
|
75
|
+
const i = t().isLoading();
|
|
76
|
+
i && !o && u.emit("start", null), u.emit("change", { isLoading: i, isOverrideState: t().overrideState !== null });
|
|
251
77
|
},
|
|
252
|
-
stopLoading: (
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
loadingCount: Math.max(0,
|
|
78
|
+
stopLoading: (e) => {
|
|
79
|
+
const o = t().isLoading(), i = e ? t().localCounters[e] ?? 0 : 0;
|
|
80
|
+
e && i > 0 && n((d) => ({
|
|
81
|
+
loadingCount: Math.max(0, d.loadingCount - 1),
|
|
256
82
|
localCounters: {
|
|
257
|
-
...
|
|
258
|
-
[
|
|
83
|
+
...d.localCounters,
|
|
84
|
+
[e]: Math.max(0, d.localCounters[e] - 1)
|
|
259
85
|
}
|
|
260
86
|
}));
|
|
261
|
-
const
|
|
262
|
-
!
|
|
87
|
+
const r = t().isLoading();
|
|
88
|
+
!r && o && u.emit("stop", null), u.emit("change", { isLoading: r, isOverrideState: t().overrideState !== null });
|
|
263
89
|
},
|
|
264
|
-
overrideLoading: (
|
|
265
|
-
const i = t().isLoading();
|
|
266
|
-
e({ overrideState: n });
|
|
90
|
+
overrideLoading: (e) => {
|
|
267
91
|
const o = t().isLoading();
|
|
268
|
-
|
|
92
|
+
n({ overrideState: e });
|
|
93
|
+
const i = t().isLoading();
|
|
94
|
+
i && !o ? u.emit("start", null) : !i && o && u.emit("stop", null), u.emit("change", { isLoading: i, isOverrideState: e !== null });
|
|
269
95
|
},
|
|
270
|
-
getLocalCounter: (
|
|
271
|
-
isLocalLoading: (
|
|
96
|
+
getLocalCounter: (e) => t().localCounters[e] ?? 0,
|
|
97
|
+
isLocalLoading: (e) => (t().localCounters[e] ?? 0) > 0
|
|
272
98
|
}
|
|
273
|
-
}))
|
|
274
|
-
function
|
|
275
|
-
const
|
|
276
|
-
t.startLoading(
|
|
277
|
-
},
|
|
278
|
-
t.isLocalLoading(
|
|
279
|
-
},
|
|
280
|
-
|
|
281
|
-
try {
|
|
282
|
-
return await s;
|
|
283
|
-
} finally {
|
|
284
|
-
i();
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
return {
|
|
288
|
-
overrideLoading: t.overrideLoading,
|
|
289
|
-
startLoading: n,
|
|
290
|
-
stopLoading: i,
|
|
291
|
-
get isLocalLoading() {
|
|
292
|
-
return t.isLocalLoading(e);
|
|
293
|
-
},
|
|
294
|
-
asyncUseLoading: o,
|
|
295
|
-
get isLoading() {
|
|
296
|
-
return t.isGlobalLoading();
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
function nt() {
|
|
301
|
-
const e = j(), { actions: t } = I((s) => s), n = () => {
|
|
302
|
-
t.startLoading(e);
|
|
303
|
-
}, i = () => {
|
|
304
|
-
t.isLocalLoading(e) && t.stopLoading(e);
|
|
305
|
-
}, o = async (s) => {
|
|
306
|
-
n();
|
|
99
|
+
}));
|
|
100
|
+
function M() {
|
|
101
|
+
const n = E(), { actions: t } = w((r) => r), e = () => {
|
|
102
|
+
t.startLoading(n);
|
|
103
|
+
}, o = () => {
|
|
104
|
+
t.isLocalLoading(n) && t.stopLoading(n);
|
|
105
|
+
}, i = async (r) => {
|
|
106
|
+
e();
|
|
307
107
|
try {
|
|
308
|
-
return await
|
|
108
|
+
return await r;
|
|
309
109
|
} finally {
|
|
310
|
-
|
|
110
|
+
o();
|
|
311
111
|
}
|
|
312
112
|
};
|
|
313
113
|
return {
|
|
314
114
|
overrideLoading: t.overrideLoading,
|
|
315
|
-
startLoading:
|
|
316
|
-
stopLoading:
|
|
115
|
+
startLoading: e,
|
|
116
|
+
stopLoading: o,
|
|
317
117
|
get isLocalLoading() {
|
|
318
|
-
return t.isLocalLoading(
|
|
118
|
+
return t.isLocalLoading(n);
|
|
319
119
|
},
|
|
320
|
-
asyncUseLoading:
|
|
120
|
+
asyncUseLoading: i,
|
|
321
121
|
get isLoading() {
|
|
322
122
|
return t.isGlobalLoading();
|
|
323
123
|
}
|
|
324
124
|
};
|
|
325
125
|
}
|
|
326
|
-
const
|
|
126
|
+
const c = {
|
|
327
127
|
Spin: "spin",
|
|
328
128
|
FadeInOut: "fadeInOut",
|
|
329
129
|
None: "none"
|
|
330
|
-
},
|
|
331
|
-
const
|
|
332
|
-
return /* @__PURE__ */
|
|
333
|
-
},
|
|
334
|
-
function
|
|
335
|
-
loadingComponent:
|
|
130
|
+
}, z = ({ scale: n = 1, animationType: t = c.Spin, animationDuration: e, children: o, style: i, className: r, id: d, prefix: g }) => {
|
|
131
|
+
const b = t, s = t === c.Spin ? 1 : t === c.FadeInOut ? 2 : 0, a = e || s, l = t === c.Spin ? "linear" : t === c.FadeInOut ? "ease-in-out" : "linear", m = t === c.None ? "none" : `${g}-${b} ${a}s ${l} infinite`;
|
|
132
|
+
return /* @__PURE__ */ f("div", { style: { animation: m, ...n !== 1 ? { zoom: n } : {}, ...i }, className: r, id: d, children: o });
|
|
133
|
+
}, P = (n) => /* @__PURE__ */ f("div", { id: "loading-circle", style: { width: "90px", height: "90px", border: "15px solid #f3f3f3", borderTop: "15px solid #009b4bff", borderRadius: "50%", boxSizing: "border-box" }, ...n }), D = (n) => /* @__PURE__ */ f("div", { style: { padding: "20px", fontSize: "25px", color: "#333", fontFamily: "system-ui, sans-serif" }, ...n, children: "Please wait..." });
|
|
134
|
+
function B({
|
|
135
|
+
loadingComponent: n,
|
|
336
136
|
loadingComponentScale: t = 1,
|
|
337
|
-
animationType:
|
|
338
|
-
animationDuration:
|
|
339
|
-
wrapperStyle:
|
|
340
|
-
wrapperClassName:
|
|
341
|
-
wrapperId:
|
|
342
|
-
animationWrapperStyle:
|
|
343
|
-
animationWrapperClassName:
|
|
344
|
-
animationWrapperId:
|
|
137
|
+
animationType: e = c.Spin,
|
|
138
|
+
animationDuration: o,
|
|
139
|
+
wrapperStyle: i,
|
|
140
|
+
wrapperClassName: r,
|
|
141
|
+
wrapperId: d,
|
|
142
|
+
animationWrapperStyle: g,
|
|
143
|
+
animationWrapperClassName: b,
|
|
144
|
+
animationWrapperId: s
|
|
345
145
|
}) {
|
|
346
|
-
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}, [
|
|
351
|
-
const
|
|
352
|
-
return
|
|
353
|
-
/* @__PURE__ */
|
|
354
|
-
/* @__PURE__ */
|
|
355
|
-
dialog#${
|
|
146
|
+
n = n || (e === c.Spin ? P : D);
|
|
147
|
+
const a = v(Math.random().toString(36).substring(2, 6).replace(/[0-9]/g, "")), { loadingCount: l, overrideState: m } = w((I) => I), p = m ?? l > 0, h = v(null);
|
|
148
|
+
C(() => {
|
|
149
|
+
p ? (h.current?.showModal(), document.body.setAttribute("inert", "")) : (h.current?.close(), document.body.removeAttribute("inert"));
|
|
150
|
+
}, [p]);
|
|
151
|
+
const S = d || "loading-wrapper-" + a.current;
|
|
152
|
+
return p ? j(
|
|
153
|
+
/* @__PURE__ */ O($, { children: [
|
|
154
|
+
/* @__PURE__ */ f("style", { children: `
|
|
155
|
+
dialog#${S}[open] {
|
|
356
156
|
display: flex !important;
|
|
357
157
|
justify-content: center;
|
|
358
158
|
align-items: center;
|
|
@@ -361,55 +161,54 @@ function ot({
|
|
|
361
161
|
max-width: 100%;
|
|
362
162
|
max-height: 100%;
|
|
363
163
|
}
|
|
364
|
-
@keyframes ${
|
|
164
|
+
@keyframes ${a.current}-spin {
|
|
365
165
|
0% { transform: rotate(0deg); }
|
|
366
166
|
100% { transform: rotate(360deg); }
|
|
367
167
|
}
|
|
368
|
-
@keyframes ${
|
|
168
|
+
@keyframes ${a.current}-fadeInOut {
|
|
369
169
|
0%, 100% { opacity: 0.2; }
|
|
370
170
|
50% { opacity: 1; }
|
|
371
171
|
}
|
|
372
|
-
body:has(dialog#${
|
|
172
|
+
body:has(dialog#${S}[open]) {
|
|
373
173
|
overflow: hidden;
|
|
374
174
|
}
|
|
375
175
|
body {
|
|
376
176
|
scrollbar-gutter: stable;
|
|
377
177
|
}
|
|
378
178
|
` }),
|
|
379
|
-
/* @__PURE__ */
|
|
179
|
+
/* @__PURE__ */ f(
|
|
380
180
|
"dialog",
|
|
381
181
|
{
|
|
382
|
-
ref:
|
|
182
|
+
ref: h,
|
|
383
183
|
style: {
|
|
384
184
|
border: "none",
|
|
385
185
|
padding: 0,
|
|
386
186
|
backgroundColor: "rgba(0, 0, 0, 0.5)",
|
|
387
187
|
backdropFilter: "blur(2px)",
|
|
388
|
-
...
|
|
188
|
+
...i
|
|
389
189
|
},
|
|
390
|
-
className:
|
|
391
|
-
id:
|
|
392
|
-
children: /* @__PURE__ */
|
|
190
|
+
className: r,
|
|
191
|
+
id: S,
|
|
192
|
+
children: /* @__PURE__ */ f(z, { scale: t, animationType: e, animationDuration: o, style: g, className: b, id: s, prefix: a.current, children: L.isValidElement(n) ? n : L.createElement(n) })
|
|
393
193
|
}
|
|
394
194
|
)
|
|
395
195
|
] }),
|
|
396
196
|
document.body
|
|
397
197
|
) : null;
|
|
398
198
|
}
|
|
399
|
-
function
|
|
400
|
-
const { startLoading: t, stopLoading:
|
|
401
|
-
return
|
|
402
|
-
|
|
403
|
-
}), [
|
|
199
|
+
function U({ isLoading: n = !1 }) {
|
|
200
|
+
const { startLoading: t, stopLoading: e } = M();
|
|
201
|
+
return C(() => (n ? t() : e(), () => {
|
|
202
|
+
n && e();
|
|
203
|
+
}), [n]), null;
|
|
404
204
|
}
|
|
405
205
|
export {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
H as useLoading
|
|
206
|
+
c as AnimationType,
|
|
207
|
+
R as EventEmitter,
|
|
208
|
+
U as Loading,
|
|
209
|
+
P as LoadingCircle,
|
|
210
|
+
D as LoadingPleaseWait,
|
|
211
|
+
B as LoadingRenderer,
|
|
212
|
+
u as loadingEventTarget,
|
|
213
|
+
M as useLoading
|
|
415
214
|
};
|
|
@@ -22,21 +22,5 @@ interface Store {
|
|
|
22
22
|
isLocalLoading: (instanceId: string) => boolean;
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
-
export declare const loadingStore: import('zustand').UseBoundStore<
|
|
26
|
-
setState(partial: Store | Partial<Store> | ((state: Store) => Store | Partial<Store>), replace?: false | undefined, action?: (string | {
|
|
27
|
-
[x: string]: unknown;
|
|
28
|
-
[x: number]: unknown;
|
|
29
|
-
[x: symbol]: unknown;
|
|
30
|
-
type: string;
|
|
31
|
-
}) | undefined): void;
|
|
32
|
-
setState(state: Store | ((state: Store) => Store), replace: true, action?: (string | {
|
|
33
|
-
[x: string]: unknown;
|
|
34
|
-
[x: number]: unknown;
|
|
35
|
-
[x: symbol]: unknown;
|
|
36
|
-
type: string;
|
|
37
|
-
}) | undefined): void;
|
|
38
|
-
devtools: {
|
|
39
|
-
cleanup: () => void;
|
|
40
|
-
};
|
|
41
|
-
}>;
|
|
25
|
+
export declare const loadingStore: import('zustand').UseBoundStore<import('zustand').StoreApi<Store>>;
|
|
42
26
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokku-x/react-hook-loading-spinner",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "A lightweight and flexible React loading state hook library with built-in spinner components, global state management, and zero dependencies (except React and Zustand).",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -78,4 +78,4 @@
|
|
|
78
78
|
"dependencies": {
|
|
79
79
|
"zustand": "^5.0.10"
|
|
80
80
|
}
|
|
81
|
-
}
|
|
81
|
+
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export default function ssrLoading(): {
|
|
2
|
-
overrideLoading: (state: boolean | null) => void;
|
|
3
|
-
startLoading: () => void;
|
|
4
|
-
stopLoading: () => void;
|
|
5
|
-
readonly isLocalLoading: boolean;
|
|
6
|
-
asyncUseLoading: <R, _ extends any[]>(asyncFunction: Promise<R>) => Promise<R>;
|
|
7
|
-
readonly isLoading: boolean;
|
|
8
|
-
};
|
|
9
|
-
export * from '../store/loadingStore';
|