์ด๋ฆ์์ ์ ์ ์๋ฏ, javascript์์ CSS๋ฅผ ์์ฑ ํ ์ ์๋ ์คํ์ผ.
์คํ์ผ์ ์ ๋ฌํ๋ ์ปดํฌ๋ํธ ๋ง๋ค๊ธฐ
์ปดํฌ๋ํธ์ ์คํ์ผ ์ ์ฉํ๊ธฐ
์ด๊ธฐ ์ธํ
์ ํด์ฃผ๋ฉด className
์ด ์ด๊ธฐ ์ค์ ๊ฐ์ ๋ง๊ฒ ์๋ ๋ถ์ฌ๊ฐ ๋ฉ๋๋ค.
https://bundlehobia.com/๋ฅผ ์ฐธ๊ณ ํด ์ต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ด์ฆ๋ฅผ ํ์ธํด๋ด
์๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฉ๋์ ๋น์ทํด๋ณด์ด์ง๋ง @emotion/react
์ styled-components
๋ฅผ ๋๊ณ ๋น๊ตํด ๋ดค์ ๋ 1.5๋ฐฐ ์ ๋ ์ฐจ์ด๊ฐ ๋ฉ๋๋ค.
๋ง์ฝ styled-component๋ก ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด @emotion/styled
๋ฅผ ์ค์นํ์๋ฉด ๋ฉ๋๋ค.
$ npm install @emotion/react
$ npm install @emotion/styled
์๋์ ๊ฐ์ด className์ ์ปค์คํฐ๋ง์ด์ง ํ๋ ค๋ฉด @emotion/babel-plugin
์ ์ค์นํ๋ฉด ๋ฉ๋๋ค.
$ npm install @emotion/babel-plugin
๐ /.babelrc
labelFormat
์์ ์ค์ ๊ฐ์ ์ง์ ํด์ฃผ๋ฉด ์ธํ
๊ฐ์ ๋ฐ๋ผ์ className์ด ์ง์ ๋ฉ๋๋ค.
{
"presets": ["next/babel", "@emotion/babel-preset-css-prop"],
"plugins": [
[
"@emotion",
{
"autoLabel": "dev-only", //๊ธฐ๋ณธ๊ฐ
"labelFormat": "[dirname]-[filename]-[local]"
}
]
]
}
๐ /tsconfig.json
css props ์ค์
{
"compilerOptions": {
...
"types": ["@emotion/react/types/css-prop"]
},
...
}
import { jsx } from '@emotion/react'
const breakPoints = [375, 768, 1024, 1444]
const mq = breakPoints.map(
bp -> `@media (min-width: ${bp}px)`
)
render (
<div
css={{
color: 'pink'
[mp[0]]: {
color: 'blue'
},
[mq[1]]: {
color: 'deepblue'
}
}}
>
How to use Media Querues !
</div>
<p
css={css`
${mq[0]} {
font-size: 20px;
}
${mq[1]} {
font-size: 26px;
}
>
Emotion CSS !
</p>
)
Emotion CSS
๋ ๊ธฐ๋ณธ์ ์ผ๋ก styled-component
์ ์์ฑ ๋ฒ์ด ์ ์ฌํฉ๋๋ค.
๋ณ์ ํ๋๋ฅผ ์ ์ธํ๊ณ ๊ทธ ์์ ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด์ ์ด์ฉํด CSS ๋ด์ฉ์ ๋ณ์์ ๋ฃ์ด๋ก๋๋ค.
์ด์ ํด๋น ์คํ์ผ์ ์ ์ฉํ ๋ถ๋ถ์ css={๋ณ์๋ช
}
์ ์์ฑํด์ฃผ๋ฉด ๋ฉ๋๋ค.
<div css={container}>
<h1 className="title"> Emotion CSS </h1>
<p className="desc"> ์กฐ๊ฑด๋ถ ์คํ์ผ๋ง ์ ์ฉํ๊ธฐ </p>
</div>
const contiainer = css`
.title {
font-size: 30px;
color: pink;
.desc {
fot-size: 18px;
color: gray
}
}
`
์กฐ๊ฑด์ ๋ฐ๋ผ css๋ฅผ ๋ค๋ฅด๊ฒ ์ ์ฉ ์ํค๊ณ ์ถ์๋
css๋ฅผ ๋ด์ ๋ณ์์ props๋ก ๊ฐ์ ์ ๋ฌํด์ฃผ๋ฉด ๋ฉ๋๋ค
const normalContainer = css`` // ๊ธฐ์กด ๋ฐฉ์
const ConditionalContainer = (props) => css`` //props ์ ๋ฌํ๋ ๋ฐฉ์
๊ดํธ()
์์ ์์๋ฅผ ๋ด์ ์ ๋ฌํฉ๋๋ค.
<div css={CoditionalContainer({ tp })></div>
const ConditionalContainer = (props) => css`
padding: 2rem;
color: ${props.tp === 3
? "red"
: props.nowTime === 2
? "blue"
: props.nowTime === 1
? "green"
: "black"};
`;
Emotion CSS๋ CSS-in-์์ฑ ๋ฐฉ์์ผ๋ก ์ธ๋ผ์ธ์คํ์ผ๋ก ์ง์ ์คํ์ผ์ ์ ์ฉ ํ ์ ์์ต๋๋ค.
<span
css={css`
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
line-height: 1.5;
word-break: break-all;
white-space: no-wrap;
`}
>
{text}
</span>
์์ ๊ฐ์ด ์์ฑํ๋ค๋ฉด ์ง๊ด์ ๋ก css ์ ์ฉ์ ํ์ธ ํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ง๋ง
๋ง์ ์ค์ ์ฝ๋๊ฐ ์์ฑ ๋ ๊ฒฝ์ฐ ๊ฐ๋ ์ฑ์ ํด์น๋ค๋ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
emotion
์ styled-comopnents
์์ฑ๋ฒ๋ ์ง์ํ๋ ๊ฒ์ ๋ณด๊ณ styled-compents
์ emotion
๊ฐ์ ์ด๋ค ์ฐจ์ด์ ์ด ์์๊น ์๊ฐ์ด ๋ค์ด ์ ๋ฆฌํด๋ณด์์ต๋๋ค.
styled-components
์์๋ ํ์
์คํฌ๋ฆฝํธ๋ฅผ ์ง์ํ์ง ์์ @types/styled-components
๋ฅผ ์ถ๊ฐ๋ก ์ค์น ํด์ผํ์ง๋งemotion
์ ๊ฒฝ์ฐ ์์ฒด์์ ํ์
์คํฌ๋ฆฝํธ๋ฅผ ์ง์ํด ๋ณ๋๋ก ์ค์น ํ ํ์๊ฐ ์์ต๋๋ค.์ฐธ๊ณ ์ฌ์ดํธ