Created
March 8, 2019 06:41
-
-
Save EdwardIrby/921fec52bbe4b77f92d478f31c4702e8 to your computer and use it in GitHub Desktop.
Tokens.js a small utility function to make writing custom props a bit easier in js.
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
/** | |
* @param {...{}} objs - object containing camelCased style properties or css custom properties | |
* @returns {<{'--prop': 'value'}>} containing prefixed css custom properties to be inlined. | |
* @example Simple | |
* <Textblock style={tokens({ | |
* lineHeight: 1.2, | |
* fontSize: rem(47.78), | |
* })} /> | |
* @example Conditional Example | |
* <Textblock style={tokens( | |
* inline ? { display: 'inline' } : { display: 'block' }, | |
* { | |
* lineHeight: 1.2, | |
* fontSize: rem(47.78), | |
* }, | |
* )} /> | |
*/ | |
export const tokens = (...objs) => objs | |
.filter(Boolean) | |
.map(obj => Object.entries(obj)) | |
.reduce((acc, cur) => acc.concat(cur), []) | |
.reduce((acc, [key, value]) => ({ ...acc, [`--${key}`]: value }), {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment