postcss-hover-media-feature icon indicating copy to clipboard operation
postcss-hover-media-feature copied to clipboard

Infinite loop when using `:hover` inside `:has()` condition

Open daun opened this issue 1 year ago • 5 comments

Hi! I've stumbled upon an issue where the plugin would hang in an infinite loop when using :hover inside a :has condition. Below is a concrete example that triggered the infinite loop. I suspect this might be related to #31 and/or fixed by #32, but I'm not too familiar with the inner workings of this (fantastic!) plugin.

header:has(a:hover),
header:has(a:focus) {
  a {
    color: gray;
  }
  a:hover,
  a:focus {
    color: black;
  }
}

daun avatar Sep 28 '24 13:09 daun

Hey @daun, thanks for taking the time to create an issue with a test-case/reproduction. I'm looking into this and other issues right now. A question regarding your example: is header:has(a:hover) valid CSS? I wasn't aware that the :has pseudo-class could itself contain pseudo-classes such as :hover. Regardless, I don't think this code should be causing the package to fail.

saulhardman avatar Sep 29 '24 16:09 saulhardman

@saulhardman Sure, nesting pseudo-selectors inside :has is a thing. There's a great article on the official WebKit blog that mentions the following example:

form:has(:focus-visible) div:has(input:not(:focus-visible)) label {}

Which is just crazy! But turns out it's valid 2022+ CSS and pretty neat :)

daun avatar Sep 29 '24 20:09 daun

I'm encountering this issue as well — specifically with a pattern along the lines of .foo:has(+ .foo:hover) — trying to style the sibling preceding the immediate next sibling that's being hovered.

cdransf avatar Oct 04 '24 16:10 cdransf

Hi! Encountering same issue when using css modules.

.form :global(.react-datepicker__time-list-item:hover) {}

As a workaround, we can move :hover out of :global() selector and it definitely works. But the cause of problem wasn't obvious at all.

bodryi avatar Dec 11 '24 15:12 bodryi

As a workaround, I was able to use the @jetbrains/postcss-require-hover package as a replacement. I'm not sure if/how it differs from this plugin, and if they're planning to maintain it in the long run (it's in turn a fork of the postcss-require-hover package with some fixes), but it seems to do the basic job.

If there's plans to maintain this package -- completely understand if that's not happening for the moment -- it might be worth cribbing from @jetbrains/postcss-require-hover as well.

joliss avatar Nov 19 '25 17:11 joliss