Last active
December 22, 2021 15:00
-
-
Save crswll/cabb849f1fc8ecff0117c70843bec1f2 to your computer and use it in GitHub Desktop.
A quick example of generating `:has()` variants with Tailwind. As of right now Safari Technology Preview is the only browser with support for `:has()` and there's currently a few bugs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="group flex flex-col"> | |
<label class="text-gray-500 group-has-disabled:text-red-500">Name</label> | |
<input class="border p-2" disabled /> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasVariantsPlugin ({ addVariant }) { | |
[ | |
'checked', | |
'disabled', | |
'focus', | |
'invalid', | |
'placeholder-shown', | |
'playing', | |
].forEach((name) => { | |
addVariant( | |
`group-has-${name}`, | |
/* | |
Generates something like: | |
.group:has(:some-pseudo-class) .group-has-disabled\:some-utility-class | |
To use like: | |
<div class="group"> | |
<label class="group-has-disabled:text-red-500">Name</label> | |
<input class="border p-2" disabled> | |
</div> | |
*/ | |
[ `:merge(.group):has(:${name}) &` ] | |
) | |
}) | |
} | |
module.exports = { | |
plugins: [ hasVariantsPlugin ] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment