英文:
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("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>;
}
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 }) => {
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"
);
}
},
};
答案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("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>
);
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js" integrity="sha512-8Q6Y9XnTbOE+JNvjBQwJ2H8S+UV4uA6hiRykhdtIyDYZ2TprdNmWOUaKdGzOhyr4dCyk287OejbPvwl7lrfqrQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js" integrity="sha512-MOCpqoRoisCTwJ8vQQiciZv0qcpROCidek3GTFS6KTk2+y7munJIlKCVkFCYY+p3ErYFXCjmFjnfTTRSC1OHWQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/classnames/2.3.2/index.min.js" integrity="sha512-GqhSAi+WYQlHmNWiE4TQsVa7HVKctQMdgUMA+1RogjxOPdv9Kj59/no5BEvJgpvuMTYw2JRQu/szumfVXdowag==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.tailwindcss.com"></script>
<div id="app"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论