如何在使用模板字面量时在悬停时更改文本颜色?

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

How can I change text color on hover while using template literals?

问题

我有以下这段代码:

<div
  id={id}
  className={`rounded-lg bg-background-section h-72 p-4 flex flex-col items-center relative hover:bg-background-highlight hover:text-[${color}] transition ease-in-out`}
>
  你好
</div>

我正在渲染多个此div的实例,并为每个实例获取不同的color值。问题是,悬停效果在我手动在代码中设置所需的颜色之前不起作用:

示例:

hover:text-[#7cc327]

保存后,转到浏览器测试它(它正常工作)。

然后回到原始代码:

hover:text-[${color}]

现在它会使用变量中的颜色。我必须对我有的9种颜色都做同样的事情,但这只是浪费时间,因为它不应该这样工作,而且... 每当重新启动开发服务器时,它就停止工作。

知道出了什么问题吗?

英文:

I have the following piece of code:

&lt;div
  id={id}
  className={`rounded-lg bg-background-section h-72 p-4 flex flex-col items-center relative hover:bg-background-highlight hover:text-[${color}] transition ease-in-out`}
&gt;
  Hello there
&lt;/div&gt;

I'm rendering multiple instances of that div, getting a different color value for each one. Thing is, the hover doesn't work until I manually set the color that I want in the code:

Example:

hover:text-[#7cc327]

Save, go to the browser to test it out (it works just fine).

Go back and restore the code to its original form:

hover:text-[${color}]

And now it takes the color from the variable. I ve to do the same thing for all the 9 colors that I have, but its just a waste of time cause it shouldn't work that way and also... it stops working whenever I restart the dev server.

Any idea of whats going on?

答案1

得分: 3

根据文档

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>

你可以考虑在声明color的地方使用完整的类名(确保这段代码也包含在content文件中):

const color = `hover:text-[#7cc327]`;

<div className={`… ${color} …`}>

否则,如果颜色值真正是动态的或上述方法无法适用于你的用例,你可以使用style属性,像这样:

<div
  className={`… hover:text-[color:--color] …`}
  style={{ '--color': color }}
>
英文:

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 full class names whereever color is declared (ensuring that this code is covered in the content file globs too):

const color = `hover:text-[#7cc327]`;

&lt;div className={`… ${color} …`}&gt;

Otherwise, if the color value is going to be truly dynamic or the above method cannot work for your use case, you could use the style attribute like:

&lt;div
  className={`… hover:text-[color:--color] …`}
  style={{ &#39;--color&#39;: color }}
&gt;

huangapple
  • 本文由 发表于 2023年7月7日 01:47:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631371.html
匿名

发表评论

匿名网友

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

确定