Emotion ์ฌ์ฉํ๊ธฐ
๐์์ํ๋ฉฐ
CSS ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค emotion์ ์ฌ์ฉํด๋ณด๊ณ ์ ๋ฆฌํ๋ ๊ธ!
โ emotion
emotion์ CSS in JS ๋ฐฉ์์ผ๋ก ์คํ์ผ์ ์์ฑํ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค! CSS in JS ๋ฐฉ์์ผ๋ก ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ด๋ฏธ Styled Component๋ฅผ ์ฌ์ฉํด๋ดค๊ธฐ ๋๋ฌธ์ ์ดํดํ๋ ๊ฒ ์์ฒด๋ ์ด๋ ต์ง ์์๋ค. ๐ (์์ฑํ๋ ๋ฐฉ์๋ ๊ฑฐ์ ๋น์ทํ๊ณ โฆ.)
emotion์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์๋ ํฌ๊ฒ ๋ ๊ฐ์ง๊ฐ ์๋๋ฐ ์ด ์ค react๋ฅผ ์ด์ฉํด ์ฌ์ฉํ๋ ๋ฒ์ ๋ํด ์ ๋ฆฌํด๋ณด๊ณ ์ ํ๋ค.
1
npm install @emotion/react @emotion/styled
์ด๋ styled API๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์๋๋ง @emotion/styled
๋ฅผ ์ค์นํด์ฃผ๋ฉด ๋๋ค. ์ด์ ์งํํด๋ณด์!
โก๏ธTS ์ฌ์ฉ
๋จผ์ TSConfigย compilerOptions์ ๋ค์๊ณผ ๊ฐ์ ๋ด๋ฝ์ ์ถ๊ฐํด์ฃผ์ด์ผ ํ๋ค.
1
2
3
4
{
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react"
}
โก๏ธCSS Type
CSS ํ์ ์ ์ค์ ํ ๋ csstype ํจํค์ง๋ฅผ ์ฌ์ฉํด type์ ์ ์ํด์ค๋ค.
1
npm install csstype
โก๏ธBabel Plugin
Babel์ ์ต์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ๊ตฌํ ๋ธ๋ผ์ฐ์ ์์๋ ์ฌ์ฉํ ์ ์๋๋ก ํด์ฃผ๋ ํธ๋์คํ์ผ๋ฌ๋ค. ๐
๋ง์ฝ ํ์ฌ ์ฌ์ฉ์๊ฐ Babel์ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ๋ฌ๊ทธ์ธ์ ์ถ๊ฐํด ์ค ์ ์๋ค.
1
npm install -D @emotion/babel-plugin
โ CSS ์์ฑํ๊ธฐ
emotion์์ ์ ๊ณตํ๋ css ๊ธฐ๋ฅ์ ์ด์ฉํด ๋ค์ํ ๋ฐฉ์์ผ๋ก ์คํ์ผ๋ง ํ ์ ์๋ค.
โก๏ธ๊ฐ์ฒด๋ฆฌํฐ๋ด
๋จผ์ ์๋์ ๊ฐ์ด ๊ฐ์ฒด ๋ฆฌํฐ๋ด(๊ฐ์ฒด ์ ์ ๊ตฌ๋ฌธ) ๋ฐฉ์์ผ๋ก ์์ฑํด ์ค ์ ์๋ค. ์์ฑํ ๋ ๋๊ฐ์ ๊ดํธ `` ์์ ๋ฃ์ด์ผ ํ๋ ๊ฒ์ ์ฃผ์ํ์.
1
2
3
4
5
6
7
8
9
import { css } from "@emotion/react"
const Practice = () => (
<div
css=
>
๋ง์ฐ์ค๋ฅผ ํธ๋ฒํ์ธ์!
</div>
)
โก๏ธํ ํ๋ฆฟ๋ฆฌํฐ๋ด
๋ฐฑํฑ์ ์ฌ์ฉํด ์์ฑํ๋ ๋ฐฉ์๋ ์กด์ฌํ๋ค. ์์์ ๊ฐ์ฒด๋ฆฌํฐ๋ด์ ์ฌ์ฉํ ๋๋ backgroundColor๊ณผ ๊ฐ์ด Camel Case๋ก ์์ฑํ์ผ๋, ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด์ ๊ฒฝ์ฐ background-color์ฒ๋ผ ์ผ๋ฐ css ์์ฑ ๋ฐฉ์๊ณผ ๋์ผํ๊ฒ ์์ฑํด์ผ ํจ์ ์ฃผ์ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { css } from "@emotion/react"
const Practice = () => (
<div
css={css`
background-color: red;
&:hover {
color: lightgreen;
}
`}
>
๋ง์ฐ์ค๋ฅผ ํธ๋ฒํ์ธ์!
</div>
)
โก๏ธ๋ณ์ ์ฌ์ฉํ๊ธฐ
๋ณ์๋ก css๋ด์ฉ์ ์ ์ธํ์ฌ ์ ์ฉํ ์๋ ์๋ค!
1
2
3
4
5
6
7
8
9
10
11
12
import { css } from "@emotion/react"
const buttonStyle = css`
color: red;
&:hover {
color: lightgreen;
}
`
const Practice = () => (
<button className={buttonStyle}>๋ง์ฐ์ค๋ฅผ ํธ๋ฒํ์ธ์!</button>
)
โ Styled API ์ฌ์ฉํ๊ธฐ
Styled API๋ฅผ ์ฌ์ฉํด ์คํ์ผ์ด ์ ์ฉ๋ element๋ฅผ ๋ง๋ค ์ ์๋ค! ๊ธฐ์กด์ ์ฌ์ฉํด๋ณด์๋ Styled Component ์์ฑ๊ณผ ๋์ผํ ๋ฐฉ์์ด๋ค.
1
2
3
4
5
6
7
8
9
10
import styled from "@emotion/styled"
const Button = styled.button`
color: red;
&:hover {
color: lightgreen;
}
`
const Practice = () => <Button>๋ง์ฐ์ค๋ฅผ ํธ๋ฒํ์ธ์!</Button>
โ Emotion VS Styled-Components
๊ทผ๋ฐ ์ด์ฏค ๋๋โฆ ๋ ๊ฐ์ง์ CSS in JS๊ฐ ์ด๋ค ์ฐจ์ด์ ์ด ์๋์ง ๊ถ๊ธํด์ก๋ค.
๋จผ์ , emotion ๊ณต์ ๋ฌธ์์์๋ ์์ ๋ค์ styled API๊ฐ Styled Components์ glamorous์์ ๋ง์ ์๊ฐ์ ๋ฐ์๋ค๊ณ ์ฐ์ฌ์๋ค. ๐ค
๊นํ๋ธ โญ์ ์ฐธ๊ณ ํด๋ณด๋ฉด styled components๊ฐ 40.2k, emotion์ด 17.3k๋ก styled components๊ฐ ํจ์ฌ ๋์ง๋ง, ์ต๊ทผ ๋ค์ด๋ก๋ ์๋ฅผ ๋ณด๋ฉด @emotion/react > styled components > @emotion/styled
์ ์์์ ๋ณด์ธ๋ค. ๐ค
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ด์ฆ๋ styled components๊ฐ @emotion/react์ @emotion/styled ๋ณด๋ค ํฐ ํธ์ด๋ค.
๋ง์ฝ @emotion/react๋ง ์ฌ์ฉํ๋ค๋ฉด ๋ ๊ฐ๋ฒผ์์ง๋ค.๐
์ฌ๋ฌ ๊ธ ๋ค์ ์ฐพ์๋ณด์๋๋ฐ, ์ด๋ค ๊ธ์ emotion์ด ์ฑ๋ฅ์ ์ด์ ์ด ์๋ค๊ณ ๋ ํ์ง๋ง styled components๋ ์ฑ๋ฅ ํฅ์์ ์ด๋ฃจ๊ณ ์๋ค๊ณ ํ๋ค.
๊ฐ์ธ์ ์ผ๋ก๋ ์ฌ์ด์ฆ๊ฐ ์๊ณ , css props์ styled API๋ฅผ ๋ชจ๋ ๊ฐ์ง๊ณ ์๋ค๋ ์ ์์ emotion์ ์์ ๋ค์ด์ฃผ๊ณ ์ถ๋ค.