英文:
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.
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-600
和text-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
likeexport 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, iftheme
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$/ }, // … ], // … ];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论