-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Add [open]
variant
#4627
Add [open]
variant
#4627
Conversation
The `[open]` pseudo-selector can be used to style an element that declares the `[open]` attribute (like `<details>` or `<dialog>`). For example: ```html <details class="border-0 open:border-1" open> <!-- ... --> </details> <details class="border-0 open:border-1"> <!-- ... --> </details> ``` The first `<details>` element is expanded, and so the `open:` variant will be active (i.e. `border-1`), and the second element's will be inactive (i.e. `border-0`).
for (let variant of booleanAttributeVariants) { | ||
addVariant(variant, ({ config, modifySelectors }) => { | ||
modifySelectors(({ className }) => { | ||
return `.${e(`${variant}${config('separator')}${className}`)}[${variant}]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamwathan in addition to styling the elements that declare [open]
themselves (e.g. <details>
), it'd be really valuable to also integrate the open:
variant with the .group
and .peer
utilities.
For example:
<details class="group peer" open>
<summary>
<span class="hidden group-open:inline">Close me, I'm open</span>
<span class="inline group-open:hidden">Open me, I'm closed</span>
</summary>
</details>
<span class="hidden peer-open:inline">I'm next to an open element</span>
<span class="inline peer-open:hidden">I'm next to a closed element</span>
Since the booleanAttributeVariants
key is new, I wasn't sure how to best integrate with the existing group
and peer
support from the pseudoElementVariants
without copy-pasting. Is there an existing utility we could re-use, or would this require extracting a new one?
Native Not sure if this will be helpful, but I use a single custom variant to target both an open addVariant('open', ({ modifySelectors, separator }) => {
return modifySelectors(({ selector }) => {
return selectorParser(selectors => {
const clonedSelectors = selectors.clone();
selectors.walkClasses(classNode => {
classNode.value = `open${separator}${classNode.value}`;
classNode.parent.insertBefore(classNode, selectorParser().astSync('[open]'));
});
clonedSelectors.walkClasses(classNode => {
classNode.value = `open${separator}${classNode.value}`;
classNode.parent.insertBefore(classNode, selectorParser().astSync('[open] '));
});
selectors.append(clonedSelectors);
}).processSync(selector);
});
}); In my experience it works well, including in combination with other variants (I'm using JIT), though I guess having a single variant for both purposes could be a problem if you have nested |
@seanpdoyle Thanks for this contribution! A lot has changed to the Tailwind CSS codebase since you originally submitted this, so instead of refactoring this PR, I just created a new one (#5627) and marked you as a co-author. I also got the Thanks again! 🙌 |
The
[open]
pseudo-selector can be used to style an element thatdeclares the
[open]
attribute (like<details>
or<dialog>
).For example:
The first
<details>
element is expanded, and so theopen:
variantwill be active (i.e.
border-1
), and the second element's will beinactive (i.e.
border-0
).