Replies: 1 comment 2 replies
-
Hey @tkozuch! 👋 Yes, there is a way to customize the tailwind-merge internals to an extent. When configuring tailwind-merge, there is a property It is only documented via TypeScript and JSDoc right now here:
You can get the behavior you want by removing the responsive modifiers from the parsed class name, then tailwind-merge will treat all classes as if these modifiers weren't present. The parsed class name data is only used internally and the original class name string will be preserved in the output, so it's safe to modify that data. import { extendTailwindMerge } from 'tailwind-merge'
const responsiveModifiers = ['sm', 'md', 'lg', 'xl', '2xl']
const twMerge = extendTailwindMerge({
experimentalParseClassName({ className, parseClassName }) {
const parsed = parseClassName(className)
parsed.modifiers = parsed.modifiers.filter((mod) => !responsiveModifiers.includes(mod))
return parsed
},
// rest of config
}) Keep in mind that the API can change with any minor release since it's experimental. But I don't have any plans to change the API right now. It's more likely that I'll add it as non-experimental sooner or later. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
twMerge("p-2", "md:p-4")
returns"md:p-4 p-2"
it's a result one would expect, because when Tailwind generates CSS, media query classes are after normal classes, and for that reason,
p-2
shouldn't overwritemd:p-4
is there any way (can be a hacky one) to tell twMerge that a particular class is defined later or sooner in Tailwind? or tell it that a particular class has higher specificity?
example of what I would want:
considering that "p-2" is for some reason defined after "md:p-4" in the CSS, I would want:
twMerge("p-2", "md:p-4")
to return"p-2"
("p-2" overwrites, "md:p-4")Disclaimer:
I know it's not a primary purpose of twMerge to handle such cases. I suspect it is strongly (and probably exclusively, as the name suggest) directed to work with Tailwind originally generated CSS.
But I'll also accept any "hacky" answers - for example:
In twMerge "!p-2" will overwrite "md:p-4" because of the "important" modifier. So knowing that a particular class is defined after a particular other, I could just append "!" to the one of which I know is defined later in the CSS.
The problem with this would be however if I had more classes that I needed to merge, as I can't add multiple "!" modifiers.
What I ultimately would want:
but
Notes:
**[:where(&)]**
modifier to class can be used to reduce class specificity (https://play.tailwindcss.com/8sLgiUISGB), but as this gives desired outcome in terms of output styling applied, twMerge seems to not take it into account when reducing the number of classes which in turns could result in bloated HTML file - for this reason I would prefere to handle this on thetwMerge
, not Tailwind's level, as described in examples above. But if the solution includes modification of the class before using twMerge, I can also use it, as later in the result I could just potentially "subtract" that modification.Beta Was this translation helpful? Give feedback.
All reactions