Skip to content

Instantly share code, notes, and snippets.

@tol-is
Last active August 17, 2018 10:43
Show Gist options
  • Save tol-is/56861c51d67fd2556305c6aab1b09393 to your computer and use it in GitHub Desktop.
Save tol-is/56861c51d67fd2556305c6aab1b09393 to your computer and use it in GitHub Desktop.
Styled Components - Simple Theme Provider
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