英文:
TailwindCSS styling not applied when passing prop value and using it in template literals (with Typescript)
问题
I have a PrimaryButton
component in NextJS, where I'm using TailwindCSS for styling. The component accepts a size prop, and I'm encountering an issue where the styling doesn't apply correctly when I pass the size value from the prop and use it within template literals. However, when I check the console, it shows that the relevant CSS attribute is applied to the element, but visually it doesn't reflect the expected styling.
PrimaryButton.tsx
import Link from "next/link";
import React from "react";
type Props = {
size: number;
};
export default function PrimaryButton({ size }: Props) {
const textSize = size ? `text-[${size.toString()}px]` : "";
// Styling
const primaryBtnStyle = `text-white p-2 bg-gradient-to-r from-indigo-400 to-rose-400 rounded-3xl ${textSize}`;
return (
<>
<Link href="/signup" className={primaryBtnStyle}>
Signup
</Link>
</>
);
}
Using component
<PrimaryButton size={24} />
Is there anything I'm missing here. Any help would appreciated. Thanks
英文:
I have a PrimaryButton
component in NextJS, where I'm using TailwindCSS for styling. The component accepts a size prop, and I'm encountering an issue where the styling doesn't apply correctly when I pass the size value from the prop and use it within template literals. However, when I check the console, it shows that the relevant CSS attribute is applied to the element, but visually it doesn't reflect the expected styling.
PrimaryButton.tsx
import Link from "next/link";
import React from "react";
type Props = {
size: number;
};
export default function PrimaryButton({ size }: Props) {
const textSize = size ? `text-[${size.toString()}px]` : "";
// Styling
const primaryBtnStyle = `text-white p-2 bg-gradient-to-r from-indigo-400 to-rose-400 rounded-3xl ${textSize}`;
return (
<>
<Link href="/signup" className={primaryBtnStyle}>
Signup
</Link>
</>
);
}
Using component
<PrimaryButton size={24} />
Is there anything I'm missing here. Any help would appreciated. Thanks
答案1
得分: 1
Tailwind会搜索你的源代码以查找完整的类名,以便知道需要生成哪些CSS。这意味着你不能动态地将字符串连接在一起以形成单个类名,尽管你可以将多个完整的类名连接在一起。
根据文档的描述:
> Tailwind提取类名的最重要含义是它只会在你的源文件中找到作为完整不中断字符串存在的类。 <br> 如果你使用字符串插值或连接部分类名,Tailwind将无法找到它们,因此也不会生成相应的CSS。
考虑使用一组固定数量的类,可以在静态对象中表示,或者使用内联样式。
英文:
Tailwind searches your source code for complete class names so it knows what CSS it needs to generate. This means that you cannot dynamically concatenate strings together to form a single class name, although you can concatenate multiple full class names together.
According to 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. <br> If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS
Consider using a fixed number of classes that can be represented in a static object or using inline styles.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论