3. CSS in JS
CSS in JS ๋
CSS
๊ฐ์ ์ ๋ฆฌ
CSS-in-JS
CSS-in-JS๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํฌ๋ํธ๋ฅผ ์คํ์ผ๋งํ๋ ๊ธฐ์ ์ด๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ํ์ฑ๋ ๋, CSS๊ฐ style ํ๊ทธ์ ์์ฑ์ด ๋๊ณ , DOM์ ์ ์ฉ์ด ๋๋ค. ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ธ์ ์ด๊ณ ์ ์ง๊ฐ๋ฅํ ๋ฐฉ์์ผ๋ก ์คํ์ผ๋ง์ ํ ์ ์๊ณ , ์ปดํฌ๋ํธ ๋ ๋ฒจ์์์ CSS ์ถ์ํ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํ๋ค. ์ด์ ๊ฐ์ ์ปจ์ ์ผ๋ก ๊ตฌํ๋์ด ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ๋ง์ด ์กด์ฌํ๋ค.
Emotion
Styled Components
JSS
Tailwind CSS
์์ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ tagged template literals ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ์คํ์ผ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์๊ฒ ํ๋ค.
import styled from 'styled-components';
// Create a component that renders a <p> element with blue text
const BlueText = styled.p`
color: blue;
`;
<BlueText>My blue text</BlueText>
๊ธฐ์กด์ ์ ํต์ ์ธ CSS ๋ฐฉ์๊ณผ๋ ๋ค๋ฅด๊ฒ CSS-in-JS๋ก๋ ์คํ์ผ์ ๋์ ์ผ๋ก ๋ง๋ค ์ ์๋ค. ๋ํ CSS๋ฅผ ์บก์ํํ์ฌ ๋ชจ๋ํ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
์ ํต์ ์ธ CSS ๋ฐฉ์์ ๋ฌธ์ ์
Global Namespace
Dependencies
Dead Code Elimination
Minification
Sharing Constants
Non-deterministic Resolution
Isolation
A Unified Styling Language (2017)
Scoped styles
Critical CSS
Smarter optimisations
Package management
Non-browser styling
All You Need To Know About CSS-in-JS (2017)
Thinking in components
CSS-in-JS leverages the full power of the JavaScript ecosystem to enhance CSS.
True rules isolation
Scoped selectors
Vendor Prefixing
Code sharing
Only the styles which are currently in use on your screen are also in the DOM (react-jss).
Dead code elimination
Unit tests for CSS
์ฑ๋ฅ ์ด์
HTML, CSS ํ์ผ์ ๋ณ๋ ฌ์ ์ผ๋ก ๋ก๋ฉ์ด ๊ฐ๋ฅํ์ง๋ง, JS ํ์ผ์ ๊ฒฝ์ฐ์๋ ๋ชจ๋ ๋ค์ด๋ก๋ ๋์ง ์์ผ๋ฉด ์คํ์ ํ ์ ์๋ค. CSS-in-JS ๊ฐ์ ๊ฒฝ์ฐ์๋ ์๋ฐ์คํฌ๋ฆฝํธ์ CSS๋ฅผ ์์ฑํ๊ธฐ ๋๋ฌธ์ ํนํ ๋ฆฌ์กํธ์ ๊ฒฝ์ฐ์๋ SSR, Code Splitting์ด ์ ์ฉ๋์ง ์๋๋ค๋ฉด ๋ฒ๋ค์ ํฌ๊ธฐ๊ฐ ๊ฑฐ๋ํด์ง๋ค.
๋์:
CSS๋ฅผ ํ๋ฒํ ํ ์คํธ๋ก ์์ฑ.
React์ ์ข ์์ ์ด์ง ์์ง๋ง, React Styled Component๋ ์ง์ํจ.
CSS๋ฅผ ์ค๋ธ์ ํธ ํํ๋ก ํํ. React์ Inline Style๊ณผ ์ ์ฌํจ.
React์ ๋ฌด๊ดํ๊ฒ ์ฌ์ฉ ๊ฐ๋ฅ.
Last updated