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 has-* variants for :has(...) pseudo-class #11318

Merged
merged 3 commits into from
May 30, 2023
Merged

Add has-* variants for :has(...) pseudo-class #11318

merged 3 commits into from
May 30, 2023

Conversation

adamwathan
Copy link
Member

@adamwathan adamwathan commented May 30, 2023

This PR adds new has-*, group-has-*, and peer-has-* variants that add support for the :has() pseudo-class.

This is a recent CSS feature that makes it possible to style parent elements based on their descendants. Check out this great article on the Chrome Developers blog for lots of practical examples.

These are dynamic variants that accept a selector as their argument:

<div class="has-[[data-potato]]:grid">
  <div data-potato>...</div>
</div>

<!-- Generated CSS-->
<style>
  .has-\[\[data-potato\]\]\:grid:has([data-potato]) {
    display: grid
  }
</style>

I've included group-* and peer-* variations as well:

<div class="group">
  <div class="group-has-[[data-potato]]:grid">...</div>
  <div data-potato>...</div>
</div>

<!-- Generated CSS-->
<style>
  .group:has([data-potato]) .group-has-\[\[data-potato\]\]\:grid {
    display: grid
  }
</style>

<div>
  <div class="peer">
    <div data-potato>...</div>
  </div>
  <div class="peer-has-[[data-potato]]:grid">...</div>
</div>

<!-- Generated CSS-->
<style>
  .peer:has([data-potato]) ~ .peer-has-\[\[data-potato\]\]\:grid {
    display: grid
  }
</style>

We've decided not to add a key for has to the theme configuration, and instead just recommend using arbitrary values. If it proves to be extremely useful to define aliases for has-* we may revisit this in the future.

@adamwathan adamwathan merged commit 69d8687 into master May 30, 2023
10 checks passed
@adamwathan adamwathan deleted the has-variants branch May 30, 2023 16:21
@Gravy59
Copy link

Gravy59 commented May 30, 2023

How will this work on firefox? Is there a polyfill or similar?

@brandonmcconnell
Copy link
Contributor

How will this work on firefox? Is there a polyfill or similar?

@Gravy59 you can find one here: https://github.com/jplhomer/polyfill-css-has

Polyfills are usually outside the scope of tailwind, as TailwindCSS is purely a utility-to-CSS compiler, so I don’t think we’ll see any sort of polyfill support for this come to tailwind as a core feature, but you can still implement it on your end to make use of the utility.

Personally, I think it’s better for a framework like this to be more on the bleeding edge than wait for browser compatibility so the people who want to use utilities for progressive enhancement can.

@LorenzoBloedow
Copy link

I think there should probably be a warning when Tailwind compiles saying that Firefox still doesn't support it, with Tailwind I feel like the expectation from a lot of developers is that of not worrying too much about browser compatibility, so a heads-up would probably help a lot of folks out there!

@rleecrewmp
Copy link

Are there any plans to have an inverse of the group-* variant such as child-* to make use of modifiers and style based on specific child elements?

<div class="child-hover/first-link:bg-red-100">
  <button class="child/first-link">First Link</button>
  <button class="child/second-link">Second Link</button>
</div>

<!-- Generated CSS-->
<style>
  .child-hover\/first-link\:bg-red-100:has(.child\/first-link:hover)  {
    background-color: #fee2e2
  }
</style>

thecrypticace pushed a commit that referenced this pull request Dec 4, 2023
* Add `has-*` variants for `:has(...)` pseudo-class

* Update changelog

* Fix mistake in test

---------

Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
thecrypticace pushed a commit that referenced this pull request Dec 5, 2023
* Add `has-*` variants for `:has(...)` pseudo-class

* Update changelog

* Fix mistake in test

---------

Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
thecrypticace pushed a commit that referenced this pull request Dec 18, 2023
* Add `has-*` variants for `:has(...)` pseudo-class

* Update changelog

* Fix mistake in test

---------

Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
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.

None yet

5 participants