@stokr/components-library 2.3.74 → 2.3.76
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/dist/components/Modal/SideModal.js +81 -12
- package/dist/hooks/useTransactionPolling.js +118 -0
- package/dist/index.js +11 -0
- package/package.json +1 -1
|
@@ -13,6 +13,45 @@ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r
|
|
|
13
13
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
14
14
|
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|
15
15
|
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|
16
|
+
function findScrolledElement() {
|
|
17
|
+
let best = null;
|
|
18
|
+
function walk(el) {
|
|
19
|
+
if (!el || el.nodeType !== 1) return;
|
|
20
|
+
const s = getComputedStyle(el);
|
|
21
|
+
const scrollable = (s.overflowY === 'auto' || s.overflowY === 'scroll' || s.overflow === 'auto' || s.overflow === 'scroll') && el.scrollHeight > el.clientHeight && el.scrollTop > 0;
|
|
22
|
+
if (scrollable && (!best || el.scrollTop > best.scrollTop)) best = {
|
|
23
|
+
el,
|
|
24
|
+
scrollTop: el.scrollTop
|
|
25
|
+
};
|
|
26
|
+
for (let i = 0; i < el.children.length; i++) walk(el.children[i]);
|
|
27
|
+
}
|
|
28
|
+
walk(document.body);
|
|
29
|
+
return best;
|
|
30
|
+
}
|
|
31
|
+
function restoreScroll(saved) {
|
|
32
|
+
if (!saved) return;
|
|
33
|
+
if (saved.type === 'window') {
|
|
34
|
+
window.scrollTo(0, saved.y);
|
|
35
|
+
} else if (saved.el && document.contains(saved.el)) {
|
|
36
|
+
saved.el.scrollTop = saved.scrollTop;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function clearBodyLock() {
|
|
40
|
+
const {
|
|
41
|
+
style
|
|
42
|
+
} = document.body;
|
|
43
|
+
const docEl = document.documentElement;
|
|
44
|
+
style.position = '';
|
|
45
|
+
style.top = '';
|
|
46
|
+
style.left = '';
|
|
47
|
+
style.right = '';
|
|
48
|
+
style.width = '';
|
|
49
|
+
style.height = '';
|
|
50
|
+
style.overflow = '';
|
|
51
|
+
style.paddingRight = '';
|
|
52
|
+
docEl.style.overflow = '';
|
|
53
|
+
docEl.style.setProperty('--scrollbar-compensation', '0px');
|
|
54
|
+
}
|
|
16
55
|
const SideModal = _ref => {
|
|
17
56
|
let {
|
|
18
57
|
children,
|
|
@@ -27,26 +66,56 @@ const SideModal = _ref => {
|
|
|
27
66
|
} = _ref,
|
|
28
67
|
props = _objectWithoutProperties(_ref, _excluded);
|
|
29
68
|
const [isVisible, setIsVisible] = (0, _react.useState)(false);
|
|
69
|
+
const savedScroll = (0, _react.useRef)(null);
|
|
30
70
|
(0, _react.useEffect)(() => {
|
|
31
71
|
if (isOpen) {
|
|
72
|
+
const found = findScrolledElement();
|
|
73
|
+
if (found) savedScroll.current = found;else if (window.scrollY > 0) savedScroll.current = {
|
|
74
|
+
type: 'window',
|
|
75
|
+
y: window.scrollY
|
|
76
|
+
};else savedScroll.current = null;
|
|
77
|
+
const saved = savedScroll.current;
|
|
32
78
|
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
79
|
+
const isWindowScroll = saved && saved.type === 'window';
|
|
80
|
+
if (isWindowScroll) {
|
|
81
|
+
// Position-fixed lock: avoids browser resetting scroll when overflow changes
|
|
82
|
+
const y = saved.y;
|
|
83
|
+
const docHeight = document.documentElement.scrollHeight;
|
|
84
|
+
document.documentElement.style.overflow = 'hidden';
|
|
85
|
+
document.documentElement.style.setProperty('--scrollbar-compensation', "".concat(scrollbarWidth, "px"));
|
|
86
|
+
document.body.style.position = 'fixed';
|
|
87
|
+
document.body.style.top = "-".concat(y, "px");
|
|
88
|
+
document.body.style.left = '0';
|
|
89
|
+
document.body.style.right = '0';
|
|
90
|
+
document.body.style.width = '100%';
|
|
91
|
+
document.body.style.height = "".concat(docHeight, "px");
|
|
92
|
+
document.body.style.paddingRight = "".concat(scrollbarWidth, "px");
|
|
93
|
+
} else {
|
|
94
|
+
// Overflow lock; for element scroll we restore after lock
|
|
95
|
+
document.documentElement.style.overflow = 'hidden';
|
|
96
|
+
document.body.style.overflow = 'hidden';
|
|
97
|
+
document.body.style.paddingRight = "".concat(scrollbarWidth, "px");
|
|
98
|
+
document.documentElement.style.setProperty('--scrollbar-compensation', "".concat(scrollbarWidth, "px"));
|
|
99
|
+
if (saved) {
|
|
100
|
+
requestAnimationFrame(() => {
|
|
101
|
+
restoreScroll(saved);
|
|
102
|
+
requestAnimationFrame(() => restoreScroll(saved));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
37
106
|
setTimeout(() => setIsVisible(true), 10);
|
|
38
107
|
} else {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
document.documentElement.style.setProperty('--scrollbar-compensation', '0px');
|
|
108
|
+
const saved = savedScroll.current;
|
|
109
|
+
savedScroll.current = null;
|
|
110
|
+
clearBodyLock();
|
|
43
111
|
setIsVisible(false);
|
|
112
|
+
if (saved) requestAnimationFrame(() => restoreScroll(saved));
|
|
44
113
|
}
|
|
45
114
|
return () => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
115
|
+
const saved = savedScroll.current;
|
|
116
|
+
savedScroll.current = null;
|
|
117
|
+
clearBodyLock();
|
|
118
|
+
if (saved) requestAnimationFrame(() => restoreScroll(saved));
|
|
50
119
|
};
|
|
51
120
|
}, [isOpen]);
|
|
52
121
|
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useTransactionPolling = exports.default = void 0;
|
|
7
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
8
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
9
|
+
/**
|
|
10
|
+
* Generic polling hook. Calls fetcher (with optional payload) until isSuccess(response)
|
|
11
|
+
* is true or maxAttempts is reached.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} options
|
|
14
|
+
* @param {function(payload?: any): Promise<any>} options.fetcher - Required. Async function called each poll. Receives options.payload if provided.
|
|
15
|
+
* @param {any} [options.payload] - Optional. Passed to fetcher each poll. Omit when fetcher needs no args. For objects, use a stable reference (e.g. useMemo) to avoid restarting every render.
|
|
16
|
+
* @param {boolean} [options.enabled=true] - When false, polling does not start. Use to gate on e.g. having an id.
|
|
17
|
+
* @param {function(any): boolean} [options.isSuccess] - Predicate to treat response as success. Default: truthy check.
|
|
18
|
+
* @param {number} [options.pollingInterval=5000] - Ms between polls.
|
|
19
|
+
* @param {number} [options.maxAttempts=360] - Max poll attempts (~30 min at 5s).
|
|
20
|
+
* @param {number} [options.delayBeforeStart=2000] - Ms before first poll.
|
|
21
|
+
* @param {function(any)} [options.onSuccess] - Called with response when isSuccess(response) is true.
|
|
22
|
+
* @param {function(Error)} [options.onError] - Called on fetcher error.
|
|
23
|
+
* @param {function()} [options.onMaxAttemptsReached] - Called when attempts reach maxAttempts.
|
|
24
|
+
* @returns {{ isLoading: boolean, error: Error|null, attempts: number, isPolling: boolean, maxAttempts: number }}
|
|
25
|
+
*/
|
|
26
|
+
const useTransactionPolling = function () {
|
|
27
|
+
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
28
|
+
const {
|
|
29
|
+
fetcher,
|
|
30
|
+
payload,
|
|
31
|
+
enabled = true,
|
|
32
|
+
isSuccess = response => Boolean(response),
|
|
33
|
+
pollingInterval = 5000,
|
|
34
|
+
maxAttempts = 360,
|
|
35
|
+
delayBeforeStart = 2000,
|
|
36
|
+
onSuccess,
|
|
37
|
+
onError,
|
|
38
|
+
onMaxAttemptsReached
|
|
39
|
+
} = options;
|
|
40
|
+
if (typeof fetcher !== 'function') {
|
|
41
|
+
throw new Error('useTransactionPolling: options.fetcher is required and must be a function');
|
|
42
|
+
}
|
|
43
|
+
const [isLoading, setIsLoading] = (0, _react.useState)(false);
|
|
44
|
+
const [error, setError] = (0, _react.useState)(null);
|
|
45
|
+
const [attempts, setAttempts] = (0, _react.useState)(0);
|
|
46
|
+
const [isPolling, setIsPolling] = (0, _react.useState)(false);
|
|
47
|
+
const intervalRef = (0, _react.useRef)(null);
|
|
48
|
+
const timeoutRef = (0, _react.useRef)(null);
|
|
49
|
+
const completedRef = (0, _react.useRef)(false);
|
|
50
|
+
const pollRef = (0, _react.useRef)(null);
|
|
51
|
+
const poll = (0, _react.useCallback)(async () => {
|
|
52
|
+
if (!enabled || attempts >= maxAttempts || completedRef.current) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
setIsLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
const response = await fetcher(payload);
|
|
59
|
+
if (isSuccess(response)) {
|
|
60
|
+
completedRef.current = true;
|
|
61
|
+
setIsPolling(false);
|
|
62
|
+
setIsLoading(false);
|
|
63
|
+
onSuccess === null || onSuccess === void 0 || onSuccess(response);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error('Transaction polling error:', err);
|
|
68
|
+
setError(err);
|
|
69
|
+
onError === null || onError === void 0 || onError(err);
|
|
70
|
+
} finally {
|
|
71
|
+
setIsLoading(false);
|
|
72
|
+
setAttempts(prev => prev + 1);
|
|
73
|
+
}
|
|
74
|
+
}, [enabled, attempts, maxAttempts, fetcher, payload, isSuccess, onSuccess, onError]);
|
|
75
|
+
pollRef.current = poll;
|
|
76
|
+
(0, _react.useEffect)(() => {
|
|
77
|
+
if (!enabled) return;
|
|
78
|
+
setIsPolling(true);
|
|
79
|
+
setAttempts(0);
|
|
80
|
+
completedRef.current = false;
|
|
81
|
+
setError(null);
|
|
82
|
+
const runPoll = () => {
|
|
83
|
+
var _pollRef$current;
|
|
84
|
+
return (_pollRef$current = pollRef.current) === null || _pollRef$current === void 0 ? void 0 : _pollRef$current.call(pollRef);
|
|
85
|
+
};
|
|
86
|
+
timeoutRef.current = setTimeout(() => {
|
|
87
|
+
intervalRef.current = setInterval(runPoll, pollingInterval);
|
|
88
|
+
runPoll();
|
|
89
|
+
}, delayBeforeStart);
|
|
90
|
+
return () => {
|
|
91
|
+
if (intervalRef.current) {
|
|
92
|
+
clearInterval(intervalRef.current);
|
|
93
|
+
}
|
|
94
|
+
if (timeoutRef.current) {
|
|
95
|
+
clearTimeout(timeoutRef.current);
|
|
96
|
+
}
|
|
97
|
+
setIsPolling(false);
|
|
98
|
+
};
|
|
99
|
+
}, [enabled, payload, pollingInterval, delayBeforeStart, maxAttempts]);
|
|
100
|
+
(0, _react.useEffect)(() => {
|
|
101
|
+
if (attempts >= maxAttempts && isPolling) {
|
|
102
|
+
if (intervalRef.current) {
|
|
103
|
+
clearInterval(intervalRef.current);
|
|
104
|
+
}
|
|
105
|
+
setIsPolling(false);
|
|
106
|
+
onMaxAttemptsReached === null || onMaxAttemptsReached === void 0 || onMaxAttemptsReached();
|
|
107
|
+
}
|
|
108
|
+
}, [attempts, maxAttempts, isPolling, onMaxAttemptsReached]);
|
|
109
|
+
return {
|
|
110
|
+
isLoading,
|
|
111
|
+
error,
|
|
112
|
+
attempts,
|
|
113
|
+
isPolling,
|
|
114
|
+
maxAttempts
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
exports.useTransactionPolling = useTransactionPolling;
|
|
118
|
+
var _default = exports.default = useTransactionPolling;
|
package/dist/index.js
CHANGED
|
@@ -1466,6 +1466,17 @@ Object.keys(_useTimer).forEach(function (key) {
|
|
|
1466
1466
|
}
|
|
1467
1467
|
});
|
|
1468
1468
|
});
|
|
1469
|
+
var _useTransactionPolling = require("./hooks/useTransactionPolling");
|
|
1470
|
+
Object.keys(_useTransactionPolling).forEach(function (key) {
|
|
1471
|
+
if (key === "default" || key === "__esModule") return;
|
|
1472
|
+
if (key in exports && exports[key] === _useTransactionPolling[key]) return;
|
|
1473
|
+
Object.defineProperty(exports, key, {
|
|
1474
|
+
enumerable: true,
|
|
1475
|
+
get: function () {
|
|
1476
|
+
return _useTransactionPolling[key];
|
|
1477
|
+
}
|
|
1478
|
+
});
|
|
1479
|
+
});
|
|
1469
1480
|
var _PaymentDisplay = require("./components/Payment/PaymentDisplay");
|
|
1470
1481
|
Object.keys(_PaymentDisplay).forEach(function (key) {
|
|
1471
1482
|
if (key === "default" || key === "__esModule") return;
|