-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
lodash flatMap internals are affecting the cascade in Tailwind's output #1015
Comments
This is actually just JavaScript, numeric keys are always automatically sorted first when an object is created: In general Tailwind makes no guarantees about what should happen when you try to use two utility classes that affect the same property, you should really just only ever apply one class instead of hoping one overrides another in exactly the way that would be convenient for whatever situation you are in. Nothing to really fix here IMO, if anything we could re-order the keys in the default config file to match the order JavaScript is going to coerce the items into so it's at least less surprising. |
I think the most bulletproof solution would be to allow users to pass Map-like arrays in the config file if they want to enforce a certain cascade:
From there, we'd have to add some logic to the This feels pretty nifty, but it raises the question of what the One solution is for Another solution is for I think the best of both worlds is to allow the |
I think this would add a lot of unnecessary complexity honestly, and would rather just continue to discourage people from using two classes that target the same property at the same time. Really appreciate you thinking the problem through though 👍🏻 |
Discovered this after digging into #1013
TL;DR Tailwind probably needs to avoid
_.flatMap
to avoid unexpected results in the cascadeWhat's the problem?
Right now,
.border
comes at the bottom of the border cascade, so it overrides.border-2
etc.https://github.com/tailwindcss/tailwindcss/blob/beeaa65e4f1d046823766ca8f2410e14e12a205f/__tests__/fixtures/tailwind-output.css#L3145-L3167
This is inconsistent with the behavior of
borderRadius
andboxShadow
, two other plugins that can use thedefault
keyword to produce classes like.rounded
and.shadow
.https://github.com/tailwindcss/tailwindcss/blob/beeaa65e4f1d046823766ca8f2410e14e12a205f/__tests__/fixtures/tailwind-output.css#L5815-L5825
https://github.com/tailwindcss/tailwindcss/blob/beeaa65e4f1d046823766ca8f2410e14e12a205f/__tests__/fixtures/tailwind-output.css#L2941-L2951
Why is this happening?
Tailwind's
borderWidth
plugin uses lodash'sflatMap
function, which uses lodash'sidentity
utility internally.https://github.com/tailwindcss/tailwindcss/blob/beeaa65e4f1d046823766ca8f2410e14e12a205f/src/plugins/borderWidth.js#L18-L20
I found out that when you use lodash
identity
on an object that has any keys that can be coerced to numbers (e.g.'1'
), it will coerce those keys to numbers and sort them ascending:Therefore, when Tailwind iterates through the config object to generate CSS classes,
default
is the last key, and that translates to the.border
class showing up last as seen above.This same bug affects the
margin
andpadding
plugins, which reference thespacing
key. According to the default config'sspacing
key,.m-px
and.p-px
should show up first in the CSS:https://github.com/tailwindcss/tailwindcss/blob/beeaa65e4f1d046823766ca8f2410e14e12a205f/stubs/defaultConfig.stub.js#L129-L131
But, since
margin
andpadding
also use_.flatMap
and in turn_.identity
,.m-px
and.p-px
are at the bottom of the cascade:https://github.com/tailwindcss/tailwindcss/blob/beeaa65e4f1d046823766ca8f2410e14e12a205f/__tests__/fixtures/tailwind-output.css#L3847-L3861
Which plugins are affected?
borderWidth
borderRadius
container
inset
margin
padding
The text was updated successfully, but these errors were encountered: