Skip to content

Instantly share code, notes, and snippets.

@pomber
Created September 28, 2018 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pomber/f0bee4a332da67d03acf246b2cd67b9d to your computer and use it in GitHub Desktop.
Save pomber/f0bee4a332da67d03acf246b2cd67b9d to your computer and use it in GitHub Desktop.
Styled Components API snippets
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: palevioletred;
:hover {
color: red;
}
`;
// attrs, props, &
const Thing = styled.div.attrs({
tabIndex: 0,
margin: props => props.size || "1em",
})`
color: ${props => props.divColor || "palevioletred"};
.something {
border: 1px solid; // an element labeled ".something" inside <Thing>
}
&.foo {
background: orange; // <Thing> tagged with an additional CSS class ".foo"
}
`;
// Components className
const Link = ({ className, children }) => (
<a className={className}>
{children}
</a>
);
const StyledLink = styled(Link)`
font-size: 1em;
`;
const TomatoLink = styled(StyledLink)`
color: tomato;
`;
// Polymorphic style
const BlueThing = styled.div`
color: blue;
`;
<BlueThing as="span" />
<BlueThing as="a" />
<BlueThing as={MyComponent} />
// Component selector
const Link = styled.a`
background: papayawhip;
`;
const Icon = styled.svg`
transition: fill 0.25s;
${Link}:hover & {
fill: rebeccapurple;
}
`;
// Objects
const Foo = styled.div({
color: "rgb(97, 97, 97)",
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment