JSPM

react-medium-image-zoom

5.0.0-rc.10
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 197341
  • Score
    100M100P100Q170720F
  • License BSD-3-Clause

Accessible medium.com-style image zoom for React

Package Exports

  • react-medium-image-zoom

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-medium-image-zoom) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-medium-image-zoom

npm version npm downloads bundlephobia size

This library is a React.js iteration on Medium.com's image zoom that allows for images to work together for a “zooming” effect, is accessible (keyboard and screen readers), and works regardless of parent elements that have overflow: hidden or parents with transform properties.

As an added bonus, it should let you zoom anything.

Installation

$ npm i react-medium-image-zoom

or

$ yarn add react-medium-image-zoom

Options

interface ImageZoomOpts {
  closeText?: string
  isControlled?: boolean
  isZoomed?: boolean
  modalText?: string
  onZoomChange?: (isZoomed: boolean) => void
  openText?: string
  overlayBgColor?: string
  overlayOpacity?: number
  scrollableEl?: HTMLElement | Window
  transitionDuration?: number
  zoomMargin?: number
  zoomZindex?: number
}
  • closeText
    • accessible text for unzooming
    • default: 'Unzoom image'
  • isControlled
    • flag to specify that you intend to control the component's state yourself
    • default: false
  • isZoomed
    • flag used in conjunction with isControlled to specify the zoom / unzoom state of the component
    • default: false
  • modalText
    • accessible modal dialog text when zoomed
    • default: 'Zoomed item'
  • onZoomChange
    • callback that is called with true when the zooming should be triggered
    • default: undefined
  • openText
    • accessible text for zooming
    • default: 'Zoom image'
  • overlayBgColor
    • modal dialog overlay background color
    • default: '#fff'
  • overlayOpacity
    • modal dialog overlay opacity
    • default: 0.95
  • transitionDuration
    • length of time in milliseconds for the animations to run
    • default: 300
  • zoomMargin
    • amount the zoomed item should be offset from the window
    • default: 0
  • zoomZindex
    • z-index value the modal dialog should have
    • default: 2147483647

Usage

Regular Usage

import { useImageZoom } from 'react-medium-image-zoom'

const MyComponent = () => {
  const { ref } = useImageZoom({ /* image zoom options here */ })

  return (
    <img
      alt="that wanaka tree"
      ref={ref}
      src="/path/to/thatwanakatree.jpg"
      width="500"
    />
  )
}

Usage with <picture>

import { useImageZoom } from 'react-medium-image-zoom'

const MyComponent = () => {
  const { ref } = useImageZoom({ /* image zoom options here */ })

  return (
    <picture>
      <source media="(max-width: 800px)" srcSet="/path/to/teAraiPoint.jpg" />
      <img
        alt="that wanaka tree"
        ref={ref}
        src="/path/to/thatwanakatree.jpg"
        width="500"
      />
    </picture>
  )
}

Usage with <figure>

import { useImageZoom } from 'react-medium-image-zoom'

const MyComponent = () => {
  const { ref } = useImageZoom({ /* image zoom options here */ })

  return (
    <figure>
      <img
        alt="that wanaka tree"
        ref={ref}
        src="/path/to/thatwanakatree.jpg"
        width="500"
      />
      <figcaption>That Wanaka Tree</figcaption>
    </figure>
  )
}

Usage with non-image element

Tools like gatsby have their own Image elements that can be difficult to add a ref to. There are also instances where you may want to zoom anything from a p to an svg to a div. Here is an example of how you can do that:

import { useImageZoom } from 'react-medium-image-zoom'

const MyComponent = () => {
  const { ref } = useImageZoom({ /* image zoom options here */ })

  return (
    <div ref={ref}>
      {/* what you want the div to zoom */}
    </div>
  )
}