Custom group modifier support? #18480
-
I tried to find docs on this but it seems to be quite limited. Is there a way to add custom modifiers into group statements? Such as if we add a hocus variant, could we do something like Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @VladyslavKochetkov
tailwind.config.jsconst plugin = require('tailwindcss/plugin');
module.exports = {
content: ['./src/**/*.{html,js,jsx,ts,tsx}'],
plugins: [
plugin(function ({ addVariant, e }) {
// plain `hocus:` (element itself)
addVariant('hocus', ['&:hover', '&:focus']);
// `group-hocus:` (inside a parent that has the `.group` class)
addVariant('group-hocus', ({ modifySelectors, separator }) => {
modifySelectors(({ selector }) => {
const escaped = e(`group-hocus${separator}${selector.slice(1)}`);
return `.group:hover .${escaped}, .group:focus .${escaped}`;
});
});
}),
],
}; Now you can write: <div class="group">
<button class="group-hocus:bg-red-400 hocus:text-white">
Hover or focus me (or the group)
</button>
</div> Alternative: arbitrary group variant (Tailwind 3.3 +)If you do not want to create a plugin you can use the “arbitrary variant” syntax which lets you inline any selector: <div class="group">
<button class="group-[&:hover,&:focus]:bg-red-400">
Hover or focus the group
</button>
</div> But if you plan to reuse |
Beta Was this translation helpful? Give feedback.
-
Ok leaving this to another who needs help with this in the future. @GaneshVarma1 really helped out and sent me in the right direction. The achieve a plugins: [
function ({ addVariant, matchVariant, e }) {
addVariant("hocus", ["&:hover", "&:focus-visible"]);
matchVariant("group-hocus", (value, { modifier }) => {
return modifier
? [`:merge(.group\\/${modifier}):hover &`, `:merge(.group\\/${modifier}):focus-visible &`]
: [`:merge(.group):hover &`, `:merge(.group):focus-visible &`];
}, {
values: { DEFAULT: '' }
});
},
] |
Beta Was this translation helpful? Give feedback.
Ok leaving this to another who needs help with this in the future. @GaneshVarma1 really helped out and sent me in the right direction. The achieve a
hocus
variant that supports regularhocus:bg-red-400
, group styling viagroup-hocus:bg-red-400
as well as group styling with variants such asgroup-hocus/row:bg-red-400
you can use the following snippets.