web
web copied to clipboard
Fix compatibility issue for useMediaQuery
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/addListenercompatibility check increateQueryEntryto match the unsubscription logic inqueryUnsubscribe - 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
addEventListenerunconditionally when subscribing (line 20) - Using conditional
removeEventListener/removeListenerwhen unsubscribing (lines 52-56)
This mismatch meant that in older Safari:
- Subscription would fail trying to call non-existent
addEventListener - Even if it didn't fail, the listener wouldn't be properly registered with
addListener - Unsubscription would try to call
removeListeneron a listener that was never added