CSS-in-JSλ μ μ©ν λꡬμ΄μ§λ§, κ³Όλνκ² μ¬μ©νλ©΄ λ°νμ μ€λ²ν€λ, λ²λ€ ν¬κΈ° μ¦κ°, CSS μ΅μ ν λν λ± λ€μν λ¬Έμ λ₯Ό μΌκΈ°ν©λλ€. CSS Modules, Atomic CSS, μ‘°κ±΄λΆ CSS-in-JS μ¬μ© λ±μ λμμ κ³ λ €νμ¬ νλ‘μ νΈ μꡬμ¬νμ λ§λ μ΅μ μ μ€νμΌλ§ μ λ΅μ μ νν΄μΌ ν©λλ€.
CSS-in-JSλ μ»΄ν¬λνΈ μ€νμΌλ§μ κ°λ ₯ν λꡬμ΄μ§λ§, λͺ¨λ μν©μ μ ν©ν ν΄κ²°μ± μ μλλλ€. κ³Όλνκ² μ¬μ©νλ©΄ λ€μκ³Ό κ°μ λ¬Έμ μ μ΄ λ°μν©λλ€.
CSS-in-JSλ₯Ό μ¬μ©ν λλ λ€μκ³Ό κ°μ μ¬νμ κ³ λ €νμ¬ μ μ€νκ² μ μ©ν΄μΌ ν©λλ€.
// κ³Όλν CSS-in-JS μ¬μ© μμ (styled-components)
import styled from 'styled-components';
const Button = styled.button`
background-color: ${props => props.primary ? 'palevioletred' : 'white'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
function MyComponent() {
return (
<div>
<Button>Normal Button</Button>
<Button primary>Primary Button</Button>
</div>
);
}// CSS Modules μ¬μ© μμ
import styles from './MyComponent.module.css';
function MyComponent() {
return (
<div>
<button className={styles.button}>Normal Button</button>
<button className={`${styles.button} ${styles.primary}`}>Primary Button</button>
</div>
);
}
// MyComponent.module.css
.button {
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
}
.primary {
background-color: palevioletred;
color: white;
}