-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[Feature Proposal] - Nested Group Hover #1192
Comments
It is possible if you change the .group:hover > .group-hover\:visible {
visibility: visible;
} <div class="group">
<div>Parent</div>
<div class="invisible group-hover:visible">
<div class="group">
<div>Child</div>
<div class="invisible group-hover:visible">Grandchild</div>
</div>
</div>
</div> But there must a better way to handle nested group hovers! |
Related conversation: tailwindlabs/discuss#337 |
I would recommend solving this problem with a custom variant plugin for now, and maybe we can explore adding something to core in the future. Something like this plugin would generate const selectorParser = require('postcss-selector-parser')
module.exports = function ({ addVariant }) {
addVariant('group-2-hover', ({ modifySelectors, separator }) => {
return modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(sel => {
sel.value = `group-2-hover${separator}${sel.value}`
sel.parent.insertBefore(sel, selectorParser().astSync(prefixSelector(config.prefix, '.group-2:hover '))
)
})
}).processSync(selector)
})
})
addVariant('group-3-hover', ({ modifySelectors, separator }) => {
return modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(sel => {
sel.value = `group-3-hover${separator}${sel.value}`
sel.parent.insertBefore(sel, selectorParser().astSync(prefixSelector(config.prefix, '.group-3:hover '))
)
})
}).processSync(selector)
})
})
} |
Any chance there's an up-to-date version of the plugin above? Just tried it out with tailwindcss v1.4.6 and don't seem to be having any luck. |
@christopher4lis I created a small plugin to be able to have what I call "named groups" that is similar to what @adamwathan does here. You can take a look it here: https://github.com/ErickTamayo/tailwindcss-named-groups |
I released support for nested groups by scoping https://github.com/AndyOGo/tailwindcss-nested-groups You can play with it here |
@adamwathan @simonswiss any more recent thoughts on this? would be cool if group was scoped by default or if we had a way to name them. not sure if that's built into JIT? |
@AndyOGo thanks for your plugin! Really love it :) Unfortunatly it doesn't work with the latest tailwind/postcss version. Any plans to update it? |
@LeaHabel Anyway, just in case you have JIT enabled? |
@adamwathan can you perhaps look into this after the JIT implementation? It would be very convenient to generate these at runtime. That plugin works, but they must be manually configured. |
Hello, I'm rather new to that and I've tried your code with v2.2.19 but it doesn't work. I've look at the documentation but there is nothing similar to it :/
|
@yoobi couldn't make @adamwathan example work either. @AndyOGo plugin doesnt work for me either. The one that worked for me is https://github.com/ErickTamayo/tailwindcss-named-groups from @ErickTamayo, thanks man :) |
By looking in the source code of Tailwind CSS, I found a very concise way to add the named groups hover feature in the config, rather than installing a package. It works well (v3.0.13), not sure it's something safe to use in production though. const plugin = require('tailwindcss/plugin')
module.exports = {
theme: {
groups: ['example']
},
plugins: [
plugin(({ addVariant, theme }) => {
const groups = theme('groups') || []
groups.forEach((group) => {
addVariant(`group-${group}-hover`, () => {
return `:merge(.group-${group}):hover &`
})
})
})
]
} |
Thanks for the suggestion above, @maelquerre! 👏 It works quite well. I think that, at the moment, that's really the cleanest way to do it. In fact, I liked it so much that I extended it a bit and bundled it into a tailwind-custom-groups package, in case anyone wants to install it quickly (like me). |
@maelquerre |
@anankitpatil <div class="group-example">
<button>Dropdown</button>
<div class="absolute hidden group-example-hover:block">
<p>Content</p>
</div>
</div> |
You can also use |
I am using it. Works well!. Thanks |
With TailwindCSS v3.1, we can use arbitrary variant to create nested group <div class="group">
<div>Parent</div>
<div class="invisible group-hover:visible">
<div class="group-2">
<div>Child</div>
<div class="invisible [.group-2:hover_&]:visible">Grandchild</div>
</div>
</div>
</div> Here's the playground https://play.tailwindcss.com/wYJHX4z5fi |
Thanks for sharing, @boompikachu! That's an interesting approach. It seems to rely on the newly added Arbitrary Variants feature, though, which means the code now contains an arbitrarily defined class name. That's a first-class citizen feature of Tailwind CSS, so I think it's totally fine to use. In our case, however, we're using the Because of that, we'll continue maintaining and using tailwindcss-custom-groups. Thanks for the tip, though! |
very nice solution ; it work |
The issue seems to have existed in v3.1.8 but has been fixed in v3.2.4. |
I had success with using [.group:hover_>_&]:bg-g-400 in Tailwind 3; only for direct childs! |
That's because Tailwind can't match the class when purging. See Dynamic class names for more information about class matching. You have a few options:
|
Hi,
When creating nav dropdown, I usually just add
group
class to my<li>
element, and then to my<ul>
that is child of that<li>
I add something likeopacity-0 invisible backface-invisible group-hover:opacity-100 group-hover:visible group-hover:backface-visible
. Which works fine, but then if my dropdown will have another dropdown, this won't work.It would be awesome if we could have nested group hover. :)
The text was updated successfully, but these errors were encountered: