-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: factor out remove-react-images for lightgallery (#270)
This PR swaps out **react-images** with [**lightgallery**](https://www.lightgalleryjs.com/). The former dependency has a README that says, "Don't use this in a new project. This package hasn't been properly maintained in a long time and there are much better options available."
- Loading branch information
Showing
7 changed files
with
370 additions
and
892 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,47 @@ | ||
/** @jsx jsx */ | ||
import { jsx } from 'theme-ui' | ||
import { useState, useCallback } from 'react' | ||
import { useRef, useCallback } from 'react' | ||
|
||
import 'lightgallery/css/lg-thumbnail.css' | ||
import 'lightgallery/css/lg-zoom.css' | ||
import 'lightgallery/css/lightgallery.css' | ||
import Gallery from 'react-photo-gallery' | ||
import Carousel, { Modal, ModalGateway } from 'react-images' | ||
import lgThumbnail from 'lightgallery/plugins/thumbnail' | ||
import lgZoom from 'lightgallery/plugins/zoom' | ||
import LightGallery from 'lightgallery/react' | ||
|
||
export const PhotoGallery = ({ photos }) => { | ||
const [currentImage, setCurrentImage] = useState(0) | ||
const [viewerIsOpen, setViewerIsOpen] = useState(false) | ||
const lightGalleryRef = useRef(null) | ||
|
||
const openLightbox = useCallback((event, { index }) => { | ||
setCurrentImage(index) | ||
setViewerIsOpen(true) | ||
const instance = lightGalleryRef.current | ||
if (instance) { | ||
instance.openGallery(index) | ||
} else { | ||
console.error('LightGallery instance is not initialized') | ||
} | ||
}, []) | ||
|
||
const closeLightbox = () => { | ||
setCurrentImage(0) | ||
setViewerIsOpen(false) | ||
} | ||
|
||
return ( | ||
<div sx={{ mb: 4 }}> | ||
{/* Render photo gallery */} | ||
<Gallery photos={photos} onClick={openLightbox} /> | ||
<ModalGateway> | ||
{viewerIsOpen ? ( | ||
<Modal onClose={closeLightbox}> | ||
<Carousel | ||
currentIndex={currentImage} | ||
views={photos.map(x => ({ | ||
...x, | ||
srcset: x.srcSet, | ||
caption: x.title | ||
}))} | ||
/> | ||
</Modal> | ||
) : null} | ||
</ModalGateway> | ||
|
||
{/* Initialize LightGallery */} | ||
<LightGallery | ||
onInit={ref => { | ||
lightGalleryRef.current = ref.instance | ||
}} | ||
plugins={[lgThumbnail, lgZoom]} | ||
download={false} | ||
dynamic | ||
dynamicEl={photos.map(photo => ({ | ||
src: photo.src, | ||
thumb: photo.src, | ||
subHtml: photo.title || '' | ||
}))} | ||
speed={1000} | ||
/> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.