๐๐ป ์ค์น
// ํ๋ ์์ํฌ์ ๊ตฌ์ ๋ฐ์ง ์๋ ๊ฐ์ฅ ๋ฒ ์ด์งํ ํจํค์ง (VanillaJS ๋ฑ์์ ์ฌ์ฉ)
npm i @emotion/css
// react ์ฌ์ฉ์์๊ฒ ๊ถ์ฅ๋๋ ํจํค์ง
npm i @emotion/react
yarn add @emotion/react
๐๐ป CSS Prop
Emotion ์ฌ์ฉํด์ css-in-js ์คํ์ผ๋ง์ ํ๋ ค๋ฉด
CRA์ ๊ฒฝ์ฐ ์ฝ๋์ /** @jsx jsx*/๋ฅผ, ์๋ ๊ฒฝ์ฐ .babelrc์ ์๋์ ๊ฐ์ด ์ค์ ์ ์ถ๊ฐํด์ค๋ค.
- CRA Project
/** @jsx jsx */
import { css , jsx } from '@emotion/react';
const color = 'darkgreen';
render (
<div
css={css`
background-color: hotpink;
&:hover { color: ${ color }; }
`}
>
</div>
)
- Set .babelrc
{
"presets": ["@emotion/babel-preset-css-prop"],
"plugins": ...
}
์ฝ๋ ์๋จ์ /** @jsx jsx */ ์ฌ์ฉํ์ง ์์๋ ๋จ.
๐๐ป ์ฌ์ฉ ์์
/** @jsx jsx */
import { jsx, css } from '@emotion/react'
const baseColor = css`
color: hotpink;
`
cosnt base = css`
margin: 5px;
`
render(
<div css={base}>
<div
css={css`
${baseColor};
background-color: #eee;
`}
>
This is hotpink.
</div>
</div>
)
์์์๊ฒ ์ ์ฉํ๊ธฐ
import { jsx } from '@emotion/react'
render(
<div
css={{
color: 'darkorchid',
'& .name': {
color: 'orange'
}
}}
>
This is darkorchid.
<div className="name">This is orange</div>
</div>
)
hover, focus ๋ฑ ์ ์ฉํ๊ธฐ
const hotpinkHoverOrFocus = css({
'&:hover, &:focus': hotpink
})
๋ฏธ๋์ด ์ฟผ๋ฆฌ ์ ์ฉํ๊ธฐ
<SideTab isMobile={isMobile} css={{'@media(max-width: 500px)': {display: 'none'}}}>
๋ง์ฝ ๋ธ๋ผ์ฐ์ ๋ณ๋ก ์ง์ํ์ง ์๋ ๊ธฐ๋ฅ์ด ์์ ์ ์๋ค๋ฉด, ๋์ฒดํ ์ ์๋ ์์ฑ์ ์ถ๊ฐ
import { jsx } from '@emotion/react'
render(
<div
css={{
background: [
'red',
'linear-gradient(#e66465, #9198e5)'
],
height: 100
}}
>
This has a gradient background in browsers that support
gradients and is red in browsers that don't support
gradients
</div>
)
์ ์ญ ์คํ์ผ ์ถ๊ฐ
<div>
<Global
styles={css`
.some-class {
color: hotpink !important;
}
`}
/>
<Global
styles={{
'.some-class': {
fontSize: 50,
textAlign: 'center'
}
}}
/>
<div className="some-class">This is hotpink now!</div>
</div>
className="some-class"์๊ฒ Global styles css์ ์ฉ