TailwindCSS styling not applied when passing prop value and using it in template literals (with Typescript)

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

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} />

Console Output
TailwindCSS styling not applied when passing prop value and using it in template literals (with Typescript)

Is there anything I'm missing here. Any help would appreciated. Thanks 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 &quot;next/link&quot;;
import React from &quot;react&quot;;

type Props = {
  size: number;
};

export default function PrimaryButton({ size }: Props) {

  const textSize = size ? `text-[${size.toString()}px]` : &quot;&quot;;

  // Styling
  const primaryBtnStyle = `text-white p-2 bg-gradient-to-r from-indigo-400 to-rose-400 rounded-3xl ${textSize}`;

  return (
    &lt;&gt;
      &lt;Link href=&quot;/signup&quot; className={primaryBtnStyle}&gt;
        Signup
      &lt;/Link&gt;
    &lt;/&gt;
  );
}

Using component

 &lt;PrimaryButton size={24} /&gt;

Console Output
TailwindCSS styling not applied when passing prop value and using it in template literals (with Typescript)

Is there anything I'm missing here. Any help would appreciated. Thanks TailwindCSS styling not applied when passing prop value and using it in template literals (with Typescript)

答案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.

huangapple
  • 本文由 发表于 2023年6月26日 00:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551463.html
匿名

发表评论

匿名网友

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

确定