Bug-proof the group-
modifiers by allowing safe nesting of .group
elements
#7547
Replies: 5 comments 6 replies
-
Hey! Interesting idea for sure — I’ve been planning to solve this for a long time using a different approach, which is named groups. Originally just having My gut is that this is gonna be more flexible but does have the disadvantage of still having potential naming collisions if you don’t know about groups in the parent tree. Feels low risk to me though — even in the 500+ templates we’ve built for Tailwind UI we’ve never hit a situation where we needed nested groups so I think it’s already pretty rare. |
Beta Was this translation helpful? Give feedback.
-
Interesting. The solutions aren't mutually exclusive—i.e., could add a named-group feature for flexibility and also give the vanilla [
`:merge(.group-\\[name\\])${result} &:not(.group-\\[name\\] .group-\\[name\\] *)`,
`.group-\\[name\\] :merge(.group-\\[name\\])${result} &`
] |
Beta Was this translation helpful? Give feedback.
-
I just developed a new plugin very similar to this one that has all Tailwind 3 features. Check it out here: https://github.com/onmax/tailwindcss-labeled-groups |
Beta Was this translation helpful? Give feedback.
-
The named-group solutions certainly work, and there's something to be said for them if you find yourself facing the nested-groups problem in a single file, where you know there's a conflict as you're developing. What I don't like about the named-group approach is that it adds some conceptual overhead to solve a problem that rarely comes up in practice. It also doesn't really solve the conflicting-groups problem when the For these reasons, I prefer my selector-approach upthread that simply gives the plain-old I also think a |
Beta Was this translation helpful? Give feedback.
-
Just in case you came from Google looking for a solution (like me), it is documented here: https://tailwindcss.com/docs/hover-focus-and-other-states#differentiating-nested-groups |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
A subtle problem with the
group-
modifiers is that.group
elements generally can't be safely nested (because, e.g.,.group-hover:
classes within the inner.group
will activate when the outer.group
is hovered). See this recent discussion for an illustration of the problem: #7541. As I mention in a comment there, the problem can be hard to spot in development, since the outer and inner.group
stuff might be in completely separate files!I propose replacing the simple
`:merge(.group)${result} &`
selector with something that can withstand a layer or two of nested.group
elements.This array of selectors in an
addVariant()
would allow for one layer of nesting:And this allows for two layers of nesting (overkill maybe?):
I'm not sure if I've handled the
:merge()
business correctly (I think @RobinMalfait would know), but the basic idea is to stop each.group
element from affecting any.group-
classes within.group
descendants. Here is a working demo with two layers of nesting: https://play.tailwindcss.com/LLuZ71wHxABeta Was this translation helpful? Give feedback.
All reactions