Last active
August 17, 2018 10:43
-
-
Save tol-is/56861c51d67fd2556305c6aab1b09393 to your computer and use it in GitHub Desktop.
Styled Components - Simple Theme Provider
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import styled, { ThemeProvider } from 'styled-components'; | |
// Define theme constants | |
const theme = { | |
titleFontSize : '24px', | |
titleFontSizeMD : '32px' | |
}; | |
// Render ThemeProvider in component hierarchy | |
// pass theme via props | |
const App = () =>( | |
<ThemeProvider theme={theme}> | |
<Title>Yay</Title> | |
</ThemeProvider> | |
); | |
// props.theme is available to deep nested styled-components | |
const Title = styled.h2` | |
font-size: ${({theme}) => theme.titleFontSize}; | |
@media (min-width: 768px) { | |
font-size: ${({theme}) => theme.titleFontSizeMD}; | |
} | |
`; | |
// Render React App | |
const rootElement = document.getElementById("root"); | |
ReactDOM.render(<App />, rootElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment