UNPKG

@prefect9/ui

Version:

UI React components

161 lines (160 loc) 6.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es.array.includes.js"); require("core-js/modules/web.dom-collections.iterator.js"); var _react = require("react"); var _reactRouterDom = require("react-router-dom"); var _datetime = _interopRequireDefault(require("@prefect9/datetime")); var _anim = _interopRequireDefault(require("@prefect9/anim")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function AnimatedRouterLoader() { const navigation = (0, _reactRouterDom.useNavigation)(); const [loaderWidth, setLoaderWidth] = (0, _react.useState)(0); (0, _react.useEffect)(() => { let lastWidth = 0; const resize = () => { const newWidth = window.innerWidth; if (newWidth === lastWidth) return; setLoaderWidth(newWidth); lastWidth = newWidth; }; resize(); window.addEventListener('resize', resize, { passive: true }); window.addEventListener('load', resize, { passive: true }); return () => { window.removeEventListener('resize', resize, { passive: true }); window.removeEventListener('load', resize, { passive: true }); }; }, []); const defaultSpeed = 250; // px per sec const [animation, setAnimation] = (0, _react.useState)({ type: null, lastTime: null, lastPx: 0, loadedPx: 0 }); (0, _react.useEffect)(() => { const setLoadedPx = loadedPx => setAnimation(state => _objectSpread(_objectSpread({}, state), {}, { loadedPx })); const plusPx = loadedPx => setAnimation(state => _objectSpread(_objectSpread({}, state), {}, { loadedPx: state.loadedPx + 1 })); const toSlow = (lastTime, lastPx, loadedPx) => { setAnimation(state => { if (state.type !== 'start') return state; return _objectSpread(_objectSpread({}, state), {}, { type: 'slow', lastTime, lastPx, loadedPx }); }); }; const toPause = (lastPx, loadedPx) => { setAnimation(state => { if (state.type !== 'slow') return state; return _objectSpread(_objectSpread({}, state), {}, { type: 'pause', lastPx, loadedPx }); }); }; const toLoaded = (lastTime, lastPx, loadedPx) => { setAnimation(state => { if (state.type !== 'finish') return state; return _objectSpread(_objectSpread({}, state), {}, { type: 'loaded', lastTime, lastPx, loadedPx }); }); }; const endAnimation = () => { setAnimation(state => { if (state.type !== 'loaded') return state; return _objectSpread(_objectSpread({}, state), {}, { type: null }); }); }; (0, _anim.default)(() => { const now = new _datetime.default(); if (animation.type === 'start') { const delta = now.delta(animation.lastTime); const loadedPx = delta * defaultSpeed / 1000 + animation.lastPx; const loadedPercent = loadedPx * 100 / loaderWidth; if (loadedPercent >= 80) toSlow(now, loadedPx, loadedPx);else setLoadedPx(loadedPx); } if (animation.type === 'slow') { const delta = now.delta(animation.lastTime); const loadedPx = delta * (defaultSpeed / 20) / 1000 + animation.lastPx; const loadedPercent = loadedPx * 100 / loaderWidth; if (loadedPercent >= 98) toPause(loadedPx, loadedPx);else setLoadedPx(loadedPx); } if (animation.type === 'finish') { const delta = now.delta(animation.lastTime); const loadedPx = delta * (defaultSpeed * 20) / 1000 + animation.lastPx; if (loadedPx > loaderWidth) toLoaded(now, loadedPx, loadedPx);else setLoadedPx(loadedPx); } if (animation.type === 'loaded') { const delta = now.delta(animation.lastTime); if (delta >= 50) endAnimation();else plusPx(); } }); }, [animation, loaderWidth]); const startAnimation = () => { const now = new _datetime.default(); setAnimation(state => _objectSpread(_objectSpread({}, state), {}, { type: 'start', lastTime: now, lastPx: 0, loadedPx: 0 })); }; const finishAnimation = () => { const now = new _datetime.default(); setAnimation(state => { if (state.type === null) return state; return _objectSpread(_objectSpread({}, state), {}, { type: 'finish', lastTime: now }); }); }; (0, _react.useEffect)(() => { if (navigation.state === 'loading') startAnimation();else finishAnimation(); }, [navigation]); return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: "prefect9-page-loader", style: { display: ['start', 'slow', 'pause', 'finish', 'loaded'].includes(animation.type) ? 'block' : 'none' }, children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: "prefect9-page-loader__bar", style: { width: "".concat(animation.loadedPx, "px") } }) }); } var _default = exports.default = AnimatedRouterLoader;