2.2.1 • Published 10 months ago

pixboost-react v2.2.1

Weekly downloads
157
License
MIT
Repository
github
Last release
10 months ago

pixboost-react

Node.js CI codecov

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 configuration
    const pixboostConfig = {
        apiKey: 'MTg4MjMxMzM3MA__',
        domain: 'pixboost.com',
        breakpoints: {
            lg: {media: '(min-width: 990px)'},
            md: {media: '(min-width: 640px)'},
            sm: {}
        }
    };

    function YourComponent() {
      return (
              <HiDpiPicture alt="YO"
                            config={testConfig}
                            breakpoints={{ // operation for each breakpoint width {WIDTH} and {HEIGHT} values replaced with generated width and specified height if any
                              sm: {op: 'fit?size={WIDTH}x{HEIGHT}', height: 600}, // operation and optional fixed height
                              md: {op: 'resize?size={WIDTH}'},
                              lg: {op: 'resize?size={WIDTH}'}
                            }}
                            sizes={{ // image width for each breakpoint
                              sm: '100vw',
                              md: '640px',
                              lg: '1500px'
                            }}
                            minWidth={300}  // minimum visible size of the image
                            maxWidth={3000} // maximum visible size of the image
                            src="//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 configuration
    const pixboostConfig = {
        apiKey: 'MTg4MjMxMzM3MA__',
        domain: 'pixboost.com',
        breakpoints: {
            lg: {media: '(min-width: 990px)'},
            md: {media: '(min-width: 640px)'},
            sm: {}
        }
    };

    class YourComponent extends Component {
        render() {
            return (
                <Picture alt={"gadgets"} breakpoints={
                    {
                        lg: {
                            //Will optimise image by default
                            src: '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 breakpoints
                            hide: 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 configuration
    const pixboostConfig = {
        apiKey: 'MTg4MjMxMzM3MA__',
        domain: 'pixboost.com'
    };

    class YourComponent extends Component {
        render() {
            return (
                <Image src={'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.

2.2.1

10 months ago

2.2.0

10 months ago

2.2.0-1

11 months ago

2.1.0

1 year ago

2.0.0

1 year ago

1.6.0

3 years ago

1.5.1

3 years ago

1.5.0

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.0.0

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago