티스토리 뷰

🚀 styled-components와 emotion, 비슷해보이는데.. 뭐가 다를까
저는 css-in-js를 styled-components로 시작하게 되어서 줄곧 styled-components만 사용했었는데요. 그런데 emotion을 사용하는 회사도 많아서 한번 사용해 보았는데 사용법이 거의 동일해서 대체 어떤 차이가 있는지 궁금해 정리하게 되었습니다 :)
제공하는 기능
라이브러리 | props 전달 | 미디어 쿼리 | 전역 스타일링 | 중첩 선택자 | Server Side Rendering |
테마 스타일링 |
스타일 컴포넌트 상속 |
styled- components |
⭕ | ⭕ | ⭕ | ⭕ | ⭕ | ⭕ | ⭕ |
emotion | ⭕ | ⭕ | ⭕ | ⭕ | ⭕ | ⭕ | ⭕ |
사용 트랜드
@emotion/core vs @emotion/react vs styled-components | npm trends
Comparing trends for @emotion/core 11.0.0 which has 3,236,445 weekly downloads and unknown number of GitHub stars vs. @emotion/react 11.10.6 which has 4,693,839 weekly downloads and unknown number of GitHub stars vs. styled-components 5.3.9 which has 4,644
npmtrends.com
위 사이트에서 npm 패키지들의 트렌드를 손쉽게 파악할 수 있는데요. 실제로 emotion과 styled-components 중 어떤 패키지를 많이 사용하는지 알 수 있습니다. 두 패키지 모두 많은 개발자들이 사용하는 것을 알 수 있었지만, 제가 지금 작성하고 있는 날을 기준으로 했을 때는 엎치락 뒤치락 하는 것 같네요..
@emotion/react
> Simple styling in React.. Latest version: 11.10.6, last published: 2 months ago. Start using @emotion/react in your project by running `npm i @emotion/react`. There are 6846 other projects in the npm registry using @emotion/react.
www.npmjs.com
styled-components
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress. Latest version: 5.3.9, last published: 23 days ago. Start using styled-components in your project by running `npm i styled-components`. There are 2
www.npmjs.com
실제 npm에서 이번 주 다운로드 수로 비교해보면 emotion 다운로드 수가 50,000 정도 더 높았습니다.
The State of CSS 2021: CSS-in-JS
Learn to build scalable React applications using the latest tools and techniques available in the React ecosystem! This course is modular, where you can pick and choose various react ecosystems you want to learn.
2021.stateofcss.com
위 사이트에서 인기있는 라이브러리들을 확인할 수 있는데요. emotion의 경우 지속적으로 만족도가 떨어지고 있는 것을 확인할 수 있었습니다.
성능
emoition의 번들 사이즈는 7.9kB이고 styled-components의 번들 사이즈는 12.7kB라고 합니다. 두 라이브러리 모두 거대하지 않지만, emotion이 좀 더 사이즈가 작은 것을 알 수 있었습니다.
속도의 경우에도 emotion이 근소하게 더 빠르다고 합니다.
결론적으로, 성능상 유의미한 차이는 없다고 생각되었습니다.
emotion의 차별점
1. css props 기능
✨ 인라인 스타일
emotion jsx에서 제공해주는 css 속성을 활용하면 이를 클래스로 변환해주어서, 기존 인라인으로는 사용할 수 없었던 media query, pseudo selector, nested selector 등을 사용할 수 있다고 합니다.
import { css } from '@emotion/react'
const color = 'darkgreen'
return(
<div
css={css`
background-color: hotpink;
&:hover {
color: ${color};
}
`}
>
This has a hotpink background.
</div>
)
Emotion – The css Prop
The primary way to style elements with emotion is the css prop. It provides a concise and flexible API to style your components. There are 2 ways to get started with the css prop. Both methods result in the same compiled code. After adding the preset or se
emotion.sh
✨ css props를 결합해서 복잡한 스타일링 진행 가능
📜 theme.js 파일
const PALETTE = {
primary: { ... },
subColor: { ... },
}
const FONT_SIZE = {
small: '14px',
medium: '18px',
large: '20px',
}
const theme = {
PALETTE,
FONT_SIZE,
}
export default theme
📜 사용하는 컴포넌트.js 파일
<div css={[theme.PALETTE[primary], theme.FONT_SIZE[small]]}>
css 변수를 객체로 관리하며 위와 같이 조합하여 컴포넌트 스타일링을 진행할 수 있습니다.
아래 사이트에서 emotion으로 스타일 컴포넌트를 다양한 방법으로 사용한 방법이 소개되고 있어서 같이 첨부했습니다 :)
Storybook을 활용하여 본격적으로 디자인 시스템 구축하기
스토리북을 쓰는 방법을 어느정도 배웠으니, 이제 Hello 컴포넌트 말고 정말 디자인 시스템에 있어서 유의미한 컴포넌트들을 만들어봅시다. 그런데, 어떤 컴포넌트를 만들어야 할까요? 사실 가장
velog.io
2. SSR
emoition은 Server Side Render에서 별도의 설정 없이 동작됩니다. 반면 styled-components의 경우 ServerStyleSheet을 설정해야 한다고 합니다.
결론
- 유의미한 성능 차이는 없었습니다.
- 개발팀에서 둘 중 어느 라이브러리를 사용하고 있다 하더라도 사용법이 비슷해서 바로 적용이 가능해 보였습니다.
- emotion의 경우 css props로 css를 더 활용도 높게 조립할 수 있었습니다. 하지만 props는 styled-components도 가능하기에 styled-components로도 충분히 구현이 가능해 보였습니다.
- SSR에서는 emotion 세팅 시 더 간편합니다.
'프론트엔드 > React' 카테고리의 다른 글
[React] Jotai👻로 전역 상태 관리를 해보자 (0) | 2023.04.22 |
---|---|
[React] Hook에 대해 정확히 이해해보자📌 (0) | 2023.04.06 |
[React] Axios interceptors와 Refresh token을 이용한 토큰 관리 (0) | 2023.04.04 |
[React] React-Query 낙관적 업데이트 (0) | 2023.03.28 |
[React] 데이터를 받아오는 동안을 Suspense로 처리해보자 (0) | 2023.03.27 |
- Total
- Today
- Yesterday
- 딥러닝
- rtl
- react
- 자바스크립트 기초
- react-query
- 데이터분석
- 자바스크립트
- 타입스크립트
- frontend
- 프론트엔드 기초
- 디프만
- styled-components
- Python
- next.js
- 자바
- HTML
- 리액트 훅
- testing
- 프론트엔드 공부
- TypeScript
- JSP
- 머신러닝
- 프론트엔드
- CSS
- 스타일 컴포넌트 styled-components
- 리액트
- 프로젝트 회고
- 파이썬
- jest
- 인프런
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |