Last active
September 9, 2019 15:42
-
-
Save yoavniran/76b3ad93d43260b739089b68ba7e4aa6 to your computer and use it in GitHub Desktop.
utility to generate breakpoints with cloudinary
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 { generateImageResponsiveAttributes } from "cloudinary/lib/utils/srcsetUtils"; | |
const CLOUD = "__cloud name__"; | |
const BREAK_POINTS = 4; | |
export const getResponsiveAttributes = (id, min, max, transformation) => { | |
const sizes = new Array(BREAK_POINTS) | |
.fill(null) | |
.map((n, i) => `(max-width: ${min * (i + 1)}px) ${(min * (i + 1)) / 2}px`); | |
const srcset = generateImageResponsiveAttributes(id, {}, { | |
min_width: min, | |
max_width: max, | |
max_images: BREAK_POINTS, | |
transformation: { transformation }, | |
}, { | |
cloud_name: CLOUD, | |
}); | |
return { | |
...srcset, | |
sizes, | |
}; | |
}; |
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 {getResponsiveAttributes} from "../getResponsiveAttributes"; | |
const Image = ({publidId}) => { | |
const transformation = [{ | |
quality: "auto", | |
fetchFormat: "auto", | |
dpr: 2, | |
},]; | |
const respAttrs = getResponsiveAttributes( | |
publicId, | |
400, | |
1000, | |
transformation); | |
const url = cloudinary.url(publicId, { transformation }); | |
return ( | |
<img src={url} | |
alt={publicId} | |
srcSet={respAttrs.srcset} | |
sizes={respAttrs.sizes}/> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment