5.2.0 β€’ Published 12 days ago

react-medium-image-zoom v5.2.0

Weekly downloads
28,634
License
BSD-3-Clause
Repository
github
Last release
12 days ago

react-medium-image-zoom

npm version react-medium-image-zoom bundlejs badge npm downloads All Contributors

The original medium.com-inspired image zooming library for React.

View the storybook examples to see various usages.

Features:

Requirements to know about:

  • <dialog> element (caniuse dialog)
  • ResizeObserver (caniuse ResizeObserver)
  • Package build target is ES2021. If you need to support older environments, run this package through your build system.

Quickstart

npm install --save react-medium-image-zoom
import React from 'react'
import Zoom from 'react-medium-image-zoom'
import 'react-medium-image-zoom/dist/styles.css'

export const MyImg = () => (
  <Zoom>
    <img
      alt="That Wanaka Tree, New Zealand by Laura Smetsers"
      src="/path/to/thatwanakatree.jpg"
      width="500"
    />
  </Zoom>
)

API

You can pass these options to either the Uncontrolled (default) or Controlled components.

export interface UncontrolledProps {
  // Accessible label text for when you want to unzoom.
  // Default: 'Minimize image'
  a11yNameButtonUnzoom?: string

  // Accessible label text for when you want to zoom.
  // Default: 'Expand image'
  a11yNameButtonZoom?: string

  // Allow swipe gesture to unzoom.
  // Default: true
  canSwipeToUnzoom?: boolean

  // Your image (required).
  children: ReactNode

  // Custom CSS className to add to the zoomed <dialog>.
  classDialog?: string

  // Provide your own unzoom button icon.
  // Default: ICompress
  IconUnzoom?: ElementType

  // Provide your own zoom button icon.
  // Default: IEnlarge
  IconZoom?: ElementType

  // Swipe gesture threshold after which to unzoom.
  // Default: 10
  swipeToUnzoomThreshold?: number

  // Specify what type of element should be used for
  // internal component usage. This is useful if the
  // image is inside a <p> or <button>, for example.
  // Default: 'div'
  wrapElement?: 'div' | 'span'

  // Provide your own custom modal content component.
  ZoomContent?: (props: {
    img: ReactElement | null;
    buttonUnzoom: ReactElement<HTMLButtonElement>;
    onUnzoom: () => void;
  }) => ReactElement;

  // Higher quality image attributes to use on zoom.
  zoomImg?: ImgHTMLAttributes<HTMLImageElement>

  // Offset in pixels the zoomed image should
  // be from the window's boundaries.
  // Default: 0
  zoomMargin?: number
}

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

  // Listen for hints from the component about when you
  // should zoom (`true` value) or unzoom (`false` value)
  onZoomChange?: (value: boolean) => void
}

Basic Usage

Uncontrolled component (default)

Import the component and the CSS, wrap your image with the component, and the component will handle it's own state.

import React from 'react'
import Zoom from 'react-medium-image-zoom'
import 'react-medium-image-zoom/dist/styles.css'

// <img />
export const MyImg = () => (
  <Zoom>
    <img
      alt="That Wanaka Tree, New Zealand by Laura Smetsers"
      src="/path/to/thatwanakatree.jpg"
      width="500"
    />
  </Zoom>
)

// <div>
export const MyDiv = () => (
  <Zoom>
    <div
      aria-label="That Wanaka Tree, New Zealand by Laura Smetsers"
      role="img"
      style={{
        backgroundColor: '#fff',
        backgroundImage: `url("/path/to/thatwanakatree.jpg")`,
        backgroundPosition: '50%',
        backgroundRepeat: 'no-repeat',
        backgroundSize: 'cover',
        height: '0',
        paddingBottom: '56%',
        width: '100%',
      }}
    />
  </Zoom>
)

// <picture>
export const MyPicture = () => (
  <Zoom>
    <picture>
      <source media="(max-width: 800px)" srcSet="/path/to/teAraiPoint.jpg" />
      <img
        alt="A beautiful, serene setting in nature"
        src="/path/to/thatwanakatree.jpg"
        width="500"
      />
    </picture>
  </Zoom>
)

// <figure>
export const MyFigure = () => (
  <figure>
    <Zoom>
      <img
        alt="That Wanaka Tree, New Zealand by Laura Smetsers"
        src="/path/to/thatwanakatree.jpg"
        width="500"
      />
    </Zoom>
    <figcaption>Photo by Laura Smetsers</figcaption>
  </figure>
)

Controlled component

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}>
      <img
        alt="That wanaka tree, alone in the water near mountains"
        src="/path/to/thatwanakatree.jpg"
        width="500"
      />
    </ControlledZoom>
  )
)

export 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.

Styles

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

Custom zoom modal content

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.

export const MyImg = () => (
  <Zoom ZoomContent={CustomZoomContent}>
    <img
      alt="That Wanaka Tree, New Zealand by Laura Smetsers"
      src="/path/to/thatwanakatree.jpg"
      width="500"
    />
  </Zoom>
)

const CustomZoomContent = ({
  buttonUnzoom, // default unzoom button
  modalState,   // current state of the zoom modal: UNLOADED, LOADING, LOADED, UNLOADING
  img,          // your image, prepped for zooming
  //onUnzoom,   // unused here, but a callback to manually unzoom the image and
                //   close the modal if you want to use your own buttons or
                //   listeners in your custom experience
}) => {
  const [isLoaded, setIsLoaded] = useState(false)

  useLayoutEffect(() => {
    if (modalState === 'LOADED') {
      setIsLoaded(true)
    } else if (modalState === 'UNLOADING') {
      setIsLoaded(false)
    }
  }, [modalState])

  const classCaption = isLoaded
    ? 'zoom-caption zoom-caption--loaded'
    : 'zoom-caption'

  return <>
    {buttonUnzoom}

    <figure>
      {img}
      <figcaption className={classCaption}>
        That Wanaka Tree, also known as the Wanaka Willow, is a willow tree
        located at the southern end of Lake Wānaka in the Otago region of New
        Zealand.
        <cite className="zoom-caption-cite">
          Wikipedia, <a className="zoom-caption-link" href="https://en.wikipedia.org/wiki/That_Wanaka_Tree">
            That Wanaka Tree
          </a>
        </cite>
      </figcaption>
    </figure>
  </>
}

Migrating From v4 to v5

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 now
  • transitionDuration 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 removed
  • zoomZindex was removed, for we are using the <dialog> element now

And you can now provide zoomImg props to specify a different image to load when zooming.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

@plone/voltodafunda-markdown-editor@pil0t/gatsby-theme-novela@thirdwave-network/thirdwave-gatsby-theme@kyrelldixon/gatsby-theme-novelametta-editorcustom-gatsby-theme-novelacl2-front@garytee/gatsby-woo-elementorgatsby-woocommerce-elementor-theme@stellaris/react-rich-editorkontenbase-document-editor@castletech/pwa-module-ui@polemic/parchment@polemic/react@infinitebrahmanuniverse/nolb-react-me@rbrcsk/rich-markdown-editor@everything-registry/sub-chunk-2567merchi-product-formhello-world-editorgatsby-theme-projectagatsby-theme-operettafumadocs-uiflowstate-editorflowstate-editor-tempflowstate-editor-temp-2gatsby-theme-coacogatsby-woocommerce-themegatsby-theme-catedvoramarkfinal-project-2material-ui-react-express-mongodbmikatre-themelondonmanager-legosnext-docs-uimashixiong-editormarktownmerchi_checkoutmerchi_invoicemerchi_product_formmdsmirroropub-uiponchojsposipagesreact-nested-tablereact-zx-toolboxreact-unifiedrafae-rich-markdown-editorquick-markdown-editorqy-self-use-editorreact-misc-toolboxstyllo-markdown-editordavid-markdown-editorreact-chat-window2robin.io-reactseturon_designd4t-uid4t-ui-demodework-rich-markdown-editortraverse-markdown-editortoloka-quiz-fronttg4websitedocs-mdx-funtipe-markdown-editor@escolalms/markdown-editor@topthink/components@leuven2030/ui@mikekreeki/rich-markdown-editor@o/editor@sudaraka94/gatsby-theme-novela@sunsama/rich-markdown-editora-nice-bloggo-themerich-markdown-editor-pstrich-markdown-editor-v2rich-markdown-editor3rich-maple-markdownrich-markdownrich-markdown-editorrich-markdown-editor-customizablerich-markdown-editor-drepatedrich-markdown-editor-frich-markdown-editor-knseturon@negati-ve/gatsby-theme-novela@pil0t/gatsby-theme-novela-dolim@pinggod/gatsby-theme-wink@paragraph-xyz/rich-markdown-editor@pnegahdar/rich-markdown-editor@rocket-tutor/rocket-tutor-componentszecoreuiywb-editor-pdf@scottge/markdown-editor@openeventkit/event-siteznotes-editorztopia-ui@liwuming/rich-editorws-kf-react@locpd/rich-markdown-editorwebstudio-mui@narative/gatsby-theme-novela
5.2.0

12 days ago

5.1.11

22 days ago

5.1.10

3 months ago

5.1.9

4 months ago

5.1.7-rc.0

9 months ago

5.1.8

9 months ago

5.1.7

9 months ago

5.1.4-rc.0

1 year ago

5.2.0-beta.1

11 months ago

5.2.0-beta.0

12 months ago

5.1.4-beta.0

1 year ago

5.1.6

11 months ago

5.1.5

12 months ago

5.1.4

1 year ago

5.1.3

1 year ago

5.1.0-beta.7

2 years ago

5.1.0-beta.8

2 years ago

5.1.0-beta.9

2 years ago

5.1.0-beta.5

2 years ago

5.1.0-beta.6

2 years ago

5.1.2

1 year ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.3-beta.0

2 years ago

5.1.0-beta.1

2 years ago

5.1.0-beta.3

2 years ago

5.1.0-beta.4

2 years ago

5.0.0-beta2.8

2 years ago

5.0.0-beta2.2

2 years ago

5.0.0-beta2.3

2 years ago

5.0.0-beta2.1

2 years ago

5.0.0-beta2.6

2 years ago

5.0.0-beta2.7

2 years ago

5.0.0-beta2.4

2 years ago

5.0.0-beta2.5

2 years ago

4.4.1

2 years ago

4.4.3

2 years ago

4.4.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

5.0.1-beta.0

2 years ago

5.1.0-beta.0

2 years ago

5.0.2-beta.0

2 years ago

4.4.0

2 years ago

4.4.0-rc.1

2 years ago

4.3.6

2 years ago

4.3.7

2 years ago

4.3.5

3 years ago

4.3.4

3 years ago

3.1.3

3 years ago

4.3.2

3 years ago

4.3.3

3 years ago

5.0.0-rc.10

4 years ago

5.0.0-rc.9

4 years ago

5.0.0-rc.7

4 years ago

5.0.0-rc.8

4 years ago

5.0.0-rc.6

4 years ago

5.0.0-rc.5

4 years ago

5.0.0-rc.3

4 years ago

5.0.0-rc.4

4 years ago

5.0.0-rc.2

4 years ago

5.0.0-rc.1

4 years ago

5.0.0-rc.0

4 years ago

5.0.0-beta.12

4 years ago

5.0.0-beta.13

4 years ago

5.0.0-beta.14

4 years ago

5.0.0-beta.11

4 years ago

5.0.0-beta.10

4 years ago

5.0.0-beta.8

4 years ago

5.0.0-beta.9

4 years ago

5.0.0-beta.6

4 years ago

5.0.0-beta.7

4 years ago

5.0.0-beta.5

4 years ago

5.0.0-beta.4

4 years ago

5.0.0-beta.3

4 years ago

5.0.0-beta.2

4 years ago

5.0.0-beta.0

4 years ago

5.0.0-beta.1

4 years ago

5.0.0-alpha.12

4 years ago

5.0.0-alpha.11

4 years ago

5.0.0-alpha.10

4 years ago

5.0.0-alpha.9

4 years ago

5.0.0-alpha.8

4 years ago

5.0.0-alpha.7

4 years ago

5.0.0-alpha.6

4 years ago

5.0.0-alpha.5

4 years ago

5.0.0-alpha.4

4 years ago

5.0.0-alpha.3

4 years ago

5.0.0-alpha.2

4 years ago

5.0.0-alpha.1

4 years ago

5.0.0-alpha.0

4 years ago

4.3.1

4 years ago

4.3.0

4 years ago

4.2.0

4 years ago

4.1.0

4 years ago

4.1.0-alpha.2

4 years ago

4.1.0-alpha.1

4 years ago

4.1.0-alpha.0

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-alpha.15

4 years ago

4.0.0-alpha.14

4 years ago

4.0.0-alpha.13

4 years ago

4.0.0-alpha.12

4 years ago

4.0.0-alpha.11

4 years ago

4.0.0-alpha.10

4 years ago

4.0.0-alpha.9

4 years ago

4.0.0-alpha.8

4 years ago

4.0.0-alpha.7

4 years ago

4.0.0-alpha.6

4 years ago

4.0.0-alpha.5

4 years ago

4.0.0-alpha.4

4 years ago

4.0.0-alpha.3

4 years ago

4.0.0-alpha.2

4 years ago

4.0.0-alpha.1

4 years ago

4.0.0-alpha.0

4 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.16

5 years ago

3.0.15

5 years ago

3.0.14

6 years ago

3.0.13

6 years ago

3.0.12

6 years ago

3.0.11

6 years ago

3.0.10

6 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

2.0.7

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

3.0.1

6 years ago

2.0.4

7 years ago

3.0.0

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.9.3

7 years ago

0.9.2

7 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

7 years ago

0.6.0

7 years ago

0.5.2

7 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago