Giter Club home page Giter Club logo

react-viewer's Introduction

react-viewer

NPM version codecov

react image viewer.

Introduction

Because I can`t be comfortable using viewerjs in react, so I created react-viewer to replace it.

Installation

react >= 16.8.0 | react-dom >= 16.8.0

npm install react-viewer --save

Usage

import * as React from 'react';
import Viewer from 'react-viewer';

function App() {
  const [ visible, setVisible ] = React.useState(false);

  return (
    <div>
      <button onClick={() => { setVisible(true); } }>show</button>
      <Viewer
      visible={visible}
      onClose={() => { setVisible(false); } }
      images={[{src: '', alt: ''}]}
      />
    </div>
  );
}

Server Side (NextJS)

import React, { FC } from 'react'
import dynamic from 'next/dynamic'

const ReactViewer = dynamic(
  () => import('react-viewer'),
  { ssr: false }
)

export const Viewer: FC = () => {
  return (
    <ReactViewer
      visible={true}
      onClose={() => {}}
      images={[{src: ''}]}
    />
  )
}

export default Viewer

Server Side rest..

I'm sorry, ssr is not currently supported in 3.x, it will be fixed in 4.0.

Props

props type default description required
visible string false Viewer visible true
onClose function - Specify a function that will be called when Visible close true
images ImageDecorator[] [] image source array true
activeIndex number 0 active image index false
zIndex number 1000 Viewer css z-index false
container HTMLElement null set parent node(inline mode) false
drag boolean true whether to drag image false
attribute boolean true whether to show image attribute false
zoomable boolean true whether to show 'zoom' button false
rotatable boolean true whether to show 'rotate' button false
scalable boolean true whether to show 'scale' button false
onMaskClick (e) => void - callback function when mask is clicked false
downloadable boolean false whether to show 'download' false
noClose boolean false to not render close button false
noNavbar boolean false to not render the navbar false
noToolbar boolean false to not render the toolbar false
noImgDetails boolean false to not render image detail (WxH) false
noFooter boolean false to not render the entire footer false
changeable boolean true wheather to show change button false
customToolbar (defaultToolbarConfigs: ToolbarConfig[]) => ToolbarConfig[] - customer toolbar false
zoomSpeed number 0.05 zoom speed false
defaultSize ViewerImageSize - default image size false
defaultImg viewerdefaultimg - if load img failed, show default img false
disableKeyboardSupport boolean false disable keyboard support false
noResetZoomAfterChange boolean false preserve zoom after image change false
noLimitInitializationSize boolean false no limit image initialization size false
defaultScale number 1 set default scale false
onChange (activeImage: ImageDecorator, index: number) => void - callback when image change false
loop boolean true whether enable image loop false
disableMouseZoom boolean false whether disable mouse zoom false
downloadInNewWindow boolean false whether to download in a new window false
className string - customized CSS class false
showTotal boolean true whether to display the total number and range false
totalName string 'of' total image separator name false
maxScale number - maximum scaling false
minScale number 0.1 minimum scaling false
exportFileName string 'exportFile' customize download's filename false

ImageDecorator

props type default description required
src string - image source true
alt string - image description false
downloadUrl string - image downlaod url false
defaultSize ViewerImageSize - image size false

ViewerImageSize

props type default description required
width number - image width true
height number - image height true

ViewerDefaultImg

props type default description required
src number - image source true
width number - image width false
height number - image height false

ToolbarConfig

props type default description required
key string - tool key true
render React.ReactNode - tool render false
onClick function - callback function when action is clicked false

Keyboard support

  • Esc: Close viewer.
  • : View the previous image.
  • : View the next image.
  • : Zoom in the image.
  • : Zoom out the image.
  • Ctrl + 1: Reset the image.
  • Ctrl + ←: Rotate left the image.
  • Ctrl + →: Rotate right the image.

License

MIT

react-viewer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-viewer's Issues

Preserve zoom

Hi,

First, thanks for sharing this library !

I've used your library to display old manuscripts. The zoom feature allows to read the handwritten text, but when the next button is pressed, the zoom level is reset.

How the zoom level could be preserved between next/prev images ?

Thanks

图片循环播放

Hi,

请问有什么办法可以实现图片循环播放吗?(切到最后一张图片再点击下一张就切换到第一张图片)

How can I set viewer in inline mode

I read the documentation but I still can't get how can I implement the inline mode.

This is my code

import Viewer from 'react-viewer';

const myViewer = (image) => {
  const images = [{ src: image.url, alt: image.title }]
  const container = document.getElementById('image-viewer')

  return (
    <div id="image-viewer">
      <Viewer visible images={images} container={container} />
    </div>
  )
}

And I still see viewer in full-screen mode.

Thanks

Working with any kind of file?

Hi!

The readme talks about an "images" array. Could we put the viewerjs accepted files instead? (pdf and openDocument format )

Thanks,

Zoom speed

Hi,
Is there some way to set the speed of the zoom?
I need the zoom to be two times faster than currently.

Thanks!

1.2.0

  • add animation when visible change;
  • support inline mode

broken URL

How to use the default image if there is a broken URL ?

0.0.1

  • viewer core
  • add button icon
  • add toolbar
  • optimize image zoom
  • optimise image change

react V0.14.4版本中好像不支持transitionend 事件,图片出不来

在react V0.14.7版本中好像不支持transitionend 事件。
所以预览的时候图片一直出不来,为了兼容低版本

  if (this.state.visible && this.state.transitionEnd) { 
    // 触发不了transitionend事件 这里一直进不来
    let images = this.props.images || [];
    if (images.length > 0 && this.state.activeIndex >= 0) {
      activeImg = images[this.state.activeIndex];
    }
  }

how to change the modal position?

The picture modal is always on the top of the screen.....

my use case:
If the page has the scroll bar and the picture is on the bottom of the screen, when I click the picture,
the modal is on the top of the screen.... I just want it works like the bootstrap modal....

After the contextmenu is triggered, the drag state is continuously maintained.

After the right mouse button triggers the contextmenu, the mouseup event is invalid, causing the drag state to remain. It is recommended to close the right mouse drag.
handleMouseDown = (e) => { if (!this.props.visible || !this.props.drag) { return; } e.preventDefault(); e.stopPropagation(); if(e.button === 0){ this.setState({ isMouseDown: true, mouseX: e.nativeEvent.clientX, mouseY: e.nativeEvent.clientY, }); } }

no support for mobile phones

On my mobile phone, dragging and zooming (by pinching) doesn't work, and that's a big shame. Especially dragging, which makes this component pretty much unusable for mobiles.

Scroll affect viewer if I set a container for Viewer

So, I think it's not a bug, maybe to need do it optional.
When I use container attribute like in your example in demo site when active inline mode. When you scroll the page in inline mode (you can resize the window to active a scroll) viewer have a reaction on scroll page, and you mouse outside container with Viewer

I use the last version the package

Error: No PostCSS Config found

import 'react-viewer/dist/index.css';

这句代码会导致下面这个错误:

ERROR in ./node_modules/react-viewer/dist/index.css (./node_modules/css-loader!./node_modules/postcss-loader/src!./node_modules/sass-loader/lib/loader.js!./node_modules/react-viewer/dist/index.css)
Module build failed (from ./node_modules/postcss-loader/src/index.js):
Error: No PostCSS Config found in: /Users/hmr/Documents/ngx-trunk/node_modules/react-viewer/dist
at config.load.then (/Users/hmr/Documents/ngx-trunk/node_modules/postcss-load-config/src/index.js:55:15)
at
@ ./node_modules/react-viewer/dist/index.css 2:14-130 21:1-42:3 22:19-135
@ ./src/common/components/media/MediaViewer.js
@ ./src/web/entity/EntityViewer.js
@ ./src/web/entity/EntityBody.js
@ ./src/web/entity/Entity.js
@ ./src/web/app/routes.js
@ ./src/web/app/App.js
@ ./src/web/app/Root.js
@ ./src/web/main.js
@ ./src/web/index.js
@ multi webpack-hot-middleware/client?path=http://192.168.1.101:8080/__webpack_hmr ./src/web/index.js

请大神指教!谢谢

建议

你好 , 我在练习reactDemo的时候 在移动端有一个查看图片的需求 故此从网上找到了这个插件
在实际操作过程中 发现该插件对移动端好像并不友好
目前已知两个问题:
1. 没有可以修改Parent/child元素style的api;
2. 没有提供手势缩放的效果, 按钮缩放略显鸡肋;
希望可以获得改善 提高使用体验 谢谢.

activeIndex resets on intervals

We have problem with the image viewer activeIndex being reset whenever an interval in our code executes. Any idea what might cause this?

Below is a gif illustrating our problem.

viewer-bug

downloading images

If i want to implement an API for downloading images。
Is it troublesome to configure?

API Suggestion

Hi @infeng :

I have some suggestions for React-Viewer.

  1. I would like the image to be fixed in the center. Or it should be a property to be set in the Ex:
    <Viewer imgFixed = {true} />

  2. Some of the functions in the function bar should be optional. Like

2016-12-27 5 40 47

Ex:
const viewerFunctionBarSetting = {
  flip-horizontal: true,
  flip-vertical: false,
  rotate-clockwise: true,
  rotate-counterclockwise: false,
  zoom: true, //(availability for zooming in and out)
}
function render = ()=>(
   <Viewer 
       functionBarSetting = {this.viewerFunctionBarSetting}
       resolution-visibility = {false}/>
)

These are my suggestions for the react-viewer!

Download and changeable

Can u make changeable to read from props ?
Download it directly without opening the new window
README change download to downloadable in props list

can't close react-viewer in mobile phones

In computer webpage, I can use "esc" key to exit the viewer, However, in mobile phone when I want to close the viewer, I can't find places to close the viewer.
2017-01-23 12 16 57

Maybe there can be a gesture or a button available for exiting the viewer?

Thanks!!

无法用滚轮进行缩放

2.5.0版本,demo是可以用滚轮缩放的,但是我自己用在antd项目下时无法用滚轮缩放,其他快捷键都是可以用的,上代码:

<Viewer
    visible={this.state.visible}
    onClose={() => {
        this.setState({ visible: false });
    }}
    onMaskClick={() => {
        this.setState({ visible: false });
    }}
    images={imageList}
    zoomSpeed={0.3}
    activeIndex={this.state.activeIndex}
/>

github page

make github page to introduce the project

Not able to see the images when "Viewer" is placed inside a modal

I placed the Viewer with inline container in a modal. Only seeing the small image previews and close button. When trying the Viewer in modal view, the images are getting displayed behind main modal. What can I do to fix this? I want to use inline view

Thanks

Renders in full screen instead of inline

[email protected]

I have the following code:

                    <Grid container alignItems="center" justify="center">
                      <Grid item xs={12}>
                        <Typography>
                          Документ "{document.name}" (id {document.id})
                        </Typography>
                      </Grid>
                      <Grid item xs={8} className={classes.images}>
                        <div
                          ref={ref => (this.container = ref)}
                          className={classes.viewer}
                        />
                      </Grid>
                      <Grid item xs={4}>
                        right
                      </Grid>

                      <Viewer
                        visible
                        container={this.container}
                        zoomSpeed={0.6}
                        images={document.images.map((image, i) => ({
                          src: `data:image/${
                            document.imageExtensions[i]
                          };base64,${image}`,
                          alt: `Страница ${i + 1} `,
                        }))}
                      />
                    </Grid>

If I leave it as is it renders in full screen mode. But if I rerender it with any changes to state:

  componentDidMount() {
    this.fixViewerInterval = setInterval(() => {
      this.setState({ fixViewer: true });
    }, 500);
  }

  componentWillUnmount() {
    clearInterval(this.fixViewerInterval);
  }

viewer appears in the place it has to be (but full screen blinks one time at page loading and body has "overflow: hidden").

So how to render it correctly without interval fix?

I'm not sure is it a bug or incorrect usage.

image isn't shown on the right place

Hi @infeng :
I have upgraded my react-viewer to 2.2.0. However there seems some problems.
2017-01-04 8 20 29

As you can see in the pic. The image which should be in the center now is in the bottom of the page.

viewer定位

viewer当前是否支持通过css来控制展示位置?
是否可支持?

感谢

Image Display

First, React-Viewer is pretty good! The function is strong, i really like it. Thank you for building it.

However, when I want to display a high resolution size picture. The way it displays bothers me. I wish it could be like
2016-12-19 7 50 49
which is well placed and well zoomed in the center
but the look will be like
2016-12-19 7 45 13
which is extended to full height

Do you have any idea how to fix this problem?

Closing Animation

Hello,
Is there a way to add custom animation to the open and close events? I would like to use a library like react-spring to make a more advanced open and close animation.

Thank you,
Will

1.1.0

  • add keyboard support
  • add flip button

Custom toolbar

I need to add a custom button to the toolbar. But props doesn't seem to provide this feature. Can the current version fulfill my needs? If not, is there any plan to add this feature?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.