31
loading...
This website collects cookies to deliver better user experience
npm install styled-components
yarn add styled-components
StyledButton.js
import styled from "styled-components";
export default styled.button`
background-color: #7b4cd8;
color: #fff;
padding: 10px;
border: none;
border-radius: 5px;
font-size: 1.25rem;
`
import StyledButton from "./components/StyledButton";
const MyApp = () => {
...
return(
<StyledButton>I am a styled button! 😼</StyledButton>
)
...
}
export default MyApp;
StyledButton
will have the exact same appearance. StyledButtonWithProps.js
import styled from "styled-components";
export default styled.button`
background-color: ${props => props.bg};
color: ${props => props.color};
padding: 10px;
border: none;
border-radius: 5px;
font-size: 1.25rem;
`
import StyledButtonWithProps from "./components/StyledButtonWithProps";
const MyApp = () => {
...
return(
<StyledButtonWithProps bg="#ffc3c3" color="#b6201e">Button 🍓</StyledButtonWithProps>
<StyledButtonWithProps bg="#ffdaca" color="#d85d16">Button 🍑</StyledButtonWithProps>
<StyledButtonWithProps bg="#fff4c7" color="#bb9922">Button 🍋</StyledButtonWithProps>
)
...
}
export default MyApp;
StyledButton.js
import styled from "styled-components";
export default styled.button`
${props => props.buttonType && props.buttonType==="primary" ?
`background-color: #7b4cd8; color: #fff; font-size: 1.25rem;` :
`background-color: #ff31ca; color: #ffe6f9; font-size: 0.95rem;`};
padding: 10px;
border: none;
border-radius: 5px;
`
import StyledButton from "./components/StyledButton";
const MyApp = () => {
...
return(
<StyledButton buttonType="primary">I am a Primary Button! 👆</StyledButton>
<StyledButton>I am a different kind of button! 👇</StyledButton>
)
...
}
export default MyApp;
SuperButton.js
import styled from "styled-components";
export default styled.button`
color: #fff;
width: 200px;
height: 50px;
border: none;
border-radius: 5px;
font-size: 1.25rem;
`
ChildrenButton.js
import styled from "styled-components";
import SuperButton from "./SuperButton";
export default styled(SuperButton)`
background-color: ${props => props.bg};
`
import ChildrenButton from "./components/ChildrenButton";
const MyApp = () => {
...
return(
<ChildrenButton bg="deeppink">Hello! 👋 </ChildrenButton>
<ChildrenButton bg="hotpink">Hello! 👋 </ChildrenButton>
<ChildrenButton bg="coral">Hello! 👋 </ChildrenButton>
)
...
}
export default MyApp;
StyledDiv.js
import styled from "styled-components";
export default styled.div`
p{
font-family: 'Arial', sans-serif;
font-size: 1.5rem;
color: deeppink;
&.primary{
font-weight: bold;
}
}
`
import StyledDiv from "./components/StyledDiv";
const MyApp = () => {
...
return(
<StyledDiv>
<p className="primary">Hello from a Styled Div!</p>
</StyledDiv>
)
...
}
export default MyApp;
<p>
element to append some content to it. Let's take a step further and let's add that content based on a condition.StyledP.js
import styled from "styled-components";
export default styled.p`
{$props => props.before ? `
color: #7b4cd8;
&::before {
content: "${props.before}";
}` :
`color: #ff31ca;`
}
`
import StyledP from "./components/StyledP";
const MyApp = () => {
...
return(
<StyledP before="_">CODE</StyledP>
<StyledP>I don't have a ::before 😢</StyledP>
)
...
}
export default MyApp;
<p>
element and the component will receive the specified text color. Otherwise, since the condition would never be satisfied, the only style property the component would get would be the color.