cynx-lib 1.3.106 → 1.3.108

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cynx-lib",
3
- "version": "1.3.106",
3
+ "version": "1.3.108",
4
4
  "type": "module",
5
5
  "main": "./index.cjs",
6
6
  "module": "./index.js",
@@ -1,94 +1,94 @@
1
- import { useState, useLayoutEffect, useEffect, useRef, useCallback } from 'react';
2
-
3
- /**
4
- * Universal display and viewport measurement hook (Ruler).
5
- * Measures element rect relative to window viewport, computes remaining available height,
6
- * and dynamically calculates rows for grid / table / skeleton layouts.
7
- *
8
- * @param {Object|number} arg1 - Options object or rowHeight (for backward compatibility)
9
- * @param {number} [arg2] - initialOffset (for backward compatibility)
10
- */
11
- export function useRuler(arg1 = 51, arg2 = 240) {
12
- const isObject = arg1 && typeof arg1 === 'object' && !Array.isArray(arg1);
13
- const options = isObject ? arg1 : {};
14
- const rowHeight = isObject ? (options.rowHeight ?? 51) : (typeof arg1 === 'number' ? arg1 : 51);
15
- const initialOffset = isObject ? (options.initialOffset ?? 240) : (typeof arg2 === 'number' ? arg2 : 240);
16
- const bottomBuffer = options.bottomBuffer ?? 24;
17
- const minLimit = options.minHeight ?? 200;
18
- const maxLimit = options.maxHeight ?? null;
19
- const minRows = options.minRows ?? 3;
20
- const maxRows = options.maxRows ?? 25;
21
-
22
- const containerRef = useRef(null);
23
-
24
- const [availableHeight, setAvailableHeight] = useState(() => {
25
- if (typeof window === 'undefined') return minLimit;
26
- return Math.max(minLimit, window.innerHeight - initialOffset - bottomBuffer);
27
- });
28
-
29
- const [rows, setRows] = useState(() => {
30
- if (typeof window === 'undefined') return 8;
31
- return Math.max(minRows, Math.floor((window.innerHeight - initialOffset) / rowHeight));
32
- });
33
-
34
- const calculate = useCallback(() => {
35
- if (!containerRef.current) return;
36
- const rect = containerRef.current.getBoundingClientRect();
37
-
38
- let avail = window.innerHeight - rect.top - bottomBuffer;
39
- if (avail < minLimit) avail = minLimit;
40
- if (maxLimit && avail > maxLimit) avail = maxLimit;
41
- const roundedAvail = Math.round(avail);
42
-
43
- // Guard against infinite re-renders from subpixel fluctuations
44
- setAvailableHeight((prev) => (Math.abs(prev - roundedAvail) > 1 ? roundedAvail : prev));
45
-
46
- const thead = containerRef.current.querySelector('thead');
47
- const theadHeight = thead ? thead.offsetHeight : 0;
48
- const cardHeader =
49
- containerRef.current.firstElementChild !== thead?.parentElement &&
50
- !containerRef.current.firstElementChild?.contains(thead)
51
- ? containerRef.current.querySelector(':scope > div:first-child')
52
- : null;
53
- const cardHeaderHeight = cardHeader ? cardHeader.offsetHeight : 0;
54
-
55
- const bodyAvailable = roundedAvail - theadHeight - cardHeaderHeight;
56
- if (bodyAvailable > 0) {
57
- const calculated = Math.floor(bodyAvailable / rowHeight);
58
- const nextRows = Math.max(minRows, Math.min(maxRows, calculated));
59
- setRows((prev) => (prev !== nextRows ? nextRows : prev));
60
- }
61
- }, [rowHeight, bottomBuffer, minLimit, maxLimit, minRows, maxRows]);
62
-
63
- useLayoutEffect(() => {
64
- calculate();
65
- }, [calculate]);
66
-
67
- useEffect(() => {
68
- calculate();
69
- window.addEventListener('resize', calculate);
70
-
71
- let observer = null;
72
- if (typeof ResizeObserver !== 'undefined' && containerRef.current?.parentElement) {
73
- observer = new ResizeObserver(() => {
74
- calculate();
75
- });
76
- observer.observe(containerRef.current.parentElement);
77
- }
78
-
79
- return () => {
80
- window.removeEventListener('resize', calculate);
81
- if (observer) observer.disconnect();
82
- };
83
- }, [calculate]);
84
-
85
- return {
86
- containerRef,
87
- availableHeight,
88
- minHeight: availableHeight,
89
- rows,
90
- };
91
- }
92
-
93
- export const useAutoSkeletonRows = useRuler;
94
- export default useRuler;
1
+ import { useState, useLayoutEffect, useEffect, useRef, useCallback } from 'react';
2
+
3
+ /**
4
+ * Universal display and viewport measurement hook (Ruler).
5
+ * Measures element rect relative to window viewport, computes remaining available height,
6
+ * and dynamically calculates rows for grid / table / skeleton layouts.
7
+ *
8
+ * @param {Object|number} arg1 - Options object or rowHeight (for backward compatibility)
9
+ * @param {number} [arg2] - initialOffset (for backward compatibility)
10
+ */
11
+ export function useRuler(arg1 = 51, arg2 = 240) {
12
+ const isObject = arg1 && typeof arg1 === 'object' && !Array.isArray(arg1);
13
+ const options = isObject ? arg1 : {};
14
+ const rowHeight = isObject ? (options.rowHeight ?? 51) : (typeof arg1 === 'number' ? arg1 : 51);
15
+ const initialOffset = isObject ? (options.initialOffset ?? 240) : (typeof arg2 === 'number' ? arg2 : 240);
16
+ const bottomBuffer = options.bottomBuffer ?? 24;
17
+ const minLimit = options.minHeight ?? 200;
18
+ const maxLimit = options.maxHeight ?? null;
19
+ const minRows = options.minRows ?? 3;
20
+ const maxRows = options.maxRows ?? 25;
21
+
22
+ const containerRef = useRef(null);
23
+
24
+ const [availableHeight, setAvailableHeight] = useState(() => {
25
+ if (typeof window === 'undefined') return minLimit;
26
+ return Math.max(minLimit, window.innerHeight - initialOffset - bottomBuffer);
27
+ });
28
+
29
+ const [rows, setRows] = useState(() => {
30
+ if (typeof window === 'undefined') return 8;
31
+ return Math.max(minRows, Math.floor((window.innerHeight - initialOffset) / rowHeight));
32
+ });
33
+
34
+ const calculate = useCallback(() => {
35
+ if (!containerRef.current) return;
36
+ const rect = containerRef.current.getBoundingClientRect();
37
+
38
+ let avail = window.innerHeight - rect.top - bottomBuffer;
39
+ if (avail < minLimit) avail = minLimit;
40
+ if (maxLimit && avail > maxLimit) avail = maxLimit;
41
+ const roundedAvail = Math.round(avail);
42
+
43
+ // Guard against infinite re-renders from subpixel fluctuations
44
+ setAvailableHeight((prev) => (Math.abs(prev - roundedAvail) > 1 ? roundedAvail : prev));
45
+
46
+ const thead = containerRef.current.querySelector('thead');
47
+ const theadHeight = thead ? thead.offsetHeight : 0;
48
+ const cardHeader =
49
+ containerRef.current.firstElementChild !== thead?.parentElement &&
50
+ !containerRef.current.firstElementChild?.contains(thead)
51
+ ? containerRef.current.querySelector(':scope > div:first-child')
52
+ : null;
53
+ const cardHeaderHeight = cardHeader ? cardHeader.offsetHeight : 0;
54
+
55
+ const bodyAvailable = roundedAvail - theadHeight - cardHeaderHeight;
56
+ if (bodyAvailable > 0) {
57
+ const calculated = Math.floor(bodyAvailable / rowHeight);
58
+ const nextRows = Math.max(minRows, Math.min(maxRows, calculated));
59
+ setRows((prev) => (prev !== nextRows ? nextRows : prev));
60
+ }
61
+ }, [rowHeight, bottomBuffer, minLimit, maxLimit, minRows, maxRows]);
62
+
63
+ useLayoutEffect(() => {
64
+ calculate();
65
+ }, [calculate]);
66
+
67
+ useEffect(() => {
68
+ calculate();
69
+ window.addEventListener('resize', calculate);
70
+
71
+ let observer = null;
72
+ if (typeof ResizeObserver !== 'undefined' && containerRef.current?.parentElement) {
73
+ observer = new ResizeObserver(() => {
74
+ calculate();
75
+ });
76
+ observer.observe(containerRef.current.parentElement);
77
+ }
78
+
79
+ return () => {
80
+ window.removeEventListener('resize', calculate);
81
+ if (observer) observer.disconnect();
82
+ };
83
+ }, [calculate]);
84
+
85
+ return {
86
+ containerRef,
87
+ availableHeight,
88
+ minHeight: availableHeight,
89
+ rows,
90
+ };
91
+ }
92
+
93
+ export const useAutoSkeletonRows = useRuler;
94
+ export default useRuler;