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

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

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

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

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

    1. export default function Button(props) {
    2. const color = props.color;
    3. const DICTIONARY = {
    4. red: 'from-red-600 to-red-900 ',
    5. green: 'from-green-600 to-green-900 ',
    6. // …
    7. };
    8. // …
    9. return (
    10. <button
    11. className={`… ${DICTIONARY[color]}`}
    12. >
  • 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):

    1. export default function Button(props) {
    2. const color = props.color;
    3. const styles = {
    4. // Convert from `color` variable
    5. };
    6. // …
    7. return (
    8. <button
    9. className={}
    10. style={style}
    11. >
  • safelist the classes, if you have a limited number of known colors:

    1. module.exports = {
    2. safelist: [
    3. { pattern: /^from-(red|green|blue)-600$/ },
    4. { pattern: /^to-(red|green|blue)-900$/ },
    5. // …
    6. ],
    7. // …
    8. ];

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:

确定