-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[v4] addComponents
is adding styles to @layer utilities
instead of @layer components
#15045
Comments
Hey! In v4 this is done on purpose. The biggest benefit of the split in v3 is that you can always override a component with more specific utilities. However, in v4 we improved sorting of all the utilities, which means that this rule should still apply but it reduces the amount of concepts in the system. Can you share some of the issues you are seeing because of this? |
Thanks for the quick answer. Sure. For example here I have 2 plugins:
But I have no control over the order:
For example why one plugin came before |
Scenario Workaround
But these solutions increases the CSS size and complexity. So I hope there's a way I can control the styles for component (.card) always come before the modifier styles (.bigcard) and Tailwind utility classes. /* component */
.card {
background: yellow;
}
/* modifiers */
.bigcard {
background: green;
padding: 2rem;
}
/* utilities */
.p-10 {
padding: calc(var(--spacing) * 10);
} So the style for this HTML would be predictable <div class="card">default</div>
<div class="card bigcard">bigcard style overrides the default</div>
<div class="card bg-red-500">bg-red-500 overrides the default</div> |
Yep I see, the order inside of your CSS is indeed very important. In v4 how it works is that we sort all the utilities (and components) based on the used properties in the CSS and the amount of properties you used. The idea is that a component with multiple properties (which is typically the case) will be sorted before the ones with fewer properties. In your case The order difference between It also looks like the Can you make a reproduction with a real example just to verify that you are still running into these issues? |
I see! Thanks for the info. 👍 I think you can close this issue if there's no plan to use
Can you confirm If I understood the logic correctly: |
@saadeghi I ran into similar problems while migrating to v4 and decided to use this pattern: @layer utilities.components.base, utilities.components.modifier;
@utility btn {
@layer components.base {
/* base css for btn */
}
}
@utility btn--outlined {
@layer components.modifier {
/* specific css for outlined */
}
} So for example, for this markup... <button class="btn btn--outlined bg-blue-500">...</button> ...specificity is correctly observed: Verbose? Yes, but works well for my use cases. Similarly in JS syntax: api.addUtilities({
'.btn': {
'@layer components.base': {
// ...
},
},
}) Hope that's helpful. |
Related to this, I would add a new function that adds styles outside the TW layer structure: Something like:
|
Similar issues, when using the @layer utilities {
/* Forms */
.form-input,
.form-textarea,
.form-select,
.form-multiselect,
.form-checkbox,
.form-radio{
@apply block w-full rounded border-0 py-1.5 cursor-pointer
text-gray-900 shadow-xs ring ring-inset ring-gray-200
placeholder:text-gray-400 focus:ring-2 focus:ring-inset
focus:ring-indigo-600 sm:text-sm sm:leading-6;
} The issue here is now if I want to do a It feels like the form plugin would want to be added in the components layer, since they apply a lot of styles that you want to control and be able to override on top of inline. Here is a working example of the issue: https://play.tailwindcss.com/kuXFgLDpPH |
What version of Tailwind CSS are you using?
v4.0.0-alpha.34
What build tool (or framework if it abstracts the build tool) are you using?
@tailwindcss/cli
Reproduction URL
https://github.com/saadeghi/tw4-component-layer-issue
Describe your issue
These are the layers in output CSS file:
Expectation
It's expected for
addComponents
to add styles to@layer components
Current behavior
Currently
addComponents
adds styles to@layer utilities
, similar toaddUtilities
Plugin example:
https://github.com/saadeghi/tw4-component-layer-issue/blob/master/myplugin.js
Generated style:
https://github.com/saadeghi/tw4-component-layer-issue/blob/9b7a944690a35d55c7406756e30cc98c7a239623/output.css#L516
The text was updated successfully, but these errors were encountered: