Commercetools tutorial
Commercetools implementation
The react-cloudimage-responsive library is a React component that allows you to easily add responsive images to your web application using the Cloudimage service. With this package, you can load and display optimized images that are automatically resized based on the device screen size and pixel density.
It is a simple and easy-to-use package that can help you improve the performance and user experience of your web application by optimizing it.
Installation
Prerequisites
You can create an account on the Cloudimage registration page.
Install the component
You can install react-cloudimage-responsive using npm or yarn:
or
Usage
To use react-cloudimage-responsive, you need to provide it with a Cloudimage URL and some additional parameters. Here's an example:
In this example, we're passing three props to the "CloudimageResponsive" component:
alt: The alternate text to display for the image. This is required for accessibility reasons.
image: The URL of the original image to be optimized and resized by Cloudimage.
cloudimageConfig: An object that contains the Cloudimage configuration parameters. This includes your Cloudimage API token, the base URL of your Cloudimage service, and the resizing, quality, and format parameters for the image.
Configuration options
Here are the configuration parameters you can use in the "cloudimageConfig" object:
token
Type: String | Default: "demo" | required
Your Cloudimage customer token. Subscribe for a Cloudimage account to get one. The subscription takes less than a minute and is totally free.
customDomain
Type: String | Default: "cloudimage.io" | optional
If you use a custom CNAME for your cloudimage integration, you can set it here.
baseURL
Type: String | Default: "/" | optional
Your image folder on server, this alows to shorten your origin image URLs.
apiVersion
Type: String |Default: 'v7' | optional
Allow to use a specific version of API.
set a specific version of API
disable API version
Note: this will disregard your token above as this should be built into the CNAME entry.
doNotReplaceURL
Type: bool | Default: false
If set to true the plugin will only add query params to the given source of image.
lazyLoading
Type: Bool | Default: true | optional
Only images close to the client's viewport will be loaded, hence accelerating the page loading time. The plugin uses react-lazyload library to achieve it.
lazyLoadOffset
Type: Number/Array(Number) | Default: 100
Say if you want to preload a component even if it's 100px below the viewport (user have to scroll 100px more to see this component), you can set offset props to 100. On the other hand, if you want to delay loading a component even if it's top edge has already appeared at viewport, set offset to negative number.
Library supports horizontal lazy load out of the box. So when you provide this prop with number like 100 it will automatically set left edge offset to 100 and top edge to 100;
If you provide this prop with array like [100, 200], it will set left edge offset to 100 and top offset to 200.
params
Type: String | Default: 'org_if_sml=1' | optional
Applies default Cloudimage operations/ filters to your image, e.g. brightness, contrast, rotation... Multiple params can be applied, separated by "&" e.g. wat_scale=35&wat_gravity=northeast&wat_pad=10&grey=1
alternative syntax: type: Object
placeholderBackground
Type: String | Default: '#f4f4f4' | optional
Placeholder coloured background while the image is loading or use it to set your custom placeholder image or gif
For example:
lowQualityPreview
Type: Object
lowQualityPreview.minImgWidth number (default: 400) - minimum width of an image to load a low-quality preview image
presets
Type: Object
Default:
Breakpoints shortcuts to use in image size property, can be overridden.
limitFactor
Type: Number | Default: 100 | optional
Rounds up the size of an image to the nearest limitFactor value.
For example:
For an image with width 358px and limitFactor equal to 100, the plugin will round up to 400px.
For an image with width 358px and limitFactor equal to 5, the plugin will round up to 360px.
devicePixelRatioList
Type: [Number,...] | Default: [1, 1.5, 2] | optional
List of supported device pixel ratios. If there is no need to support retina devices, you should set an empty array devicePixelRatioList: [].
delay
Type: Number | optional
Delay for processing an image after rendering component.
NOTE: Normally, the parameter is not needed but in some cases with integrating third-party libraries, it can fix wrong calculation of image container.
ImageSizeAttributes
Type: String | possible values: 'use', 'ignore', 'take-ratio' | Default: 'use'
If width and height attributes are set:
use - width & height attributes values will be used to calculate image size (according to user's DPR) and ratio.
take-ratio - width & height attributes values will be used only to calculate ratio.
ignore - width & height attributes will be ignored.
If width and height attributes are NOT set, image container size will be detected to calculate result image size (according to user's DPR)
Note: If only width or height attributes is set, ratio is going to be taken from ci-ratio image attribute
Properties
Here are the properties you can use with the "CloudimageResponsive" component:
alt: The alternate text to display for the image. This is required for accessibility reasons.
image: The URL of the original image to be optimized and resized by Cloudimage.
cloudimageConfig: An object that contains the Cloudimage configuration parameters.
className: An optional class name to apply to the image container.
style: An optional object that contains CSS styles to apply to the image container.
Last updated