4. styled-components
1. styled-components
styled-components @swc/plugin-styled-components
์คํ์ผ์ด ์ ์ฉ๋ ์ปดํฌ๋ํธ(styled-component)๋ฅผ ์ฝ๊ฒ ๋ง๋ค ์ ์๋ ๋๊ตฌ
๐๐ปโโ๏ธ๏ธ ์ฃผ์ ์ฌํญ
์ ๋ช ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๋๋ถ๋ถ โ์โ ๋ง๋ค์ด์ก๋์ง ํน์ ํ๊ณ ์๋ ์ฒ ํ ๋ฑ์ด ์ ์ ๋ฆฌ๋์ด ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋๋ ๊ณต์๋ฌธ์๋ฅผ ๊ผญ ์ดํด๋ณด๋ ์ต๊ด์ ๋ค์ด๋ ๊ฒ์ด ์ข์ต๋๋ค.
๐ก ์ฐธ๊ณ ๋ ํผ๋ฐ์ค styled-components basics
๐ IDE ํ๋ฌ๊ทธ์ธ ์ค์นํด์ ์ฌ์ฉํ ๊ฒ - ์คํ์ผ ์ฝ๋๋ฅผ ๋ฌธ์์ด๋ก ์ธ์ํด์ ์๋ฌด๋ฐ ๊ตฌ๋ถ์ด ์ ๋๋๋ฐ, ํ๋ฌ๊ทธ์ธ ์ค์นํ๋ฉด CSS์์ ์ธ์ ๐ Babel Plugin ์ SWC์์ ์ธ ์ ์๋๋ก ํฌํ ํ ๊ฒ๋ ํจ๊ป ์ค์นํ ๊ฒ(SSR ์ง์ ๋ฑ์ ์ํ ๊ณต์ ๊ถ์ฅ์ฌํญ)
์ฅ์
์๋ ์ค์ CSS ํ์ด์ง์ ๋ ๋๋ง๋๋ ์ปดํฌ๋ํธ๋ฅผ ์ถ์ ํ๊ณ ํด๋น ์คํ์ผ๋ง ์๋ ์ ์ฉ, ์ฌ์ฉ์๊ฐ ํ์ํ ์ต์ํ์ ์ฝ๋๋ฅผ ๋ก๋
ํด๋์ค ์ด๋ฆ ๋ฒ๊ทธ ์์ ๊ณ ์ ํ ํด๋์ค ์ด๋ฆ์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์ค๋ณต, ๊ฒน์นจ ๋๋ ๋ง์ถค๋ฒ ์ค๋ฅ ๋ฐฉ์ง
์์ฌ์ด CSS ์ญ์ ์คํ์ผ์ด ํน์ ์ปดํฌ๋ํธ์ ์ฐ๊ฒฐ๋์ด ์๊ธฐ ๋๋ฌธ์, ์ปดํฌ๋ํธ๊ฐ ์ญ์ ๋๋ฉด ๋ชจ๋ ์คํ์ผ๋ ํจ๊ป ์ญ์ ๋จ
๊ฐ๋จํ ๋์ ์คํ์ผ๋ง ํด๋์ค๋ฅผ ์๋์ผ๋ก ๊ด๋ฆฌํ ํ์ ์์ด, props ๋๋ global theme์ ๊ธฐ๋ฐ์ผ๋ก ์คํ์ผ์ ์กฐ์ ํ๋ ๊ฒ์ด ๊ฐ๋จํ๊ณ ์ง๊ด์
์ ์ง ๋ณด์ ์ฉ์ด ์ปดํฌ๋ํธ์ ์ํฅ์ ์ฃผ๋ ์คํ์ผ์ ์ฐพ๊ธฐ ์ํด ์ฌ๋ฌ ํ์ผ์ ๋ค์ง ํ์๊ฐ ์์
์๋ ๋ฒค๋ ํ๋ฆฌํฝ์ค CSS๋ฅผ ํ์ฌ ํ์ค์ ๋ง๊ฒ ์์ฑํ๊ณ , ๋๋จธ์ง๋ ์คํ์ผ ์ปดํฌ๋ํธ๊ฐ ์ฒ๋ฆฌ
2. ์ฌ์ฉ ๋ฐฉ๋ฒ
ํจํค์ง ์ค์น
npm i styled-components
npm i -D @types/styled-components @swc/plugin-styled-components
.swcrc
ํ์ผ ์์ฑ
.swcrc
ํ์ผ ์์ฑ{
"jsc": {
"experimental": {
"plugins": [
[
"@swc/plugin-styled-components",
{
"displayName": true,
"ssr": true
}
]
]
}
}
}
๊ฐ๋จํ Styled Component ๋ง๋ค๊ธฐ
import styled from 'styled-components';
const Paragraph = styled.p`
color: #00F;
strong {
font-size: 2em;
}
`;
export default function Greeting() {
return (
<Paragraph>
Hello, world
<strong>!</strong>
</Paragraph>
);
}
๋ธ๋ผ์ฐ์ ์์๋ class๋ก ๋ณํ, ์ธ๋ผ์ธ ์คํ์ผ์ด ์๋๋ผ CSS
๐ก styled ๊ฐ class๋ฅผ ์ถ๊ฐํด์ค
<p class="sc-bgqQcB fSBjJp">Hello!</p>
sc๋ styled-components ๋ฅผ ์๋ฏธ์ค์ฒฉ(nested) ์ ์ง์ ํด๋น ์ปดํฌ๋ํธ์ ํ์ ํ๊ทธ์๋ง ์ ์ฉ๋๋ ๊ฒ
์์ ๋ฐ์ ์ถ๊ฐ๋ก ์คํ์ผ ์
ํ๊ธฐ
์ปดํฌ๋ํธ๋ฅผ ์์๋ฐ์ ์ค๋ณต์ ์ค์ด๊ณ ์ถ๊ฐ๋ก ์คํ์ผ๋ง
import styled from 'styled-components';
const Paragraph = styled.p`
color: #00F;
`;
const BigParagraph = styled(Paragraph)`
font-size: 2rem;
strong {
font-size: 1em;
}
`;
export default function Greeting() {
return (
<BigParagraph>
Hello, world!
</BigParagraph>
);
}

์์๋ฐ์ ๊ฐ์ ๋ฎ์ด์ธ ์ ์์
๊ธฐ์กด ์ปดํฌ๋ํธ์ ์คํ์ผ ์
ํ๊ธฐ
โ ๏ธ ๊ธฐ์กด ์ปดํฌ๋ํธ๊ฐ Class๋ฅผ ์ก์์ค์ผ ํ๋ค๋ ์ ์ ์ฃผ์
styled ๊ฐ class๋ฅผ ์ถ๊ฐํด์ฃผ๊ธฐ ๋๋ฌธ์, className์ ๋ฐ์์ ์ฒ๋ฆฌํด์ผ ํจ
className์ ์๋ ์กด์ฌํ๋ ์์ฑ์ด๊ธฐ ๋๋ฌธ์ React.HTMLAttributes<HTMLElement>
ํ์
์ผ๋ก ์ค์ ํ๋ฉด ๋จ
import styled from 'styled-components';
function HelloWorld({className}: React.HTMLAttributes<HTMLElement>) {
return (
<p className={className}>
Hello, world!
</p>
);
}
const Greeting = styled(HelloWorld)`
color: #00F;
`;
export default Greeting;
โ๏ธ ์กฐ๊ฐ๊ธ
styled
๊ฐ ์ด๋ค ์ญํ ์ ํ๋์ง ๋ชจ๋ฅด๊ณ ์์๋๋ฐ, ๋ธ๋ผ์ฐ์ ์์ class๋ก ๋ณํ๋๋๋ก class๋ฅผ ์์ฑํด์ค๋ค๋ ๊ฒ์ ์ฒ์ ์๊ฒ ๋์๋ค. ์ด๋ฐ ์ด์ ๋ก ๊ธฐ์กด ์ปดํฌ๋ํธ์ ์คํ์ผ์ ์ฃผ๋ ค๋ฉดclassName
์ ๋๊ฒจ์ฃผ๋๋ก ํด์ผํ๋ค. ์ฒ์ ๊ฐ์๋ฅผ ๋ค์ ๋๋ ์ดํด๊ฐ ์ ์๋๋๋ฐ ๋ช ๋ฒ ๋ฐ๋ณตํด์ ๋ค์ผ๋ ๊นจ๋ซ๋ ์๊ฐ์ด ์๋ค.
Last updated