React Image Gallery

2.1.2 · active · verified Sun Apr 19

react-image-gallery is a feature-rich, responsive image gallery component designed for React applications. Currently stable at version 2.1.2, it receives regular patch updates, indicating active maintenance. Key capabilities include native mobile swipe gestures, customizable thumbnails with various positioning options, browser or CSS-based fullscreen modes, comprehensive keyboard navigation, and built-in RTL support. It distinguishes itself by offering extensive customization via CSS custom properties and the ability to render custom content like videos or iframes within slides. The library aims to provide a robust and flexible solution for displaying image carousels, supporting modern React versions from 16 through 19 via peer dependencies, and ships with TypeScript types for an enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the ImageGallery component with a basic set of images, including TypeScript types for props and refs, and shows an example of programmatic control. It also includes the crucial CSS import for proper styling.

import { useRef } from "react";
import ImageGallery from "react-image-gallery";
import "react-image-gallery/styles/image-gallery.css"; // Essential for styling since v2.1.0
import type { GalleryItem, ImageGalleryRef } from "react-image-gallery";

const images: GalleryItem[] = [
  {
    original: "https://picsum.photos/id/1018/1000/600/",
    thumbnail: "https://picsum.photos/id/1018/250/150/",
    description: "A beautiful mountain landscape"
  },
  {
    original: "https://picsum.photos/id/1015/1000/600/",
    thumbnail: "https://picsum.photos/id/1015/250/150/",
    description: "A calm lake with boats"
  },
  {
    original: "https://picsum.photos/id/1019/1000/600/",
    thumbnail: "https://picsum.photos/id/1019/250/150/",
    description: "City skyline at sunset"
  },
];

function MyGallery() {
  const galleryRef = useRef<ImageGalleryRef>(null);

  return (
    <div style={{ maxWidth: '800px', margin: 'auto', padding: '20px' }}>
      <h2>My Awesome Image Gallery</h2>
      <ImageGallery
        ref={galleryRef}
        items={images}
        onSlide={(index) => console.log("Slid to", index)}
        showFullscreenButton={true}
        showPlayButton={true}
        lazyLoad={true}
        thumbnailPosition="bottom"
      />
      <button onClick={() => galleryRef.current?.slideToIndex(0)} style={{ marginTop: '15px' }}>
        Go to First Slide
      </button>
    </div>
  );
}

export default MyGallery;

view raw JSON →