Last active
September 15, 2020 08:30
-
-
Save dominictobias/4914e6f23f887ef5938b2f1424cc9490 to your computer and use it in GitHub Desktop.
React CSS Theme
This file contains hidden or 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
const makeCssTheme = (jsTheme, namespace) => | |
Object.entries(jsTheme).reduce((cssTheme, [key, value]) => ({ | |
...cssTheme, | |
[`--${namespace}-${key}`]: value, | |
}), {}); |
This file contains hidden or 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
function MyApp({ theme }) { | |
const cssTheme = useMemo(() => makeCssTheme(theme, 'xx'), [theme]); | |
return ( | |
<div style={cssTheme}> | |
{/* | |
Any component in here now has access to: | |
var(--xx-buttonPadding), var(--xx-background) etc. | |
*/} | |
</div> | |
); | |
} |
This file contains hidden or 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
const yourTheme = { | |
background: 'black', | |
textColor: 'white', | |
fontFamily: '"Roboto", sans-serif', | |
fontWeight: 500, | |
buttonPadding: '10px', | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment