that looks like a circle
<Zoom><divaria-label="A blue circle"style={{width: 300,height: 300,borderRadius: '50%',backgroundColor: '#0099ff'}}/></Zoom>
Controlled component (Controlled)
Import the Controlled component and the CSS, wrap whatever you want to be "zoomable" with this component and then dictate the zoomed/unzoomed state to the component. Here, we will automatically zoom the component once the image has loaded:
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.
There is also an example in the Storybook Examples of how to use a Controlled component to create a full-screen slideshow gallery.
API
Both uncontrolled & controlled components
You can pass these options to either the default or controlled components.
Prop
Type
Required
Default
Details
closeText
String
no
'Unzoom Image'
Accessible label text for when you want to unzoom
openText
String
no
'Zoom Image'
Accessible label text for when you want to zoom
overlayBgColorEnd
String
no
'rgba(255, 255, 255, 0.95)'
Modal overlay background color at end of zoom
overlayBgColorStart
String
no
'rgba(255, 255, 255, 0)'
Modal overlay background color at start of zoom
portalEl
Element
no
document.body
DOM Element to which we will append the zoom modal
scrollableEl
Window
no
window
DOM Element to which we will listen for scroll events to determine if we should unzoom
transitionDuration
Number
no
300
Transition duration in milliseconds for the component to use on zoom and unzoom. Set this to 0 to disable the animation
wrapElement
String
no
'div'
Wrapper element
wrapStyle
Object
no
null
Optional style object to pass to the wrapper element. Useful when you want the container to be width: '100%', for example
zoomMargin
Number
no
0
Offset in pixels the zoomed image should be from the window' boundaries
zoomZindex
Number
no
2147483647
z-index value for the zoom overlay
Only the controlled component
You can pass these options to only the controlled component.
Prop
Type
Required
Default
Details
isZoomed
bool
yes
false
Tell the component whether or not it should be zoomed
onZoomChange
Function
no
Function.prototype
Listen for hints from the component about when you should zoom (true value) or unzoom (false value)
In v3, you would pass properties for your normal image that would be zoomed, and you would pass an optional zoomImage that would be a higher quality image that would replace the original image when zoomed.
The problem with v3 was that it tried to assume too many things about what it is you were trying to zoom, and this resulted in overly complex and near-unmaintainable code that had a number of bugs.
In v4, you can zoom the bridge example above like this:
Please see the Controlled component (Controlled) section for further documentation regarding controlled components that used the isZoomed, onZoom, and onUnzoom properties.
Contributors
Thanks goes to these wonderful people (emoji key):
@rpearce I have converted the project to TS, but there are still 2 things to do. Tests and Storybook are working.
1st - I wasn't sure about the types for the parentRefportalElscrollableEl so I was going to leave that to you.
2nd - In the tsconfig.json file if you have declaration set to true, it generate the d.ts types automatically but they are not very readable in certain cases, so I thought to ask your preference and also leave to you, in case you wanted to build a more custom type file with comments, etc...
Reviewed by dougg0k at 2020-03-05 17:39
2. Incorrect scale on iOS with constrained unzoomed width
This issue is a:
Bug report
Expected behavior
That the zoomed image would scale correctly on all platforms.
Actual behavior
On iOS, the zoomed image is scaled based on the rendered size of the unzoomed image and appears blurry.
The zooming does work as expected on desktop and Android. This issue appears to be related to iOS only.
Steps to reproduce behavior
Apply a constrained width to an unzoomed image (e.g. 20%)
View the page with an iOS device
Run the linked the example below in the dev server and view it with an iOS device (both Safari and Chrome for iOS exhibit the same behavior).
I made some improvements by adding a {zoom: 2} property and halving the scale factor. This appears to make the device render the full resolution. So the image is now clear. But the side effect is that the X,Y coordinate logic pushes the image out of the viewport.
I saw that a rewrite of the library is in progress, so I decided to first report this issue and provide a means of reproducing the symptom first.
Reviewed by 0x6e6562 at 2019-01-10 14:46
3. React Portal is incompatible with SSR
Hi,
I've been using react-medium-image-zoom in a Gatsby project and while in development it has been working, it doesnt when you try to build your project.
ERROR #95312
"document" is not available during server side rendering.
See our docs page for more info on this error: https://gatsby.dev/debug-html
68 | overlayBgColorStart: overlayBgColorStart,
69 | parentRef: wrapRef,
> 70 | portalEl: portalEl || document.body,
| ^
71 | scrollableEl: scrollableEl || window,
72 | transitionDuration: transitionDuration,
73 | zoomMargin: zoomMargin,
WebpackError: ReferenceError: document is not defined
- Controlled.js:70 Controlled
node_modules/react-medium-image-zoom/dist/esm/Controlled.js:70:1
But there is another possible solution that you would need to ask the consumer to also install this package if he/she were to use react-medium-image-zoom in Gatsby https://www.gatsbyjs.org/packages/gatsby-plugin-portal/
Reviewed by dougg0k at 2020-03-03 12:54
4. Add srcset support and pass all props to zoomImage
This PR addresses issue #42.
All props are now passed down to the Zoom img component, with special cases for src, srcset, and style.
One note/question: the hasLoaded state on the Zoom component is being set in two places
https://github.com/rpearce/react-medium-image-zoom/blob/master/src/react-medium-image-zoom.js#L259
https://github.com/rpearce/react-medium-image-zoom/blob/master/src/react-medium-image-zoom.js#L297
Maybe I'm missing something, but they seem to each reference a difference load event - one being the component mounting, the other being the img element loading. Is this right?
Reviewed by jeremybini at 2017-05-02 17:02
5. Implemented wrapElement option and updated stories
This option accepts βdivβ or βspanβ and defaults to βdivβ.
This pull request closes https://github.com/rpearce/react-medium-image-zoom/issues/236.
I never used Storybook. Crossing my fingers I didnβt mess things up. Standby for feedback.
Pre-Flight Checklist
I have made sure to
[x] add tests to maintain 100% test coverage & add a Storybook example (when
this makes sense)
[x] manually test all the Storybook examples
[x] run $ yarn build to build the project code
Reviewed by sunknudsen at 2020-03-08 01:36
6. Controlled vs uncontrolled
Fixes #30
Fixes #49
when you pass a non-null value to isZoomed on component instantiation, the component should only zoom/unzoom when that isZoomed prop changes. This means that the consumer is in charge of when it's open or closed. Clicking on the image will not do anything. If it is a controlled component but then you do not pass isZoomed with a non-null value, then an error will be raised telling you that you must control it.
if you do not pass true or false to isZoomed, then the component will control itself. This means that if you initially pass isZoomed={null} and then pass isZoomed={true} (or false), then it will throw an error. If it is an uncontrolled component but then you do pass isZoomed with a non-null value, then an error will be raised telling you that you must not control it.
Please help test the sh*t out of this.
cc @cbothner and @rajit
Reviewed by rpearce at 2017-08-13 01:27
7. Feature / Support Request - Slideshow
This issue is a kind of both:
Feature request
Question / support request
Description
Is there any possibility or example to implement a Slideshow with this package? I have tried this with a controlled component, but when I set the isZoomed property to false on one image and to true on another image, the open and close animations come into play, doesn't look very nice.
So my Question: Is there any way to open an Image Slider/Colorbox with this beautiful effect?
Reviewed by Daveiano at 2018-12-15 16:44
8. styles for zoomImage prop don't work
Expected behavior
I expected style key to work with the zoomImage prop.
9. Combination of next/image with layout responsive
This issue is a:
Question / support request
Description
Using next/image so far so great. But when using layout='responsive', the image's width and height are somehow set to 0. Is there any way around? https://nextjs.org/docs/api-reference/next/image#layout
Reviewed by xmflsct at 2021-04-17 22:41
10. bundle size
After releasing patch 4.0.2 bundle size grow to 160kb
I'm importing as suggested in the readme
It seems that it's coming from the data-rmiz-wrap property that is set as hidden when the user clicks on the image. If I force the value to be set as visible while zoomed in, I don't get the flicker when zooming out.
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.
React Image Viewer [email protected] has been rewriten with React Hooks and all apis of it have been redesigned. If you are using [email protected], please