英文:
Tailwind CSS not loading custom classes on React app
问题
index.css文件定义了React应用中的多个主题和按钮样式。tailwind.config.js文件用于配置Tailwind CSS。Button组件接受一个名为variant的属性,但有问题,自定义的.btn-类有时无法正确应用样式。当样式未渲染时,它们甚至不会显示在Google开发者工具的样式选项卡中。检查元素时,类存在,但样式不在样式选项卡中显示。
这可能是由于动态类名生成的问题,可以尝试以下几步来解决:
- 确保variant属性传递给Button组件的时候是有效的,例如'primary'、'secondary'等。
- 确保Button组件在tailwind.config.js中的content配置范围内。
- 确保使用的Tailwind CSS版本与项目兼容。
如果问题仍然存在,您可能需要进一步调查或提供更多代码和详细信息以获得更具体的帮助。
英文:
I have a React app that uses Tailwind CSS and multiple themes.
index.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
[data-theme='default'] {
--color-primary: #008cba;
--color-secondary: #e7e7e7;
--color-success: #4caf50;
--color-danger: #f44336;
}
[data-theme='secondary'] {
--color-primary: #af8ffa;
--color-secondary: #e7e7e7;
--color-success: #4caf50;
--color-danger: #f44336;
}
}
@layer components {
.btn-primary {
background-color: theme(colors.primary);
color: theme(colors.danger);
}
.btn-secondary {
background-color: theme(colors.secondary);
color: black;
}
.btn-success {
background-color: theme(colors.success);
}
.btn-danger {
background-color: theme(colors.danger);
}
}
tailwind.config.js:
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {
colors: {
primary: 'var(--color-primary)',
secondary: 'var(--color-secondary)',
success: 'var(--color-success)',
danger: 'var(--color-danger)',
},
},
},
plugins: [],
};
I defined a simple button component that takes a variant ('primary' | 'secondary' | 'success' | 'danger') as a prop:
import { ButtonProps } from './Button.types';
function Button({ variant, children, ...rest }: ButtonProps) {
return (
<button
className={`border-none py-2 px-4 text-center inline-block text-md border-r-4 cursor-pointer btn-${variant}`}
{...rest}>
{children}
</button>
);
}
export default Button;
The problem is that the custom .btn- classes, like .btn-primary, don't get applied consistently. Most of the time they won't render, however if while the app is loaded I change the btn class and I hardcode something btn-primary then the styles load fine. After that I can change it back to btn-${variant} and the styles load fine. At this point I can also change the styles in index.css and the new styles also get applied.
As a side note, when they don't get rendered they won't even show in the styles tab in google developer tools. If I inspect the element the class is there, but the styles don't show in the styles tab.
I don't really know what I'm missing here.
答案1
得分: 0
根据文档:
Tailwind提取类名的最重要影响是它只会找到作为完整不中断字符串存在于源文件中的类。
如果您使用字符串插值或连接部分类名,Tailwind将无法找到它们,因此不会生成相应的CSS:
不要动态构造类名
<div class="text-{{ error ? 'red' : 'green' }}-600"></div>
在上面的示例中,字符串
text-red-600
和text-green-600
不存在,因此Tailwind不会生成这些类。相反,确保您使用的任何类名都存在完整:
始终使用完整的类名
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
英文:
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
> html
> <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
>
> html
> <div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论