fix: use-mobile hook crashes on older Safari without MediaQueryList.addEventListener - #235
Conversation
…ddEventListener
useIsMobile() called mql.addEventListener('change', onChange) directly
on a MediaQueryList. Older Safari (Catalina and before) only implements
the deprecated addListener/removeListener pair, causing a runtime crash.
The sibling hook useMediaQuery.ts already has an attachMediaListener
helper with a try/catch fallback for this exact case. Use the same
pattern here for consistency and cross-browser compatibility.
Used by Sidebar and Dropdrawer components.
|
Thanks for this, @Ayush7614 — the problem is real and the instinct to reuse the pattern we already had is the right one. 🙏 I checked the claims before reading the prose:
Two things before this can go in. 1. The
|
…expect-error - lib.dom declares addListener/removeListener (deprecated but present), so the @ts-expect-error directives were unused and failed tsc (TS2578). - Export attachMediaListener + MediaQueryCallback from useMediaQuery.ts and import them here, removing the duplicated helper.
|
Thanks for the review @Vect0rM Addressed both review points on branch
Verified locally: |
|
Both points addressed, and the second one done properly rather than minimally — thanks, @Ayush7614. 🙏 I re-verified
One trivial nit, deliberately not blocking the merge — noting it so it's on record rather than lost:
Merging. Thanks for taking a compatibility path most people never think about, and then leaving it tidier than you found it. 🧭 |
useIsMobile() called mql.addEventListener('change', onChange) directly on a MediaQueryList. Older Safari (Catalina and before) only implements the deprecated addListener/removeListener pair, causing a runtime crash.
The sibling hook useMediaQuery.ts already has an attachMediaListener helper with a try/catch fallback for this exact case. Use the same pattern here for consistency and cross-browser compatibility.
Used by Sidebar and Dropdrawer components.