๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Front
TailwindCSS
02-classNames

1. classNames

1.1 ์„ค์น˜

1
yarn add classnames

1.2 ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•

1
import classNames from 'classnames'
2
3
classNames('one', 'two') // = โ€˜one twoโ€˜
4
classNames('one', { two: true }) // = โ€˜one twoโ€˜
5
classNames('one', { two: false }) // = โ€˜oneโ€˜
6
classNames('one', ['two', 'three']) // = โ€˜one two threeโ€˜
7
8
const myClass = 'hello'
9
classNames('one', myClass, { myCondition: true }) // = โ€˜one hello myCondition'

์ด๋Ÿฐ ์‹์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํ•ฉํ•ด CSS ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ปดํฌ๋„ŒํŠธ์—์„œ ์กฐ๊ฑด๋ถ€๋กœ ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค.

1
/**
2
* highlighted ๊ฐ’์ด true๋ฉด, highlighted ํด๋ž˜์Šค๊ฐ€ ์ ์šฉ
3
* theme์œผ๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ๋ฌธ์ž์—ด์€ ๋‚ด์šฉ ๊ทธ๋Œ€๋กœ ํด๋ž˜์Šค์— ์ ์šฉ
4
*/
5
const MyComponent = ({ highlighted, theme }) => (
6
<div className={classNames('MyComponent', { highlighted }, theme)}>Hello</div>
7
)

props ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์Šคํƒ€์ผ์„ ์ฃผ๊ธฐ๊ฐ€ ์‰ฝ๋‹ค.

๋ฐ˜๋Œ€๋กœ ์œ„ ์ฝ”๋“œ๋ฅผ classnames๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ์ ์šฉํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1
const MyComponent = ({ highlighted, theme }) => (
2
<div className={`MyComponent ${theme} ${highlighted ? 'highlighted' : ''}`}>Hello</div>
3
)