Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created December 2, 2020 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/9f49fc2da77dbe34b8d533d62f2e42a5 to your computer and use it in GitHub Desktop.
Save tzkmx/9f49fc2da77dbe34b8d533d62f2e42a5 to your computer and use it in GitHub Desktop.
custom responsive picture item using pixboost service
import React from 'react'
const host = 'https://media.jefrancomix.work'
const apiKey = 'pixboost'
const imageBoosted = (relativeUrl, resize = false) => {
return 'https://pixboost.com/api/2/img/' +
host +
relativeUrl +
(resize
? '/resize?size=' + parseInt(resize, 10) + '&'
: '/optimise?') +
apiKey
}
export function ResponsiveImage ({
relativeUrl,
alternateText,
widthRatio,
breakpointRatios = {}
}) {
return (
<picture>
<source
media='(max-width: 480px)'
srcSet={imageBoosted(relativeUrl, 480 *
(breakpointRatios[480] ? breakpointRatios[480] : widthRatio))}
/>
<source
media='(max-width: 576px)'
srcSet={imageBoosted(relativeUrl, 576 *
(breakpointRatios[576] ? breakpointRatios[576] : widthRatio))}
/>
<source
media='(max-width: 768px)'
srcSet={imageBoosted(relativeUrl, 768 *
(breakpointRatios[768] ? breakpointRatios[768] : widthRatio))}
/>
<source
media='(max-width: 992px)'
srcSet={imageBoosted(relativeUrl, 992 *
(breakpointRatios[992] ? breakpointRatios[992] : widthRatio))}
/>
<source
media='(max-width: 1200px)'
srcSet={imageBoosted(relativeUrl, 1200 *
(breakpointRatios[1200] ? breakpointRatios[1200] : widthRatio))}
/>
<source
media='(max-width: 1366px)'
srcSet={imageBoosted(relativeUrl, 1366 *
(breakpointRatios[1366] ? breakpointRatios[1366] : widthRatio))}
/>
<img
src={imageBoosted(relativeUrl)}
alt={alternateText}
/>
</picture>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment