1.0.6 • Published 2 years ago
react-viewer-soda v1.0.6
react-viewer-soda
🥤 viewerjs component for react. Automatically detect changes in internal images.
Usage
Image
type:
export interface ImageProps extends ImgHTMLAttributes<HTMLImageElement> {
/** You can get the instance of the viewer by passing a ref to the viewer property in props. */
viewer?: ForwardedRef<Viewer>
/** options for viewer */
options?: ViewerOptions
}Using it is just like a normal img element:
import { FC } from "react"
import { Image } from "react-viewer-soda"
import Viewer from "viewerjs"
const App: FC = () => {
const viewer = useRef<Viewer>(null)
return (
<div>
<Image viewer={viewer} src="https://images.unsplash.com/photo-1608037521244-f1c6c7635194" width={640} />
</div>
)
}
export default AppImageGroup
type:
export interface ImageGroupProps extends HTMLAttributes<HTMLDivElement> {
/** You can get the instance of the viewer by passing a ref to the viewer property in props. */
viewer?: ForwardedRef<Viewer>
/** You can specify the mode for filtering images.
* When the mode is 'exclude', img elements with 'data-no-rvs' will not be previewed.
* When the mode is 'include', only img elements with 'data-rvs' will be previewed.
* The default value is 'exclude'. */
filterMode?: "exclude" | "include"
/** options for viewer */
options?: ViewerOptions
/** Whether to automatically detect changes in the internal images.
* The default value is true. */
autoUpdate?: boolean
}Using it is just like a normal div element:
import { FC, useRef } from "react"
import { ImageGroup } from "react-viewer-soda"
import Viewer from "viewerjs"
const App: FC = () => {
const viewer = useRef<Viewer>(null)
return (
<ImageGroup viewer={viewer}>
<img src="https://w.wallhaven.cc/full/4o/wallhaven-4oggo9.jpg" width={640} />
<img src="https://w.wallhaven.cc/full/nr/wallhaven-nrz77q.jpg" width={640} />
{/* The image below will not be previewed */}
<img data-no-rvs src="https://w.wallhaven.cc/full/4d/wallhaven-4d666l.jpg" width={640} />
</ImageGroup>
)
}
export default AppTips
You can provide the
data-rvs-urlon theimgelement to change the image for the preview:<img src="small.jpg" data-rvs-url="large.jpg" />ImageGrouphas anautoUpdateproperty with a default value oftrue. When enabled, it detects changes in the internal images and automatically callsviewer.update. If yourimgelements are not going to change, you can set it tofalse. Only these changes will be detected:- The addition or deletion of
imgelements. - The
data-rvsanddata-no-rvsattributes ofimgelements.
If your
imgelements will only change attributes other thandata-rvsanddata-no-rvs, such assrcordata-rvs-url, there is no need to enableautoUpdate.- The addition or deletion of