An open API service indexing awesome lists of open source software.

https://github.com/chenbin92/react-image-zoom

Image zoom for React
https://github.com/chenbin92/react-image-zoom

image-zoom react

Last synced: 2 months ago
JSON representation

Image zoom for React

Awesome Lists containing this project

README

          

# react-image-zoom

> Forked from [react-medium-image-zoom](https://github.com/rpearce/react-medium-image-zoom)

This library is a different implementation of [Medium.com's image zoom](https://medium.com/design/image-zoom-on-medium-24d146fc0c20) that allows for a low-res and high-res images to work together for β€œzooming” effects and works regardless of parent elements that have `overflow: hidden` or [parents with transform properties](https://codepen.io/rpearce/pen/MEyOmb). Versions `>=3.0.0` are compatible with [React.js](https://github.com/facebook/react) `>=16.x`; if you need compatibility with `react@^0.14.0 || ^15.0.0`, please use version `2.x`.

You can [view the demo here](https://rpearce.github.io/react-medium-image-zoom/).

![demo](https://user-images.githubusercontent.com/592876/30678463-7ef720f0-9ee6-11e7-925d-924da1b58157.gif)

## Installation
```
$ npm install --save react-zoom-images
```

## Usage
```js
import ImageZoom from 'react-zoom-images'

function MyComponent(props) {
return (


Some text...

Some text...



)
}
```

| Prop | Type | Required | Default | Details |
| --- | --- | --- | --- | --- |
| `image` | object | yes | none | The original image by default |
| `zoomImage` | object | no | `image` | The image to be used for zooming |
| `originalImage` | string | no | `image` | Specify the original image (`image`, `zoomImage`οΌ‰ |
| `zoomTransitionDuration` | number | no | `300ms` | Animation time when opening the zoomImage |
|`unzoomTransitionDuration` | number | no | `300ms` | Animation time when closing the zoomImage |
| `zoomMargin` | number | no | `40` | Pixel number to offset zoomed image from the window |
| `isZoomed` | boolean | no | `false` | For more direct control over the zoom state |
| `shouldHandleZoom` | func | no | `(event) => true` | Pass this callback to intercept a zoom click event and determine whether or not to zoom. Function must return a truthy or falsy value |
| `shouldReplaceImage` | boolean | no | `true` | Once the image has been "zoomed" and downloaded the larger image, this replaces the original `image` with the `zoomImage` |
| `shouldRespectMaxDimension` | boolean | no | `false` | When `true`, don't make the zoomed image's dimensions larger than the original dimensions. Only supported if no `zoomImage` is provided. Will also disable the zooming if the image's is already rendered at its maximum width & height |
| `defaultStyles` | object | no | `{}` | For fine-grained control over all default styles (`zoomContainer`, `overlay`, `image`, `zoomImage`) |
| `onZoom` | func | no | `() => {}` | Pass this callback to respond to a zoom interaction. |
| `onUnzoom` | func | no | `() => {}` | Pass this callback to respond to an unzoom interaction. |

Each one of these image props accepts normal `image` props, for example:

| Prop | Type | Required | Details |
| ------ | ---- | ------- | ------- |
| `src` | string | yes | The source for the image |
| `alt` | string | no | The alt text for the image |
| `className` | string | no | Classes to apply to the image |
| `style` | object | no | Additional styles to apply to the image |
| ... | ... | no | ... |

## Controlled vs Uncontrolled Modes
Similar to how an `` works in React, if the consumer initially chooses to control the `isZoomed` value, then this means the consumer is now responsible for telling the component the value of `isZoomed`. If the consumer instantiates the component with a non-null `isZoomed` value and subsequently does _not_ pass a value for it on updates, then an error will be thrown notifying the consumer that this is a controlled component.

The reverse is true, as well. If the component is instantiated without an `isZoomed` value, then the component will handle its own `isZoomed` state. If a non-null `isZoomed` prop is passed _after_ instantiation, then an error will be thrown notifying the consumer that this component controls its own state.

## Browser Support
Currently, this has only been tested on the latest modern browsers. Pull requests are welcome.

## Development
The source code is located within the `src` directory. Use `$ npm run build` to build the main file as well as the example during development and/or use `$ npm run dev` to have it watch for changes to `src/` and `example/src`.

You can view the built example as a file via `$ open example/build/index.html`, or you can use `$ npm run dev` to start a local dev server and navigate to [http://localhost:3000](http://localhost:3000).

### Storybook
This project's different options and use cases are documented in [storybook](https://github.com/storybooks/storybook). You can use this in dev like so:

* `$ yarn run storybook` (or `$ npm run storybook`)
* navigate to http://localhost:6006

The page should look like this:

![image](https://user-images.githubusercontent.com/592876/30678386-097f05c2-9ee6-11e7-936d-56a12b59d4b8.png)

## Contributing

1. Check out the [issues](https://github.com/rpearce/react-medium-image-zoom/issues)
1. Fork this repository
1. Clone your fork
1. Check out a feature branch (`$ git checkout -b my-feature`)
1. Make your changes
1. Run `$ yarn run build` to compile your changes and build the example
1. Test your example (see the "Development" section above)
1. Push your branch to your GitHub repo
1. Create a pull request from your branch to this repo's `X-0-stable` branch (where `X` is the correct major version you're targeting)
1. When all is merged, pull down the upstream changes to your master
* `$ git remote add upstream git@github.com:rpearce/react-medium-image-zoom.git`
* `$ git fetch upstream`
* `$ git merge upstream/master`
1. Delete your feature branch

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

| [
Cameron Bothner](https://github.com/cbothner)
[πŸ’»](https://github.com/rpearce/react-medium-image-zoom/commits?author=cbothner "Code") [πŸ“–](https://github.com/rpearce/react-medium-image-zoom/commits?author=cbothner "Documentation") [πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Acbothner "Bug reports") [πŸ’‘](#example-cbothner "Examples") [πŸ€”](#ideas-cbothner "Ideas, Planning, & Feedback") [πŸ‘€](#review-cbothner "Reviewed Pull Requests") [⚠️](https://github.com/rpearce/react-medium-image-zoom/commits?author=cbothner "Tests") | [
Jeremy Bini](https://github.com/jeremybini)
[πŸ’»](https://github.com/rpearce/react-medium-image-zoom/commits?author=jeremybini "Code") [πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Ajeremybini "Bug reports") | [
ismay](https://ismaywolff.nl)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Aismay "Bug reports") [πŸ€”](#ideas-ismay "Ideas, Planning, & Feedback") | [
Rajit Singh](https://www.qeek.co)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Arajit "Bug reports") | [
Roberto Saccon](https://github.com/rsaccon)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Arsaccon "Bug reports") | [
wtfdaemon](https://github.com/wtfdaemon)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Awtfdaemon "Bug reports") | [
Robert Pearce](https://robertwpearce.com)
[πŸ’»](https://github.com/rpearce/react-medium-image-zoom/commits?author=rpearce "Code") [πŸ’¬](#question-rpearce "Answering Questions") [⚠️](https://github.com/rpearce/react-medium-image-zoom/commits?author=rpearce "Tests") [πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Arpearce "Bug reports") [πŸ’‘](#example-rpearce "Examples") [🎨](#design-rpearce "Design") [πŸ‘€](#review-rpearce "Reviewed Pull Requests") [πŸ€”](#ideas-rpearce "Ideas, Planning, & Feedback") [πŸ“–](https://github.com/rpearce/react-medium-image-zoom/commits?author=rpearce "Documentation") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [
Josh Sloat](http://www.joshsloat.com)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Ajoshsloat "Bug reports") [πŸ’»](https://github.com/rpearce/react-medium-image-zoom/commits?author=joshsloat "Code") [πŸ’‘](#example-joshsloat "Examples") [πŸ‘€](#review-joshsloat "Reviewed Pull Requests") [πŸ€”](#ideas-joshsloat "Ideas, Planning, & Feedback") [πŸ“–](https://github.com/rpearce/react-medium-image-zoom/commits?author=joshsloat "Documentation") [🎨](#design-joshsloat "Design") [πŸ’¬](#question-joshsloat "Answering Questions") | [
Aswin](https://github.com/aswinckr)
[πŸ’¬](#question-aswinckr "Answering Questions") | [
Alex Shelkovskiy](https://github.com/alexshelkov)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Aalexshelkov "Bug reports") | [
Adrian Bindiu](http://adrian-design.com)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3ASnowsoul "Bug reports") | [
Kendall Buchanan](https://github.com/kendagriff)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Akendagriff "Bug reports") | [
Kaycee](https://github.com/HippoDippo)
[πŸ’»](https://github.com/rpearce/react-medium-image-zoom/commits?author=HippoDippo "Code") | [
Anuj](http://shuffle.do)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Aoyeanuj "Bug reports") [πŸ’¬](#question-oyeanuj "Answering Questions") |
| [
Ludwig Frank](https://github.com/ludwigfrank)
[πŸ›](https://github.com/rpearce/react-medium-image-zoom/issues?q=author%3Aludwigfrank "Bug reports") [πŸ’»](https://github.com/rpearce/react-medium-image-zoom/commits?author=ludwigfrank "Code") |

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!