Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text color getting overwritten when using custom font size #418

Closed
rzec-allma opened this issue May 17, 2024 · 5 comments
Closed

text color getting overwritten when using custom font size #418

rzec-allma opened this issue May 17, 2024 · 5 comments
Labels
context-v2 Related to tailwind-merge v2

Comments

@rzec-allma
Copy link

Describe the bug

I have a custom font size of xxs however when I use that with a text color, the colors get overwritten even when using a custom merge.

To Reproduce

import { extendTailwindMerge } from 'tailwind-merge';

// not sure if this is configured correctly
const merge = extendTailwindMerge({ extend: { classGroups: { 'font-size': ['xxs'] } } });

console.log(merge('text-2xl text-red-500', 'text-xxs'));
// outputs `text-2xl text-xxs` instead of `text-red-500 text-xxs`

Expected behavior

Should output text-red-500 text-xxs

Environment

  • tailwind-merge version: 2.3.0
@dcastil dcastil added the context-v2 Related to tailwind-merge v2 label May 19, 2024
@dcastil
Copy link
Owner

dcastil commented May 19, 2024

Hey @rzec-allma! 👋

You need to add the full class as a value in the config, then it should work correctly.

const twMerge = extendTailwindMerge({
  extend: {
    classGroups: {
      'font-size': ['text-xxs'],
    },
  },
})

You can check it out here: https://codesandbox.io/p/sandbox/tailwind-merge-418-rm77n7?file=%2Fsrc%2Findex.ts%3A13%2C1-19%2C4

@rzec-allma
Copy link
Author

Make sense, that does work, thanks.

@ozguruysal
Copy link

@dcastil, I have the same issue and your suggestion solves my problem but curious why wouldn't this work? I have font size classes text-label-sm, text-label-md and even more.

const twMerge = extendTailwindMerge({
  extend: {
    classGroups: {
      "font-size": [(classPart: string) => /^text-label-*/.test(classPart)],
    },
  },
})

@dcastil
Copy link
Owner

dcastil commented May 22, 2024

Hey @ozguruysal! 👋

ah yeah, that's a limitation of tailwind-merge. tailwind-merge works best when you keep as much of the class as possible static and conversely often doesn't work when you consume the entire class with a validator (the function with the regex).

You can change your code to this to make it work:

import { extendTailwindMerge, validators } from 'tailwind-merge'

const twMerge = extendTailwindMerge({
  extend: {
    classGroups: {
      "font-size": [{ "text-label": [validatos.isAny] }],
    },
  },
})

I added the issue #419 to explain this topic in more detail in the docs.

@ozguruysal
Copy link

@dcastil sorry about late respond. Thank you very much, that works. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
context-v2 Related to tailwind-merge v2
Projects
None yet
Development

No branches or pull requests

3 participants