Last active
May 27, 2019 14:37
-
-
Save akhrabrov/3f1df247c86b2626024e7ae3c86ab386 to your computer and use it in GitHub Desktop.
Merge theme providers in styled components
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 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