How to add conflicting classes bypass? #256
-
I've the following tailwind.config.ts setup /** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
colors: {
white: {
50: "#FFFFFF",
},
secondary: {
green: {
500: "#10D298",
},
}
},
fontFamily: {
body: ["Open Sans"],
},
fontSize: {
"button-1": [
"0.9375rem", //15px
{
lineHeight: "1.4375rem", //23px
letterSpacing: "0.5px",
fontWeight: 600,
},
],
"button-2": [
"0.8125rem", //13px
{
lineHeight: "1.25rem", //20px
letterSpacing: "0.5px",
fontWeight: 600,
},
],
},
},
plugins: [],
}; Helper class configuring import { clsx, type ClassValue } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";
const cn = (...inputs: ClassValue[]) => {
const modfiedInput = clsx(inputs);
const customTwMerge = extendTailwindMerge({});
const mergedClasses = customTwMerge(modfiedInput);
return mergedClasses;
};
export { cn }; I've the following button class import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "../../utils";
/** @see https://vincentdusautoir.com/posts/button-variants-tailwindcss */
const solidVariants = cva("", {
variants: {
variant: {
primary:
"bg-secondary-green-500 text-white-50", <-- CONFLICT
},
},
});
const outlineVariants = cva("", {
variants: {
variant: {
primary: "border border-secondary-green-500 text-secondary-green-500", <-- CONFLICT
secondary: "border border-gray-300 text-gray-800",
},
},
});
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-lg disabled:cursor-not-allowed disabled:opacity-75",
{
variants: {
variant: {
"primary-solid": solidVariants({ variant: "primary" }),
"primary-outline": outlineVariants({ variant: "primary" }),
},
size: {
none: "",
sm: "px-3 py-2 text-button-2", <-- CONFLICT
base: "px-2 py-3 text-button-1", <-- CONFLICT
},
},
defaultVariants: {
variant: "primary-solid",
size: "base",
},
}
);
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean;
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
(
{ className, variant, size, asChild = false, type = "button", ...props },
ref
) => {
const Comp = asChild ? Slot : "button";
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
type={type}
{...props}
/>
);
}
);
Button.displayName = "Button";
export { Button, buttonVariants }; On my button,
I've looked at https://github.com/dcastil/tailwind-merge/blob/v1.10.0/docs/configuration.md#extending-the-tailwind-merge-config, but i'm still not sure how to configure tailwind-merge to detect the conflict and use it together. Side note, I think it'll be an improvement if the examples are using actual classes from tailwindcss instead of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @tanshunyuan! 👋 There are several things in here: Where to place call to
|
Beta Was this translation helpful? Give feedback.
Hey @tanshunyuan! 👋
There are several things in here:
Where to place call to
extendTailwindMerge
Regarding your
cn
helper function, I'd strongly advice to put theextendTailwindMerge
call outside of thecn
function. In that call a lot of setup is happening and if you call the function on everycn
call, you're doing that setup over and over again which might degrade the performance of your app unnecessarily. This code should fix this: