Help needed with light/dark mode setup #261
Answered
by
dcastil
princemuel
asked this question in
Help
-
Hi. Please I need help configuring tailwind-merge to accept both light and dark mode classes. Currently, when I pass in this class, Here's my current setup import { cx } from 'cva';
import { twMerge } from 'tailwind-merge';
export const cn = (...args: ClassValue[]) => twMerge(cx(args)) And I'm using it with class-variance-authority like so import { cn } from '@/lib';
import type { VariantProps } from 'cva';
import { cva } from 'cva';
const textVariants = cva('', {
defaultVariants: {
intent: 'primary',
size: 's',
},
variants: {
intent: {
primary: 'text-brand-900 dark:text-white',
secondary: 'text-brand-400 dark:text-brand-400',
outline: '',
},
size: {
s: '',
xs: 'text-300 leading-200',
sm: 'text-400 leading-400',
md: 'text-500 leading-300',
lg: 'text-600 leading-400',
xl: 'text-700 leading-500',
},
weight: {
bold: 'font-bold',
medium: 'font-medium',
regular: 'font-normal',
},
},
compoundVariants: [
{
intent: 'secondary',
size: 'xs',
weight: 'bold',
class: 'tracking-100',
},
],
});
interface TextVariants extends VariantProps<typeof textVariants> {}
const text = (variants: TextVariants, className = '') =>
cn(textVariants(variants), className);
type Props<E extends React.ElementType = 'p'> = ElementProps<E> & TextVariants;
const Text = <E extends React.ElementType = 'p'>({
as,
intent,
weight,
size,
className,
children,
...rest
}: Props<E>) => {
const Rendered = as || 'p';
return (
<Rendered className={text({ intent, weight, size }, className)} {...rest}>
{children}
</Rendered>
);
};
export { Text }; |
Beta Was this translation helpful? Give feedback.
Answered by
dcastil
Jun 21, 2023
Replies: 1 comment 6 replies
-
Hey @princemuel! 👋 Could you create a reproducible example with https://codesandbox.io/s/tailwind-merge-playground-cssr35?file=/src/index.ts and post the code here? Just tried it out with this code and it seems to work fine. const mergeData: MergeData[] = [
{
args: ['text-blue-500 dark:text-white'],
},
] Output:
|
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the repro! I see the class
text-700
which tailwind-merge interprets as a color and therefore removes the preceding classtext-brand-900
. I don't know what type of class that is, but if it isn't a color, you'll need to configure tailwind-merge to know what type of class it is. Here is a guide on how to do it: https://github.com/dcastil/tailwind-merge/blob/v1.13.2/docs/recipes.md#adding-custom-scale-from-tailwind-config-to-tailwind-merge-configE.g. if it's a font-size class, you need this code: