Skip to content

Repository files navigation

pixboost-react

Node.js CIcodecov

Library for integrating Pixboost API into the React applications.

Table of Contents:

Installation

To install the library:

npm install --save pixboost-react

There are 3 components that library provides:

  • HiDpiPicture and Picture for responsive images.
  • Image for images that don't need to change size depending on the device.

Library supports React version 14+.

Examples

You can find examples of how to use components in the example application.

Example application is also deployed at http://www.midday.coffee/react-demo/index.html

Responsive images

There are two types of responsive images components available in the library. Both implementations are using <picture> tag for high dpi screens optimisation.

  • HiDpiPicture - Using combination of srcset and sizes attributes, so browser can pick the most suitable one.
  • Picture - Displays fixed sizes images on defined media breakpoints

Which one to use?

HiDpiPicture is preferred for large images because any up-scaling artifacts will be more visible to the user on the screens with DPI > 1. Examples are hero banners, product images, zoom-in images.

Note that you would also need original images to be in a good quality and at least 3x of intended viewports size. For instance, if your product image takes up to 1000px then the original image should be at least 3000px wide.

Picture and Image are a good choice for small images like thumbnails, icons, etc.

HiDpiPicture

HiDpiPicture is a modern way of implementing responsive images using srcset and sizes attributes where we provide browser with variants of the image and visual size constraints to display. The browser then will make hard work for us and pick the best option to load and display.

Example:

// Reusable configurationconstpixboostConfig={apiKey: 'MTg4MjMxMzM3MA__',domain: 'pixboost.com',breakpoints: {lg: {media: '(min-width: 990px)'},md: {media: '(min-width: 640px)'},sm: {}}};functionYourComponent(){return(<HiDpiPicturealt="YO"config={testConfig}breakpoints={{// operation for each breakpoint width {WIDTH} and {HEIGHT} values replaced with generated width and specified height if anysm: {op: 'fit?size={WIDTH}x{HEIGHT}',height: 600},// operation and optional fixed heightmd: {op: 'resize?size={WIDTH}'},lg: {op: 'resize?size={WIDTH}'}}}sizes={{// image width for each breakpointsm: '100vw',md: '640px',lg: '1500px'}}minWidth={300}// minimum visible size of the imagemaxWidth={3000}// maximum visible size of the imagesrc="//here.com/logo.png"/>)}

Properties (mandatory properties marked with *):

PropertyTypeDefaultDescription
src*StringSource url or path with alias to the source image.
breakpoints*ObjectObject that sets an option for each visual breakpoint. You can use only breakpoints that you specified in the configuration property
breakpoints.op*StringOperation to perform. By default is optimise. You can use {WIDTH} and {HEIGHT} placeholders.
breakpoints.heightNumberSetting the fixed height for the image on the given breakpoint
sizes*ObjectObject that specifies width of the image for the particular breakpoint. See More Info.
minWidth*Number
maxWidth*Number
config*ObjectConfiguration
alt*Stringalt text for an image
lazyBooleanfalseIf true, then will enable lazy loading for the image.
imgPropsObjectPass through props to <img> tag.

Picture

Picture component renders different sizes of the image for different visual breakpoints.

Below is a simple example:

//This is a reusable configurationconstpixboostConfig={apiKey: 'MTg4MjMxMzM3MA__',domain: 'pixboost.com',breakpoints: {lg: {media: '(min-width: 990px)'},md: {media: '(min-width: 640px)'},sm: {}}};classYourComponentextendsComponent{render(){return(<Picturealt={"gadgets"}breakpoints={{lg: {//Will optimise image by defaultsrc: 'https://cdn.pixabay.com/photo/2015/01/21/14/14/apple-606761_960_720.jpg'},md: {src: 'https://cdn.pixabay.com/photo/2015/02/02/15/28/bar-621033_960_720.jpg',op: 'fit?size=300x300'},sm: {//We can hide image for certain breakpointshide: true}}}config={pixboostConfig}/>)}}

Properties (mandatory properties marked with *):

PropertyTypeDefaultDescription
breakpoints*ObjectObject that sets an option for each visual breakpoint. You can use only breakpoints that you specified in the configuration property
breakpoints.src*StringURL of the original image
breakpoints.opStringoptimiseOperation to perform. By default is optimise.
breakpoints.hideBooleanfalseIf true then will hide image on the breakpoint.
config*ObjectConfiguration
altStringalt text for an image
lazyBooleantrueIf true, then will enable lazy loading for the picture.
imgPropsObjectPass through props to <img> tag.

Non-responsive images

An Image component will be rendered the same on all screen sizes.

Example:

//This is a reusable configurationconstpixboostConfig={apiKey: 'MTg4MjMxMzM3MA__',domain: 'pixboost.com'};classYourComponentextendsComponent{render(){return(<Imagesrc={'https://cdn.pixabay.com/photo/2016/05/10/15/29/bear-1383980_960_720.jpg'}alt={'lazy bear'}config={pixboostConfig}op={'resize?size=200'}/>)}}

Properties (mandatory properties marked with *):

PropertyTypeDefaultDescription
src*StringURL of the original image
config*ObjectConfiguration
altStringalt text for an image
lazyBooleantrueIf true, then will enable lazy loading for the image.

Configuration

PropertyTypeDefaultDescription
apiKey*StringPixboost API key
domain*StringCustom domain name that you setup or pixboost.com
breakpointsObjectKey-Value pair of breakpoints that you are using for responsive images
breakpoints.mediaStringMedia query for the breakpoint

Browsers Support

The library supports all major browsers including Chrome, Chromium based (including IE), Firefox, Safari.

Build

To build from sources:

npm install
npm run build

Result will be generated into the lib/ folder.

About

React library for integration with Pixboost API

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages