React – TailwindCss – EventModel组件中的颜色调整不正确

huangapple go评论70阅读模式
英文:

React - TailwindCss - Color tones not working correctly in EventModel component

问题

I noticed that when specifying the color tones, only the blue color appears. What should I do to make the other colors show up?

我注意到在指定颜色色调时,只有蓝色出现。我应该怎么做才能让其他颜色显示出来?

If I change the BG-500 to BG-100, it starts to appear as gray.

如果我将 BG-500 更改为 BG-100,它开始显示为灰色。

英文:

I'm creating an event model in React and using an array for different background color tones. However, I noticed that when specifying the color tones, only the blue color appears. What should I do to make the other colors show up? Here's the code snippet:

import React, { useContext, useState } from 'react'
import GlobalContext from '../context/GlobalContext'

const labelsClasses = [
  "indigo",
  "gray",
  "blue",
  "pink",
  "purple",
  "red",
];

export default function EventModel() {
  const [title, setTitle] = useState('')
  const [description, setDescription] = useState('')
  const { setShowEventModel, daySelected } = useContext(GlobalContext)

  return (
    <div className='h-screen w-full fixed left-0 top-0 flex justify-center items-center'>
      <form className='bg-white rounded-lg shadow-2xl w-1/4'>
        <header className='bg-gray-100 px-4 py-2 flex justify-between items-center'>
          <span className='material-icons-outlined text-gray-400'>
            drag_handle
          </span>
          <button onClick={() => setShowEventModel(false)}>
            <span className='material-icons-outlined text-gray-400'>
              close
            </span>
          </button>
        </header>
        <div className="p-3">
          <div className="grid grid-cols-1/5 items-end gap-y-7">
            <div></div>
            {/* Other components */}
            <div className="flex gap-x-2">
              {labelsClasses.map((lblClass, i) => (
                <span
                  key={i}
                  className={`bg-${lblClass}-700 w-5 h-5 rounded-full flex items-center justify-center cursor-pointer`}
                >
                  <span className='material-icons-outlined text-black text-sm'>
                    check
                  </span>
                </span>
              ))}
            </div>
          </div>
        </div>
      </form>
    </div>
  )
}

If I change the BG-500 to BG-100, it starts to appear as gray.

className={`bg-${lblClass}-700 w-5 h-5 rounded-full flex items-center justify-

答案1

得分: 1

以下是您要翻译的内容:

根据文档

Tailwind提取类名的最重要影响是它只会查找在您的源文件中存在为“完整不中断字符串”的类。

如果您使用字符串插值或连接部分类名,Tailwind将无法找到它们,因此不会生成相应的CSS:

不要动态构造类名

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

在上面的示例中,字符串text-red-600text-green-600不存在,因此Tailwind不会生成这些类。
相反,请确保您使用的任何类名都存在完整:

始终使用完整的类名

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

您可以考虑在labelsClasses 中使用完整的类名,如下所示:

const labelsClasses = [
  "bg-indigo-700",
  "bg-gray-700",
  "bg-blue-700",
  "bg-pink-700",
  "bg-purple-700",
  "bg-red-700",
];



{labelsClasses.map((lblClass, i) => (
  <span
    key={i}
    className={`${lblClass} …`}
  >
英文:

As per the documentation:

> The most important implication of how Tailwind extracts class names is that it will only find classes that exist as complete unbroken strings in your source files.
>
> 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
>
> vue
&gt; &lt;div class=&quot;text-{{ error ? &#39;red&#39; : &#39;green&#39; }}-600&quot;&gt;&lt;/div&gt;
&gt;

> 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
>
> vue
&gt; &lt;div class=&quot;{{ error ? &#39;text-red-600&#39; : &#39;text-green-600&#39; }}&quot;&gt;&lt;/div&gt;
&gt;

You could consider have the full class names in labelsClasses like:

const labelsClasses = [
  &quot;bg-indigo-700&quot;,
  &quot;bg-gray-700&quot;,
  &quot;bg-blue-700&quot;,
  &quot;bg-pink-700&quot;,
  &quot;bg-purple-700&quot;,
  &quot;bg-red-700&quot;,
];



{labelsClasses.map((lblClass, i) =&gt; (
  &lt;span
    key={i}
    className={`${lblClass} …`}
  &gt;

huangapple
  • 本文由 发表于 2023年6月15日 00:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定