Install react-image-gallery
masterInstall the package using npm to add the responsive image gallery component to your React project.
npm install react-image-galleryrepository·master·Indexed 26 days ago
https://github.com/xiaolin/react-image-galleryA responsive and highly customizable React carousel image gallery component featuring thumbnail support, mobile swipe, fullscreen mode, and autoplay. Version 2.1.2 provides a flexible API for controlling navigation, custom rendering via render props, and programmatic control through the ImageGalleryRef interface.
Install the package using npm to add the responsive image gallery component to your React project.
npm install react-image-galleryTo use the gallery, import ImageGallery and its required CSS file. Provide an array of GalleryItem objects to the items prop. You can also use a ref with the ImageGalleryRef type to access imperative methods like play() or slideToIndex().
import { useRef } from "react";
import ImageGallery from "react-image-gallery";
import "react-image-gallery/styles/image-gallery.css";
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/",
},
{
original: "https://picsum.photos/id/1015/1000/600/",
thumbnail: "https://picsum.photos/id/1015/250/150/",
},
{
original: "https://picsum.photos/id/1019/1000/600/",
thumbnail: "https://picsum.photos/id/1019/250/150/",
},
];
function MyGallery() {
const galleryRef = useRef<ImageGalleryRef>(null);
return (
<ImageGallery
ref={galleryRef}
items={images}
onSlide={(index) => console.log("Slid to", index)}
/>
);
}You can access the following methods on the ImageGalleryRef instance to control the gallery programmatically:
play(): starts the slideshowpause(): pauses the slideshowtogglePlay(): toggles between play and pausefullScreen(): enters fullscreen modeexitFullScreen(): exits fullscreen modetoggleFullScreen(): toggles fullscreen modeslideToIndex(index): slides to a specific indexgetCurrentIndex(): returns the current indexUse the following render props to inject custom components into the gallery:
renderItem: Function for custom slide renderingrenderThumbInner: Function for custom thumbnail renderingrenderCustomControls: Function to render custom controls on the current sliderenderLeftNav: Function for custom left navigation componentrenderRightNav: Function for custom right navigation componentrenderTopNav: Function for custom top navigation (vertical mode)renderBottomNav: Function for custom bottom navigation (vertical mode)renderPlayPauseButton: Function for custom play/pause buttonrenderFullscreenButton: Function for custom fullscreen buttonThe items prop is a required array of objects. Each object represents a slide and can contain the following properties:
original: image source URLthumbnail: thumbnail source URLfullscreen: fullscreen image URL (defaults to original)originalHeight: image height (html5 attribute)originalWidth: image width (html5 attribute)loading: "lazy" or "eager" (HTML5 attribute)thumbnailHeight: image height (html5 attribute)thumbnailWidth: image width (html5 attribute)thumbnailLoading: "lazy" or "eager" (HTML5 attribute)originalClass: custom image classthumbnailClass: custom thumbnail classrenderItem: Function for custom rendering a specific sliderenderThumbInner: Function for custom thumbnail rendereroriginalAlt: image altthumbnailAlt: thumbnail image altoriginalTitle: image titlethumbnailTitle: thumbnail image titlethumbnailLabel: label for thumbnaildescription: description for imagesrcSet: image srcset (html5 attribute)sizes: image sizes (html5 attribute)bulletClass: extra class for the bullet of the itemUse the following callback props to respond to gallery interactions:
onImageError: callback(event) - overrides onErrorImageURLonThumbnailError: callback(event) - overrides onErrorImageURLonThumbnailClick: callback(event, index)onBulletClick: callback(event, index)onImageLoad: callback(event)onSlide: callback(currentIndex)onBeforeSlide: callback(nextIndex)onScreenChange: callback(isFullscreen)onPause: callback(currentIndex)onPlay: callback(currentIndex)onClick: callback(event)onTouchMove: callback(event)onTouchEnd: callback(event)onTouchStart: callback(event)onMouseOver: callback(event)onMouseLeave: callback(event)The ImageGallery component accepts several props to control its behavior and appearance:
Display & Navigation
infinite: Boolean, default true - loop infinitelyshowNav: Boolean, default trueshowThumbnails: Boolean, default truethumbnailPosition: String, default bottom (top, right, bottom, left)showFullscreenButton: Boolean, default trueuseBrowserFullscreen: Boolean, default true - if false, uses CSS-based fullscreenshowPlayButton: Boolean, default trueshowBullets: Boolean, default falsemaxBullets: Number, default undefined - max bullets shown (minimum 3, active bullet stays centered)showIndex: Boolean, default falseslideVertically: Boolean, default false - slide vertically instead of horizontallyisRTL: Boolean, default false - right-to-left modeAutoplay & Transitions
autoPlay: Boolean, default falseslideDuration: Number, default 550 (ms)slideInterval: Number, default 3000swipingTransitionDuration: Number, default 0 (ms)Interaction & Constraints
disableThumbnailScroll: Boolean, default falsedisableKeyDown: Boolean, default falsedisableSwipe: Boolean, default falsedisableThumbnailSwipe: Boolean, default falseflickThreshold: Number, default 0.4 - swipe velocity thresholdswipeThreshold: Number, default 30 - percentage of slide width needed to trigger navigationstopPropagation: Boolean, default false - call stopPropagation on swipe eventsuseWindowKeyDown: Boolean, default true - use window or element for key eventsOther
lazyLoad: Boolean, default falsestartIndex: Number, default 0onErrorImageURL: String, default undefined - fallback image URL for failed loadsindexSeparator: String, default ' / ' (ignored if showIndex is false)additionalClass: String, additional class for the root nodeThe ImageGallery component is the primary entry point for the library. It is a forward-ref component that accepts ImageGalleryProps and exposes an ImageGalleryRef.
import ImageGallery from 'react-image-gallery';
const items = [
{ original: 'image1.jpg', thumbnail: 'thumb1.jpg' },
{ original: 'image2.jpg', thumbnail: 'thumb2.jpg' },
];
const MyGallery = () => <ImageGallery items={items} />;// Note: The actual implementation is in ImageGallery.tsx
// This is the type definition for the default export
declare const ImageGallery: React.ForwardRefExoticComponent<
ImageGalleryProps & React.RefAttributes<ImageGalleryRef>
>;
export default ImageGallery;The ImageGalleryRef interface provides methods to programmatically control the gallery instance via a React ref. Use these methods to trigger playback, fullscreen mode, or specific slide navigation.
Available methods:
play(): Start auto-play.pause(): Stop auto-play.togglePlay(): Toggle auto-play state.fullScreen(): Enter fullscreen mode.exitFullScreen(): Exit fullscreen mode.toggleFullScreen(): Toggle fullscreen mode.slideToIndex(index: number, event?: SlideEvent): Navigate to a specific slide index.getCurrentIndex(): Returns the current slide index.export interface ImageGalleryRef {
play: () => void;
pause: () => void;
togglePlay: () => void;
fullScreen: () => void;
exitFullScreen: () => void;
toggleFullScreen: () => void;
slideToIndex: (index: number, event?: SlideEvent) => void;
getCurrentIndex: () => number;
}The GalleryItem interface defines the configuration for each individual image in the gallery. You can specify URLs for the original image, thumbnails, and fullscreen versions, as well as metadata like alt text, titles, and descriptions. It also supports custom rendering for the item itself or its thumbnail.
Key properties include:
original: URL of the main image (required).thumbnail: URL of the thumbnail image.fullscreen: URL of the fullscreen image.description: Text shown below the image.renderItem: Custom function to render the item.renderThumbInner: Custom function to render the thumbnail inner content.export interface GalleryItem {
original: string;
thumbnail?: string;
fullscreen?: string;
originalWidth?: string;
originalHeight?: string;
thumbnailWidth?: string | number;
thumbnailHeight?: string | number;
originalAlt?: string;
thumbnailAlt?: string;
originalTitle?: string;
thumbnailTitle?: string;
description?: string;
thumbnailLabel?: string;
originalClass?: string;
thumbnailClass?: string;
bulletClass?: string;
loading?: "eager" | "lazy";
thumbnailLoading?: "eager" | "lazy";
srcSet?: string;
sizes?: string;
imageSet?: ImageSet[];
renderItem?: (item: GalleryItem) => ReactNode;
renderThumbInner?: (item: GalleryItem) => ReactNode;
bulletOnClick?: (event: MouseEvent<HTMLButtonElement>, index: number) => void;
}The ImageGalleryProps interface defines the configuration options for the main ImageGallery component. This includes settings for autoplay, navigation visibility, thumbnail positioning, and event callbacks.
Common configuration categories:
showBullets, showFullscreenButton, showIndex, showNav, showPlayButton, showThumbnails.autoPlay, infinite, isRTL, lazyLoad, slideVertically, startIndex.thumbnailPosition ('top' | 'bottom' | 'left' | 'right'), slideDuration, slideInterval.renderItem, renderThumbInner, renderFullscreenButton, renderPlayPauseButton, etc.export interface ImageGalleryProps {
items: GalleryItem[];
additionalClass?: string;
autoPlay?: boolean;
disableKeyDown?: boolean;
disableSwipe?: boolean;
disableThumbnailScroll?: boolean;
disableThumbnailSwipe?: boolean;
flickThreshold?: number;
indexSeparator?: string;
infinite?: boolean;
isRTL?: boolean;
lazyLoad?: boolean;
onErrorImageURL?: string;
showBullets?: boolean;
maxBullets?: number;
showFullscreenButton?: boolean;
showIndex?: boolean;
showNav?: boolean;
showPlayButton?: boolean;
showThumbnails?: boolean;
slideDuration?: number;
slideInterval?: number;
slideOnThumbnailOver?: boolean;
slideVertically?: boolean;
startIndex?: number;
stopPropagation?: boolean;
swipeThreshold?: number;
swipingTransitionDuration?: number;
thumbnailPosition?: ThumbnailPosition;
useBrowserFullscreen?: boolean;
useTranslate3D?: boolean;
useWindowKeyDown?: boolean;
// Event Callbacks
onBeforeSlide?: OnBeforeSlideCallback;
onBulletClick?: OnBulletClickCallback;
onClick?: OnClickCallback;
onImageError?: OnImageErrorCallback;
onImageLoad?: OnImageLoadCallback;
onMouseLeave?: OnMouseCallback;
onMouseOver?: OnMouseCallback;
onPause?: OnPauseCallback;
onPlay?: OnPlayCallback;
onScreenChange?: OnScreenChangeCallback;
onSlide?: OnSlideCallback;
onThumbnailClick?: OnThumbnailClickCallback;
onThumbnailError?: OnThumbnailErrorCallback;
onTouchEnd?: OnTouchCallback;
onTouchMove?: OnTouchCallback;
onTouchStart?: OnTouchCallback;
// Render Callbacks
renderBottomNav?: RenderNavCallback;
renderCustomControls?: RenderCustomControlsCallback;
renderFullscreenButton?: RenderFullscreenCallback;
renderItem?: RenderItemCallback;
renderLeftNav?: RenderNavCallback;
renderPlayPauseButton?: RenderPlayPauseCallback;
renderRightNav?: RenderNavCallback;
renderThumbInner?: RenderThumbInnerCallback;
renderTopNav?: RenderNavCallback;
}