Pedro Tech - Styled Components Full Tutorial - Style Your Components in React (Mar 17, 2021)
Link: https://www.youtube.com/watch?v=-FZzPHSLauc
Description
ReactJS Styled Components tutorial! In this video I will go over everything you need to know about the library! Styled-Components is a css library in react which allow you to style components very easily!
🚀 Learn ReactJS By Building 6 Projects: https://codedamn.com/learn/reactjs-pr...
- PedroTech-ReactJSStyledComponentsFull-20210317
- 1 - Project Setup
- 2 - Basic Usage Demonstration
- 3 - Multiple styled components from the same file
- 4 - Passing props in Styled Components
- 5 - Hover, Active, Focus
- 6 - Nesting styles and ones state affecting by another's interaction
- 7 - Using Custom Elements in Styled components
- 8 - GlobalStyles.style.js
- Getting Started with Create React App
npx create-react-app pedrotech-reactjsstyledcomponentsfull-20210317
cd npx create-react-app pedrotech-reactjsstyledcomponentsfull-20210317
yarn start
- App.js
- index.js
- reportWebVitals.js
vscode-styled-componentsj - Julien Poissonnier
vscode-styled-components - Visual Studio Marketplace
yarn add styled-components
create new components/Button.style.js
import styled from 'styled-components'
export const Button = styled.button`
width:200px;
height:50px;
background-color:red;
`
App.js
import React from 'react'
import { Button } from './components/Button.style'
const App = () => {
return (
<div>
<Button>Click</Button>
<Button>Click</Button>
<Button>Click</Button>
</div>
)
}
export default App
Button.style.js
import styled from 'styled-components'
export const RedButton = styled.button`
width:200px;
height:50px;
background-color:red;
`
export const BlueButton = styled.button`
width:200px;
height:50px;
background-color:blue;
`
export const GreenButton = styled.button`
width:200px;
height:50px;
background-color:green;
`
App.js
import React from 'react'
import { RedButton, BlueButton, GreenButton } from './components/Button.style'
const App = () => {
return (
<div>
<RedButton>Click</RedButton>
<BlueButton>Click</BlueButton>
<GreenButton>Click</GreenButton>
</div>
)
}
export default App
but then seen here ^^ they are just basically the same button but different colors which is inefficient
components/Button.style.js
import styled from 'styled-components'
export const Button = styled.button`
width:200px;
height:50px;
background-color:${props => props.backgroundColor};
`
App.tsx
import React from 'react'
import { Button } from './components/Button.style'
const App = () => {
return (
<div>
<Button backgroundColor="red">Click</Button>
<Button backgroundColor="blue">Click</Button>
<Button backgroundColor="yellow">Click</Button>
</div>
)
}
export default App
components/Button.style.js
export const Button = styled.button`
width:200px;
height:50px;
background-color:${props => props.backgroundColor};
&:hover{
background-color:coral;
}
&:active{
background-color: pink;
}
`
components/Button.style.js
import styled from 'styled-components'
export const Button = styled.button`
width:200px;
height:50px;
background-color:${props => props.backgroundColor};
/* similar to .Button:hover label{} */
&:hover{
background-color:coral;
& label{
color:green;
}
}
&:active{
background-color: pink;
}
`
export const ButtonLabel = styled.label`
font-size:25px;
color:white;
`
create new components/Button.js:
import React from 'react'
const Button = ({ className, buttonLabel }) => {
return <button className={className}><label>{buttonLabel}</label></button>
}
export default Button
Button.style.js
import styled from 'styled-components'
import Button from './Button'
export const StyledButton = styled(Button)`
width:200px;
height:50px;
background-color:${props => props.backgroundColor};
/* similar to .Button:hover label{} */
&:hover{
background-color:coral;
& label{
color:green;
}
}
&:active{
background-color: pink;
}
`
export const ButtonLabel = styled.label`
font-size:25px;
color:white;
`
import the component then observe the export statement of StyleButton
App.js
import React from 'react'
import { StyledButton } from './components/Button.style'
import { AppContainer } from './components/Container.style'
const App = () => {
return (
<AppContainer>
<StyledButton backgroundColor="red" buttonLabel="Red" />
<StyledButton backgroundColor="blue" buttonLabel="Blue" />
<StyledButton backgroundColor="yellow" buttonLabel="Yellow" />
</AppContainer>
)
}
export default App
create new src/GlobalStyles.style.js
import { createGlobalStyle } from 'styled-components'
export const GlobalStyles = createGlobalStyle`
body{
background-color:pink;
margin:0px;
padding:0px
}
`
app.js
import React from 'react'
import { StyledButton } from './components/Button.style'
import { AppContainer } from './components/Container.style'
import { GlobalStyles } from './GlobalStyles.style'
const App = () => {
return (
<AppContainer>
<GlobalStyles />
<StyledButton backgroundColor="red" buttonLabel="Red" />
<StyledButton backgroundColor="blue" buttonLabel="Blue" />
<StyledButton backgroundColor="yellow" buttonLabel="Yellow" />
</AppContainer>
)
}
export default App
This project was bootstrapped with Create React App.
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
Builds the app for production to the build
folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed!
See the section about deployment for more information.
Note: this is a one-way operation. Once you eject
, you can’t go back!
If you aren’t satisfied with the build tool and configuration choices, you can eject
at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject
will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use eject
. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
You can learn more in the Create React App documentation.
To learn React, check out the React documentation.
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify