Gathering detailed insights and metrics for react-medium-image-zoom
Gathering detailed insights and metrics for react-medium-image-zoom
Gathering detailed insights and metrics for react-medium-image-zoom
Gathering detailed insights and metrics for react-medium-image-zoom
π π The original medium.com-inspired image zooming library for React (since 2016)
npm install react-medium-image-zoom
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,907 Stars
1,433 Commits
100 Forks
11 Watching
6 Branches
20 Contributors
Updated on 29 Nov 2024
Minified
Minified + Gzipped
TypeScript (91.36%)
CSS (4.2%)
JavaScript (2.73%)
Nix (0.67%)
MDX (0.5%)
Shell (0.38%)
Dockerfile (0.17%)
Cumulative downloads
Total Downloads
Last day
-13.7%
18,806
Compared to previous day
Last week
-4.7%
112,913
Compared to previous week
Last month
-4.8%
498,429
Compared to previous month
Last year
86.2%
5,598,886
Compared to previous year
32
The original medium.com-inspired image zooming library for React.
View the storybook examples to see various usages.
Features:
<img />
, including all object-fit
values, any object-position
,
and loading="lazy"
<div>
and <span>
with any background-image
,
background-size
,
and background-position
<picture>
with <source />
and <img />
<figure>
with <img />
<svg>
dependencies
Requirements to know about:
<dialog>
element (caniuse dialog)ResizeObserver
(caniuse ResizeObserver)ES2021
. If you need to support older environments,
run this package through your build system.1npm install --save react-medium-image-zoom
1import React from 'react' 2import Zoom from 'react-medium-image-zoom' 3import 'react-medium-image-zoom/dist/styles.css' 4 5export const MyImg = () => ( 6 <Zoom> 7 <img 8 alt="That Wanaka Tree, New Zealand by Laura Smetsers" 9 src="/path/to/thatwanakatree.jpg" 10 width="500" 11 /> 12 </Zoom> 13)
You can pass these options to either the Uncontrolled
(default) or
Controlled
components.
1export interface UncontrolledProps { 2 // Accessible label text for when you want to unzoom. 3 // Default: 'Minimize image' 4 a11yNameButtonUnzoom?: string 5 6 // Accessible label text for when you want to zoom. 7 // Default: 'Expand image' 8 a11yNameButtonZoom?: string 9 10 // Allow swipe gesture to unzoom. 11 // Default: true 12 canSwipeToUnzoom?: boolean 13 14 // Your image (required). 15 children: ReactNode 16 17 // Custom CSS className to add to the zoomed <dialog>. 18 classDialog?: string 19 20 // Provide your own unzoom button icon. 21 // Default: ICompress 22 IconUnzoom?: ElementType 23 24 // Provide your own zoom button icon. 25 // Default: IEnlarge 26 IconZoom?: ElementType 27 28 // Swipe gesture threshold after which to unzoom. 29 // Default: 10 30 swipeToUnzoomThreshold?: number 31 32 // Specify what type of element should be used for 33 // internal component usage. This is useful if the 34 // image is inside a <p> or <button>, for example. 35 // Default: 'div' 36 wrapElement?: 'div' | 'span' 37 38 // Provide your own custom modal content component. 39 ZoomContent?: (props: { 40 img: ReactElement | null; 41 buttonUnzoom: ReactElement<HTMLButtonElement>; 42 onUnzoom: () => void; 43 }) => ReactElement; 44 45 // Higher quality image attributes to use on zoom. 46 zoomImg?: ImgHTMLAttributes<HTMLImageElement> 47 48 // Offset in pixels the zoomed image should 49 // be from the window's boundaries. 50 // Default: 0 51 zoomMargin?: number 52}
You can pass these options to only the Controlled
component.
1export interface ControlledProps { 2 // ...same as UncontrolledProps 3 4 // Tell the component whether or not it should be zoomed 5 // Default: false 6 isZoomed: boolean 7 8 // Listen for hints from the component about when you 9 // should zoom (`true` value) or unzoom (`false` value) 10 onZoomChange?: (value: boolean) => void 11}
Import the component and the CSS, wrap your image with the component, and the component will handle it's own state.
1import React from 'react' 2import Zoom from 'react-medium-image-zoom' 3import 'react-medium-image-zoom/dist/styles.css' 4 5// <img /> 6export const MyImg = () => ( 7 <Zoom> 8 <img 9 alt="That Wanaka Tree, New Zealand by Laura Smetsers" 10 src="/path/to/thatwanakatree.jpg" 11 width="500" 12 /> 13 </Zoom> 14) 15 16// <div> 17export const MyDiv = () => ( 18 <Zoom> 19 <div 20 aria-label="That Wanaka Tree, New Zealand by Laura Smetsers" 21 role="img" 22 style={{ 23 backgroundColor: '#fff', 24 backgroundImage: `url("/path/to/thatwanakatree.jpg")`, 25 backgroundPosition: '50%', 26 backgroundRepeat: 'no-repeat', 27 backgroundSize: 'cover', 28 height: '0', 29 paddingBottom: '56%', 30 width: '100%', 31 }} 32 /> 33 </Zoom> 34) 35 36// <picture> 37export const MyPicture = () => ( 38 <Zoom> 39 <picture> 40 <source media="(max-width: 800px)" srcSet="/path/to/teAraiPoint.jpg" /> 41 <img 42 alt="A beautiful, serene setting in nature" 43 src="/path/to/thatwanakatree.jpg" 44 width="500" 45 /> 46 </picture> 47 </Zoom> 48) 49 50// <figure> 51export const MyFigure = () => ( 52 <figure> 53 <Zoom> 54 <img 55 alt="That Wanaka Tree, New Zealand by Laura Smetsers" 56 src="/path/to/thatwanakatree.jpg" 57 width="500" 58 /> 59 </Zoom> 60 <figcaption>Photo by Laura Smetsers</figcaption> 61 </figure> 62)
Import the Controlled
component and the CSS, wrap your image with the
component, and then dictate the isZoomed
state to the component.
1import React, { useCallback, useState } from 'react' 2import { Controlled as ControlledZoom } from 'react-medium-image-zoom' 3import 'react-medium-image-zoom/dist/styles.css' 4 5const MyComponent = () => { 6 const [isZoomed, setIsZoomed] = useState(false) 7 8 const handleZoomChange = useCallback(shouldZoom => { 9 setIsZoomed(shouldZoom) 10 }, []) 11 12 return ( 13 <ControlledZoom isZoomed={isZoomed} onZoomChange={handleZoomChange}> 14 <img 15 alt="That wanaka tree, alone in the water near mountains" 16 src="/path/to/thatwanakatree.jpg" 17 width="500" 18 /> 19 </ControlledZoom> 20 ) 21) 22 23export default MyComponent
The onZoomChange
prop accepts a callback that will receive true
or false
based on events that occur (like click or scroll events) to assist you in
determining when to zoom and unzoom the component.
You can import the default styles from react-medium-image-zoom/dist/styles.css
and override the values from your code, or you can copy the styles.css
file and alter it to your liking. The latter is the best
option, given rem
s should be used instead of px
to account for different
default browser font sizes, and it's hard for a library to guess at what these
values should be.
An example of customizing the transition duration, timing function, overlay
background color, and unzoom button styles with :focus-visible
can be found in
this story: https://rpearce.github.io/react-medium-image-zoom/?path=/story/img--custom-modal-styles
If you want to customize the zoomed modal experience with a caption, form, or
other set of components, you can do so by providing a custom component to the
ZoomContent
prop.
View the live example of custom zoom modal content.
Below is some example code that demonstrates how to use this feature.
1export const MyImg = () => ( 2 <Zoom ZoomContent={CustomZoomContent}> 3 <img 4 alt="That Wanaka Tree, New Zealand by Laura Smetsers" 5 src="/path/to/thatwanakatree.jpg" 6 width="500" 7 /> 8 </Zoom> 9) 10 11const CustomZoomContent = ({ 12 buttonUnzoom, // default unzoom button 13 modalState, // current state of the zoom modal: UNLOADED, LOADING, LOADED, UNLOADING 14 img, // your image, prepped for zooming 15 //onUnzoom, // unused here, but a callback to manually unzoom the image and 16 // close the modal if you want to use your own buttons or 17 // listeners in your custom experience 18}) => { 19 const [isLoaded, setIsLoaded] = useState(false) 20 21 useLayoutEffect(() => { 22 if (modalState === 'LOADED') { 23 setIsLoaded(true) 24 } else if (modalState === 'UNLOADING') { 25 setIsLoaded(false) 26 } 27 }, [modalState]) 28 29 const classCaption = isLoaded 30 ? 'zoom-caption zoom-caption--loaded' 31 : 'zoom-caption' 32 33 return <> 34 {buttonUnzoom} 35 36 <figure> 37 {img} 38 <figcaption className={classCaption}> 39 That Wanaka Tree, also known as the Wanaka Willow, is a willow tree 40 located at the southern end of Lake WΔnaka in the Otago region of New 41 Zealand. 42 <cite className="zoom-caption-cite"> 43 Wikipedia, <a className="zoom-caption-link" href="https://en.wikipedia.org/wiki/That_Wanaka_Tree"> 44 That Wanaka Tree 45 </a> 46 </cite> 47 </figcaption> 48 </figure> 49 </> 50}
Here are the prop changes from v4
to be aware of:
closeText
was renamed to a11yNameButtonUnzoom
openText
was renamed to a11yNameButtonZoom
overlayBgColorStart
was removed and is now controlled via the CSS selector [data-rmiz-modal-overlay="hidden"]
overlayBgColorEnd
was removed and is now controlled via the CSS selector [data-rmiz-modal-overlay="visible"]
portalEl
was removed, for we are using the <dialog>
element nowtransitionDuration
was removed and is now controlled via the CSS selectors [data-rmiz-modal-overlay]
and [data-rmiz-modal-img]
wrapElement
was removed then added back in v5.1.0
wrapStyle
was removedzoomZindex
was removed, for we are using the <dialog>
element nowAnd you can now provide zoomImg
props to specify a different image to load when zooming.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
30 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 10
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 1/9 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More