react-image-gallery

repository·master·Indexed 26 days ago

https://github.com/xiaolin/react-image-gallery

A 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.

Tokens
4K
Snippets
6
Records
11
Agent score
37%

What's inside react-image-gallery

  1. Basic usage of ImageGallery

    master

    To 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)}
        />
      );
    }
  2. Control ImageGallery via Ref

    master

    You can access the following methods on the ImageGalleryRef instance to control the gallery programmatically:

    • play(): starts the slideshow
    • pause(): pauses the slideshow
    • togglePlay(): toggles between play and pause
    • fullScreen(): enters fullscreen mode
    • exitFullScreen(): exits fullscreen mode
    • toggleFullScreen(): toggles fullscreen mode
    • slideToIndex(index): slides to a specific index
    • getCurrentIndex(): returns the current index
  3. Customize ImageGallery rendering

    master

    Use the following render props to inject custom components into the gallery:

    • renderItem: Function for custom slide rendering
    • renderThumbInner: Function for custom thumbnail rendering
    • renderCustomControls: Function to render custom controls on the current slide
    • renderLeftNav: Function for custom left navigation component
    • renderRightNav: Function for custom right navigation component
    • renderTopNav: Function for custom top navigation (vertical mode)
    • renderBottomNav: Function for custom bottom navigation (vertical mode)
    • renderPlayPauseButton: Function for custom play/pause button
    • renderFullscreenButton: Function for custom fullscreen button
  4. Configure ImageGallery items

    master

    The items prop is a required array of objects. Each object represents a slide and can contain the following properties:

    • original: image source URL
    • thumbnail: thumbnail source URL
    • fullscreen: 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 class
    • thumbnailClass: custom thumbnail class
    • renderItem: Function for custom rendering a specific slide
    • renderThumbInner: Function for custom thumbnail renderer
    • originalAlt: image alt
    • thumbnailAlt: thumbnail image alt
    • originalTitle: image title
    • thumbnailTitle: thumbnail image title
    • thumbnailLabel: label for thumbnail
    • description: description for image
    • srcSet: image srcset (html5 attribute)
    • sizes: image sizes (html5 attribute)
    • bulletClass: extra class for the bullet of the item
  5. Handle ImageGallery events

    master

    Use the following callback props to respond to gallery interactions:

    • onImageError: callback(event) - overrides onErrorImageURL
    • onThumbnailError: callback(event) - overrides onErrorImageURL
    • onThumbnailClick: 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)
  6. Configure ImageGallery component props

    master

    The ImageGallery component accepts several props to control its behavior and appearance:

    Display & Navigation

    • infinite: Boolean, default true - loop infinitely
    • showNav: Boolean, default true
    • showThumbnails: Boolean, default true
    • thumbnailPosition: String, default bottom (top, right, bottom, left)
    • showFullscreenButton: Boolean, default true
    • useBrowserFullscreen: Boolean, default true - if false, uses CSS-based fullscreen
    • showPlayButton: Boolean, default true
    • showBullets: Boolean, default false
    • maxBullets: Number, default undefined - max bullets shown (minimum 3, active bullet stays centered)
    • showIndex: Boolean, default false
    • slideVertically: Boolean, default false - slide vertically instead of horizontally
    • isRTL: Boolean, default false - right-to-left mode

    Autoplay & Transitions

    • autoPlay: Boolean, default false
    • slideDuration: Number, default 550 (ms)
    • slideInterval: Number, default 3000
    • swipingTransitionDuration: Number, default 0 (ms)

    Interaction & Constraints

    • disableThumbnailScroll: Boolean, default false
    • disableKeyDown: Boolean, default false
    • disableSwipe: Boolean, default false
    • disableThumbnailSwipe: Boolean, default false
    • flickThreshold: Number, default 0.4 - swipe velocity threshold
    • swipeThreshold: Number, default 30 - percentage of slide width needed to trigger navigation
    • stopPropagation: Boolean, default false - call stopPropagation on swipe events
    • useWindowKeyDown: Boolean, default true - use window or element for key events

    Other

    • lazyLoad: Boolean, default false
    • startIndex: Number, default 0
    • onErrorImageURL: String, default undefined - fallback image URL for failed loads
    • indexSeparator: String, default ' / ' (ignored if showIndex is false)
    • additionalClass: String, additional class for the root node
  7. Use the ImageGallery component

    master

    The 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;
  8. Use ImageGalleryRef to control the gallery

    master

    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;
    }
  9. Configure GalleryItem objects

    master

    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;
    }
  10. Configure ImageGalleryProps

    master

    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:

    • Display Controls: showBullets, showFullscreenButton, showIndex, showNav, showPlayButton, showThumbnails.
    • Behavior: autoPlay, infinite, isRTL, lazyLoad, slideVertically, startIndex.
    • Navigation: thumbnailPosition ('top' | 'bottom' | 'left' | 'right'), slideDuration, slideInterval.
    • Custom Rendering: 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;
    }