Skip to content

Instantly share code, notes, and snippets.

@akhrabrov
Last active May 27, 2019 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akhrabrov/3f1df247c86b2626024e7ae3c86ab386 to your computer and use it in GitHub Desktop.
Save akhrabrov/3f1df247c86b2626024e7ae3c86ab386 to your computer and use it in GitHub Desktop.
Merge theme providers in styled components
import React from 'react';
import { ThemeProvider }, styled from 'styled-components';
const globalTheme = {
mainColor: 'black',
borderColor: '#99abb3',
borderRadius: '7px',
mixins: {
link: `
color: red;
cursor: pointer;
`,
},
};
const getLocalMixins = theme => ({
flexColumn: `
display: flex;
flex-direction: column;
`,
borderWithRadius: `
border: 1px ${theme.borderColor} solid;
border-radius: ${theme.borderRadius}
`,
});
const localVariables = {
mainColor: 'red',
secondColor: 'blue',
};
const getMergedTheme = theme => ({
...theme,
...localVariables,
mixins: {
...theme.mixins,
...getLocalMixins(theme),
},
});
const StyledComponent = styled.div`
width: 150px;
height: 150px;
color: ${({ theme }) => theme.mainColor};
${({ theme }) => theme.mixins.flexColumn}
`;
export default () => {
return (
<ThemeProvider theme={globalTheme}>
<ThemeProvider theme={getMergedTheme}>
<MyFeature>
<StyledComponent />
<MyFeature />
</ThemeProvider>
</ThemeProvider>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment