Inner Image Zoom

React Inner Image Zoom

  • Package

    react-inner-image-zoom

  • Source code

    Open repo on GitHub

  • Changelog

    Open changelog on GitHub

Quickstart

1

Install

react install react-inner-image-zoom

2

Import

import InnerImageZoom from 'react-inner-image-zoom'

3

Style

import 'react-inner-image-zoom/lib/styles.min.css'

4

Template

<InnerImageZoom src="/path/to/image.jpg" />

Installation

Note: Version 2.0.0 introduces React hooks and requires React v16.8.0 or above. To use this package with older versions of React, install with npm install react-inner-image-zoom@1.3.0 or yarn add react-inner-image-zoom@1.3.0 instead of the instructions below.

NPM

npm install react-inner-image-zoom

Yarn

yarn add react-inner-image-zoom

TypeScript

Type declarations were added with version 4.0.0. For older installations, type definitions are available through DefinitelyTyped and can be installed with:

npm install --save-dev @types/react-inner-image-zoom

Styling

You can download the raw styles.css file or, if your build supports it, import the stylesheet directly from node_modules using:

import 'react-inner-image-zoom/lib/styles.min.css';

Usage

Import and render the component:

import InnerImageZoom from 'react-inner-image-zoom'

...

<InnerImageZoom src="/path/to/image.jpg" zoomSrc="/path/to/zoom-image.jpg" />

Props

NameTypeDefaultDescription
srcstringRequired. URL for the original image.
sourcesarrayA list of image sources for using the picture tag to serve the appropriate original image (see below for more details).
widthnumberWidth attribute for original image.
heightnumberHeight attribute for original image.
hasSpacerbooleanfalseIf true, gets the original image's aspect ratio based on the width and height props and creates a spacer to prevent cumulative layout shift.
imgAttributesobjectImg and global attributes for the original image (excluding src, width, height, and style which are set elsewhere)
zoomSrcstringURL for the larger zoom image. Falls back to original image src if not defined.
zoomScalenumber1Multiplied against the natural width and height of the zoomed image. This will generally be a decimal (example, 0.9 for 90%).
zoomPreloadbooleanfalseIf set to true, preloads the zoom image instead of waiting for mouseenter and (unless on a touch device) persists the image on mouseleave.
moveType"pan"   |   "drag"panThe user behavior for moving zoomed images on non-touch devices.
zoomType"click"   |   "hover"clickThe user behavior for triggering zoom. When using hover, combine with zoomPreload to avoid flickering on rapid mouse movements.
fadeDurationnumber150Fade transition time in milliseconds. If zooming in on transparent images, set this to 0 for best results.
fullscreenOnMobilebooleanfalseEnables fullscreen zoomed image on touch devices below a specified breakpoint.
mobileBreakpointnumber640The maximum breakpoint for fullscreen zoom image when fullscreenOnMobile is true.
hideCloseButtonbooleanfalseHides the close button on touch devices. If set to true, zoom out is triggered by tap.
hideHintbooleanfalseHides the magnifying glass hint.
classNamestringCustom classname for styling the component.
afterZoomIn() => voidFunction to be called after zoom in.
afterZoomOut() => voidFunction to be called after zoom out.

Ref

The ref prop forwards an object with the container (the root figure element) and portal DOM nodes. portal grants access to the zoomed image on touch devices when fullscreenOnMobile is set and is only available while the image is zoomed. When using with TypeScript, InnerImageZoomRef can be imported to use as a type argument with useRef.

Sources

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:

NameTypeDefaultDescription
srcSetstringSrcset attribute for source tag.
sizesstringSizes attribute for source tag.
mediastringAn attribute containing a media condition for use with the srcset.
typestringAn image MIME type. This is useful for using newer formats like WebP.

Be the first to sponsor this project

And have your logo featured here

Sponsor