How to make it work with @apply? #122
Replies: 3 comments 5 replies
-
I'm not sure if tailwind-merge will have an elegant way to solve this issue. By using the cascading rules of CSS, you can achieve the desired behavior by changing the order the classes are merged. The code below should solve the issue. import s from './Button.module.css'
const Button = ({ color, children, className }) => {
<button className={twMerge(className, s[color])}>
{children}
</button>
} |
Beta Was this translation helpful? Give feedback.
-
tailwind-merge only merges the default Tailwind classes by default. If you define new classes that you want to add to the merging behavior, you'll need to add them to the tailwind-merge config (read here about creating a custom tailwind-merge config). Adding a bunch of custom classes which use const Button = ({ color, children, className }) => {
<button className={twMerge(COLOR_CLASSNAMES[color], className)}>
{children}
</button>
}
const COLOR_CLASSNAMES = {
blue: 'bg-blue-500',
red: 'bg-red-500',
} |
Beta Was this translation helpful? Give feedback.
-
Just to leave it here, this is a conversion from my CSS @apply to object. import React from 'react';
import { cnMerge } from 'utils/cn-merge';
import Spinner from 'components/spinner';
type ButtonProps = {
children: React.ReactNode;
fullWidth?: boolean;
loading?: boolean;
variant?: 'filled' | 'outlined';
color?: 'primary' | 'secondary';
size?: 'base' | 'lg';
} & React.ComponentProps<'button'>;
const s = {
root: /*tw:*/ `inline-flex items-center justify-center rounded-full font-semibold duration-150 disabled:pointer-events-none disabled:opacity-75`,
fullWidth: /*tw:*/ `w-full`,
variants: {
'filled.primary': /*tw:*/ `bg-[#FAA806] text-[#FFFFFF] hover:bg-[#EE9F04]`,
'filled.secondary': /*tw:*/ `bg-[#373E4B] text-[#97A3B7] hover:bg-[#343A47]`,
'outlined.primary': /*tw:*/ `border-[#FAA806] text-[#FAA806]`,
'outlined.secondary': /*tw:*/ `border-[#373E4B] text-[#373E4B]`,
},
sizes: {
base: /*tw:*/ `px-8 py-3`,
lg: /*tw:*/ `px-12 py-5`,
},
spinner: {
primary: /*tw:*/ `fill-[#bc7e03] text-white`,
secondary: /*tw:*/ `fill-[#292e38] text-white`,
},
};
const Button = React.forwardRef<any, ButtonProps>(
(
{
children,
variant = 'filled',
color = 'primary',
size = 'base',
fullWidth,
loading,
disabled,
className,
...props
},
forwardedRef
) => {
const classes = cnMerge(
s.root,
s.variants[`${variant}.${color}`],
s.sizes[size],
{ [s.fullWidth]: fullWidth },
className
);
return (
<button
ref={forwardedRef}
className={classes}
disabled={disabled || loading}
{...props}
>
{children}
{loading && (
<span className="ml-1.5">
<Spinner className={s.spinner[color]} />
</span>
)}
</button>
);
}
);
Button.displayName = 'Button';
export default Button; |
Beta Was this translation helpful? Give feedback.
-
I use CSS Modules + Tailwind and I use @apply to add my classes in CSS however tailwind-merge seems to not override those classes. Is there a way to make it work?
https://stackblitz.com/edit/github-hjypmo-re6jq8?file=pages/index.tsx
Beta Was this translation helpful? Give feedback.
All reactions