Skip to content

Instantly share code, notes, and snippets.

@crswll
Last active December 22, 2021 15:00
Show Gist options
  • Save crswll/cabb849f1fc8ecff0117c70843bec1f2 to your computer and use it in GitHub Desktop.
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.
<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>
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