TailwindCSS动态类在Reactjs中没有编译。

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

TailwindCSS Dynamic classes aren't comiled in reactjs

问题

在自定义的 React 组件中,我通过 props 传递了颜色名称,并像这样使用它:

<div className={`bg-${color}-100 mx-auto p-5 inline-block rounded-full`}>
  <div className={`border-4 p-3 border-${color}-800 rounded-full`}>
    <Icon name={icon} className={`text-6xl text-${color}-800`} />
  </div>
</div>

颜色是在 tailwind.config.js 文件中扩展的,如下所示:

export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {
      colors: {
        'primary': {
          '50': '#f0f9ff',
          '100': '#e0f2fe',
          // 其他颜色...
        },
        // 其他颜色...
      },
    },
  },
  safelist: [
    'bg-primary-100',
    'bg-secondary-100',
    'bg-success-100',
    'bg-warning-100',
    'bg-danger-100',
    'text-danger-800',
    'border-danger-800'
  ],
  plugins: [],
};

我通过使用 safelist 暂时解决了这个问题,但我需要更多的类,无法手动将它们添加到 safelist 中。

我想通过 props 传递颜色,并希望它们由 tailwindcss 编译。

英文:

I was passing color name through props in custom react component.
And using it like this.

&lt;div className={`bg-${color}-100 mx-auto p-5 inline-block rounded-full`}&gt;
  &lt;div className={`border-4 p-3 border-${color}-800 rounded-full`}&gt;
    &lt;Icon name={icon} className={`text-6xl text-${color}-800`} /&gt;
  &lt;/div&gt;
&lt;/div&gt;

The colors are extend in tailwind.config.js file.

/** @type {import(&#39;tailwindcss&#39;).Config} */
export default {
  content: [
    &quot;./index.html&quot;,
    &quot;./src/**/*.{js,ts,jsx,tsx}&quot;,
  ],
  theme: {
    extend: {
      colors: {
        &#39;primary&#39;: {
          &#39;50&#39;: &#39;#f0f9ff&#39;,
          &#39;100&#39;: &#39;#e0f2fe&#39;,
          &#39;200&#39;: &#39;#bbe6fc&#39;,
          &#39;300&#39;: &#39;#7ed3fb&#39;,
          &#39;400&#39;: &#39;#3abcf6&#39;,
          &#39;500&#39;: &#39;#10a4e7&#39;,
          &#39;600&#39;: &#39;#0485c7&#39;,
          &#39;700&#39;: &#39;#05699f&#39;,
          &#39;800&#39;: &#39;#085984&#39;,
          &#39;900&#39;: &#39;#0d4a6d&#39;,
          &#39;950&#39;: &#39;#092f48&#39;,
        },
        // Other colors...
      },
    },
  },
  safelist: [
    &#39;bg-primary-100&#39;,
    &#39;bg-secondary-100&#39;,
    &#39;bg-success-100&#39;,
    &#39;bg-warning-100&#39;,
    &#39;bg-danger-100&#39;,
    &#39;text-danger-800&#39;,
    &#39;border-danger-800&#39;
  ],
  plugins: [],
};

I temporarily fixed the problem by using safelist. But I need more classes and I can't add them mannually in safelist.

I want to pass colors through props and want them to be compiled by tailwindcss.

答案1

得分: 0

根据文档

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>

您可以考虑使用类似以下的字典:

const COLORS = {
  primary: {
    bg: 'bg-primary-100',
    border: 'border-primary-800',
    text: 'text-primary-800',
  },
  // …
};
// …
<div className={`${COLORS[color].bg} mx-auto p-5 inline-block rounded-full`}>
  <div className={`border-4 p-3 ${COLORS[color].border} rounded-full`}>
    <Icon name={icon} className={`text-6xl ${COLORS[color].text}`} />
英文:

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 using a dictionary like:

const COLORS = {
  primary: {
    bg: &#39;bg-primary-100&#39;,
    border: &#39;border-primary-800&#39;,
    text: &#39;text-primary-800&#39;,
  },
  // …
};
// …
&lt;div className={`${COLORS[color].bg} mx-auto p-5 inline-block rounded-full`}&gt;
  &lt;div className={`border-4 p-3 ${COLORS[color].border} rounded-full`}&gt;
    &lt;Icon name={icon} className={`text-6xl ${COLORS[color].text}`} /&gt;

huangapple
  • 本文由 发表于 2023年7月23日 16:55:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747392.html
匿名

发表评论

匿名网友

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

确定