1.2.4 • Published 2 years ago

zoom-loading-detector v1.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

zoom-loading-detector

GitHub Actions npm package TypeScript definitions on DefinitelyTyped

A React component for magnifying an image within its original container. Zoom behavior can be triggered on click or hover and the zoomed image can be moved by dragging on touch devices and either dragging or pan on hover on non-touch devices. The component supports responsive images, loading placeholders, optional fullscreen zoom on mobile, and more. In addition a loading view will can be added when the image load for the first time then the zoom feature will work perfectly.

NPM link

https://www.npmjs.com/package/zoom-loading-detector

Installation

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

NPM

npm install zoom-loading-detector

Yarn

yarn add zoom-loading-detector

TypeScript

For TypeScript users, type definitions are available through DefinitelyTyped and can be installed with:

npm install --save-dev @types/zoom-loading-detector

Styling

I was originally importing the CSS directly into the component but I've recently realized that makes too many assumptions about the wider build process. You can now download the raw CSS file at:

/src/InnerImageZoom/styles.css

or the minified raw minified version at:

/src/InnerImageZoom/styles.min.css

to include however you see fit. Or, if your setup supports it, import the files directory from your node_modules using:

import 'zoom-loading-detector/lib/InnerImageZoom/styles.css';

or:

import 'zoom-loading-detector/lib/InnerImageZoom/styles.min.css';

Usage

Import and render the component:

import InnerImageZoom from 'zoom-loading-detector';
import 'react-activity/dist/Sentry.css'
...

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

Props

PropTypeDefaultDescription
srcString(Required) 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). The imgAttributes keys should follow the React DOM element naming conventions.
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.
moveTypeStringpanpan or drag. The user behavior for moving zoomed images on non-touch devices.
zoomTypeStringclickclick or hover. The 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.
afterZoomInFunctionFunction to be called after zoom in.
afterZoomOutFunctionFunction to be called after zoom out.
loaderColorstringrgba(0,0,0,1)the loading view color ,
loaderSizenumber30number to determine the size of the loading view size,
overrideLoaderstyleobjectstyle object, it will override the small loader style,
overrideLoadingContainerStyleobjectit will override the whole loader style,
setContainerWidthfunctioncallback function that return the width of the whole container of the image,
setContainerHeightfunctioncallback function that returns the height of the whole container of the image
containerHeightnumberheight returned from the setContainer state,
containerWidthnumberwidth returned from the setContainer width,

code example

import InnerImageZoom from 'zoom-loading-detector';
import 'react-activity/dist/Sentry.css'

const ZoomWithLoading =()=>{
    
  const [containerHeight, setContainerHeight] = useState(0)
  const [containerWidth, setContainerWidth] = useState(0)
  const [zoomIn, setZoomIn] = useState(false)

    return(
        <InnerImageZoom
          setContainerWidth={(containerWidth) => {
            setContainerWidth(containerWidth)
          }}
          setContainerHeight={(containerHeight) => {
            setContainerHeight(containerHeight)
          }}
          overrideLoaderstyle={{
            top: containerHeight / 2.25,
            left: containerWidth / 2.25
          }}
          loaderSize={containerHeight / 16}
          loaderColor={'rgba(1,1,1,1)'}
          overrideLoadingContainerStyle={{ backgroundColor: 'rgba(213,213,213,0.22)' }}
          className={`${zoomIn ? 'w-full ' : 'w-3/5'}`}
          fullscreenOnMobile={true}
          src={'/path/to/image.jpg'}
          zoomSrc={'/path/to/image.jpg'}
          zoomPreload={false}
          hideHint={true}
          afterZoomIn={() => setZoomIn(true)}
          afterZoomOut={() => setZoomIn(false)}
        />
    )
}
    
1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago