Styled Components Flashcards
Styled Components
Library to style css in js
- automatic critical CSS
- no class name bugs
- easier deletion of css
- simple dynamic styling
- adapting the styling of a component based on its props or global theme
- painless maintenance
- automatic vendor prefixing
Creating a styled component
import styled from ‘styled-components’
styled returns a React Component, so the name of the styled component, should also follow react convention.
const BasicTitle = styled.h1
text-align: center;
text-transform: capitalize;
Under the hood, styled component passes the children prop, so it automatically renders what is inside the styled component
How do you pass props to Styled Component?
In styled components we pass in the props. Prop name is up to you.
Similarly, to typical React Component we can access it in our styled component
color: ${({color}) => color && ‘red’};
How do you extend styled component and why?
You can add additional styles on top of a component.
export const DefaultButton = styled.button` color: ${({color}) => color && 'red'}; padding: 0.2em 2em; border: none; border-radius: 4px; `
export const HipsterButton = styled(DefaultButton)` width: 400px; background: transparent; color: red; border: 1px solid red; `
How do you create more complex Components?
The convention is that you create a Wrapper
const Wrapper = styled.div` h1 { text-transform: capitalize; text-align: center; }
.underline { width: 5rem; height: 0.25rem; background: red; margin: 0 auto; } `
and return it in React Components JSX
What is an alternative way, to Wrap React Components with Styled Component?
It’s not very convenient, because it’s harder to make code modular. index.js and styles.js files.
I should only extend Styled Components.
const ComplexTitle = ({title, className}) => {
return ( <div> <h1>{title}</h1> <div></div> </div> ); };
const Wrapper = styled(ComplexTitle)` h1 { text-transform: capitalize; text-align: center; }
.underline { width: 5rem; height: 0.25rem; background: red; margin: 0 auto; } `;
export default Wrapper;
What does ‘as’ prop do to an element?
“as” is a polymorphic prop to dynamically swap out the element that receives the styles you wrote:
Link with Button Styles
What is the best way to setup global style variables?
There are two options:
1) We can setup a css variables in a GlobalStyles.js file, which we include in our App component. Then we can refer in every styled component by css variables - var(–text-color)
2) We can setup a file with variables in standard objects, which we import to the styled components.
Usually, the 1st option is the better one, however there is one upside of the 2nd option: css variables can only store a single value. In the named export, we can for instance store a function that will create us a desired border:
const getBorder = ({width, type, color }) => ${width}px ${type} ${color}
How can you provide theming in Styled Components?
- Wrap your app in ThemeProvider
- You can pass theme prop, to your ThemeProvider passing the JavaScript object.
- On the component side, you can access the theme by passing a function:
color: ${(props) => props.theme.color};
How do you add animations in styled components?
import {keyframes} from ‘styled-components’
const spinner = keyframes` to { transform: rotate(360deg); } `
const Loading = styled.div` // some styles animation: ${spinner} 0.6s linear infinite; `
How can you add attributes to styled component
You can chain .attrs method and pass either an object or a function, that returns an object.
Object works only for the most simple examples (static attribute), if you would like to set the attribute based on props, you should use the the function, like:
const Button = styled.button.attrs((props) => { return {type: props.type} })``
Whats the purpose of css function?
css is a helper function that can be imported from
import styled, {css} from ‘styled-components/macro’;
allows us to generate css from template literals with interpolations, from functions based on variables.
const Button = styled.button.attrs((props) => { return { type: props.type || 'button'} })` background: var(--primary); border: none; color: white; cursor: pointer; ${({type}) => { return ( type === 'submit' && css` display: block; width: 100%; margin-top: 1rem; border-radius: 0.25rem; ` ) }} `
What’s another way to setup style properties conditionally?
css function allows you to interpolate and flatten the styles conditionally, however if you would like to setup styles for a specific property.
const Wrapper = styled.div` p { color: ${({ price }) => { if (price > 10) return "red"; else if (price > 5) return "yellow"; else return "green"; }}; } `;