phx-react
Version:
PHX REACT
47 lines • 1.87 kB
JavaScript
;
// components/LoadingProvider.tsx
'use client';
exports.__esModule = true;
exports.PHXLoadingProvider = exports.useLoading = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var react_2 = require("react");
var navigation_1 = require("next/navigation");
var LoadingContext = (0, react_2.createContext)({
loading: false,
// eslint-disable-next-line @typescript-eslint/no-empty-function
setLoading: function (_v) { },
progress: 0
});
var useLoading = function () { return (0, react_2.useContext)(LoadingContext); };
exports.useLoading = useLoading;
function PHXLoadingProvider(_a) {
var children = _a.children;
var _b = (0, react_2.useState)(false), loading = _b[0], setLoading = _b[1];
var pathname = (0, navigation_1.usePathname)();
var _c = (0, react_2.useState)(0), progress = _c[0], setProgress = _c[1];
(0, react_2.useEffect)(function () {
setLoading(false);
setProgress(100);
}, [pathname]);
(0, react_2.useEffect)(function () {
if (loading) {
var timerId_1 = setInterval(function () {
if (progress <= 90) {
var nextProgress = progress + 5;
setProgress(nextProgress);
}
}, 100);
return function () { return clearInterval(timerId_1); };
}
else {
var timerId_2 = setTimeout(function () {
setProgress(0);
}, 400);
return function () { return clearTimeout(timerId_2); };
}
}, [loading, progress]);
return react_1["default"].createElement(LoadingContext.Provider, { value: { loading: loading, setLoading: setLoading, progress: progress } }, children);
}
exports.PHXLoadingProvider = PHXLoadingProvider;
//# sourceMappingURL=loading-provider.js.map