Zoom Plugin

The Zoom plugin adds a zoom feature to the lightbox. Zoom is supported for image slides by default and can be enabled for custom slide types.

The plugin supports the following input devices and gestures:

Input deviceZoomPan
Touchscreen
  • pinch-to-zoom
  • double-tap
  • swipe
Touchpad
  • pinch-to-zoom
  • double-tap
  • double-click
  • scroll (swipe with two fingers)
  • click-and-drag
Keyboard
  • +, -
  •  + =,  + -,  + 0
  •  + =,  + -,  + 0
  • , , ,
Mouse
  • zoom (Ctrl + mouse wheel)
  • double-click
  • scroll (mouse wheel)
  • click-and-drag

Documentation

The Zoom plugin adds the following Lightbox properties.

PropertyTypeDescription
zoom

{
  ref?: React.ForwardedRef​<ZoomRef>;
  minZoom?: number;
  maxZoomPixelRatio?: number;
  zoomInMultiplier?: number;
  doubleTapDelay?: number;
  doubleClickDelay?: number;
  doubleClickMaxStops?: number;
  keyboardMoveDistance?: number;
  wheelZoomDistanceFactor?: number;
  pinchZoomDistanceFactor?: number;
  pinchZoomV4?: boolean;
  scrollToZoom?: boolean;
  supports?: readonly SlideTypeKey[];
  maxZoom?: number | ((slide: Slide) => number | undefined);
}

Zoom plugin settings:

  • ref - Zoom plugin ref. See Zoom Ref for details.
  • minZoom - override minimum zoom level (default: 1.0)
  • maxZoomPixelRatio - ratio of image pixels to physical pixels at maximum zoom level
  • zoomInMultiplier - zoom-in multiplier
  • doubleTapDelay - double-tap maximum time delay (deprecated)
  • doubleClickDelay - double-click maximum time delay (deprecated)
  • doubleClickMaxStops - maximum number of zoom-in stops via double-click or double-tap
  • keyboardMoveDistance - keyboard move distance
  • wheelZoomDistanceFactor - wheel zoom distance factor
  • pinchZoomDistanceFactor - pinch zoom distance factor (deprecated)
  • pinchZoomV4 - if true, enables the experimental pinch zoom implementation slated for v4
  • scrollToZoom - if true, enables image zoom via scroll gestures for mouse and trackpad users
  • supports - custom slide types that support zoom (e.g., ["custom-slide"])
  • maxZoom - maximum zoom level for custom slide types; when a function, return undefined to use the default (default: 8)

Default: { minZoom: 1, maxZoomPixelRatio: 1, zoomInMultiplier: 2, doubleTapDelay: 300, doubleClickDelay: 500, doubleClickMaxStops: 2, keyboardMoveDistance: 50, wheelZoomDistanceFactor: 100, pinchZoomDistanceFactor: 100, scrollToZoom: false, maxZoom: 8 }

animation

{
  zoom?: number;
}

Zoom animation duration.

Default: 500

render

{
  slide?: ({..., zoom, maxZoom}: {..., zoom: number; maxZoom: number }) => React.ReactNode;
  buttonZoom?: (props: ZoomRef) => React.ReactNode;
  iconZoomIn?: () => React.ReactNode;
  iconZoomOut?: () => React.ReactNode;
}

Custom render functions:

  • slide - Zoom plugin adds zoom and maxZoom props to the slide render function
  • buttonZoom - render custom Zoom In / Zoom Out control
  • iconZoomIn - render custom Zoom In icon
  • iconZoomOut - render custom Zoom Out icon
on

{
  zoom?: ({ zoom }: { zoom: number }) => void;
}

Callbacks:

  • zoom - a callback called when zoom level changes

Custom Slide Types

By default, the Zoom plugin supports only image slides. You can enable zoom for custom slide types using the supports and maxZoom properties.

<Lightbox
  slides={slides}
  plugins={[Zoom]}
  zoom={{
    // enable zoom for custom slide types
    supports: ["custom-slide"],
    // maximum zoom level for custom slide types (default: 8)
    maxZoom: 4,
    // or use a function for per-slide max zoom
    // maxZoom: (slide) => (slide.type === "custom-slide" ? 4 : 8),
  }}
  render={{
    slide: ({ slide, zoom, maxZoom }) => {
      if (slide.type === "custom-slide") {
        return <MyCustomSlide slide={slide} zoom={zoom} maxZoom={maxZoom} />;
      }
    },
  }}
/>

Custom slide types must provide a render.slide function that renders the slide content. The Zoom plugin wraps the rendered content with a zoom container that handles all zoom gestures, transformations, and offset clamping automatically.

For image slides, the maximum zoom level is calculated from the image dimensions and maxZoomPixelRatio. Custom render functions for image slides are also supported as long as the slide provides width and height attributes. For custom slide types, use maxZoom to set the maximum zoom level (default: 8). When maxZoom is a function, return undefined to use the default value.

TypeScript users must augment the SlideTypes interface to register custom slide types. See Custom Slides for details.

Zoom Ref

The Zoom plugin provides a ref object to control the plugin features externally.

// Zoom ref usage example

const zoomRef = React.useRef(null);

// ...

return (
  <>
    <Lightbox
      slides={slides}
      plugins={[Inline, Zoom]}
      zoom={{ ref: zoomRef }}
      inline={{
        style: { width: "100%", maxWidth: "900px", aspectRatio: "3 / 2" },
      }}
    />

    <button type="button" onClick={() => zoomRef.current?.zoomIn()}>
      Zoom In
    </button>

    <button type="button" onClick={() => zoomRef.current?.zoomOut()}>
      Zoom Out
    </button>
  </>
);
PropertyTypeDescription
zoomnumberCurrent zoom level.
maxZoomnumberMaximum zoom level.
offsetXnumberHorizontal offset.
offsetYnumberVertical offset.
disabledboolean
If true, zoom is unavailable for the current slide.
zoomIn() => void
Increase zoom level using zoomInMultiplier.
zoomOut() => void
Decrease zoom level using zoomInMultiplier.
changeZoom(targetZoom: number, rapid?: boolean, dx?: number, dy?: number) => void

Change zoom level.

  • targetZoom - target zoom value
  • rapid - if true, change zoom level without animation effect
  • dx - horizontal offset
  • dy - vertical offset

Example

import Lightbox from "yet-another-react-lightbox";
import Zoom from "yet-another-react-lightbox/plugins/zoom";
import "yet-another-react-lightbox/styles.css";

// ...

return (
  <Lightbox
    plugins={[Zoom]}
    // ...
  />
);

Live Demo

You can adjust the live demo settings below.

Sandbox

Edit on StackBlitz