|
1 |
| -import { useState } from 'react'; |
| 1 | +import { useEffect, useMemo, useRef, useState } from 'react'; |
2 | 2 |
|
3 |
| -import { Gallery as ReactGridGallery } from 'react-grid-gallery'; |
4 |
| -import Lightbox from 'react-image-lightbox'; |
| 3 | +import debounce from 'lodash.debounce'; |
| 4 | +import PhotoSwipeLightbox from 'photoswipe/lightbox'; |
5 | 5 |
|
6 |
| -import type { ImageProps } from '@/types/common'; |
| 6 | +import { GALLERY } from '@/constants/gallery'; |
| 7 | +import { cn } from '@/utils/styles'; |
7 | 8 |
|
8 |
| -import 'react-image-lightbox/style.css'; |
| 9 | +import type { GalleryImage } from '@/types/image'; |
| 10 | +import type { FC } from 'react'; |
| 11 | + |
| 12 | +import 'photoswipe/style.css'; |
9 | 13 |
|
10 | 14 | interface Props {
|
11 |
| - images: ImageProps[]; |
| 15 | + images: GalleryImage[]; |
12 | 16 | }
|
13 | 17 |
|
14 |
| -// global polyfill for react-image-lightbox |
15 |
| -window.global = window.global || window; |
| 18 | +type LoadedStates = Record<string, boolean>; |
| 19 | + |
| 20 | +const { PAGE_SIZE, INITIAL_PAGE, OBSERVER_DEBOUNCE, GALLERY_ID } = GALLERY; |
| 21 | + |
| 22 | +const fetchImagesUpToPage = (images: GalleryImage[], nextPage: number): GalleryImage[] => { |
| 23 | + const endIndex = nextPage * PAGE_SIZE; |
| 24 | + return images.slice(0, endIndex); |
| 25 | +}; |
| 26 | + |
| 27 | +const Gallery: FC<Props> = ({ images }) => { |
| 28 | + const [loadedImages, setLoadedImages] = useState<GalleryImage[]>([]); |
| 29 | + const [page, setPage] = useState<number>(INITIAL_PAGE); |
| 30 | + const observerTarget = useRef(null); |
| 31 | + |
| 32 | + const [loadedStates, setLoadedStates] = useState<LoadedStates>({}); |
| 33 | + // calculate if new page is loaded on scroll |
| 34 | + // not for blur transition |
| 35 | + const isAllImagesLoaded = useMemo( |
| 36 | + () => Object.values(loadedStates).every(Boolean), |
| 37 | + [loadedStates, loadedImages.length] |
| 38 | + ); |
| 39 | + |
| 40 | + const isEnd = loadedImages.length === images.length; |
| 41 | + |
| 42 | + // converts page to loaded images |
| 43 | + useEffect(() => { |
| 44 | + const upToPageImages = fetchImagesUpToPage(images, page); |
| 45 | + setLoadedImages(upToPageImages); |
| 46 | + }, [page, images]); |
| 47 | + |
| 48 | + // sets only page |
| 49 | + useEffect(() => { |
| 50 | + const callback: IntersectionObserverCallback = (entries) => { |
| 51 | + // must wait here for images to load |
| 52 | + if (!isEnd && isAllImagesLoaded && entries[0].isIntersecting) { |
| 53 | + setPage((prevPage) => prevPage + 1); |
| 54 | + } |
| 55 | + }; |
| 56 | + const debouncedCallback = debounce(callback, OBSERVER_DEBOUNCE); |
| 57 | + const options: IntersectionObserverInit = { threshold: 1 }; |
| 58 | + |
| 59 | + const observer = new IntersectionObserver(debouncedCallback, options); |
| 60 | + |
| 61 | + const observerRef = observerTarget.current; |
| 62 | + if (observerRef) observer.observe(observerRef); |
| 63 | + |
| 64 | + return () => { |
| 65 | + if (observerRef) observer.unobserve(observerRef); |
| 66 | + }; |
| 67 | + // page dependency is important for initial load to work for all resolutions |
| 68 | + }, [observerTarget, page, isEnd]); |
16 | 69 |
|
17 |
| -const Gallery: React.FC<Props> = ({ images }) => { |
18 |
| - const [index, setIndex] = useState(-1); |
| 70 | + // lightbox |
| 71 | + useEffect(() => { |
| 72 | + let lightbox: PhotoSwipeLightbox | null = new PhotoSwipeLightbox({ |
| 73 | + gallery: '#' + GALLERY_ID, |
| 74 | + children: 'a', |
| 75 | + pswpModule: () => import('photoswipe'), |
| 76 | + }); |
| 77 | + lightbox.init(); |
19 | 78 |
|
20 |
| - const currentImage = images[index]; |
21 |
| - const nextIndex = (index + 1) % images.length; |
22 |
| - const nextImage = images[nextIndex] || currentImage; |
23 |
| - const prevIndex = (index + images.length - 1) % images.length; |
24 |
| - const prevImage = images[prevIndex] || currentImage; |
| 79 | + return () => { |
| 80 | + lightbox?.destroy(); |
| 81 | + lightbox = null; |
| 82 | + }; |
| 83 | + }, []); |
25 | 84 |
|
26 |
| - const handleClick = (index: number, _item: ImageProps) => setIndex(index); |
27 |
| - const handleClose = () => setIndex(-1); |
28 |
| - const handleMovePrev = () => setIndex(prevIndex); |
29 |
| - const handleMoveNext = () => setIndex(nextIndex); |
| 85 | + const handleLoad = (src: string) => { |
| 86 | + setLoadedStates((prev) => ({ ...prev, [src]: true })); |
| 87 | + }; |
30 | 88 |
|
31 | 89 | return (
|
32 |
| - <div> |
33 |
| - <ReactGridGallery images={images} onClick={handleClick} enableImageSelection={false} /> |
34 |
| - {!!currentImage && ( |
35 |
| - <Lightbox |
36 |
| - imageTitle={currentImage.caption} |
37 |
| - mainSrc={currentImage.originalSrc} |
38 |
| - mainSrcThumbnail={currentImage.src} |
39 |
| - nextSrc={nextImage.originalSrc} |
40 |
| - nextSrcThumbnail={nextImage.src} |
41 |
| - prevSrc={prevImage.originalSrc} |
42 |
| - prevSrcThumbnail={prevImage.src} |
43 |
| - onCloseRequest={handleClose} |
44 |
| - onMovePrevRequest={handleMovePrev} |
45 |
| - onMoveNextRequest={handleMoveNext} |
46 |
| - /> |
47 |
| - )} |
48 |
| - </div> |
| 90 | + <> |
| 91 | + <div |
| 92 | + id={GALLERY_ID} |
| 93 | + className="pswp-gallery grid grid-cols-1 gap-1 sm:grid-cols-2 lg:grid-cols-3" |
| 94 | + > |
| 95 | + {loadedImages.map((image) => ( |
| 96 | + <a |
| 97 | + key={`${GALLERY_ID}--${image.lightbox.src}`} |
| 98 | + // lightbox doesn't support responsive image |
| 99 | + href={image.lightbox.src} |
| 100 | + data-pswp-width={image.lightbox.width} |
| 101 | + data-pswp-height={image.lightbox.height} |
| 102 | + target="_blank" |
| 103 | + rel="noreferrer" |
| 104 | + > |
| 105 | + <img |
| 106 | + {...image.thumbnail} |
| 107 | + onLoad={() => handleLoad(image.thumbnail.src)} |
| 108 | + alt="Gallery image" |
| 109 | + className={cn( |
| 110 | + 'w-full transition-all duration-[2s] ease-in-out', |
| 111 | + loadedStates[image.thumbnail.src] |
| 112 | + ? 'opacity-100 blur-0 grayscale-0' |
| 113 | + : 'opacity-75 blur-sm grayscale' |
| 114 | + )} |
| 115 | + /> |
| 116 | + </a> |
| 117 | + ))} |
| 118 | + </div> |
| 119 | + {/* control threshold with margin-top */} |
| 120 | + <div ref={observerTarget} className="mt-0"></div> |
| 121 | + </> |
49 | 122 | );
|
50 | 123 | };
|
51 | 124 |
|
|
0 commit comments