Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Conversation

seanpdoyle
Copy link
Contributor

The [open] pseudo-selector can be used to style an element that
declares the [open] attribute (like <details> or <dialog>).

For example:

<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).

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}]`
Copy link
Contributor Author

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?

@MichaelAllenWarner
Copy link
Contributor

Native [open] support would be great.

Not sure if this will be helpful, but I use a single custom variant to target both an open details and any of its descendants:

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 details elements.

@reinink reinink mentioned this pull request Sep 28, 2021
@reinink
Copy link
Member

reinink commented Sep 28, 2021

@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 group and peer versions working. 👍

Thanks again! 🙌

@reinink reinink closed this Sep 28, 2021
@seanpdoyle seanpdoyle deleted the open-variant branch September 28, 2021 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants