A React component for magnifying an image within its original container. The zoom behavior is triggered on click and includes default image panning on hover that switches to drag to move if a touch device is detected. The component supports responsive images and optional fullscreen zoom on mobile.
npm install react-inner-image-zoom
yarn add react-inner-image-zoom
Import and render the component:
import InnerImageZoom from 'react-inner-image-zoom';
...
<InnerImageZoom src="/path/to/image.jpg" zoomSrc="/path/to/zoom-image.jpg" />
This is the simplest usage. For additional examples, visit the demo page.
Prop | Type | Default | Description |
---|---|---|---|
src | String | (Required) URL for the original image. | |
srcSet | String | Default srcset attribute for a responsive original image. | |
sizes | String | Default sizes attribute for use with srcset. | |
sources | Array | A list of image sources for using the picture tag to serve the appropriate original image (see below for more details). | |
zoomSrc | String | URL for the larger zoom image. Falls back to original image src if not defined. | |
zoomSrcSet | String | Default srcset attribute for a responsive zoom image. Falls back to original image srcset if not defined | |
alt | String | Alternative text for the original image. | |
fadeDuration | Number | 150 | Fade transition time in milliseconds. |
fullscreenOnMobile | Boolean | false | Enables fullscreen zoomed image on touch devices below a specified breakpoint. |
mobileBreakpoint | Number | 640 | The maximum breakpoint for fullscreen zoom image when fullscreenOnMobile is true. |
className | String | Custom classname for styling the component. | |
afterZoomIn | Function | Function to be called after zoom in. | |
afterZoomOut | Function | Function to be called after zoom out. |
This prop accepts an array of objects which it uses to create a picture tag and source elements. The component looks for the following optional properties and you can find additional details on responsive images here:
Prop | Type | Default | Description |
---|---|---|---|
srcSet | String | Srcset attribute for source tag. | |
sizes | String | Sizes attribute for source tag. | |
media | String | An attribute containing a media condition for use with the srcset. | |
type | String | An image MIME type. This is useful for using newer formats like WebP. |
Please submit issues or requests here.
Most of the implementation choices for this component are based on use cases I've encountered in the past. For example, I chose a click to zoom trigger because it's been the most requested on product detail pages I've worked on. If there's a demand for zoom on hover or other additional functionality, I'd be open to looking into it so feel free to ask.