@slithy/react-grid-gallery 0.1.1 → 0.1.2
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/index.js +17 -7
- package/package.json +6 -3
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// src/computeGridLayout.ts
|
|
2
2
|
function computeGridLayout(items, columns, cellWidth, cellHeight) {
|
|
3
|
-
if (items.length === 0 || columns <= 0 || cellWidth <= 0) return [];
|
|
3
|
+
if (items.length === 0 || !Number.isFinite(columns) || !Number.isFinite(cellWidth) || !Number.isFinite(cellHeight) || columns <= 0 || cellWidth <= 0 || cellHeight < 0) return [];
|
|
4
|
+
const columnCount = Math.max(1, Math.floor(columns));
|
|
4
5
|
const rows = [];
|
|
5
|
-
for (let i = 0; i < items.length; i +=
|
|
6
|
+
for (let i = 0; i < items.length; i += columnCount) {
|
|
6
7
|
rows.push({
|
|
7
|
-
items: items.slice(i, i +
|
|
8
|
+
items: items.slice(i, i + columnCount),
|
|
8
9
|
width: cellWidth,
|
|
9
10
|
height: cellHeight
|
|
10
11
|
});
|
|
@@ -67,6 +68,12 @@ function useVirtualWindow(containerRef, enabled, scrollContainerRef) {
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
// src/useGridGallery.ts
|
|
71
|
+
function finitePositive(value, fallback) {
|
|
72
|
+
return Number.isFinite(value) && value > 0 ? value : fallback;
|
|
73
|
+
}
|
|
74
|
+
function finiteNonNegative(value, fallback = 0) {
|
|
75
|
+
return Number.isFinite(value) && value >= 0 ? value : fallback;
|
|
76
|
+
}
|
|
70
77
|
function useGridGallery(items, options, scrollContainerRef) {
|
|
71
78
|
const containerRef = useRef2(null);
|
|
72
79
|
const [containerWidth, setContainerWidth] = useState2(0);
|
|
@@ -92,13 +99,16 @@ function useGridGallery(items, options, scrollContainerRef) {
|
|
|
92
99
|
}, []);
|
|
93
100
|
const onError = useCallback((_key) => {
|
|
94
101
|
}, []);
|
|
102
|
+
const rawColumns = typeof options.columns === "function" ? options.columns(containerWidth) : options.columns;
|
|
95
103
|
const resolvedColumns = Math.max(
|
|
96
104
|
1,
|
|
97
|
-
Math.round(
|
|
105
|
+
Math.round(finitePositive(rawColumns, 1))
|
|
98
106
|
);
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
107
|
+
const rawGap = typeof options.gap === "function" ? options.gap(containerWidth) : options.gap ?? 0;
|
|
108
|
+
const resolvedGap = finiteNonNegative(rawGap);
|
|
109
|
+
const rawAspectRatio = typeof options.aspectRatio === "function" ? options.aspectRatio(containerWidth) : options.aspectRatio ?? 1;
|
|
110
|
+
const resolvedAspectRatio = finitePositive(rawAspectRatio, 1);
|
|
111
|
+
const cellWidth = containerWidth > 0 ? Math.max(0, Math.floor((containerWidth - resolvedGap * (resolvedColumns - 1)) / resolvedColumns)) : 0;
|
|
102
112
|
const cellHeight = cellWidth > 0 ? Math.round(cellWidth / resolvedAspectRatio) : 0;
|
|
103
113
|
const rows = useMemo(() => {
|
|
104
114
|
if (cellWidth === 0) return [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slithy/react-grid-gallery",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "React grid gallery component.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -28,13 +28,16 @@
|
|
|
28
28
|
"tsup": "^8",
|
|
29
29
|
"typescript": "^5",
|
|
30
30
|
"vitest": "^4.1.2",
|
|
31
|
-
"@slithy/
|
|
32
|
-
"@slithy/
|
|
31
|
+
"@slithy/eslint-config": "0.0.0",
|
|
32
|
+
"@slithy/tsconfig": "0.0.0"
|
|
33
33
|
},
|
|
34
34
|
"repository": {
|
|
35
35
|
"type": "git",
|
|
36
36
|
"url": "https://github.com/mjcampagna/react-grid-gallery"
|
|
37
37
|
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
38
41
|
"author": "mjcampagna",
|
|
39
42
|
"license": "MIT",
|
|
40
43
|
"scripts": {
|