只有红色在使用Tailwind样式的React组件中起作用。

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

Only colour red working in react component styled with tailwind

问题

我正在尝试使用Tailwind和React创建自定义按钮元素。我不知道为什么当我应用除红色以外的任何颜色时,它就不起作用。

英文:

I'm trying to make custom button element using tailwind and react. I don't know why when i'm apllying any color different then red it just doesn't work.

只有红色在使用Tailwind样式的React组件中起作用。

只有红色在使用Tailwind样式的React组件中起作用。

只有红色在使用Tailwind样式的React组件中起作用。

I tried with many different colors and I really can't find why it's working only with red.

答案1

得分: 0

根据文档的说明:

> Tailwind提取类名的最重要影响是它只会在源文件中存在作为_完整不中断字符串_的类名。
>
> 如果您使用字符串插值或连接部分类名在一起,Tailwind将无法找到它们,因此不会生成相应的CSS:
>
> 不要动态构造类名
>
> vue > <div class="text-{{ error ? 'red' : 'green' }}-600"></div> >
> 在上面的示例中,字符串text-red-600text-green-600不存在,因此Tailwind不会生成这些类。
> 相反,请确保您使用的任何类名都完整存在:
>
> 始终使用完整的类名
>
> vue > <div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div> >

您可以:

  • 将整个类包含在传递给 props.color 的变量中,如下所示:
    export default function Button(props) {
      const color = props.color;
      return (
        <button
          className={`… ${color} …`}
         >
    
    <Button color="from-green-600 to-green-900 ">
    
  • color 到 Tailwind 类名创建一个字典:
    export default function Button(props) {
      const color = props.color;
      const DICTIONARY = {
        red: 'from-red-600 to-red-900 ',
        green: 'from-green-600 to-green-900 ',
        // …
      };
      // …
      return (
        <button
          className={`… ${DICTIONARY[color]}`}
         >
    
  • 如果 theme 可以转换为有效的CSS颜色值,可以使用 style 属性进行真正的动态颜色,或者您可以从Tailwind获取颜色
    export default function Button(props) {
      const color = props.color;
      const styles = {
        // 从 `color` 变量转换
      };
      // …
      return (
        <button
          className={}
          style={style}
         >
    
  • 如果您知道的颜色有限,可以使用safelist
    module.exports = {
      safelist: [
        { pattern: /^from-(red|green|blue)-600$/ },
        { pattern: /^to-(red|green|blue)-900$/ },
        // …
      ],
      // …
    ];
    
英文:

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
> <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
>
> vue
> <div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
>

You could:

  • Have the entire class in the variable you pass to props.color like

    export default function Button(props) {
      const color = props.color;
      return (
        <button
          className={`… ${color} …`}
         >
    
    <Button color="from-green-600 to-green-900 ">
    
  • Have a dictionary for color to Tailwind class names:

    export default function Button(props) {
      const color = props.color;
      const DICTIONARY = {
        red: 'from-red-600 to-red-900 ',
        green: 'from-green-600 to-green-900 ',
        // …
      };
      // …
      return (
        <button
          className={`… ${DICTIONARY[color]}`}
         >
    
  • Use the style attribute for truly dynamic colors, if theme can be converted to a valid CSS color value (or you could get the color from Tailwind):

    export default function Button(props) {
      const color = props.color;
      const styles = {
        // Convert from `color` variable
      };
      // …
      return (
        <button
          className={}
          style={style}
         >
    
  • safelist the classes, if you have a limited number of known colors:

    module.exports = {
      safelist: [
        { pattern: /^from-(red|green|blue)-600$/ },
        { pattern: /^to-(red|green|blue)-900$/ },
        // …
      ],
      // …
    ];
    

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

发表评论

匿名网友

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

确定