Grouping variants together #8337
Replies: 17 comments 18 replies
-
Huge +1 for this. Grouping would make reading tailwind classes so much easier. IMO readability goes down exponentially the more classes and variants of those classes gets mixed together. Twin Marco does a great job of grouping and even sub grouping. I would love to see this incorporated into tailwindcss natively EDIT: after reading the Adam's twitter thread I see they've researched this issue extensively. Hoping one day it becomes an addition without performance issues |
Beta Was this translation helpful? Give feedback.
-
References: Variant Groups in WindiCSS |
Beta Was this translation helpful? Give feedback.
-
I wrote a transformer that parses the files before tailwind uses them to spread the grouped variants into single class names. This should remove the increased css bundle size. const expandVariant= createTransformer({ separatorChar: ' ' })
expandVariant('lg:(max-w-2xl w-full pb-2) xl:pb-32')
// lg:max-w-2xl lg:w-full lg:pb-2 xl:pb-32 // tailwind.config.js
const createTransformer = require('tailwind-group-variant')
module.exports = {
content: {
files: ['./app/**/*.{ts,tsx,jsx,js,mdx}'],
transform: createTransformer({ separatorChar: ' ' }),
},
...
} |
Beta Was this translation helpful? Give feedback.
-
The argument Adam made on Twitter is one of the worst arguments a developer could make. I fail to understand why they did not consider separating grouped classes first and then compiling them. This simple idea came to the mind of @milamer and I also thought about it. This should be very very easy, right? |
Beta Was this translation helpful? Give feedback.
-
@adamwathan that was a compelling argument. Yeah, you're right. Doing all that to accommodate class name expansion is a bad decision. But I didn't mean Tailwind to become a compiler. I meant somehow to hook into existing compilers and do some work. Some pre-compile work. However, I still think we need to find a way to expand our classes somehow. Why do I say that? It's because we're using Tailwind for literally every project we have. It's now about a year I guess that we have not written one line of CSS in any other form. That's how good your invention is. And I'm truly thankful for it. It helps a lot with clean code + rapid development. Yet that My recommendation is that we have a simple run-time that is not enabled by default and we could configure it to opt-in. For example in my
Then in my layout code:
Then in our code, we can write:
|
Beta Was this translation helpful? Give feedback.
-
I'm really hoping this gets implemented due to long chained states. E.g.
Could simply be written as:
|
Beta Was this translation helpful? Give feedback.
-
I've been thinking, would this be feasible & desirable syntax for longer more complex tailwind classes? (Inspired by styled-components) import { tw } from 'tailwind'
const Component = () => {
return (
<div className={tw`
bg-red-500 text-white p-4 rounded
supports-[not(container-type:inline-size)]:
w-1/2 mx-auto sm:w-full sm:mx-0
min-height-[10px]:
h-10 sm:h-20 md:h-30 lg:h-40 xl:h-50
hover:
focus:
bg-blue-500 text-black p-8 rounded
`}>
Hi
</div>
)
} If done with tailwind currently, it would look like: const Component = () => {
return (
<div className="
bg-red-500
text-white
p-4
rounded
supports-[not(container-type:inline-size)]:w-1/2
supports-[not(container-type:inline-size)]:mx-auto
supports-[not(container-type:inline-size)]:sm:w-full
supports-[not(container-type:inline-size)]:sm:mx-0
supports-[not(container-type:inline-size)]:min-height-[10px]:h-10
supports-[not(container-type:inline-size)]:min-height-[10px]:sm:h-20
supports-[not(container-type:inline-size)]:min-height-[10px]:md:h-30
supports-[not(container-type:inline-size)]:min-height-[10px]:lg:h-40
supports-[not(container-type:inline-size)]:min-height-[10px]:xl:h-50
supports-[not(container-type:inline-size)]:min-height-[10px]:hover:bg-blue-500
supports-[not(container-type:inline-size)]:min-height-[10px]:hover:text-black
supports-[not(container-type:inline-size)]:min-height-[10px]:hover:p-8
supports-[not(container-type:inline-size)]:min-height-[10px]:hover:rounded"
supports-[not(container-type:inline-size)]:min-height-[10px]:focus:bg-blue-500
supports-[not(container-type:inline-size)]:min-height-[10px]:focus:text-black
supports-[not(container-type:inline-size)]:min-height-[10px]:focus:p-8
supports-[not(container-type:inline-size)]:min-height-[10px]:focus:rounded"
>
Hi
</div>
)
} To address Adam's findings of group variants causing more html/css bloat, the I've read @RobinMalfait 's comment with the example let classes = tw`md:(font-bold,underline)` and am aware this probably would benefit only JS projects, but I think it would greatly benefit those who do use JS. |
Beta Was this translation helpful? Give feedback.
-
Semi related to: #10925 |
Beta Was this translation helpful? Give feedback.
-
Just curious, how have others dealt with multi-chained states and queries in Tailwind?
Aware these may be contrived examples but they're not far off from my experience using css (e.g. i want to style my buttons to handle a combination of motion preference, dark/light modes, hover/active/disabled states, variant (solid, outline) + intent (error, success, primary), width at particular container queries with a media query fallback.) At some point, grouping variants together may not be a sufficient solution to aid readability without the clarity of an indented/nested CSS structure to easily "drill-down" to what styles apply to a given state of a component. |
Beta Was this translation helpful? Give feedback.
-
This would be a great feature, even if it is something we have to opt into with a note about how not all systems may play well with it. Tailwind quickly becomes a pain to read and organize once there are many conditional class styles being applied. I have not found a great solution for this other than moving to something that isn't Tailwind, or trying to implement one of these custom workarounds to add the feature ourselves. I have seen regular enough requests for the feature over the years, it is a shame there isn't an official option to help reduce redundant code that can really bloat the class names in our files as it would be a great quality of life improvement. |
Beta Was this translation helpful? Give feedback.
-
@stephenkoo your solution is great. The point is that the Tailwind team has gone proud (no offense, just saying what I see). They do not listen to the community anymore. It happens to a lot of teams over the years. They started something extremely amazing, a revolution. But that made them think that they know everything and the rest of the community knows nothing. That's why we're here now. We use Tailwind for 100% of our projects. We do not have one line of CSS or LESS or SASS or CSS-in-JS or styled-components or any other tech. Yet we have experienced extremely dirty, unmaintainable code as you mentioned. When we want to do combine things, the current solution is simply not enough. |
Beta Was this translation helpful? Give feedback.
-
@adamwathan What is the downside of supporting the grouping syntax on Tailwind side, and leaving the template transformation responsibility to userland? Tailwind could simply let the door open for the ecosystem to accommodate to it. This would be an additive feature, not impacting the current status quo. To make this feature work, we need involvement from both the ecosystem and Tailwind-side. Tailwind could simply (even silently, or not loudly communicated) do its part. I understand that it might be confusing that Tailwind provides something which doesn't do anything on its own, but it would be more like an "enabler" feature. But seeing the demand and that how intuitive this would be anyway, I think it'd be worth. I also see the possible pitfall of having double parsers (one from Tw, one from ecosystem) which does the distribution. But given the relative simplicity of the syntax, at least on a conceptual level, I still think it could be worth. |
Beta Was this translation helpful? Give feedback.
-
Thanks to @wongjn , this can now be done relatively easy: Kudos @wongjn ! |
Beta Was this translation helpful? Give feedback.
-
It's sad that we can't be given this feature in any form. Because in reality it is not convenient to write it and debug it too. |
Beta Was this translation helpful? Give feedback.
-
Would love to see this feature to come up, would save lots of headaches upon debuging components or even editing them. Not so much text would be nicer |
Beta Was this translation helpful? Give feedback.
-
Man, its so sad. The absence of this feature makes the exact mess as the lib intends to solve. supports-[not(container-type:inline-size)]:min-height-[10px]:h-10
supports-[not(container-type:inline-size)]:min-height-[10px]:sm:h-20
supports-[not(container-type:inline-size)]:min-height-[10px]:md:h-30
supports-[not(container-type:inline-size)]:min-height-[10px]:lg:h-40
supports-[not(container-type:inline-size)]:min-height-[10px]:xl:h-50 |
Beta Was this translation helpful? Give feedback.
-
Based on the content above, I'm wondering why can't we implement it for specific build tools? Same for Tailwind here, build this feature for those Vite users (which I believe it's now the majority of web projects), and just mention this won't work for Next.js, PHP, Ruby, etc. I don't really see the issue there, the simple theory is, if you can benefit from it, then use it, otherwise go for the traditional way. Some other framework have successfully done this, at least for the JS ecosystem, so I don't see why Tailwind can't do so. |
Beta Was this translation helpful? Give feedback.
-
It would be great if we could group the classes used inside classes based on their variants. Now if we want to change two styles on hover state, we have to write
hover:bg-red-500 hover:text-white
and so on. It would be great if we could group these styles together for hover state so we could write something likehover:(bg-red-500 text-white)
. We could group all other states like that too. It would really make the developer experience a lot better. Some other frameworks like WindiCSS, UnoCSS are already doing it. It would be great to see this in Tailwind too.Beta Was this translation helpful? Give feedback.
All reactions