The original medium.com-inspired image zooming library for React (since 2016)
The original medium.com-inspired image zooming library for React (since 2016)
The original medium.com-inspired image zooming library for React.
View the storybook examples to see various usages.
Features:
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 ``<figure> with ``<svg>dependenciesRequirements 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.npm install --save react-medium-image-zoomimport React from 'react'
import Zoom from 'react-medium-image-zoom'
import 'react-medium-image-zoom/dist/styles.css'
export const MyImg = () => (
<Zoom>
</Zoom>
)You can pass these options to either the Uncontrolled (default) or
Controlled components.
…You can pass these options to only the Controlled component.
export interface ControlledProps {
// ...same as UncontrolledProps
// Tell the component whether or not it should be zoomed
// Default: false
isZoomed: boolean
}Import the component and the CSS, wrap your image with the component, and the component will handle it's own state.
…Import the Controlled component and the CSS, wrap your image with the
component, and then dictate the isZoomed state to the component.
import React, { useCallback, useState } from 'react'
import { Controlled as ControlledZoom } from 'react-medium-image-zoom'
import 'react-medium-image-zoom/dist/styles.css'
const MyComponent = () => {
const [isZoomed, setIsZoomed] = useState(false)
const handleZoomChange = useCallback(shouldZoom => {
setIsZoomed(shouldZoom)
}, [])
return (
<ControlledZoom isZoomed={isZoomed} onZoomChange={handleZoomChange}>
</ControlledZoom>
)
}
export default MyComponentThe 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 rems 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.
…Here are the prop changes from v4 to be aware of:
closeText was renamed to a11yNameButtonUnzoomopenText was renamed to a11yNameButtonZoomoverlayBgColorStart 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.0wrapStyle 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):
Robert Pearce ⚠️ |
Cameron Bothner ⚠️ |
Jeremy Bini |
ismay |
Rajit Singh |
Roberto Saccon |
wtfdaemon |
Josh Sloat |
Aswin |
Alex Shelkovskiy |
Adrian Bindiu |
Kendall Buchanan |
Kaycee |
Anuj |
Ludwig Frank |
No open issues yet, or sync has not completed.