pparkssang93 / mindmap

0 stars 0 forks source link

TailwindCSS dynamic styling #1

Open pparkssang93 opened 4 days ago

pparkssang93 commented 4 days ago

๋ฌธ์ œ ์ƒํ™ฉ ๐Ÿ‘ฟ

ํ•˜์ง€๋งŒ px๊ฐ’์€ ์ œ๋Œ€๋กœ ๋ฐ”๋€Œ๋Š”๋ฐ, ๋งˆ์šฐ์Šค ํฌ์ธํ„ฐ์— ์œ„์น˜ํ•œ ๊ณณ์— element๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜๋‹ค.

before:

<div className={`${state ? `block absolute bottom-[${state.y}px] left-[${state.x}px]` : "hidden"}`}>



TailwindCSS

The way Tailwind scans your source code for classes is intentionally very simple. ์‹ค์ œ๋กœ ์ฝ”๋“œ๋ฅผ ํŒŒ์‹ฑํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ , ์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์Šค ์ด๋ฆ„์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

We donโ€™t just limit our search to class="~" attributes because you could be using classes anywhere, like in some JavaScript for toggling a menu:

<script>
  menuButton.addEventListener('click', function () {
    let classList = document.getElementById('nav').classList
    classList.toggle('hidden')
    classList.toggle('block')
  })
</script>

์ด ๋งค์šฐ ๊ฐ„๋‹จํ•œ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ, Tailwind๋Š” JSX์™€ ๊ฐ™์€ ์–ด๋–ค ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€๋„ ๋งค์šฐ ์‹ ๋ขฐ์„ฑ ์žˆ๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

const sizes = {
  md: 'px-4 py-2 rounded-md text-base',
  lg: 'px-5 py-3 rounded-lg text-lg',
}

const colors = {
  indigo: 'bg-indigo-500 hover:bg-indigo-600 text-white',
  cyan: 'bg-cyan-600 hover:bg-cyan-700 text-white',
}

export default function Button({ color, size, children }) {
  let colorClasses = colors[color]
  let sizeClasses = sizes[size]

  return (
    <button type="button" className={`font-bold ${sizeClasses} ${colorClasses}`}>
      {children}
    </button>
  )
}



Dynamic class names

The most important implication of how Tailwind extracts class names is ์†Œ์Šค ํŒŒ์ผ์—์„œ ์™„์ „ํ•˜๊ณ  unbroken strings๋กœ ์กด์žฌํ•˜๋Š” ํด๋ž˜์Šค๋งŒ ์ฐพ๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS:

โŒ Donโ€™t construct class names dynamically

<div class="text-{{ error ? 'red' : 'green' }}-600"></div>

In the example above, the strings text-red-600 and text-green-600 do not exist, so Tailwind will not generate those classes.

Instead, make sure any class names youโ€™re using exist in full:

๐Ÿ”ต Always use complete class names

<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>

React๋‚˜ Vue์™€ ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์ด๋Š” props๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ๊ตฌ์„ฑํ•ด์„œ๋Š” ์•ˆ ๋œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

โŒ Donโ€™t use props to build class names dynamically

function Button({ color, children }) {
  return (
    <button className={`bg-${color}-600 hover:bg-${color}-500 ...`}>
      {children}
    </button>
  )
}

Instead, map props to complete class names that are statically detectable at build-time:

๐Ÿ”ต Always map props to static class names

function Button({ color, children }) {
  const colorVariants = {
    blue: 'bg-blue-600 hover:bg-blue-500',
    red: 'bg-red-600 hover:bg-red-500',
  }

  return (
    <button className={`${colorVariants[color]} ...`}>
      {children}
    </button>
  )
}



ํ•˜์ง€๋งŒ, ์›ํ•˜๋Š” ์œ„์น˜์— ์š”์†Œ๋ฅผ ์œ„์น˜์‹œํ‚ค๊ณ  ์‹ถ์œผ๋ฉด, ๊ฐ’์„ ๋™์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด์•ผํ•œ๋‹ค.

pparkssang93 commented 3 days ago

ํ•ด๊ฒฐ โญ๏ธ

style ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ผ์ธ ์Šคํƒ€์ผ์„ ์ ์šฉ.