Skip to content

Commit 16abc60

Browse files
committed
refactor: Make lazy loading optional
1 parent f4f0326 commit 16abc60

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/ImageGallery.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,9 @@ test("image gallery works with custom thumbnail border", () => {
152152
/>
153153
);
154154
});
155+
156+
test("image gallery works with lazy loading", () => {
157+
render(
158+
<ImageGallery imagesInfoArray={imagesArray} lazy={true} lazyFromIndex={6} />
159+
);
160+
});

src/ImageGallery.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export function ImageGallery({
1010
gapSize = 24,
1111
fixedCaption = false,
1212
thumbnailBorder = "3px solid #fff",
13+
lazy = true,
14+
lazyFromIndex = 6,
1315
customStyles = {},
1416
}: ImageGalleryPropsType) {
1517
const [imgSrcInfo, setImgSrcInfo] = useState<ImgSrcInfoType | null>(null);
@@ -165,7 +167,7 @@ export function ImageGallery({
165167
}
166168
>
167169
<img
168-
loading={index > 5 ? "lazy" : "eager"}
170+
loading={lazy && index >= lazyFromIndex ? "lazy" : "eager"}
169171
alt={imageInfo.alt}
170172
src={imageInfo.gridSrc || imageInfo.src}
171173
onClick={() =>
@@ -310,7 +312,7 @@ export function ImageGallery({
310312
}
311313
>
312314
<img
313-
loading="lazy"
315+
loading={lazy ? "lazy" : "eager"}
314316
src={imgSrcInfo?.src}
315317
srcSet={imgSrcInfo?.srcSet}
316318
sizes={imgSrcInfo?.mediaSizes}
@@ -355,7 +357,7 @@ export function ImageGallery({
355357
>
356358
{imagesInfoArray.map((imageInfo, index) => (
357359
<img
358-
loading="lazy"
360+
loading={lazy ? "lazy" : "eager"}
359361
ref={slideNumber - 1 === index ? activeThumbImgRef : null}
360362
style={{
361363
border: slideNumber - 1 === index ? thumbnailBorder : "",

src/ImageGallery.types.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface ImageGalleryPropsType {
3131
gapSize?: number;
3232
fixedCaption?: boolean;
3333
thumbnailBorder?: string;
34+
lazy?: boolean;
35+
lazyFromIndex?: number;
3436
customStyles?: ImageGalleryStylesType;
3537
}
3638

0 commit comments

Comments
 (0)