react-packages

@react-packages/image-thumbnail

React component to create a thumbnail of an exisiting <img/> element without creating a new <img/> with the same src. It’s useful if the image is a non-cached/dynamic resource and the thumbnail is created asynchronously. If we use another <img/> with the same src but smaller size then it will trigger another request to the server for the same resouce.

Basic usage

The following example shows how to use the component with default options:

import Thumbnail from '@react-packages/image-thumbnail';

...

const imgRef = React.useRef<HTMLImageElement>(null);
...
<img id="imageId" ref={imgRef} ... />
...
<Thumbnail image={imgRef.current /*or `document.getElementById("imageId")`*/} ... />

The component props

More options

To change some options, we must use createComponent function as the following example:

import {createComponent} from '@react-packages/image-thumbnail';
const Thumbnail = createComponent({
    ...
});

...

<Thumbnail image={imgRef.current /*or `document.getElementById("imageId")`*/} ... />

createComponent function takes one parameter whose type of object. The object is defined as follows:

type Params = {
    noImage?: React.ReactNode,
    ratioX?: number,
    ratioY?: number,
    styles?: {
        background?: TStyle,
        content?: TStyle,
    },
};

where TStyle is

{
    className?: string,
    style?: CSSProperties,
}