Created
August 22, 2019 18:21
-
-
Save vtenfys/d63cd57985bca6c367c5380d04dd2487 to your computer and use it in GitHub Desktop.
Storyblok Responsive Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import PropTypes from 'prop-types' | |
function transformImage(image, options) { | |
const imageService = 'https://img2.storyblok.com/' | |
const path = image.replace('https://a.storyblok.com', '') | |
return imageService + options.join('/') + path | |
} | |
function getSrcSet({ src, width, height, format, quality, scales }) { | |
const srcSet = [] | |
scales.forEach(scale => { | |
const size = `${scale * width}x${scale * height}` | |
const filters = `filters:quality(${quality}):format(${format})` | |
const resultSrc = transformImage(src, [size, filters]) | |
if (scale === 1) { | |
srcSet.push(resultSrc) | |
} else { | |
srcSet.push(`${resultSrc} ${scale}x`) | |
} | |
}) | |
return srcSet.join(', ') | |
} | |
function ResponsiveImg({ | |
src, | |
alt, | |
width, | |
height, | |
quality = 90, | |
formats = ['webp', 'jpeg'], | |
defaultFormat = 'jpeg', | |
scales = [1, 2, 3] | |
}) { | |
return ( | |
<picture> | |
{formats.map(format => ( | |
<source | |
key={format} | |
srcSet={getSrcSet({ src, width, height, format, quality, scales })} | |
type={`image/${format}`} | |
/> | |
))} | |
<img | |
src={getSrcSet({ | |
src, | |
width, | |
height, | |
format: defaultFormat, | |
quality, | |
scales: [1] | |
})} | |
alt={alt} | |
width={width} | |
height={height} | |
/> | |
</picture> | |
) | |
} | |
ResponsiveImg.propTypes = { | |
src: PropTypes.string.isRequired, | |
alt: PropTypes.string.isRequired, | |
width: PropTypes.number.isRequired, | |
height: PropTypes.number.isRequired, | |
quality: PropTypes.number, | |
formats: PropTypes.arrayOf(PropTypes.string.isRequired), | |
defaultFormat: PropTypes.string, | |
scales: PropTypes.arrayOf(PropTypes.number.isRequired) | |
} | |
export default ResponsiveImg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage example: