Last active
January 4, 2023 13:01
-
-
Save joshuabaker/642e48f443ab5996969dfc7137213542 to your computer and use it in GitHub Desktop.
Converts Chakra responsive syntax (array or object) to sizes for use with image elements.
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 { useTheme } from "@chakra-ui/react"; | |
import { objectToArrayNotation } from "@chakra-ui/utils"; | |
export default function useChakraResponsiveImageSizes(sizes) { | |
const theme = useTheme(); | |
const details = theme.__breakpoints.details; | |
if (!sizes) { | |
return null; | |
} | |
if (typeof sizes === "string") { | |
return sizes; | |
} | |
if (!Array.isArray(sizes)) { | |
sizes = objectToArrayNotation(sizes); | |
} | |
const last = sizes.pop(); | |
return sizes | |
.map((size, index) => | |
size && details[index] | |
? `(max-width: ${details[index].maxW}) ${size}` | |
: null | |
) | |
.concat([last]) | |
.filter(Boolean) | |
.join(", "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage