Skip to content

Instantly share code, notes, and snippets.

@fgnass
Last active April 20, 2017 07:03
Show Gist options
  • Save fgnass/b678ef44e484896e16db9941a1ff2885 to your computer and use it in GitHub Desktop.
Save fgnass/b678ef44e484896e16db9941a1ff2885 to your computer and use it in GitHub Desktop.
Pattern for styled components with lots of states
import styled, { css } from 'styled-components';
const Button = styled.button`
font-size: 1em;
margin: 1em;
border: 2px solid palevioletred;
border-radius: 3px;
/* This gets hard to read if there is more than one state: */
background: ${props => props.primary && 'palevioletred'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
`;
const Button = styled.button`
font-size: 1em;
margin: 1em;
border: 2px solid palevioletred;
border-radius: 3px;
color: palevioletred;
/* Use nested css blocks instead: */
${props => props.primary && css`
background: palevioletred;
color: white;
`}
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment