CDD (Component Driven Development) : ํ๋ฉด์ ๋ ๋ฆฝ์ ์ด๊ณ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ก ๊ตฌ์ฑํ์ฌ ๊ฐ๋ฐํ๋ ๋ฐฉ๋ฒ๋ก
๋ฆฌ์กํธ๋ CDD๋ฅผ ํ ์ ์๊ฒ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค. ๊ทธ๋์ js ์์ html์ ๋ฃ์ด ์ปดํฌ๋ํธ๋ฅผ ์์ฑํ๋ ๋ฐฉ์์ผ๋ก ๊ฐ๋ฐํ ์ ์์๋๋ฐ, Styled Components๋ css๋ ํฌํจํด์ ๋ฆฌ์กํธ ์ปดํฌ๋ํธ๋ก ๊ตฌ์ฑํด ๊ฐ๋ฐํ ์ ์๊ฒ ๋์์ค๋ค.
Styled Components
CSS-in-JS ๋ฐฉ์(css ๋ด์ฉ๋ javascript ์์์ ์์ฑํ ์ ์๋๋ก ํจ)์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, ์คํ์ผ์ ๋ฆฌ์กํธ ์ปดํฌ๋ํธ๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ๋ค.
Styled Components์ ์ฅ๋จ์
์ฅ์
- CSS๋ ์ปดํฌ๋ํธํ ํ ์ ์์
- CSS์ JS์ ์ํธ์์ฉ์ด ์ฌ์ (์๋ฌด๋๋ ๊ฐ์ ํ์ผ์์ ์์ฑํ๊ธฐ ๋๋ฌธ์)
- Class์ ์ด๋ฆ์ ์๋์ผ๋ก ์ง์ด์ค
๋จ์
- ๊ฒฐ๊ตญ์๋ ์ปดํฌ๋ํธ๋ฅผ ์์ฑํด์ผ ์คํ์ผ์ ์ ์ฉํ ์ ์๊ธฐ ๋๋ฌธ์ Javascript์ ํฌ๊ธฐ๊ฐ ๋ฌด๊ฑฐ์์ง
- ์คํ์ผ์ด ์ปดํฌ๋ํธ ๋ ๋๋ง ์์ ์์ ์ ์ฉ๋๊ธฐ ๋๋ฌธ์ ๋ ๋๋ง ์ฑ๋ฅ์ด ์ ํ๋ ๊ฐ๋ฅ์ฑ์ด ์์
- Class ์ด๋ฆ์ด ์์๋ณด๊ธฐ ํ๋ฆ
์ฌ์ฉ ์ค๋น
์ค์น
npm install styled-components
๋ถ๋ฌ์ค๊ธฐ
styled components๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ ์์น์์ ๋ค์๊ณผ ๊ฐ์ด importํ๋ค.
import styled from "styled-components"
์ต์คํ ์
styled component๋ฅผ ์ฌ์ฉํ ๋ css ์์ฑ ๋ฑ์ ์๋์์ฑ ์์ผ์ฃผ๊ธฐ ๋๋ฌธ์ ๋ ํธ๋ฆฌํ๋ค.
https://marketplace.visualstudio.com/items/?itemName=styled-components.vscode-styled-components
vscode-styled-components - Visual Studio Marketplace
Extension for Visual Studio Code - Syntax highlighting for styled-components
marketplace.visualstudio.com
์ฌ์ฉ ๋ฐฉ๋ฒ
1. ์ปดํฌ๋ํธ ๋ง๋ค๊ธฐ
const ์ปดํฌ๋ํธ = styled.ํ๊ทธ์ข
๋ฅ`
css์์ฑ1 : ์์ฑ๊ฐ;
css์์ฑ2 : ์์ฑ๊ฐ;
`
์์ 1
๊ฐ๋จํ๊ฒ ์๊ธด ํ๋์ ๋ฒํผ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์๋ค.
App.jsx
const BlueButton = styled.button`
background-color: blue;
color: white;
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-weight: 500;
cursor: pointer;
`;
function App() {
return (
<>
<div>hello</div>
<BlueButton>ํ๋ ๋ฒํผ</BlueButton>
</>
);
}
์์ 2
์ด๋ฒ์๋ ์์ ์์๋ค์ ์์ง/์ํ ๊ฐ์ด๋ฐ๋ก ์ ๋ ฌํ๋ ๋ถ๋ชจ์์ Container ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์๋ค.
App.jsx
const Container = styled.div`
width: 100vw;
height: 100vh;
display: grid;
place-items: center;
`;
function App() {
return (
<Container>
<div>hello</div>
<BlueButton>ํ๋ ๋ฒํผ</BlueButton>
</Container>
);
}
2. ์ปดํฌ๋ํธ ์ฌ์ฌ์ฉํ๊ธฐ
const ์ปดํฌ๋ํธ = styled(์ฌ์ฌ์ฉํ ์ปดํฌ๋ํธ)`
์ถ๊ฐํ css์์ฑ1 : ์์ฑ๊ฐ;
์ถ๊ฐํ css์์ฑ2 : ์์ฑ๊ฐ;
`
์์
App.jsx
์ฒ์์ ๋ง๋ ํ๋ ๋ฒํผ์ ์์๋ฐ์ ํฐ ํ๋ ๋ฒํผ์ ์คํ์ผ์ ๊ฐ์ง LargeBlueButton ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค์๋ค.
const LargeBlueButton = styled(BlueButton)`
font-size: 2em;
padding: 2em 3em;
`;
function App() {
return (
<Container>
<div>hello</div>
<BlueButton>ํ๋ ๋ฒํผ</BlueButton>
<LargeBlueButton>ํฐ ํ๋ ๋ฒํผ</LargeBlueButton>
</Container>
);
}
+
๊ฐ๋ฐ์ ๋๊ตฌ์ Elements ํญ์ ํ์ธํด๋ณด๋ฉด ์์ฑํ๋ ์ปดํฌ๋ํธ๋ค์๊ฒ ์๋์ผ๋ก ํด๋์ค๋ช ์ด ์์ฑ๋์ด์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด๋ styled components์์ ๋ด๊ฐ ์ปดํฌ๋ํธ์ ์์ฑํ ์คํ์ผ ๋ด์ฉ๋ค์ css๋ก ์ ์ฉ์ํฌ ๋, ์์ฒด์ ์ผ๋ก ์์์ ํด๋์ค๋ช ์ ์์ฑํด ์ค์ ํด์ฃผ๋ ๊ฒ์ ์ ์ ์๋ค.
3. Props ์ฌ์ฉํ๊ธฐ
const ์ปดํฌ๋ํธ์ด๋ฆ = styled.ํ๊ทธ์ข
๋ฅ`
css์์ฑ : ${props => ํจ์ ์ฝ๋}
`
์์ 1
props๋ก ์๋ฌด ๊ฐ๋ ์ ๋ฌํ์ง ์์์ ๋๋ ๊ธฐ๋ณธ๊ฐ 'black'์ด ์ค์ ๋๊ณ ์๋ค.
const PropsButton = styled.button`
background-color: ${(props) => props.backgroundColor || 'black'};
`;
function App() {
return (
<Container>
<PropsButton>Props ๋ฒํผ</PropsButton>
</Container>
);
}
์ด์ props๋ก ๋ฐฐ๊ฒฝ ์์ ๊ฐ์ ์ ๋ฌํ๋ฉด ์ ์ ์ฉ์ด ๋๋ ๊ฒ์ ํ์ธํด๋ณผ ์ ์๋ค.
const PropsButton = styled.button`
background-color: ${(props) => props.backgroundColor || 'black'};
`;
function App() {
return (
<Container>
<div>hello</div>
<BlueButton>ํ๋ ๋ฒํผ</BlueButton>
<PropsButton backgroundColor="green">Props ๋ฒํผ</PropsButton>
</Container>
);
}
๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ค๋ฅธ ์์ฑ๋ค๋ ๋ ์ถ๊ฐํด๋ณผ ์ ์๋ค.
const PropsButton = styled.button`
background-color: ${(props) => props.backgroundColor || 'black'};
color: ${(props) => props.color || 'white'};
border: ${(props) => props.border || 'none'};
border-radius: ${(props) => props.radius || '0'};
padding: ${(props) => props.padding || '0'};
width: ${(props) => props.width || 'auto'};
font-size: ${(props) => props.fontSize || 'auto'};
font-weight: ${(props) => props.fontWeight || 'auto'};
`;
function App() {
return (
<Container>
<div>hello</div>
<BlueButton>ํ๋ ๋ฒํผ</BlueButton>
<PropsButton
backgroundColor="green"
color="black"
border="1px solid black"
padding="10px">
Props ๋ฒํผ
</PropsButton>
</Container>
);
}
์์ 2
์ด๋ฅผ ์์ฉํด์ rgba()ํจ์ ๋ด์ ๊ฐ ๊ฐ์ props๋ก ๋ฐ์์ ํ๋ฉด์ ๋ ๋๋งํด์ค ์ ์๋ค.
const BackgroundColorDiv = styled.div`
width: 300px;
height: 100px;
background-color: rgba(
${(props) => props.input1},
${(props) => props.input2},
${(props) => props.input3},
${(props) => props.input4}
);
`;
function App() {
const [input1, setInput1] = useState(0);
const [input2, setInput2] = useState(0);
const [input3, setInput3] = useState(0);
const [input4, setInput4] = useState(0);
return (
<Container>
<input
type="range"
value={input1}
onChange={(e) => setInput1(e.target.value)}
min={0}
max={255}
/>
<span>{input1}</span>
<input
type="range"
value={input2}
onChange={(e) => setInput2(e.target.value)}
min={0}
max={255}
/>
<span>{input2}</span>
<input
type="range"
value={input3}
onChange={(e) => setInput3(e.target.value)}
min={0}
max={255}
/>
<span>{input3}</span>
<input
type="range"
value={input4}
onChange={(e) => setInput4(e.target.value)}
min={0}
max={1}
step={0.01}
/>
<span>{input4}</span>
<BackgroundColorDiv
input1={input1}
input2={input2}
input3={input3}
input4={input4}
/>
</Container>
);
}
Container ์ปดํฌ๋ํธ์ backgroundColor์ props๊ฐ๋ค์ ์ค์ ํด๋๋ฉด ๋ฐฐ๊ฒฝ ์๊น ์ ์ฒด๊ฐ ๋ฐ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
const Container = styled.div`
width: 100vw;
height: 100vh;
display: grid;
place-items: center;
background-color: rgba(
${(props) => props.input1},
${(props) => props.input2},
${(props) => props.input3},
${(props) => props.input4}
);
`;
function App() {
const [input1, setInput1] = useState(0);
const [input2, setInput2] = useState(0);
const [input3, setInput3] = useState(0);
const [input4, setInput4] = useState(0);
return (
<Container input1={input1} input2={input2} input3={input3} input4={input4}>
{/* input์ ์์ ๋์ผํ๋ฏ๋ก ์๋ต */}
</Container>
);
}
export default App;
4. ์ ์ญ ์คํ์ผ ์ค์ ํ๊ธฐ
// import ํ๊ธฐ
import {createGlobalStyle} from "styled-components"
// ์ต์์ ์ปดํฌ๋ํธ์ ๊ฐ์ ธ๋ค ์ฌ์ฉ
const GlobalStyle = createGlobalStyle`
์ ์ญcss์์ฑ1 : ์์ฑ๊ฐ;
์ ์ญcss์์ฑ2 : ์์ฑ๊ฐ;
`
์ ์ญ ์คํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํ ์ ์๋ค.
cssํ์ผ์ ๊ตณ์ด ๋ฐ๋ก ์์ฑํ์ง ์๋๋ผ๋ ์ต์๋จ ์ปดํฌ๋ํธ์ธ App์์ GlobalStyle์ ์ค์ ํ ๋ถ๋ฌ์ค๋ฉด ๋์ผํ ํจ๊ณผ๋ฅผ ๋ผ ์ ์์ผ๋ฉฐ,
scss์ ๋ณ์๋ ๋ฏน์ค์ธ์ ์ง์ํ์ง ์์ง๋ง, (์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์ถฉ๋ถํ ์ปค๋ฒ๊ฐ ๊ฐ๋ฅํ๋ค.) ์ค์ฒฉ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋ค.
const GlobalStyle = createGlobalStyle`
* {
margin : 0;
padding : 0;
}
input {
width : 90%;
}
`;
function App() {
const [input1, setInput1] = useState(0);
const [input2, setInput2] = useState(0);
const [input3, setInput3] = useState(0);
const [input4, setInput4] = useState(0);
return (
<>
<GlobalStyle />
<Container>
{/* ์ค๋ต */}
</Container>
</>
);
}
์ฐธ๊ณ
https://www.samsungsds.com/kr/insights/web_component.html
https://www.elancer.co.kr/blog/detail/290
'react' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React / Todo-List ๋ง๋ค๊ธฐ (2) (0) | 2025.04.17 |
---|---|
React / ์คํ์ผ๋ง (3) - tailwindcss (0) | 2025.04.17 |
React / ์คํ์ผ๋ง (1) - SCSS (0) | 2025.04.15 |
React / custom Hook (1) | 2025.04.11 |
React / useRef (0) | 2025.04.11 |