【发布时间】:2021-06-08 21:09:05
【问题描述】:
我正在尝试使用 ReactJS 和 TailwindCSS 创建一个设计系统。
我创建了一个默认的Button 组件,其基本样式如下:
import React from "react";
import classNames from "classnames";
const Button = React.forwardRef(
({ children, className = "", onClick }, ref) => {
const buttonClasses = classNames(
className,
"w-24 py-3 bg-red-500 text-white font-bold rounded-full"
);
const commonProps = {
className: buttonClasses,
onClick,
ref
};
return React.createElement(
"button",
{ ...commonProps, type: "button" },
children
);
}
);
export default Button;
然后我在我的页面中使用Button,例如:
import Button from "../src/components/Button";
export default function IndexPage() {
return (
<div>
<Button onClick={() => console.log("TODO")}>Vanilla Button</Button>
<div className="h-2" />
<Button
className="w-6 py-2 bg-blue-500 rounded-sm"
onClick={() => console.log("TODO")}
>
Custom Button
</Button>
</div>
);
}
这是显示的内容:
有些属性像 background-color 一样被覆盖,但有些不是(其余的)。
原因是 TailwindCSS 提供的类是按照 bg-blue-500 放在 bg-red-500 之后的顺序编写的,因此会覆盖它。另一方面,自定义按钮中提供的其他类在基本按钮上的类之前编写,因此不会覆盖样式。
TailwindCSS 会发生这种行为,但只要类顺序可以产生这种情况,其他任何样式方法都可能会发生这种情况。
您是否有任何解决方法/解决方案来启用这种自定义?
如果需要,这里是完整的CodeSanbox。
【问题讨论】:
标签: javascript css reactjs tailwind-css