1.0.3 • Published 5 years ago

coffeekraken-s-responsive-img-component v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Coffeekraken s-responsive-img-component

Provide a nice way to make images responsive without using srcset and sizes attributes.

View demo

Table of content

  1. Demo
  2. Install
  3. Get Started
  4. Javascript API
  5. Preset properties
  6. Sugar Web Components Documentation
  7. Browsers support
  8. Code linting
  9. Contribute
  10. Who are Coffeekraken?
  11. Licence

Install

npm install coffeekraken-s-responsive-img-component --save

Get Started

First, import the component into your javascript file like so:

import SResponsiveImgComponent from 'coffeekraken-s-responsive-img-component'

Register a preset

SResponsiveImgComponent.registerPreset('banner', {
  widths: [{
    width: 340,
    name: 'mobile',
    pixelRatios: [1,2]
  }, {
    width: 640,
    name: 'tablet',
    pixelRatios: [1,2]
  }, {
    width: 1280,
    name: 'desktop',
    pixelRatios: [1,2]
  }],
  computeSrc: (src, widthObj) => {
    return src + `?auto=compress&w={width}&fit=crop&fm=png&dpr={pixelRatio}`
  }
})

Then simply use it inside your html like so:

<img is="s-responsive-img" lazy-src="https://assets.imgix.net/hp/snowshoe.jpg" preset="banner" />

Preset properties

Here's the list of properties that the presets accept.

widths

This property is the main one. It's an array of width object. Make sure you enter the width objects in ascendant width order. Here's the supported width object properties:

  • width : Specify an image size available for this image.
  • name : A name for this image size.
  • pixelRatios : An array of available pixel ratios for this image
  • Any other key/value pairs you want that will be available in the computeSrc function

computeSrc

This property represent a function that will be called before applying the new image src. You will have access to the width object corresponding to the better size to apply. Here's the attributes that you will have:

  • src : The original image source taken from the original HTML
  • widthObj : The best suited width object for this image with all your custom key/pairs values

This function need to return the new image src to apply. Here's an example:

{
  widths: [{
    // widths here...
  }],
  computeSrc: (src, widthObj) => {
    // {width} is the same as ${widthObj.width}
    return src + `?auto=compress&w={width}&fit=crop&fm=png&dpr={pixelRatio}`
  }
}

If you set some "tokens" like {width} in the url, they will be replaced with the corresponding width object property.

Browsers support

IE / EdgeFirefoxChromeSafari
IE11+last 2 versionslast 2 versionslast 2 versions

As browsers are automatically updated, we will keep as reference the last two versions of each but this component can work on older ones as well.

The webcomponent API (custom elements, shadowDOM, etc...) is not supported in some older browsers like IE10, etc... In order to make them work, you will need to integrate the corresponding polyfill.

Code linting

This package uses some code linting rules. Here's the list:

  1. StandardJS for javascript files
  2. Stylelint with stylelint-config-standard for scss files

Your commits will not been accepted if the code style is not respected!

Contribute

This is an open source project and will ever be! You are more that welcomed to contribute to his development and make it more awesome every day. To do so, you have several possibilities:

  1. Share the love ❤️
  2. Declare issues
  3. Fix issues
  4. Add features
  5. Build web component

Who are Coffeekraken

We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.

More on who we are

License

The code is available under the MIT license. This mean that you can use, modify, or do whatever you want with it. This mean also that it is shipped to you for free, so don't be a hater and if you find some issues, etc... feel free to contribute instead of sharing your frustrations on social networks like an asshole...