Skip to content

Instantly share code, notes, and snippets.

@dominictobias
Last active September 15, 2020 08:30
Show Gist options
  • Save dominictobias/4914e6f23f887ef5938b2f1424cc9490 to your computer and use it in GitHub Desktop.
Save dominictobias/4914e6f23f887ef5938b2f1424cc9490 to your computer and use it in GitHub Desktop.
React CSS Theme
const makeCssTheme = (jsTheme, namespace) =>
Object.entries(jsTheme).reduce((cssTheme, [key, value]) => ({
...cssTheme,
[`--${namespace}-${key}`]: value,
}), {});
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>
);
}
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