React Icons 影响 Tailwind 样式。

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

React Icons affecting Tailwind styling

问题

以下是翻译的代码部分:

// 这是我现在的样式:

function Button({
  children,
  primary,
  secondary,
  success,
  warning,
  danger,
  outline,
  rounded,
}) {
  const classes = className("flex items-center px-3 py-1.5 border", {
    "border-blue-500 bg-blue-500 text-white": primary,
    "border-gray-900 bg-gray-900 text-white": secondary,
    "border-green-500 bg-green-500 text-white": success,
    "border-yellow-400 bg-yellow-400 text-white": warning,
    "border-red-500 bg-red-500 text-white": danger,
    "rounded-full": rounded,
    "bg-white": outline,
    "text-blue-500": outline && primary,
    "text-gray-500": outline && secondary,
    "text-green-500": outline && success,
    "text-yellow-400": outline && warning,
    "text-red-500": outline && danger,
  });

  return <button className={classes}>{children}</button>;
}

在Tailwind中最后列出的样式会覆盖文本颜色这样做的目的是如果按钮是outline”,我们不希望文本颜色为白色因为outline按钮的背景是白色的所以如果按钮被指定为outline”,我们希望文本的颜色发生变化这就是最后四行类的目的但自从我安装了react-icons之后它不再起作用

所谓不再起作用是指text-white仍然存在于应用了outline的地方

现在我想知道是否Button.propTypes解决方案是导致它不起作用的原因

Button.propTypes = {
  checkVariationValue: ({ primary, secondary, success, warning, danger }) => {
    const count =
      Number(!!primary) +
      Number(!!secondary) +
      Number(!!warning) +
      Number(!!success) +
      Number(!!danger);

    if (count > 1) {
      return new Error(
        "Only one of primary, success, warning, danger can be true"
      );
    }
  },
};
英文:

So I have the following styling:

function Button({
children,
primary,
secondary,
success,
warning,
danger,
outline,
rounded,
}) {
const classes = className(&quot;flex items-center px-3 py-1.5 border&quot;, {
&quot;border-blue-500 bg-blue-500 text-white&quot;: primary,
&quot;border-gray-900 bg-gray-900 text-white&quot;: secondary,
&quot;border-green-500 bg-green-500 text-white&quot;: success,
&quot;border-yellow-400 bg-yellow-400 text-white&quot;: warning,
&quot;border-red-500 bg-red-500 text-white&quot;: danger,
&quot;rounded-full&quot;: rounded,
&quot;bg-white&quot;: outline,
&quot;text-blue-500&quot;: outline &amp;&amp; primary,
&quot;text-gray-500&quot;: outline &amp;&amp; secondary,
&quot;text-green-500&quot;: outline &amp;&amp; success,
&quot;text-yellow-400&quot;: outline &amp;&amp; warning,
&quot;text-red-500&quot;: outline &amp;&amp; danger,
});
return &lt;button className={classes}&gt;{children}&lt;/button&gt;;
}

In Tailwind the last styles listed override the text color and the purpose of that is so if a button is outline then we do not want a text color of white because the background is white in an outline button, so we want the color of the text to change if the button is designated as outline. That is the purpose of the last four lines of classes, but it no longer works ever since I installed react-icons.

By no longer working I mean the "text-white" remains where outline is applied.

Now, I am wondering if the Button.propTypes solution is what clobbering it:

Button.propTypes = {
checkVariationValue: ({ primary, secondary, success, warning, danger }) =&gt; {
const count =
Number(!!primary) +
Number(!!secondary) +
Number(!!warning) +
Number(!!success) +
Number(!!danger);
if (count &gt; 1) {
return new Error(
&quot;Only one of primary, success, warning, danger can be true&quot;
);
}
},
};

答案1

得分: 1

outline 为真时,你会面临这样的情况,即会有冲突的类,例如 text-white text-blue-500(对于真值 primary)。考虑只使用一个具有相同变体的类,应用相同的属性:

function Button({
  children,
  primary,
  secondary,
  success,
  warning,
  danger,
  outline,
  rounded,
}) {
  const classes = classNames("flex items-center px-3 py-1.5 border", {
    "border-blue-500 bg-blue-500": primary,
    "border-gray-900 bg-gray-900": secondary,
    "border-green-500 bg-green-500": success,
    "border-yellow-400 bg-yellow-400": warning,
    "border-red-500 bg-red-500": danger,
    "rounded-full": rounded,
    "text-white": !outline && (primary || secondary || success || warning || danger),
    "bg-white": outline,
    "text-blue-500": outline && primary,
    "text-gray-500": outline && secondary,
    "text-green-500": outline && success,
    "text-yellow-400": outline && warning,
    "text-red-500": outline && danger,
  });

  return <button className={classes}>{children}</button>;
}

ReactDOM.createRoot(document.getElementById('app')).render(
  <React.Fragment>
    <Button primary>Foo</Button>
    <Button primary outline>Foo</Button>
    <Button secondary>Foo</Button>
    <Button secondary outline>Foo</Button>
    <Button success>Foo</Button>
    <Button success outline>Foo</Button>
    <Button warning>Foo</Button>
    <Button warning outline>Foo</Button>
    <Button danger>Foo</Button>
    <Button danger outline>Foo</Button>
  </React.Fragment>
);

希望这能帮助你!

英文:

When outline is truthy, you will end up in a situation where you will have conflicting classes, like text-white text-blue-500 (for truthy primary). Consider only having a single class with the same variant that applies the same property:

<!-- begin snippet: js hide: false console: false babel: true -->

<!-- language: lang-js -->

function Button({
children,
primary,
secondary,
success,
warning,
danger,
outline,
rounded,
}) {
const classes = classNames(&quot;flex items-center px-3 py-1.5 border&quot;, {
&quot;border-blue-500 bg-blue-500&quot;: primary,
&quot;border-gray-900 bg-gray-900&quot;: secondary,
&quot;border-green-500 bg-green-500&quot;: success,
&quot;border-yellow-400 bg-yellow-400&quot;: warning,
&quot;border-red-500 bg-red-500&quot;: danger,
&quot;rounded-full&quot;: rounded,
&quot;text-white&quot;: !outline &amp;&amp; (primary || secondary || success || warning || danger),
&quot;bg-white&quot;: outline,
&quot;text-blue-500&quot;: outline &amp;&amp; primary,
&quot;text-gray-500&quot;: outline &amp;&amp; secondary,
&quot;text-green-500&quot;: outline &amp;&amp; success,
&quot;text-yellow-400&quot;: outline &amp;&amp; warning,
&quot;text-red-500&quot;: outline &amp;&amp; danger,
});
return &lt;button className={classes}&gt;{children}&lt;/button&gt;;
}
ReactDOM.createRoot(document.getElementById(&#39;app&#39;)).render(
&lt;React.Fragment&gt;
&lt;Button primary&gt;Foo&lt;/Button&gt;
&lt;Button primary outline&gt;Foo&lt;/Button&gt;
&lt;Button secondary&gt;Foo&lt;/Button&gt;
&lt;Button secondary outline&gt;Foo&lt;/Button&gt;
&lt;Button success&gt;Foo&lt;/Button&gt;
&lt;Button success outline&gt;Foo&lt;/Button&gt;
&lt;Button warning&gt;Foo&lt;/Button&gt;
&lt;Button warning outline&gt;Foo&lt;/Button&gt;
&lt;Button danger&gt;Foo&lt;/Button&gt;
&lt;Button danger outline&gt;Foo&lt;/Button&gt;
&lt;/React.Fragment&gt;
);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js&quot; integrity=&quot;sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js&quot; integrity=&quot;sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/classnames/2.3.2/index.min.js&quot; integrity=&quot;sha512-GqhSAi+WYQlHmNWiE4TQsVa7HVKctQMdgUMA+1RogjxOPdv9Kj59/no5BEvJgpvuMTYw2JRQu/szumfVXdowag==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 22:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581793.html
匿名

发表评论

匿名网友

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

确定