react-alp-loading-bar 6.1.1 → 8.0.0
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/CHANGELOG.md +31 -0
- package/README.md +6 -6
- package/dist/definitions/index.d.ts +4 -5
- package/dist/definitions/index.d.ts.map +1 -1
- package/dist/index-browser.es.js +15 -20
- package/dist/index-browser.es.js.map +1 -1
- package/dist/{index-node18.mjs → index-node20.mjs} +11 -13
- package/dist/index-node20.mjs.map +1 -0
- package/package.json +19 -20
- package/src/index.tsx +13 -17
- package/dist/index-node18.mjs.map +0 -1
- package/src/.eslintrc.json +0 -31
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,37 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [8.0.0](https://github.com/christophehurpeau/alp/compare/react-alp-loading-bar@7.0.0...react-alp-loading-bar@8.0.0) (2025-08-02)
|
|
7
|
+
|
|
8
|
+
### ⚠ BREAKING CHANGES
|
|
9
|
+
|
|
10
|
+
* update dependencies and drop node 20
|
|
11
|
+
* update dev dependencies, replace parse-json-object-as-map with native JSON.parse, update koa
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* update babel ([a4bf455](https://github.com/christophehurpeau/alp/commit/a4bf455c715134973c56693b7425aa05de5b10f9))
|
|
16
|
+
* update dependencies and drop node 20 ([fc5b322](https://github.com/christophehurpeau/alp/commit/fc5b322e076e9a3c7c4a235d16734b89fd85e211))
|
|
17
|
+
* update dev dependencies, replace parse-json-object-as-map with native JSON.parse, update koa ([5ae7723](https://github.com/christophehurpeau/alp/commit/5ae77238cafc573fe72c5eb63b103802b8b2e537))
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* **deps:** update react monorepo ([#611](https://github.com/christophehurpeau/alp/issues/611)) ([b9ece4d](https://github.com/christophehurpeau/alp/commit/b9ece4dc070bcd49fa6c4a40534162d10087405b))
|
|
22
|
+
|
|
23
|
+
## [7.0.0](https://github.com/christophehurpeau/alp/compare/react-alp-loading-bar@6.1.1...react-alp-loading-bar@7.0.0) (2024-01-06)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### ⚠ BREAKING CHANGES
|
|
27
|
+
|
|
28
|
+
* merge to alp-node to improve maintenability, remove alp-types
|
|
29
|
+
|
|
30
|
+
### Features
|
|
31
|
+
|
|
32
|
+
* merge to alp-node to improve maintenability, remove alp-types ([ead9a2f](https://github.com/christophehurpeau/alp/commit/ead9a2fd1bcbedce0be29ea0e444c5cead99c64d))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
6
37
|
## [6.1.1](https://github.com/christophehurpeau/alp/compare/react-alp-loading-bar@6.1.0...react-alp-loading-bar@6.1.1) (2024-01-06)
|
|
7
38
|
|
|
8
39
|
Note: no notable changes
|
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
<
|
|
1
|
+
<h1 align="center">
|
|
2
2
|
react-alp-loading-bar
|
|
3
|
-
</
|
|
3
|
+
</h1>
|
|
4
4
|
|
|
5
5
|
<p align="center">
|
|
6
6
|
loading bar
|
|
7
7
|
</p>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/npm/v/react-alp-loading-bar.svg?style=flat-square"></a>
|
|
11
|
-
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/npm/dw/react-alp-loading-bar.svg?style=flat-square"></a>
|
|
12
|
-
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/node/v/react-alp-loading-bar.svg?style=flat-square"></a>
|
|
13
|
-
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/npm/types/react-alp-loading-bar.svg?style=flat-square"></a>
|
|
10
|
+
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/npm/v/react-alp-loading-bar.svg?style=flat-square" alt="npm version"></a>
|
|
11
|
+
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/npm/dw/react-alp-loading-bar.svg?style=flat-square" alt="npm downloads"></a>
|
|
12
|
+
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/node/v/react-alp-loading-bar.svg?style=flat-square" alt="node version"></a>
|
|
13
|
+
<a href="https://npmjs.org/package/react-alp-loading-bar"><img src="https://img.shields.io/npm/types/react-alp-loading-bar.svg?style=flat-square" alt="types"></a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
16
|
## Install
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { ReactElement } from
|
|
2
|
-
import { PureComponent } from
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { PureComponent } from "react";
|
|
3
3
|
interface LoadingBarProps {
|
|
4
4
|
LoadingBarComponent: React.ComponentType<{
|
|
5
5
|
progress: number;
|
|
6
6
|
}>;
|
|
7
|
+
websocket: WebsocketInterface;
|
|
7
8
|
}
|
|
8
9
|
interface LoadingBarState {
|
|
9
10
|
loading: boolean;
|
|
@@ -12,10 +13,9 @@ interface LoadingBarState {
|
|
|
12
13
|
}
|
|
13
14
|
interface WebsocketInterface {
|
|
14
15
|
isConnected: () => boolean;
|
|
15
|
-
on: (event:
|
|
16
|
+
on: (event: "connect" | "disconnect", callback: () => unknown) => void;
|
|
16
17
|
}
|
|
17
18
|
export default class LoadingBar extends PureComponent<LoadingBarProps, LoadingBarState> {
|
|
18
|
-
static contextType: import("react").Context<import("../../alp-types/lib").Context>;
|
|
19
19
|
state: {
|
|
20
20
|
loading: boolean;
|
|
21
21
|
hidden: boolean;
|
|
@@ -28,7 +28,6 @@ export default class LoadingBar extends PureComponent<LoadingBarProps, LoadingBa
|
|
|
28
28
|
componentDidMount(): void;
|
|
29
29
|
componentDidUpdate(prevProps: LoadingBarProps, prevState: LoadingBarState): void;
|
|
30
30
|
componentWillUnmount(): void;
|
|
31
|
-
getWebsocket(): WebsocketInterface;
|
|
32
31
|
private showBar;
|
|
33
32
|
private hideBar;
|
|
34
33
|
render(): ReactElement;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAmCtC,UAAU,eAAe;IACvB,mBAAmB,EAAE,KAAK,CAAC,aAAa,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC/D,SAAS,EAAE,kBAAkB,CAAC;CAC/B;AAED,UAAU,eAAe;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,UAAU,kBAAkB;IAC1B,WAAW,EAAE,MAAM,OAAO,CAAC;IAC3B,EAAE,EAAE,CAAC,KAAK,EAAE,SAAS,GAAG,YAAY,EAAE,QAAQ,EAAE,MAAM,OAAO,KAAK,IAAI,CAAC;CACxE;AAED,MAAM,CAAC,OAAO,OAAO,UAAW,SAAQ,aAAa,CACnD,eAAe,EACf,eAAe,CAChB;IACU,KAAK;;;;MAIZ;IAEF,cAAc,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;IAE/C,YAAY,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;IAE7C,cAAc,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;IAE/C,aAAa,CAAC,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;IAErC,iBAAiB,IAAI,IAAI;IAgBzB,kBAAkB,CACzB,SAAS,EAAE,eAAe,EAC1B,SAAS,EAAE,eAAe,GACzB,IAAI;IAaE,oBAAoB,IAAI,IAAI;IAOrC,OAAO,CAAC,OAAO;IAgBf,OAAO,CAAC,OAAO;IAkBN,MAAM,IAAI,YAAY;CAmBhC"}
|
package/dist/index-browser.es.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { PureComponent } from 'react';
|
|
2
|
-
import ReactAlpContext from 'react-alp-context';
|
|
3
2
|
import { jsx } from 'react/jsx-runtime';
|
|
4
3
|
|
|
4
|
+
/* eslint-disable react/destructuring-assignment */
|
|
5
|
+
|
|
5
6
|
const random = () => Math.ceil(Math.random() * 100) / 100;
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -16,29 +17,25 @@ const calculatePercent = percent => {
|
|
|
16
17
|
if (percent < 70) return percent + random() * 10 + 3;else if (percent < 80) return percent + random() + 5;else if (percent < 90) return percent + random() + 1;else if (percent < 95) return percent + 0.1;else return percent;
|
|
17
18
|
};
|
|
18
19
|
class LoadingBar extends PureComponent {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
progress: 1
|
|
25
|
-
};
|
|
26
|
-
}
|
|
20
|
+
state = {
|
|
21
|
+
loading: true,
|
|
22
|
+
hidden: true,
|
|
23
|
+
progress: 1
|
|
24
|
+
};
|
|
27
25
|
componentDidMount() {
|
|
28
|
-
|
|
29
|
-
if (websocket.isConnected()) {
|
|
26
|
+
if (this.props.websocket.isConnected()) {
|
|
30
27
|
this.setState(prevState => ({
|
|
31
28
|
loading: false,
|
|
32
29
|
progress: 100,
|
|
33
30
|
hidden: prevState.hidden || prevState.progress === 100
|
|
34
31
|
}));
|
|
35
32
|
}
|
|
36
|
-
websocket.on(
|
|
33
|
+
this.props.websocket.on("connect", () => {
|
|
37
34
|
this.setState({
|
|
38
35
|
loading: false
|
|
39
36
|
});
|
|
40
37
|
});
|
|
41
|
-
websocket.on(
|
|
38
|
+
this.props.websocket.on("disconnect", () => {
|
|
42
39
|
this.setState({
|
|
43
40
|
loading: true,
|
|
44
41
|
progress: 1,
|
|
@@ -47,6 +44,9 @@ class LoadingBar extends PureComponent {
|
|
|
47
44
|
});
|
|
48
45
|
}
|
|
49
46
|
componentDidUpdate(prevProps, prevState) {
|
|
47
|
+
if (this.props.websocket !== prevProps.websocket) {
|
|
48
|
+
throw new Error("Unsupported at the moment");
|
|
49
|
+
}
|
|
50
50
|
if (this.state.loading !== prevState.loading) {
|
|
51
51
|
if (this.state.loading) {
|
|
52
52
|
this.showBar();
|
|
@@ -61,10 +61,6 @@ class LoadingBar extends PureComponent {
|
|
|
61
61
|
if (this.first20Timeout) clearTimeout(this.first20Timeout);
|
|
62
62
|
if (this.progressTimer) clearInterval(this.progressTimer);
|
|
63
63
|
}
|
|
64
|
-
getWebsocket() {
|
|
65
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
|
|
66
|
-
return this.context.app.websocket;
|
|
67
|
-
}
|
|
68
64
|
showBar() {
|
|
69
65
|
if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);
|
|
70
66
|
if (this.resetTimeout) clearTimeout(this.resetTimeout);
|
|
@@ -102,12 +98,12 @@ class LoadingBar extends PureComponent {
|
|
|
102
98
|
return /*#__PURE__*/jsx("div", {
|
|
103
99
|
hidden: this.state.hidden,
|
|
104
100
|
style: {
|
|
105
|
-
position:
|
|
101
|
+
position: "fixed",
|
|
106
102
|
top: 0,
|
|
107
103
|
left: 0,
|
|
108
104
|
right: 0,
|
|
109
105
|
zIndex: 4,
|
|
110
|
-
pointerEvents:
|
|
106
|
+
pointerEvents: "none"
|
|
111
107
|
},
|
|
112
108
|
children: /*#__PURE__*/jsx(LoadingBarComponent, {
|
|
113
109
|
progress: this.state.progress
|
|
@@ -115,7 +111,6 @@ class LoadingBar extends PureComponent {
|
|
|
115
111
|
});
|
|
116
112
|
}
|
|
117
113
|
}
|
|
118
|
-
LoadingBar.contextType = ReactAlpContext;
|
|
119
114
|
|
|
120
115
|
export { LoadingBar as default };
|
|
121
116
|
//# sourceMappingURL=index-browser.es.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-browser.es.js","sources":["../src/index.tsx"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index-browser.es.js","sources":["../src/index.tsx"],"sourcesContent":["/* eslint-disable react/destructuring-assignment */\nimport type { ReactElement } from \"react\";\nimport { PureComponent } from \"react\";\n\n/*\nExample with antd:\nimport { Progress } from 'antd';\n\nconst LoadingBarComponent = ({ progress }) => (\n <Progress\n type=\"line\"\n status=\"active\"\n percent={progress}\n showInfo={false}\n />\n);\n*/\n\n/* number between 0 and 1 */\nconst random = (): number => Math.ceil(Math.random() * 100) / 100;\n\n/**\n * around:\n * at 100ms 20%\n * at 1s 40%\n * at 2s 60%\n * at 3s 80%\n */\nconst calculatePercent = (percent: number): number => {\n if (percent < 60) return percent + random() * 10 + 5;\n if (percent < 70) return percent + random() * 10 + 3;\n else if (percent < 80) return percent + random() + 5;\n else if (percent < 90) return percent + random() + 1;\n else if (percent < 95) return percent + 0.1;\n else return percent;\n};\n\ninterface LoadingBarProps {\n LoadingBarComponent: React.ComponentType<{ progress: number }>;\n websocket: WebsocketInterface;\n}\n\ninterface LoadingBarState {\n loading: boolean;\n hidden: boolean;\n progress: number;\n}\n\ninterface WebsocketInterface {\n isConnected: () => boolean;\n on: (event: \"connect\" | \"disconnect\", callback: () => unknown) => void;\n}\n\nexport default class LoadingBar extends PureComponent<\n LoadingBarProps,\n LoadingBarState\n> {\n override state = {\n loading: true,\n hidden: true,\n progress: 1,\n };\n\n fadeOffTimeout?: ReturnType<typeof setTimeout>;\n\n resetTimeout?: ReturnType<typeof setTimeout>;\n\n first20Timeout?: ReturnType<typeof setTimeout>;\n\n progressTimer?: ReturnType<typeof setTimeout>;\n\n override componentDidMount(): void {\n if (this.props.websocket.isConnected()) {\n this.setState((prevState) => ({\n loading: false,\n progress: 100,\n hidden: prevState.hidden || prevState.progress === 100,\n }));\n }\n this.props.websocket.on(\"connect\", () => {\n this.setState({ loading: false });\n });\n this.props.websocket.on(\"disconnect\", () => {\n this.setState({ loading: true, progress: 1, hidden: false });\n });\n }\n\n override componentDidUpdate(\n prevProps: LoadingBarProps,\n prevState: LoadingBarState,\n ): void {\n if (this.props.websocket !== prevProps.websocket) {\n throw new Error(\"Unsupported at the moment\");\n }\n if (this.state.loading !== prevState.loading) {\n if (this.state.loading) {\n this.showBar();\n } else {\n this.hideBar();\n }\n }\n }\n\n override componentWillUnmount(): void {\n if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);\n if (this.resetTimeout) clearTimeout(this.resetTimeout);\n if (this.first20Timeout) clearTimeout(this.first20Timeout);\n if (this.progressTimer) clearInterval(this.progressTimer);\n }\n\n private showBar(): void {\n if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);\n if (this.resetTimeout) clearTimeout(this.resetTimeout);\n\n this.first20Timeout = setTimeout(() => {\n this.setState({ progress: 20 });\n }, 100);\n\n this.progressTimer = setInterval(() => {\n this.setState((prevState) => {\n const newValue = calculatePercent(prevState.progress);\n return { progress: newValue };\n });\n }, 500);\n }\n\n private hideBar(): void {\n if (this.first20Timeout) clearTimeout(this.first20Timeout);\n if (this.progressTimer) clearInterval(this.progressTimer);\n\n this.fadeOffTimeout = setTimeout(() => {\n this.setState({\n progress: 100,\n });\n }, 500);\n\n this.resetTimeout = setTimeout(() => {\n this.setState({\n hidden: true,\n progress: 1,\n });\n }, 1000);\n }\n\n override render(): ReactElement {\n const LoadingBarComponent = this.props.LoadingBarComponent;\n\n return (\n <div\n hidden={this.state.hidden}\n style={{\n position: \"fixed\",\n top: 0,\n left: 0,\n right: 0,\n zIndex: 4,\n pointerEvents: \"none\",\n }}\n >\n <LoadingBarComponent progress={this.state.progress} />\n </div>\n );\n }\n}\n"],"names":["random","Math","ceil","calculatePercent","percent","LoadingBar","PureComponent","state","loading","hidden","progress","componentDidMount","props","websocket","isConnected","setState","prevState","on","componentDidUpdate","prevProps","Error","showBar","hideBar","componentWillUnmount","fadeOffTimeout","clearTimeout","resetTimeout","first20Timeout","progressTimer","clearInterval","setTimeout","setInterval","newValue","render","LoadingBarComponent","_jsx","style","position","top","left","right","zIndex","pointerEvents","children"],"mappings":";;;AAAA;;AAmBA,MAAMA,MAAM,GAAGA,MAAcC,IAAI,CAACC,IAAI,CAACD,IAAI,CAACD,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,GAAG;;AAEjE;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMG,gBAAgB,GAAIC,OAAe,IAAa;AACpD,EAAA,IAAIA,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC;EACpD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAG,GAAG,CAAC,KACvC,OAAOA,OAAO;AACrB,CAAC;AAkBc,MAAMC,UAAU,SAASC,aAAa,CAGnD;AACSC,EAAAA,KAAK,GAAG;AACfC,IAAAA,OAAO,EAAE,IAAI;AACbC,IAAAA,MAAM,EAAE,IAAI;AACZC,IAAAA,QAAQ,EAAE;GACX;AAUQC,EAAAA,iBAAiBA,GAAS;IACjC,IAAI,IAAI,CAACC,KAAK,CAACC,SAAS,CAACC,WAAW,EAAE,EAAE;AACtC,MAAA,IAAI,CAACC,QAAQ,CAAEC,SAAS,KAAM;A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MA,GAAiB;AAC9B,IAAA,MAAMC,mBAAmB,GAAG,IAAI,CAACtB,KAAK,CAACsB,mBAAmB;AAE1D,IAAA,oBACEC,GAAA,CAAA,KAAA,EAAA;AACE1B,MAAAA,MAAM,EAAE,IAAI,CAACF,KAAK,CAACE,MAAO;AAC1B2B,MAAAA,KAAK,EAAE;AACLC,QAAAA,QAAQ,EAAE,OAAO;AACjBC,QAAAA,GAAG,EAAE,CAAC;AACNC,QAAAA,IAAI,EAAE,CAAC;AACPC,QAAAA,KAAK,EAAE,CAAC;AACRC,QAAAA,MAAM,EAAE,CAAC;AACTC,QAAAA,aAAa,EAAE;OACf;MAAAC,QAAA,eAEFR,GAAA,CAACD,mBAAmB,EAAA;AAACxB,QAAAA,QAAQ,EAAE,IAAI,CAACH,KAAK,CAACG;OAAW;AAAC,KACnD,CAAC;AAEV;AACF;;;;"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { PureComponent } from 'react';
|
|
2
|
-
import ReactAlpContext from 'react-alp-context';
|
|
3
2
|
import { jsx } from 'react/jsx-runtime';
|
|
4
3
|
|
|
4
|
+
/* eslint-disable react/destructuring-assignment */
|
|
5
|
+
|
|
5
6
|
const random = () => Math.ceil(Math.random() * 100) / 100;
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -16,27 +17,25 @@ const calculatePercent = percent => {
|
|
|
16
17
|
if (percent < 70) return percent + random() * 10 + 3;else if (percent < 80) return percent + random() + 5;else if (percent < 90) return percent + random() + 1;else if (percent < 95) return percent + 0.1;else return percent;
|
|
17
18
|
};
|
|
18
19
|
class LoadingBar extends PureComponent {
|
|
19
|
-
static contextType = ReactAlpContext;
|
|
20
20
|
state = {
|
|
21
21
|
loading: true,
|
|
22
22
|
hidden: true,
|
|
23
23
|
progress: 1
|
|
24
24
|
};
|
|
25
25
|
componentDidMount() {
|
|
26
|
-
|
|
27
|
-
if (websocket.isConnected()) {
|
|
26
|
+
if (this.props.websocket.isConnected()) {
|
|
28
27
|
this.setState(prevState => ({
|
|
29
28
|
loading: false,
|
|
30
29
|
progress: 100,
|
|
31
30
|
hidden: prevState.hidden || prevState.progress === 100
|
|
32
31
|
}));
|
|
33
32
|
}
|
|
34
|
-
websocket.on(
|
|
33
|
+
this.props.websocket.on("connect", () => {
|
|
35
34
|
this.setState({
|
|
36
35
|
loading: false
|
|
37
36
|
});
|
|
38
37
|
});
|
|
39
|
-
websocket.on(
|
|
38
|
+
this.props.websocket.on("disconnect", () => {
|
|
40
39
|
this.setState({
|
|
41
40
|
loading: true,
|
|
42
41
|
progress: 1,
|
|
@@ -45,6 +44,9 @@ class LoadingBar extends PureComponent {
|
|
|
45
44
|
});
|
|
46
45
|
}
|
|
47
46
|
componentDidUpdate(prevProps, prevState) {
|
|
47
|
+
if (this.props.websocket !== prevProps.websocket) {
|
|
48
|
+
throw new Error("Unsupported at the moment");
|
|
49
|
+
}
|
|
48
50
|
if (this.state.loading !== prevState.loading) {
|
|
49
51
|
if (this.state.loading) {
|
|
50
52
|
this.showBar();
|
|
@@ -59,10 +61,6 @@ class LoadingBar extends PureComponent {
|
|
|
59
61
|
if (this.first20Timeout) clearTimeout(this.first20Timeout);
|
|
60
62
|
if (this.progressTimer) clearInterval(this.progressTimer);
|
|
61
63
|
}
|
|
62
|
-
getWebsocket() {
|
|
63
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
|
|
64
|
-
return this.context.app.websocket;
|
|
65
|
-
}
|
|
66
64
|
showBar() {
|
|
67
65
|
if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);
|
|
68
66
|
if (this.resetTimeout) clearTimeout(this.resetTimeout);
|
|
@@ -100,12 +98,12 @@ class LoadingBar extends PureComponent {
|
|
|
100
98
|
return /*#__PURE__*/jsx("div", {
|
|
101
99
|
hidden: this.state.hidden,
|
|
102
100
|
style: {
|
|
103
|
-
position:
|
|
101
|
+
position: "fixed",
|
|
104
102
|
top: 0,
|
|
105
103
|
left: 0,
|
|
106
104
|
right: 0,
|
|
107
105
|
zIndex: 4,
|
|
108
|
-
pointerEvents:
|
|
106
|
+
pointerEvents: "none"
|
|
109
107
|
},
|
|
110
108
|
children: /*#__PURE__*/jsx(LoadingBarComponent, {
|
|
111
109
|
progress: this.state.progress
|
|
@@ -115,4 +113,4 @@ class LoadingBar extends PureComponent {
|
|
|
115
113
|
}
|
|
116
114
|
|
|
117
115
|
export { LoadingBar as default };
|
|
118
|
-
//# sourceMappingURL=index-
|
|
116
|
+
//# sourceMappingURL=index-node20.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-node20.mjs","sources":["../src/index.tsx"],"sourcesContent":["/* eslint-disable react/destructuring-assignment */\nimport type { ReactElement } from \"react\";\nimport { PureComponent } from \"react\";\n\n/*\nExample with antd:\nimport { Progress } from 'antd';\n\nconst LoadingBarComponent = ({ progress }) => (\n <Progress\n type=\"line\"\n status=\"active\"\n percent={progress}\n showInfo={false}\n />\n);\n*/\n\n/* number between 0 and 1 */\nconst random = (): number => Math.ceil(Math.random() * 100) / 100;\n\n/**\n * around:\n * at 100ms 20%\n * at 1s 40%\n * at 2s 60%\n * at 3s 80%\n */\nconst calculatePercent = (percent: number): number => {\n if (percent < 60) return percent + random() * 10 + 5;\n if (percent < 70) return percent + random() * 10 + 3;\n else if (percent < 80) return percent + random() + 5;\n else if (percent < 90) return percent + random() + 1;\n else if (percent < 95) return percent + 0.1;\n else return percent;\n};\n\ninterface LoadingBarProps {\n LoadingBarComponent: React.ComponentType<{ progress: number }>;\n websocket: WebsocketInterface;\n}\n\ninterface LoadingBarState {\n loading: boolean;\n hidden: boolean;\n progress: number;\n}\n\ninterface WebsocketInterface {\n isConnected: () => boolean;\n on: (event: \"connect\" | \"disconnect\", callback: () => unknown) => void;\n}\n\nexport default class LoadingBar extends PureComponent<\n LoadingBarProps,\n LoadingBarState\n> {\n override state = {\n loading: true,\n hidden: true,\n progress: 1,\n };\n\n fadeOffTimeout?: ReturnType<typeof setTimeout>;\n\n resetTimeout?: ReturnType<typeof setTimeout>;\n\n first20Timeout?: ReturnType<typeof setTimeout>;\n\n progressTimer?: ReturnType<typeof setTimeout>;\n\n override componentDidMount(): void {\n if (this.props.websocket.isConnected()) {\n this.setState((prevState) => ({\n loading: false,\n progress: 100,\n hidden: prevState.hidden || prevState.progress === 100,\n }));\n }\n this.props.websocket.on(\"connect\", () => {\n this.setState({ loading: false });\n });\n this.props.websocket.on(\"disconnect\", () => {\n this.setState({ loading: true, progress: 1, hidden: false });\n });\n }\n\n override componentDidUpdate(\n prevProps: LoadingBarProps,\n prevState: LoadingBarState,\n ): void {\n if (this.props.websocket !== prevProps.websocket) {\n throw new Error(\"Unsupported at the moment\");\n }\n if (this.state.loading !== prevState.loading) {\n if (this.state.loading) {\n this.showBar();\n } else {\n this.hideBar();\n }\n }\n }\n\n override componentWillUnmount(): void {\n if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);\n if (this.resetTimeout) clearTimeout(this.resetTimeout);\n if (this.first20Timeout) clearTimeout(this.first20Timeout);\n if (this.progressTimer) clearInterval(this.progressTimer);\n }\n\n private showBar(): void {\n if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);\n if (this.resetTimeout) clearTimeout(this.resetTimeout);\n\n this.first20Timeout = setTimeout(() => {\n this.setState({ progress: 20 });\n }, 100);\n\n this.progressTimer = setInterval(() => {\n this.setState((prevState) => {\n const newValue = calculatePercent(prevState.progress);\n return { progress: newValue };\n });\n }, 500);\n }\n\n private hideBar(): void {\n if (this.first20Timeout) clearTimeout(this.first20Timeout);\n if (this.progressTimer) clearInterval(this.progressTimer);\n\n this.fadeOffTimeout = setTimeout(() => {\n this.setState({\n progress: 100,\n });\n }, 500);\n\n this.resetTimeout = setTimeout(() => {\n this.setState({\n hidden: true,\n progress: 1,\n });\n }, 1000);\n }\n\n override render(): ReactElement {\n const LoadingBarComponent = this.props.LoadingBarComponent;\n\n return (\n <div\n hidden={this.state.hidden}\n style={{\n position: \"fixed\",\n top: 0,\n left: 0,\n right: 0,\n zIndex: 4,\n pointerEvents: \"none\",\n }}\n >\n <LoadingBarComponent progress={this.state.progress} />\n </div>\n );\n }\n}\n"],"names":["random","Math","ceil","calculatePercent","percent","LoadingBar","PureComponent","state","loading","hidden","progress","componentDidMount","props","websocket","isConnected","setState","prevState","on","componentDidUpdate","prevProps","Error","showBar","hideBar","componentWillUnmount","fadeOffTimeout","clearTimeout","resetTimeout","first20Timeout","progressTimer","clearInterval","setTimeout","setInterval","newValue","render","LoadingBarComponent","_jsx","style","position","top","left","right","zIndex","pointerEvents","children"],"mappings":";;;AAAA;;AAmBA,MAAMA,MAAM,GAAGA,MAAcC,IAAI,CAACC,IAAI,CAACD,IAAI,CAACD,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,GAAG;;AAEjE;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMG,gBAAgB,GAAIC,OAAe,IAAa;AACpD,EAAA,IAAIA,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC;EACpD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAG,GAAG,CAAC,KACvC,OAAOA,OAAO;AACrB,CAAC;AAkBc,MAAMC,UAAU,SAASC,aAAa,CAGnD;AACSC,EAAAA,KAAK,GAAG;AACfC,IAAAA,OAAO,EAAE,IAAI;AACbC,IAAAA,MAAM,EAAE,IAAI;AACZC,IAAAA,QAAQ,EAAE;GACX;AAUQC,EAAAA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QAAQ,CAAC;AACZN,QAAAA,MAAM,EAAE,IAAI;AACZC,QAAAA,QAAQ,EAAE;AACZ,OAAC,CAAC;KACH,EAAE,IAAI,CAAC;AACV;AAESuB,EAAAA,MAAMA,GAAiB;AAC9B,IAAA,MAAMC,mBAAmB,GAAG,IAAI,CAACtB,KAAK,CAACsB,mBAAmB;AAE1D,IAAA,oBACEC,GAAA,CAAA,KAAA,EAAA;AACE1B,MAAAA,MAAM,EAAE,IAAI,CAACF,KAAK,CAACE,MAAO;AAC1B2B,MAAAA,KAAK,EAAE;AACLC,QAAAA,QAAQ,EAAE,OAAO;AACjBC,QAAAA,GAAG,EAAE,CAAC;AACNC,QAAAA,IAAI,EAAE,CAAC;AACPC,QAAAA,KAAK,EAAE,CAAC;AACRC,QAAAA,MAAM,EAAE,CAAC;AACTC,QAAAA,aAAa,EAAE;OACf;MAAAC,QAAA,eAEFR,GAAA,CAACD,mBAAmB,EAAA;AAACxB,QAAAA,QAAQ,EAAE,IAAI,CAACH,KAAK,CAACG;OAAW;AAAC,KACnD,CAAC;AAEV;AACF;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-alp-loading-bar",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "loading bar",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "Christophe Hurpeau <christophe@hurpeau.com> (https://christophe.hurpeau.com)",
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"homepage": "https://github.com/christophehurpeau/alp",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
16
|
+
"node": ">=20.11.0"
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
|
-
"main": "./dist/index-
|
|
19
|
+
"main": "./dist/index-node20.mjs",
|
|
20
20
|
"types": "./dist/definitions/index.d.ts",
|
|
21
21
|
"module": "./dist/index-browser.es.js",
|
|
22
22
|
"browser": "./dist/index-browser.es.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
".": {
|
|
26
26
|
"types": "./dist/definitions/index.d.ts",
|
|
27
27
|
"node": {
|
|
28
|
-
"import": "./dist/index-
|
|
28
|
+
"import": "./dist/index-node20.mjs"
|
|
29
29
|
},
|
|
30
30
|
"browser": {
|
|
31
31
|
"import": "./dist/index-browser.es.js"
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"clean": "yarn clean:build",
|
|
43
43
|
"clean:build": "pob-babel-clean-out dist",
|
|
44
44
|
"lint": "yarn run lint:eslint",
|
|
45
|
-
"lint:eslint": "yarn ../.. run eslint --
|
|
45
|
+
"lint:eslint": "yarn ../.. run eslint --quiet packages/react-alp-loading-bar",
|
|
46
46
|
"watch": "yarn clean:build && rollup --config rollup.config.mjs --watch"
|
|
47
47
|
},
|
|
48
48
|
"browserslist": {
|
|
@@ -55,35 +55,34 @@
|
|
|
55
55
|
]
|
|
56
56
|
},
|
|
57
57
|
"pob": {
|
|
58
|
-
"
|
|
58
|
+
"bundler": "rollup-babel",
|
|
59
|
+
"entries": [
|
|
60
|
+
"index"
|
|
61
|
+
],
|
|
62
|
+
"envs": [
|
|
59
63
|
{
|
|
60
64
|
"target": "node",
|
|
61
|
-
"version": "
|
|
65
|
+
"version": "20"
|
|
62
66
|
},
|
|
63
67
|
{
|
|
64
68
|
"target": "browser"
|
|
65
69
|
}
|
|
66
70
|
],
|
|
67
|
-
"entries": [
|
|
68
|
-
"index"
|
|
69
|
-
],
|
|
70
71
|
"jsx": true
|
|
71
72
|
},
|
|
72
73
|
"prettier": "@pob/root/prettier-config",
|
|
73
74
|
"peerDependencies": {
|
|
74
|
-
"react": "^
|
|
75
|
+
"react": "^19.0.0"
|
|
75
76
|
},
|
|
76
77
|
"dependencies": {
|
|
77
|
-
"@babel/runtime": "^7.
|
|
78
|
-
"alp-types": "3.1.1",
|
|
79
|
-
"react-alp-context": "6.1.1"
|
|
78
|
+
"@babel/runtime": "^7.28.2"
|
|
80
79
|
},
|
|
81
80
|
"devDependencies": {
|
|
82
|
-
"@babel/core": "7.
|
|
83
|
-
"@babel/preset-env": "7.
|
|
84
|
-
"@babel/preset-react": "7.
|
|
85
|
-
"pob-babel": "
|
|
86
|
-
"react": "
|
|
87
|
-
"typescript": "5.
|
|
81
|
+
"@babel/core": "7.28.0",
|
|
82
|
+
"@babel/preset-env": "7.28.0",
|
|
83
|
+
"@babel/preset-react": "7.27.1",
|
|
84
|
+
"pob-babel": "43.7.0",
|
|
85
|
+
"react": "19.1.1",
|
|
86
|
+
"typescript": "5.8.3"
|
|
88
87
|
}
|
|
89
88
|
}
|
package/src/index.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
/* eslint-disable react/destructuring-assignment */
|
|
2
|
+
import type { ReactElement } from "react";
|
|
3
|
+
import { PureComponent } from "react";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
6
|
Example with antd:
|
|
@@ -37,6 +37,7 @@ const calculatePercent = (percent: number): number => {
|
|
|
37
37
|
|
|
38
38
|
interface LoadingBarProps {
|
|
39
39
|
LoadingBarComponent: React.ComponentType<{ progress: number }>;
|
|
40
|
+
websocket: WebsocketInterface;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
interface LoadingBarState {
|
|
@@ -47,15 +48,13 @@ interface LoadingBarState {
|
|
|
47
48
|
|
|
48
49
|
interface WebsocketInterface {
|
|
49
50
|
isConnected: () => boolean;
|
|
50
|
-
on: (event:
|
|
51
|
+
on: (event: "connect" | "disconnect", callback: () => unknown) => void;
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
export default class LoadingBar extends PureComponent<
|
|
54
55
|
LoadingBarProps,
|
|
55
56
|
LoadingBarState
|
|
56
57
|
> {
|
|
57
|
-
static override contextType = ReactAlpContext;
|
|
58
|
-
|
|
59
58
|
override state = {
|
|
60
59
|
loading: true,
|
|
61
60
|
hidden: true,
|
|
@@ -71,18 +70,17 @@ export default class LoadingBar extends PureComponent<
|
|
|
71
70
|
progressTimer?: ReturnType<typeof setTimeout>;
|
|
72
71
|
|
|
73
72
|
override componentDidMount(): void {
|
|
74
|
-
|
|
75
|
-
if (websocket.isConnected()) {
|
|
73
|
+
if (this.props.websocket.isConnected()) {
|
|
76
74
|
this.setState((prevState) => ({
|
|
77
75
|
loading: false,
|
|
78
76
|
progress: 100,
|
|
79
77
|
hidden: prevState.hidden || prevState.progress === 100,
|
|
80
78
|
}));
|
|
81
79
|
}
|
|
82
|
-
websocket.on(
|
|
80
|
+
this.props.websocket.on("connect", () => {
|
|
83
81
|
this.setState({ loading: false });
|
|
84
82
|
});
|
|
85
|
-
websocket.on(
|
|
83
|
+
this.props.websocket.on("disconnect", () => {
|
|
86
84
|
this.setState({ loading: true, progress: 1, hidden: false });
|
|
87
85
|
});
|
|
88
86
|
}
|
|
@@ -91,6 +89,9 @@ export default class LoadingBar extends PureComponent<
|
|
|
91
89
|
prevProps: LoadingBarProps,
|
|
92
90
|
prevState: LoadingBarState,
|
|
93
91
|
): void {
|
|
92
|
+
if (this.props.websocket !== prevProps.websocket) {
|
|
93
|
+
throw new Error("Unsupported at the moment");
|
|
94
|
+
}
|
|
94
95
|
if (this.state.loading !== prevState.loading) {
|
|
95
96
|
if (this.state.loading) {
|
|
96
97
|
this.showBar();
|
|
@@ -107,11 +108,6 @@ export default class LoadingBar extends PureComponent<
|
|
|
107
108
|
if (this.progressTimer) clearInterval(this.progressTimer);
|
|
108
109
|
}
|
|
109
110
|
|
|
110
|
-
getWebsocket(): WebsocketInterface {
|
|
111
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
|
|
112
|
-
return (this.context as any).app.websocket;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
111
|
private showBar(): void {
|
|
116
112
|
if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);
|
|
117
113
|
if (this.resetTimeout) clearTimeout(this.resetTimeout);
|
|
@@ -153,12 +149,12 @@ export default class LoadingBar extends PureComponent<
|
|
|
153
149
|
<div
|
|
154
150
|
hidden={this.state.hidden}
|
|
155
151
|
style={{
|
|
156
|
-
position:
|
|
152
|
+
position: "fixed",
|
|
157
153
|
top: 0,
|
|
158
154
|
left: 0,
|
|
159
155
|
right: 0,
|
|
160
156
|
zIndex: 4,
|
|
161
|
-
pointerEvents:
|
|
157
|
+
pointerEvents: "none",
|
|
162
158
|
}}
|
|
163
159
|
>
|
|
164
160
|
<LoadingBarComponent progress={this.state.progress} />
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index-node18.mjs","sources":["../src/index.tsx"],"sourcesContent":["import type { ReactElement } from 'react';\nimport { PureComponent } from 'react';\nimport ReactAlpContext from 'react-alp-context';\n\n/*\nExample with antd:\nimport { Progress } from 'antd';\n\nconst LoadingBarComponent = ({ progress }) => (\n <Progress\n type=\"line\"\n status=\"active\"\n percent={progress}\n showInfo={false}\n />\n);\n*/\n\n/* number between 0 and 1 */\nconst random = (): number => Math.ceil(Math.random() * 100) / 100;\n\n/**\n * around:\n * at 100ms 20%\n * at 1s 40%\n * at 2s 60%\n * at 3s 80%\n */\nconst calculatePercent = (percent: number): number => {\n if (percent < 60) return percent + random() * 10 + 5;\n if (percent < 70) return percent + random() * 10 + 3;\n else if (percent < 80) return percent + random() + 5;\n else if (percent < 90) return percent + random() + 1;\n else if (percent < 95) return percent + 0.1;\n else return percent;\n};\n\ninterface LoadingBarProps {\n LoadingBarComponent: React.ComponentType<{ progress: number }>;\n}\n\ninterface LoadingBarState {\n loading: boolean;\n hidden: boolean;\n progress: number;\n}\n\ninterface WebsocketInterface {\n isConnected: () => boolean;\n on: (event: 'connect' | 'disconnect', callback: () => unknown) => void;\n}\n\nexport default class LoadingBar extends PureComponent<\n LoadingBarProps,\n LoadingBarState\n> {\n static override contextType = ReactAlpContext;\n\n override state = {\n loading: true,\n hidden: true,\n progress: 1,\n };\n\n fadeOffTimeout?: ReturnType<typeof setTimeout>;\n\n resetTimeout?: ReturnType<typeof setTimeout>;\n\n first20Timeout?: ReturnType<typeof setTimeout>;\n\n progressTimer?: ReturnType<typeof setTimeout>;\n\n override componentDidMount(): void {\n const websocket = this.getWebsocket();\n if (websocket.isConnected()) {\n this.setState((prevState) => ({\n loading: false,\n progress: 100,\n hidden: prevState.hidden || prevState.progress === 100,\n }));\n }\n websocket.on('connect', () => {\n this.setState({ loading: false });\n });\n websocket.on('disconnect', () => {\n this.setState({ loading: true, progress: 1, hidden: false });\n });\n }\n\n override componentDidUpdate(\n prevProps: LoadingBarProps,\n prevState: LoadingBarState,\n ): void {\n if (this.state.loading !== prevState.loading) {\n if (this.state.loading) {\n this.showBar();\n } else {\n this.hideBar();\n }\n }\n }\n\n override componentWillUnmount(): void {\n if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);\n if (this.resetTimeout) clearTimeout(this.resetTimeout);\n if (this.first20Timeout) clearTimeout(this.first20Timeout);\n if (this.progressTimer) clearInterval(this.progressTimer);\n }\n\n getWebsocket(): WebsocketInterface {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return\n return (this.context as any).app.websocket;\n }\n\n private showBar(): void {\n if (this.fadeOffTimeout) clearTimeout(this.fadeOffTimeout);\n if (this.resetTimeout) clearTimeout(this.resetTimeout);\n\n this.first20Timeout = setTimeout(() => {\n this.setState({ progress: 20 });\n }, 100);\n\n this.progressTimer = setInterval(() => {\n this.setState((prevState) => {\n const newValue = calculatePercent(prevState.progress);\n return { progress: newValue };\n });\n }, 500);\n }\n\n private hideBar(): void {\n if (this.first20Timeout) clearTimeout(this.first20Timeout);\n if (this.progressTimer) clearInterval(this.progressTimer);\n\n this.fadeOffTimeout = setTimeout(() => {\n this.setState({\n progress: 100,\n });\n }, 500);\n\n this.resetTimeout = setTimeout(() => {\n this.setState({\n hidden: true,\n progress: 1,\n });\n }, 1000);\n }\n\n override render(): ReactElement {\n const LoadingBarComponent = this.props.LoadingBarComponent;\n\n return (\n <div\n hidden={this.state.hidden}\n style={{\n position: 'fixed',\n top: 0,\n left: 0,\n right: 0,\n zIndex: 4,\n pointerEvents: 'none',\n }}\n >\n <LoadingBarComponent progress={this.state.progress} />\n </div>\n );\n }\n}\n"],"names":["random","Math","ceil","calculatePercent","percent","LoadingBar","PureComponent","contextType","ReactAlpContext","state","loading","hidden","progress","componentDidMount","websocket","getWebsocket","isConnected","setState","prevState","on","componentDidUpdate","prevProps","showBar","hideBar","componentWillUnmount","fadeOffTimeout","clearTimeout","resetTimeout","first20Timeout","progressTimer","clearInterval","context","app","setTimeout","setInterval","newValue","render","LoadingBarComponent","props","_jsx","style","position","top","left","right","zIndex","pointerEvents","children"],"mappings":";;;;AAmBA,MAAMA,MAAM,GAAGA,MAAcC,IAAI,CAACC,IAAI,CAACD,IAAI,CAACD,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,GAAG,CAAA;;AAEjE;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMG,gBAAgB,GAAIC,OAAe,IAAa;AACpD,EAAA,IAAIA,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;EACpD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAGJ,MAAM,EAAE,GAAG,CAAC,CAAC,KAChD,IAAII,OAAO,GAAG,EAAE,EAAE,OAAOA,OAAO,GAAG,GAAG,CAAC,KACvC,OAAOA,OAAO,CAAA;AACrB,CAAC,CAAA;AAiBc,MAAMC,UAAU,SAASC,aAAa,CAGn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
|
-
"parser": "@typescript-eslint/parser",
|
|
4
|
-
"parserOptions": {
|
|
5
|
-
"EXPERIMENTAL_useProjectService": true,
|
|
6
|
-
"project": "packages/react-alp-loading-bar/tsconfig.json"
|
|
7
|
-
},
|
|
8
|
-
"plugins": ["@typescript-eslint"],
|
|
9
|
-
"extends": [
|
|
10
|
-
"@pob/eslint-config-typescript",
|
|
11
|
-
"@pob/eslint-config-typescript-react"
|
|
12
|
-
],
|
|
13
|
-
"ignorePatterns": ["*.d.ts"],
|
|
14
|
-
"overrides": [
|
|
15
|
-
{
|
|
16
|
-
"files": ["**/*.test.{ts,tsx}", "__tests__/**/*.{ts,tsx}"],
|
|
17
|
-
"extends": ["@pob/eslint-config-typescript/test"],
|
|
18
|
-
"env": {
|
|
19
|
-
"jest": true
|
|
20
|
-
},
|
|
21
|
-
"rules": {
|
|
22
|
-
"import/no-extraneous-dependencies": [
|
|
23
|
-
"error",
|
|
24
|
-
{
|
|
25
|
-
"devDependencies": true
|
|
26
|
-
}
|
|
27
|
-
]
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
]
|
|
31
|
-
}
|