Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Created May 27, 2020 06:33
Show Gist options
  • Save kettanaito/496b0b26a0cf75adc6b325b1457664b8 to your computer and use it in GitHub Desktop.
Save kettanaito/496b0b26a0cf75adc6b325b1457664b8 to your computer and use it in GitHub Desktop.
CSS Variables in Styled Components
import { createGlobalStyle, css } from 'styled-components'
const GlobalStyle = createGlobalStyle`
html {
/* Maps all colors from the `theme` to CSS variables: */
${({ theme }) => Object.entries(theme.colors).map(([name, value]) => css`
--${camelCaseToKebabCase(name)}: ${value};
`)}
}
`
const App = () => (
<>
<GlobalStyle>
<RestOfTheApp />
</>
)
import styled from 'styled-components'
export const Component = styled.div`
/* References a CSS variable: */
color: var(--color-gray-light);
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment