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

Should arrays be supported? #1

Closed
gjtorikian opened this issue Aug 20, 2022 · 2 comments
Closed

Should arrays be supported? #1

gjtorikian opened this issue Aug 20, 2022 · 2 comments

Comments

@gjtorikian
Copy link
Owner

cc dcastil/tailwind-merge#127

@dcastil
Copy link

dcastil commented Aug 24, 2022

Awesome that you ported tailwind-merge to Ruby, love it!

Here's the reason why I added support for arrays in tailwind-merge:

Sometimes there are styles which only get applied with conditions. E.g. a button which gets some styles when it isSomeVariant and some more when it isSomeVariant && isLarge. Previously that would look like this:

twMerge('…', isSomeVariant && '…', isSomeVariant && isLarge && '…')

The isSomeVariant condition needs to be duplicated for the second string. That's ok in this example but sometimes the condition can get more complex:

twMerge(
    '…',
    someArray.length === 0 && !isNotSomeOtherVariant && '…',
    someArray.length === 0 && !isNotSomeOtherVariant && isLarge && '…'
)

It's easy to make mistakes here, especially if you need to change something about the condition in the future and forget to update the other condition. In the last example I'd probably move the condition someArray.length === 0 && !isNotSomeOtherVariant into a separate variable.

But this can be solved with an array as well which makes it possible to inline the variant condition while not needing to duplicate it.

twMerge(
    '…',
    someArray.length === 0 && !isNotSomeOtherVariant && [
        '…',
        isLarge && '…'
    ]
)

@gjtorikian
Copy link
Owner Author

Hey hey! Thank you so much for building the original NPM package. I had been trying the same thing with some hideous regular expressions.

As for me, I hit a performance issue with the way I was composing Tailwind classes as a string; I decided to just throw an Array at this gem instead. But, just now, I figured out a way to workaround my issue, so this is now less necessary for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants