Enforce the order of some variants #6018
Merged
+172
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The order of variants can be important for certain scenario's.
For example, if you have:
This would result in:
This requires the following DOM structure:
However, switching the order might be useful.
This would result in:
This requires the following DOM structure:
This is a contrived example, but there are scenarios (see our Typography plugin) where it does make sense to switch the order sometimes.
However, in some scenario's it doesn't make sense because you would generate invalid css selectors (well.. not invalid, but selectors that won't work).
Such an example is when you have
hover:before:text-center
vsbefore:hover:text-center
. This would result in:To fix this, we move all the pseudo elements to the back of the selector, otherwise the selector wouldn't even work. This is implemented on a selector level and not on a variant API level. Which means that this would also work for third party plugins.
With this PR, the examples we used earlier (
hover:before:text-center
vsbefore:hover:text-center
) would result in:The cool part here is that we didn't change the base selector found in the HTML. This is because of the new
addVariant
API introduced in #5809.We only append and prepend selector parts to the existing base class (the candidate found in the HTML), instead of re-building the selector from scratch.
This PR does mean that if you used
hover:before:text-center
in one file, andbefore:hover:text-center
in the other, that there is a bit of duplication going on. Maybe that's fine, maybe in the future we can warn about this to enforce consistency. But at least it will work now instead of introducing a bug.Fixes: #6016