Skip to content

Commit f7b62f8

Browse files
committed
add photoswipe lightbox, works
1 parent 2f9049d commit f7b62f8

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"github-slugger": "^2.0.0",
5050
"mdast-util-to-string": "^4.0.0",
5151
"object-treeify": "^5.0.1",
52+
"photoswipe": "^5.4.4",
5253
"react": "^18.3.1",
5354
"react-dom": "^18.3.1",
5455
"react-grid-gallery": "^1.0.1",

src/components/react/NewGallery.tsx

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { useEffect, useRef, useState } from 'react';
22

3+
import PhotoSwipeLightbox from 'photoswipe/lightbox';
4+
35
import type { ImageProps } from '@/libs/gallery/transform';
46
import type { FC } from 'react';
57

8+
import 'photoswipe/style.css';
9+
10+
import { GALLERY_ID } from '@/constants/gallery';
11+
612
interface Props {
713
images: ImageProps[];
814
}
915

10-
const PAGE_SIZE = 3 as const; // Todo: make it responsive
16+
// step
17+
const PAGE_SIZE = 6 as const; // Todo: make it responsive
18+
// page dependency in useEffect is more important
1119
const INITIAL_PAGE = 1 as const;
1220

1321
const fetchImagesUpToPage = (images: ImageProps[], nextPage: number): ImageProps[] => {
@@ -46,15 +54,42 @@ const NewGallery: FC<Props> = ({ images }) => {
4654
// page dependency is important for initial load to work for all resolutions
4755
}, [observerTarget, page]);
4856

57+
// lightbox
58+
useEffect(() => {
59+
let lightbox: PhotoSwipeLightbox | null = new PhotoSwipeLightbox({
60+
gallery: '#' + GALLERY_ID,
61+
children: 'a',
62+
pswpModule: () => import('photoswipe'),
63+
});
64+
lightbox.init();
65+
66+
return () => {
67+
lightbox?.destroy();
68+
lightbox = null;
69+
};
70+
}, []);
71+
4972
return (
5073
<>
51-
<div className="grid grid-cols-1 gap-1 md:grid-cols-2 lg:grid-cols-3">
74+
<div
75+
id={GALLERY_ID}
76+
className="pswp-gallery grid grid-cols-1 gap-1 sm:grid-cols-2 lg:grid-cols-3"
77+
>
5278
{loadedImages.map((image) => (
53-
<img key={image.xs.src} src={image.xs.src} alt="thumbnail image" className="" />
79+
<a
80+
key={`${GALLERY_ID}--${image.xl.src}`}
81+
href={image.xl.src}
82+
data-pswp-width={image.xl.width}
83+
data-pswp-height={image.xl.height}
84+
target="_blank"
85+
rel="noreferrer"
86+
>
87+
<img src={image.xs.src} alt="Gallery image" className="w-full" />
88+
</a>
5489
))}
5590
</div>
5691
{/* control threshold with margin-top */}
57-
<div ref={observerTarget} className="h-8 border border-red-500 mt-0"></div>
92+
<div ref={observerTarget} className="mt-0"></div>
5893
</>
5994
);
6095
};

src/constants/gallery.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export const EXCLUDE_IMAGES = ['avatar1.jpg', 'square-night1.jpg'];
1+
export const EXCLUDE_IMAGES = ['avatar1.jpg', 'square-night1.jpg'] as const;
2+
3+
export const GALLERY_ID = 'my-gallery' as const;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5340,6 +5340,11 @@ pend@~1.2.0:
53405340
resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50"
53415341
integrity sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==
53425342

5343+
photoswipe@^5.4.4:
5344+
version "5.4.4"
5345+
resolved "https://registry.yarnpkg.com/photoswipe/-/photoswipe-5.4.4.tgz#e045dc036453493188d5c8665b0e8f1000ac4d6e"
5346+
integrity sha512-WNFHoKrkZNnvFFhbHL93WDkW3ifwVOXSW3w1UuZZelSmgXpIGiZSNlZJq37rR8YejqME2rHs9EhH9ZvlvFH2NA==
5347+
53435348
picocolors@^1.0.0, picocolors@^1.1.1:
53445349
version "1.1.1"
53455350
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.1.1.tgz#3d321af3eab939b083c8f929a1d12cda81c26b6b"

0 commit comments

Comments
 (0)