web icon indicating copy to clipboard operation
web copied to clipboard

Fix compatibility issue for useMediaQuery

Open reekystive opened this issue 4 months ago • 0 comments

Summary

This PR fixes an asymmetric compatibility issue in useMediaQuery where the addEventListener compatibility check was removed during subscription but still present during unsubscription, causing errors in older Safari versions.

The recent commit 4880540 removed the addEventListener / addListener fallback logic in createQueryEntry but kept it in queryUnsubscribe, creating an inconsistency. This causes issues in Safari versions that don't support addEventListener on MediaQueryList (Safari < 14).

Changes

  • Restored the addEventListener / addListener compatibility check in createQueryEntry to match the unsubscription logic in queryUnsubscribe
  • Ensures symmetric handling of event listeners for both subscription and unsubscription

Problem

In older Safari versions, MediaQueryList only supports the deprecated addListener / removeListener methods. The code was:

  • Using addEventListener unconditionally when subscribing (line 20)
  • Using conditional removeEventListener / removeListener when unsubscribing (lines 52-56)

This mismatch meant that in older Safari:

  1. Subscription would fail trying to call non-existent addEventListener
  2. Even if it didn't fail, the listener wouldn't be properly registered with addListener
  3. Unsubscription would try to call removeListener on a listener that was never added

reekystive avatar Oct 23 '25 03:10 reekystive