React PhotoSwipe Gallery Component

4.0.0 · active · verified Tue Apr 21

react-photoswipe-gallery is a React component wrapper that integrates the PhotoSwipe JavaScript image gallery library into React applications. It simplifies the process of creating responsive image galleries by abstracting PhotoSwipe's imperative API into declarative React components like `Gallery` and `Item`. The current stable version is 4.0.0, which includes breaking changes focused on security and refactoring. The project maintains a moderate release cadence, with patch and minor updates appearing every few months, and major versions released less frequently. Its key differentiator lies in providing a native React experience for PhotoSwipe, managing the gallery's lifecycle and element references within the React component model, reducing boilerplate code, and ensuring compatibility with modern React features.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up a basic image gallery using the `Gallery` and `Item` components. It includes the necessary PhotoSwipe CSS, custom options for the gallery, and correctly handles image references and click events to open the lightbox, showcasing two sample images.

import React, { useRef } from 'react';
import { Gallery, Item } from 'react-photoswipe-gallery';
import 'photoswipe/dist/photoswipe.css'; // Essential PhotoSwipe base styles

const MyPhotoGallery: React.FC = () => {
  return (
    <Gallery
      options={{
        bgOpacity: 0.8,
        zoomAnimationDuration: 300
      }}
    >
      <Item
        original="https://picsum.photos/id/1018/1000/600/"
        thumbnail="https://picsum.photos/id/1018/100/60/"
        width="1000"
        height="600"
        caption="A serene landscape from Unsplash"
        cropped
      >
        {({ ref, open }) => (
          <img
            ref={ref as React.MutableRefObject<HTMLImageElement>}
            onClick={open}
            src="https://picsum.photos/id/1018/100/60/"
            alt="Landscape thumbnail"
            style={{ cursor: 'pointer', margin: '5px', borderRadius: '4px' }}
          />
        )}
      </Item>
      <Item
        original="https://picsum.photos/id/1015/1000/600/"
        thumbnail="https://picsum.photos/id/1015/100/60/"
        width="1000"
        height="600"
        caption="Abstract patterns and colors"
        cropped
      >
        {({ ref, open }) => (
          <img
            ref={ref as React.MutableRefObject<HTMLImageElement>}
            onClick={open}
            src="https://picsum.photos/id/1015/100/60/"
            alt="Abstract thumbnail"
            style={{ cursor: 'pointer', margin: '5px', borderRadius: '4px' }}
          />
        )}
      </Item>
    </Gallery>
  );
};

export default MyPhotoGallery;

view raw JSON →