Provide a nice way to make images responsive without using srcset and sizes attributes.
- Demo
- Install
- Get Started
- Javascript API
- Preset properties
- Sugar Web Components Documentation
- Browsers support
- Code linting
- Contribute
- Who are Coffeekraken?
- Licence
npm install coffeekraken-s-responsive-img-component --save
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" />
Here's the list of properties that the presets accept.
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
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 HTMLwidthObj
: 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.
IE / Edge |
Firefox |
Chrome |
Safari |
---|---|---|---|
IE11+ | last 2 versions | last 2 versions | last 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.
This package uses some code linting rules. Here's the list:
- StandardJS for javascript files
- Stylelint with stylelint-config-standard for
scss
files
Your commits will not been accepted if the code style is not respected!
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:
We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.
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...