๊ฐœ๋ฐœ ๐Ÿพ/ReactJS

React ์Šคํƒ€์ผ๋ง - emotion

JOTOKKI 2021. 2. 1. 19:30
728x90

emotion๊ณต์‹ ์‚ฌ์ดํŠธ

: emotion.sh/docs/introduction

 

Emotion - Introduction

Emotion is a library designed for writing css styles with JavaScript. It provides powerful and predictable style composition in addition to a great developer experience with features such as source maps, labels, and testing utilities. Both string and objec

emotion.sh

์ €๋Š” React ๊ฐœ๋ฐœ์‹œ ์ฃผ๋กœ emotion์œผ๋กœ ์Šคํƒ€์ผ๋ง์„ ํ•ฉ๋‹ˆ๋‹ค. 

์Šคํƒ€์ผ๋งŒ ๋”ฐ๋กœ ๋นผ์„œ ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ํŽธํ•˜๊ณ  SASS์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

ํ•จ์ˆ˜ํ˜• ์Šคํƒ€์ผ๋ง๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. 

 

 

1. ์„ค์น˜ ๋ฐ Demo ์†Œ์Šค ์‚ฌ์šฉ

yarn add @emotion/react

๊ณต์‹ ์‚ฌ์ดํŠธ์— ์žˆ๋Š” ์˜ˆ์ œ๋ฅผ ๊ฐ€์ ธ์™€ ๋ณด์ž.

 

/** @jsx jsx */
import { css, jsx } from '@emotion/react'
const color = 'white'

function App() {
  return (
    <div className="App">
       <div
        css={css`
          padding: 32px;
          background-color: hotpink;
          font-size: 24px;
          border-radius: 4px;
          &:hover {
            color: ${color};
          }
        `}
      >
        Hover to change color.
      </div>
    </div>
  );
}

export default App;

 

๋ฆฌ์•กํŠธ ์ตœ์„ ๋ฒ„์ „์œผ๋กœ ์„ค์น˜ํ–ˆ๊ฑฐ๋‚˜ npx๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚  ๊ป๋‹ˆ๋‹ค.

 

errorSyntaxError: pragma and pragmaFrag cannot be set when runtime is automatic.

 

๊ธฐ์กด์— ์ž˜ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ๊ฐ‘์ž๊ธฐ ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ, ์ €๋„ ๋งŽ์ด ๋‹นํ™ฉํ–ˆ๋Š”๋ฐ์š”.

์ด์œ ๋Š” React17๋กœ ์—…๋ฐ์ดํŠธ ๋˜๋ฉด์„œ ๋ฐ”๋€ runtime ์—”์ง„์ด emotion10 ๊ณผ ์ถฉ๋Œ ๋‚œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์€(๋งˆ์Œ์— ๋“ค์ง„ ์•Š์ง€๋งŒ... ์กฐ๋งŒ๊ฐ„ React๋‚˜ emotion ๋‘˜ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์—…๋ฐ์ดํŠธ ํ•˜๊ธธ ๋ฐ”๋ผ๋ฉฐ... ๐Ÿ˜ฅ)

๊ธฐ์กด runtime์„ classic ๋ชจ๋“œ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

/** @jsx jsx */ ๋Œ€์‹  /** @jsxImportSource @emotion/react */ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

/** @jsxImportSource @emotion/react */ 
import { css, jsx } from '@emotion/react'
const color = 'white'

function App() {
  return (
    <div className="App">
       <div
        css={css`
          padding: 32px;
          background-color: hotpink;
          font-size: 24px;
          border-radius: 4px;
          &:hover {
            color: ${color};
          }
        `}
      >
        Hover to change color.
      </div>
    </div>
  );
}

export default App;

์ž˜ ๋‚˜์˜ต๋‹ˆ๋‹ค. ๐Ÿ˜

 

 


2. CSS ๊ณตํ†ต ๊ฐ’ ์ •์˜

์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ํฐํŠธ ํฌ๊ธฐ, ์ปฌ๋Ÿฌ ๋“ฑ์„ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๋ฉด, ์œ ์ง€๋ณด์ˆ˜ํ•˜๊ธฐ ์ข‹๊ณ , ์ฝ”๋“œ๋„ ํ›จ์”ฌ ๊น”๋”ํ•ด ์ง‘๋‹ˆ๋‹ค. 

 

๊ณตํ†ต ์ƒ‰์„ ๋”ฐ๋กœ ๋นผ์„œ colors.js ๋ผ๋Š” ํŒŒ์ผ์— ๊ฐ์ฒด๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

์‚ฌ์šฉํ•  ํŽ˜์ด์ง€์—์„œ ๋ถˆ๋Ÿฌ์™€ ${color} ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

// colors.js
export const colors = {
    default: 'darkseagreen',
    warning: 'darkorange',
    danger: 'lightcoral',
}
/** @jsxImportSource @emotion/react */
import { css, jsx } from '@emotion/react'
import { colors } from './colors'

function App() {
  return (
    <div className="App">
       <button css={buttonStyle}>DEFAULT</button>
    </div>
  );
}

const buttonStyle = css`
  background: ${colors.default};  // darkseagreen ์ปฌ๋Ÿฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด
  color: #fff;
  border: 0 none;
  padding: 8px; 
  margin: 5px;
  border-radius: 3px;
`

export default App;

 

3. ํ•จ์ˆ˜ํ˜•์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ

props๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ ์กฐ๊ฑด๋ถ€๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

props๊ฐ’์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์œผ๋ ค๋ฉด ํ•จ์ˆ˜ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

{...}
function App() {
  return (
    <div className="App">
       <button css={buttonStyle('default')}>DEFAULT</button>
       <button css={buttonStyle('warning')}>DEFAULT</button>
       <button css={buttonStyle('danger')}>DEFAULT</button>
    </div>
  );
}
{...}
const buttonStyle = (type) => {
  return css`
    background: ${
      type === 'default' ? colors.default :
      type === 'warning' ? colors.warning :
      type === 'danger' ? colors.danger : 'lightgray'
    };
    color: #fff;
    border: 0 none;
    padding: 8px; 
    margin: 5px;
    border-radius: 3px;
  `
}

๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜์ž๋‹ˆ ์ƒ‰์ด ์ถ”๊ฐ€๋ ๋•Œ๋งˆ๋‹ค ๋˜ ์ถ”๊ฐ€ํ•ด์•ผ๋˜์ง€์š”? 

๊ฐ์ฒดํ˜•ํƒœ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด, ๊ตณ์ด ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ํ‚ค ๊ฐ’๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋ฉด ๋˜๊ฒ ๋„ค์š”.

const buttonStyle = (type) => {
  return css`
    background: ${colors[type]};
    color: #fff;
    border: 0 none;
    padding: 8px; 
    margin: 5px;
    border-radius: 3px;
  `
}

 

ํ›จ์”ฌ ๋ณด๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค. ๐Ÿ˜

 

๋ชจ๋‘ ์ ์šฉ๋œ ํ™”๋ฉด

 

๋ฐ˜์‘ํ˜•