# Styled Components
v5+
# Basics
- It's css-in-js
- Installation (opens new window)
- It creates a new React component with
<htmlTag>
or<AnyReactComponent>
with styles. - Scss like Preprocessor used is Stylis (opens new window)
- Allows nesting(
&
), pseudo-selector, etc
- Allows nesting(
|
|
# Passed Props
// style
background: ${(props) => (props.primary ? "cyan" : "transparent")};
color: ${(props) => (props.color || "red"};
// jsx
<div>
<Title primary> foo </Title>
<Title color="blue"> foo </Title>
</div>
# Extend
|
|
# Styling any Components
- It's same as extend above. Just with AnyReactComponent
- Needs
className
|
|
# Css Nesting
- Provided by Stylis.
|
|
# Attaching additional props/attrs
|
|
# Animations
|
|
# Advanced
# Theme
<ThemeProvider>
uses ContextAPI.
|
|
# API
# css helper
|
|
# Tooling
# Stylelint
- Avoid it since my stylint config for
react-refresh-app
is working nicely for StyledComponents too.