@stokr/components-library 2.3.74 → 2.3.75

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.
@@ -13,6 +13,21 @@ 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
+ }
16
31
  const SideModal = _ref => {
17
32
  let {
18
33
  children,
@@ -27,8 +42,14 @@ const SideModal = _ref => {
27
42
  } = _ref,
28
43
  props = _objectWithoutProperties(_ref, _excluded);
29
44
  const [isVisible, setIsVisible] = (0, _react.useState)(false);
45
+ const savedScroll = (0, _react.useRef)(null);
30
46
  (0, _react.useEffect)(() => {
31
47
  if (isOpen) {
48
+ const found = findScrolledElement();
49
+ if (found) savedScroll.current = found;else if (window.scrollY > 0) savedScroll.current = {
50
+ type: 'window',
51
+ y: window.scrollY
52
+ };else savedScroll.current = null;
32
53
  const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
33
54
  document.documentElement.style.overflow = 'hidden';
34
55
  document.body.style.overflow = 'hidden';
@@ -36,17 +57,35 @@ const SideModal = _ref => {
36
57
  document.documentElement.style.setProperty('--scrollbar-compensation', "".concat(scrollbarWidth, "px"));
37
58
  setTimeout(() => setIsVisible(true), 10);
38
59
  } else {
60
+ const saved = savedScroll.current;
61
+ savedScroll.current = null;
39
62
  document.documentElement.style.overflow = '';
40
63
  document.body.style.overflow = '';
41
64
  document.body.style.paddingRight = '';
42
65
  document.documentElement.style.setProperty('--scrollbar-compensation', '0px');
43
66
  setIsVisible(false);
67
+ if (saved) {
68
+ requestAnimationFrame(() => {
69
+ if (saved.type === 'window') window.scrollTo(0, saved.y);else if (saved.el && document.contains(saved.el)) saved.el.scrollTop = saved.scrollTop;
70
+ });
71
+ }
44
72
  }
45
73
  return () => {
74
+ const saved = savedScroll.current;
75
+ savedScroll.current = null;
46
76
  document.documentElement.style.overflow = '';
47
77
  document.body.style.overflow = '';
48
78
  document.body.style.paddingRight = '';
49
79
  document.documentElement.style.setProperty('--scrollbar-compensation', '0px');
80
+ if (saved && saved.el && document.contains(saved.el)) {
81
+ requestAnimationFrame(() => {
82
+ saved.el.scrollTop = saved.scrollTop;
83
+ });
84
+ } else if (saved && saved.type === 'window') {
85
+ requestAnimationFrame(() => {
86
+ window.scrollTo(0, saved.y);
87
+ });
88
+ }
50
89
  };
51
90
  }, [isOpen]);
52
91
 
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stokr/components-library",
3
- "version": "2.3.74",
3
+ "version": "2.3.75",
4
4
  "description": "STOKR - Components Library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",