๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

main/React

[React] Emotion ์ ์šฉํ•˜๊ธฐ 1 - @emotion/styled (Styled Components)

๐Ÿคท๐Ÿป‍โ™€๏ธ ์ผ๋ฐ˜ CSS๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  Emotion์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š”?

JavaScript๋กœ CSS ์Šคํƒ€์ผ์„ ์ž‘์„ฑํ•˜๊ธฐ์— ํŽธํ•˜๊ฒŒ ์„ค๊ณ„๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.! (css-in-js)

 

๐Ÿคท๐Ÿป‍โ™€๏ธ ๊ทธ๋ ‡๋‹ค๋ฉด Emotion ๋ฟ์ธ๊ฐ€?

No! [styled components] ๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์žˆ์Œ.

npm trends์—์„œ ์ง€๋‚œ 6๊ฐœ์›”๊ฐ„ ๋‹ค์šด๋กœ๋“œํšŸ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๋‹ˆ ์‚ฌ์šฉ๋ฅ ์€ emotion์ด ์ข€ ๋” ๋†’์•˜๋‹ค.

 


@emotion/styled์„ ๋จผ์ € ์•Œ์•„๋ณด์ž.

 

๐Ÿ‘‰๐Ÿป ์„ค์น˜

// react์—์„œ ์Šคํƒ€์ผ API๋กœ ์ง์ ‘ ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํŒจํ‚ค์ง€
npm i @emotion/styled
yarn add @emotion/styled

 

๐Ÿ‘‰๐Ÿป ์„ค์ •

emotion์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” babel์˜ ์„ค์ •์„ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผํ•˜๋Š”๋ฐ,

๋งŒ์•ฝ react๋ฅผ CRA ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ–ˆ๋‹ค๋ฉด eject์„ ํ•ด์ค€ ํ›„ babel์„ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

CRA ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ์™„์ „ํ•˜์ง€๋Š” ์•Š์œผ๋‹ˆ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด .babelrc ์„ค์ •์„ ๊ถŒ์žฅ

 

- CRA Project

// plugin ์‚ฌ์šฉ์‹œ
npm install --save-dev @emotion/babel-plugin
yarn add --dev @emotion/babel-plugin

 

- Set .babelrc

{
  "plugins": ["@emotion"]
}

 

 

๐Ÿ‘‰๐Ÿป Styled Components

React Component๋ฅผ ์ง์ ‘ Customํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ!

 

props๋ฅผ ๋ฐ›์•„ ์™€์„œ ๋กœ์ง์„ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

import styled from '@emotion/styled';

const MyButton = styled.button`
  color: ${props =>
    props.primary ? 'hotpink' : 'skyblue'};
`;
  
render(
  <Container>
    <MyButton primary>This is My Button.</MyButton>
  </Container>
)

 

props์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

import styled from '@emotion/styled'

const SideTab = styled.div`
  display: ${({isMobile}) => {
    console.log(isMobile, "isMobile")  // ๋ฐ›์•„์˜จ isMobile์˜ boolean ๊ฐ’ ์ถœ๋ ฅ
    return isMobile ? 'none' : 'block'
  }};
`;

const TestSideTab = styled.div(
  {color: 'purple'},
  props => ({fontSize: props.fontSize})
);

function SideTabComponent() {
  const isMobile = useMediaQuery({maxDeviceWidth: 500});  // boolean
  
  return (
    <SideTab isMobile={isMobile}> 
      <TestSideTab fontSize="200">Hi</TestSideTab>
    </SideTab>
  )
}

 

๋ถ€๋ชจ ํƒœ๊ทธ์—์„œ ์ž์‹ ํƒœ๊ทธ์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝ์‹œ์ผœ์ค„ ์ˆ˜ ์žˆ๋‹ค. CSS ์ ์šฉ ์ˆœ์„œ์— ์œ ์˜!

import styled from '@emotion/styled'
      
const MyButton = styled.button`
  color: ${props =>
    props.primary ? 'hotpink' : 'skyblue'};
`;

const Test = styled.div`
  color: hotpink;
  ${MyButton} {color: green;};
  
  & > button {  // ์ž์‹์˜ button๋งŒ
    margin: 5px;
    border: none;
    background-color: transparent;
    color: aqua;
  }
`;
      
function Compo() {
  return (
    <MyButton>My Color is skyblue</MyButton>
    <Test>Hi I'm Test Component
      <MyButton primary>My Color is green.</MyButton>
    </Test>
  )
}

 

 

 

Emotion - Styled Components

styled is a way to create React components that have styles attached to them. It’s available from @emotion/styled. styled was heavily inspired by styled-components and glamorous Styling elements and components styled is very similar to css except you cal

emotion.sh