Created
March 11, 2021 17:01
-
-
Save Cornally/db32294e5ab93aff5d286f2d7ef833b7 to your computer and use it in GitHub Desktop.
Utils for generating responsive styles
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 { createUseStyles } from 'react-jss' | |
export const breakpoints = { | |
'xs': 480, | |
'sm': 720, | |
'md': 1280, | |
'lg': 1440, | |
'xl': 1920 | |
} | |
const styles = [{ | |
prefix: 'p', | |
props: ['padding'] | |
}, { | |
prefix: 'm', | |
props: ['margin'] | |
}, { | |
prefix: 'mr', | |
props: ['marginRight'] | |
}, { | |
prefix: 'ml', | |
props: ['marginLeft'] | |
}, { | |
prefix: 'mt', | |
props: ['marginTop'] | |
}, { | |
prefix: 'mb', | |
props: ['marginBottom'] | |
}, { | |
prefix: 'mx', | |
props: ['marginLeft', 'marginRight'] | |
}, { | |
prefix: 'my', | |
props: ['marginTop', 'marginBottom'] | |
}] | |
// Multiplier for sizes Based on REMs | |
const sizeBase = 0.25 | |
// Suffix for sizing | |
const sizeIncrements = [0, 1, 2, 3, 4, 5, 6, 8, 10, 16, 24] | |
const generateResponsiveStyles = (styles, breakpoints) => { | |
return Object.entries(breakpoints).map(([breakpoint, size]) => ({ | |
[`@media (min-width: ${size}px)`] : { | |
...styles.map(({ prefix, props }) => | |
sizeIncrements.map(increment => ({ | |
[`${breakpoint}:${prefix}-${increment}`] : { | |
...props.map(prop => ({ [prop]: sizeBase * increment + 'rem' })) | |
} | |
})) | |
).flat() | |
} | |
})) | |
} | |
const generateStyles = (styles) => { | |
return styles.map(({ prefix, prop }) => { | |
return sizeIncrements.map(increment => { | |
return { | |
[`${prefix}-${increment}`]: { | |
[prop]: sizeBase * increment + 'rem' | |
} | |
} | |
}) | |
}) | |
} | |
export const responsiveUtils = generateResponsiveStyles(styles, breakpoints).flat(6) | |
export const standardUtils = generateStyles(styles).flat(6) | |
// export default createUseStyles({ | |
// // ...output | |
// }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment