HTML + JS + CSS๊น์ง ๋ฌถ์ด์ ํ๋์ JSํ์ผ ์์์ ์ปดํฌ๋ํธ ๋จ์๋ก ๊ฐ๋ฐํ ์ ์๊ฒ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, CSS๋ฅผ ์ปดํฌ๋ํธ ์์ผ๋ก ์บก์ํํ๋ค.
ํฐ๋ฏธ๋์ ์๋์ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํด styled components ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๊ณ ,
# with npm
$ npm install --save styled-components
# with yarn
$ yarn add styled-components
package.josn์ ์๋์ ์ฝ๋๋ฅผ ์ถ๊ฐํ์ฌ ์ฌ๋ฌ ๋ฒ์ ์ styled components๊ฐ ์ค์น๋๋ ๊ฒ์ ๋ฐฉ์งํ๋ค.
{
"resolutions": {
"styled-components": "^5"
}
}
์ด ํ, app.js์ import styled from "styled-components"
๋ฅผ ์ต์๋จ์ ์์ฑํด import ํด์ฃผ๋ฉด styled components๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
ES6์ Templete Literals ๋ฌธ๋ฒ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ฐฑํฑ( ` )์ ์ฌ์ฉํ์ฌ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ ๋ค.
import styled from "styled-components";
//Styled Components๋ก ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๊ณ
const Button = styled.button`
background-color: blue;
color: white;
`;
export default function App() {
// React ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ฏ์ด ์ฌ์ฉํ๋ค.
return <Button>Button</Button>;
}
๋ง๋ค์ด์ง ์ปดํฌ๋ํธ๋ฅผ ์ฌํ์ฉํ์ฌ ์๋ก์ด ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ๋ค.
import styled from "styled-components";
//Styled Components๋ก ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๊ณ
const Button = styled.button`
background-color: blue;
color: white;
`;
// ์์ ์ปดํฌ๋ํธ๋ฅผ ์ฌํ์ฉํ์ฌ ์๋ก์ด ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ๋ค.
const BigButton = styled(Button)`
padding: 10px;
margin-top: 10px;
`;
export default function App() {
// React ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ฏ์ด ์ฌ์ฉํ๋ค.
return (
<>
<Button>Button</Button>
<br />
<BigButton>Big Button</BigButton>
</>
);
}
React ์ปดํฌ๋ํธ์ฒ๋ผ props๋ฅผ ๋ด๋ ค์ค ์ ์์ผ๋ฉฐ, props ๊ฐ์ ๋ฐ๋ผ์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ ์๋ ์๋ค.
import styled from "styled-components";
//๋ฐ์์จ prop์ ๋ฐ๋ผ ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ ํด์ค๋ค
// props์ ๊ฐ์ด blue๋ฉด background์ ์์ blue, ์๋๋ฉด white
const Button = styled.button`
background: ${(props) => (props.blue ? "blue" : "white")};
`;
export default function App() {
return (
<>
<Button>Button</Button>
<Button blue>Button</Button>
</>
);
}
import styled from "styled-components";
//๋ฐ์์จ prop ๊ฐ์ผ๋ก ๋ ๋๋งํ๋ค
const Button = styled.button`
background: ${(props) => (props.color ? props.color : "white")};
`;
//๋ค์๊ณผ ๊ฐ์ ํ์์ผ๋ก๋ ํ์ฉํ ์ ์๋ค.
const Button2 = styled.button`
background: ${(props) => props.color || "white"};
`;
export default function App() {
return (
<>
<Button>Button1</Button>
<Button color="blue">Button1</Button>
<Button color="red">Button1</Button>
<br />
<Button2>Button2</Button2>
<Button2 color="yellow">Button2</Button2>
<Button2 color="pink">Button2</Button2>
</>
);
}
import styled from "styled-components";
// ์ ์ญ ์คํ์ผ ์ค์ ์ ์ํ ํจ์ import
import { createGlobalStyle } from "styled-components";
// ์ ์ญ ์คํ์ผ ์ค์
const GlobalStyle = createGlobalStyle`
button {
padding : 5px;
margin : 2px;
border-radius : 5px;
}
`
//๋ฐ์์จ prop ๊ฐ์ผ๋ก ๋ ๋๋งํ๋ค
const Button = styled.button`
background: ${(props) => (props.color ? props.color : "white")};
`;
//๋ค์๊ณผ ๊ฐ์ ํ์์ผ๋ก๋ ํ์ฉํ ์ ์๋ค.
const Button2 = styled.button`
background: ${(props) => props.color || "white"};
`;
export default function App() {
return (
<>
<GlobalStyle />{/*์ ์ญ ์คํ์ผ ์ปดํฌ๋ํธ ์ ์ฉ*/}
<Button>Button1</Button>
<Button color="blue">Button1</Button>
<Button color="red">Button1</Button>
<br />
<Button2>Button2</Button2>
<Button2 color="yellow">Button2</Button2>
<Button2 color="pink">Button2</Button2>
</>
);
}
์์
// app.js
import "./styles.css";
export default function App() {
return <button id="practice">Practice!</button>;
}
// CSS
* {
margin: 0.5rem;
}
#practice {
padding: 1rem;
font-size: 2rem;
background: powderblue;
border-radius: 1rem;
transition: 0.5s;
}
#practice:hover {
background: cornflowerblue;
color: white;
transition: 0.5s;
}
์์ ์ฝ๋(๊ธฐ๋ณธ CSS)์ styled components๋ฅผ ์ ์ฉ ํ๋ฉด ์๋์ ๊ฐ๋ค.
// app.js
import "./styles.css";
import styled from "styled-components";
import { createGlobalStyle } from "styled-components";
// Styled Components๋ ์ด๋ฏธ ์ค์น๋์ด ์์ต๋๋ค. import ํ๋ ๊ฒ ์์ง ๋ง์ธ์.
// ์ ์ญ ์คํ์ผ๋ Styled Components ์ปดํฌ๋ํธ๋ก ๋ฐ๊ฟ์ ์ ์ฉํด๋ณด์ธ์.
// #practice:hover ์์ฑ์ ์ด๋ป๊ฒ Styled Components ์ปดํฌ๋ํธ์ ์ ์ฉํ ์ ์์๊น์? ๊ฒ์์ ํตํด ์์๋ณด๊ณ ์ ์ฉํด๋ณด์ธ์.
const GlobalStyle = createGlobalStyle`
button {
margin: 0.5rem;
}
`
const Button = styled.button`
padding: 1rem;
font-size: 2rem;
background: powderblue;
border-radius: 1rem;
transition: 0.5s;
&:hover{
background: cornflowerblue;
color: white;
transition: 0.5s;
}
`;
export default function App() {
return (
<>
<GlobalStyle />
<Button>Practice!</Button>
</>
);
}
Reference: ์ฝ๋์คํ ์ด์ธ