@n-ivan/react-justified-gallery 1.0.0
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/LICENSE +21 -0
- package/dist/justified-gallery.js +171 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ivan Nesterovic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { jsx as g } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as T, useState as C, useCallback as F, useEffect as G, useMemo as U } from "react";
|
|
3
|
+
function k(o, t) {
|
|
4
|
+
let e = null;
|
|
5
|
+
const i = (...n) => {
|
|
6
|
+
e !== null && clearTimeout(e), e = setTimeout(() => {
|
|
7
|
+
o(...n), e = null;
|
|
8
|
+
}, t);
|
|
9
|
+
};
|
|
10
|
+
return i.cancel = () => {
|
|
11
|
+
e !== null && (clearTimeout(e), e = null);
|
|
12
|
+
}, i;
|
|
13
|
+
}
|
|
14
|
+
function B(o) {
|
|
15
|
+
const t = T(null), [e, i] = C(0), n = F(() => {
|
|
16
|
+
if (t.current) {
|
|
17
|
+
const l = t.current.getBoundingClientRect().width;
|
|
18
|
+
i(l);
|
|
19
|
+
}
|
|
20
|
+
}, []);
|
|
21
|
+
return G(() => {
|
|
22
|
+
const l = t.current;
|
|
23
|
+
if (!l) return;
|
|
24
|
+
n();
|
|
25
|
+
const h = k(n, o), c = new ResizeObserver(() => {
|
|
26
|
+
h();
|
|
27
|
+
});
|
|
28
|
+
return c.observe(l), () => {
|
|
29
|
+
c.disconnect(), h.cancel();
|
|
30
|
+
};
|
|
31
|
+
}, [n, o]), { containerRef: t, width: e };
|
|
32
|
+
}
|
|
33
|
+
function D(o) {
|
|
34
|
+
for (let t = 0; t < o.length; t++) {
|
|
35
|
+
const e = o[t];
|
|
36
|
+
if (e.length === 0)
|
|
37
|
+
throw new Error(`Row ${t} is empty. All rows must contain at least one image.`);
|
|
38
|
+
for (let i = 0; i < e.length; i++) {
|
|
39
|
+
const n = e[i];
|
|
40
|
+
if (typeof n.width != "number" || n.width <= 0)
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Image at row ${t}, index ${i} has invalid width. Expected positive number, got: ${n.width}`
|
|
43
|
+
);
|
|
44
|
+
if (typeof n.height != "number" || n.height <= 0)
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Image at row ${t}, index ${i} has invalid height. Expected positive number, got: ${n.height}`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function O(o, t, e) {
|
|
52
|
+
return D(o), o.map((i) => {
|
|
53
|
+
const n = e * (i.length - 1), l = t - n, h = i.reduce((a, s) => a + s.width / s.height, 0), c = l / h;
|
|
54
|
+
return {
|
|
55
|
+
images: i.map((a) => {
|
|
56
|
+
const s = a.width / a.height, f = c * s;
|
|
57
|
+
return {
|
|
58
|
+
image: a,
|
|
59
|
+
computedWidth: f,
|
|
60
|
+
computedHeight: c
|
|
61
|
+
};
|
|
62
|
+
}),
|
|
63
|
+
height: c
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const _ = 8, x = 150;
|
|
68
|
+
function P({
|
|
69
|
+
images: o,
|
|
70
|
+
gap: t = _,
|
|
71
|
+
renderImage: e,
|
|
72
|
+
resizeDebounce: i = x,
|
|
73
|
+
lazyLoad: n = !0,
|
|
74
|
+
onImageLoad: l,
|
|
75
|
+
onImageError: h,
|
|
76
|
+
containerStyle: c,
|
|
77
|
+
rowStyle: w
|
|
78
|
+
}) {
|
|
79
|
+
const { containerRef: a, width: s } = B(i), f = U(() => s === 0 || o.length === 0 ? [] : O(o, s, t), [o, s, t]), $ = (u) => (r) => {
|
|
80
|
+
l == null || l(u, r);
|
|
81
|
+
}, R = (u) => (r) => {
|
|
82
|
+
h == null || h(u, r);
|
|
83
|
+
}, E = (u) => {
|
|
84
|
+
const { image: r, computedWidth: m, computedHeight: d } = u;
|
|
85
|
+
return /* @__PURE__ */ g(
|
|
86
|
+
"img",
|
|
87
|
+
{
|
|
88
|
+
src: r.src,
|
|
89
|
+
alt: r.alt ?? "",
|
|
90
|
+
width: m,
|
|
91
|
+
height: d,
|
|
92
|
+
loading: n ? "lazy" : void 0,
|
|
93
|
+
onLoad: $(r),
|
|
94
|
+
onError: R(r),
|
|
95
|
+
style: {
|
|
96
|
+
display: "block",
|
|
97
|
+
width: m,
|
|
98
|
+
height: d
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
return s === 0 ? /* @__PURE__ */ g(
|
|
104
|
+
"div",
|
|
105
|
+
{
|
|
106
|
+
ref: a,
|
|
107
|
+
role: "region",
|
|
108
|
+
"aria-label": "Image gallery",
|
|
109
|
+
style: {
|
|
110
|
+
width: "100%",
|
|
111
|
+
...c
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
) : /* @__PURE__ */ g(
|
|
115
|
+
"div",
|
|
116
|
+
{
|
|
117
|
+
ref: a,
|
|
118
|
+
role: "region",
|
|
119
|
+
"aria-label": "Image gallery",
|
|
120
|
+
style: {
|
|
121
|
+
width: "100%",
|
|
122
|
+
...c
|
|
123
|
+
},
|
|
124
|
+
children: f.map((u, r) => /* @__PURE__ */ g(
|
|
125
|
+
"div",
|
|
126
|
+
{
|
|
127
|
+
role: "group",
|
|
128
|
+
"aria-label": `Gallery row ${r + 1}`,
|
|
129
|
+
style: {
|
|
130
|
+
display: "flex",
|
|
131
|
+
gap: t,
|
|
132
|
+
marginBottom: r < f.length - 1 ? t : 0,
|
|
133
|
+
...w
|
|
134
|
+
},
|
|
135
|
+
children: u.images.map((m, d) => {
|
|
136
|
+
const { image: p, computedWidth: b, computedHeight: y } = m, W = d === 0, A = d === u.images.length - 1, v = {
|
|
137
|
+
image: p,
|
|
138
|
+
computedWidth: b,
|
|
139
|
+
computedHeight: y,
|
|
140
|
+
originalWidth: p.width,
|
|
141
|
+
originalHeight: p.height,
|
|
142
|
+
rowIndex: r,
|
|
143
|
+
imageIndex: d,
|
|
144
|
+
isFirstInRow: W,
|
|
145
|
+
isLastInRow: A
|
|
146
|
+
}, H = e ? e(v) : E(v);
|
|
147
|
+
return /* @__PURE__ */ g(
|
|
148
|
+
"div",
|
|
149
|
+
{
|
|
150
|
+
tabIndex: 0,
|
|
151
|
+
role: "img",
|
|
152
|
+
"aria-label": p.alt || `Image ${r + 1}-${d + 1}`,
|
|
153
|
+
style: {
|
|
154
|
+
width: b,
|
|
155
|
+
height: y,
|
|
156
|
+
flexShrink: 0
|
|
157
|
+
},
|
|
158
|
+
children: H
|
|
159
|
+
},
|
|
160
|
+
`${r}-${d}`
|
|
161
|
+
);
|
|
162
|
+
})
|
|
163
|
+
},
|
|
164
|
+
r
|
|
165
|
+
))
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
export {
|
|
170
|
+
P as JustifiedGallery
|
|
171
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@n-ivan/react-justified-gallery",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A React component for creating justified image galleries",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"gallery",
|
|
8
|
+
"justified",
|
|
9
|
+
"images",
|
|
10
|
+
"layout"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/n-ivan/react-justified-gallery#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/n-ivan/react-justified-gallery/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/n-ivan/react-justified-gallery.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Ivan Nesterovic",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/justified-gallery.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/justified-gallery.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc && vite build",
|
|
36
|
+
"dev": "vite",
|
|
37
|
+
"demo": "vite --config demo/vite.config.ts",
|
|
38
|
+
"test": "vitest",
|
|
39
|
+
"lint": "tsc --noEmit"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@testing-library/jest-dom": "^6.1.5",
|
|
43
|
+
"@testing-library/react": "^14.1.2",
|
|
44
|
+
"@types/node": "^25.0.5",
|
|
45
|
+
"@types/react": "^18.2.45",
|
|
46
|
+
"@types/react-dom": "^18.2.18",
|
|
47
|
+
"@vitejs/plugin-react": "^4.2.1",
|
|
48
|
+
"jsdom": "^23.0.1",
|
|
49
|
+
"typescript": "^5.3.3",
|
|
50
|
+
"vite": "^5.0.8",
|
|
51
|
+
"vitest": "^1.0.4"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"react": "^18.0.0",
|
|
55
|
+
"react-dom": "^18.0.0"
|
|
56
|
+
},
|
|
57
|
+
"module": "./dist/justified-gallery.js"
|
|
58
|
+
}
|