Skip to content

fix: use-mobile hook crashes on older Safari without MediaQueryList.addEventListener - #235

Open
Ayush7614 wants to merge 1 commit into
AtomicBot-ai:mainfrom
Ayush7614:fix/use-mobile-addlistener-fallback
Open

fix: use-mobile hook crashes on older Safari without MediaQueryList.addEventListener#235
Ayush7614 wants to merge 1 commit into
AtomicBot-ai:mainfrom
Ayush7614:fix/use-mobile-addlistener-fallback

Conversation

@Ayush7614

@Ayush7614 Ayush7614 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

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.

…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.
@Vect0rM

Vect0rM commented Aug 18, 2026

Copy link
Copy Markdown
Member

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:

  • The crash is genuine on our target platforms. Tauri supports macOS 10.15+, and the WKWebView that ships with Catalina implements only the deprecated addListener/removeListener pair on MediaQueryList. useIsMobile() called mql.addEventListener('change', …) unguarded.
  • The blast radius is bigger than it looksuseIsMobile feeds components/ui/sidebar.tsx:88 and components/ui/dropdrawer.tsx:48, i.e. the app shell. A throw there is not a degraded widget, it's a blank window.
  • Matching useMediaQuery.ts is the correct call — same try/catch shape, same cleanup contract, and the hook keeps returning a disposer so the useEffect contract is unchanged.

Two things before this can go in.

1. The @ts-expect-error directives break the build

lib.dom does declare addListener/removeListener (deprecated, but present), so those calls type-check fine and the directives are unused — which is itself an error under our config:

a.ts(12,5): error TS2578: Unused '@ts-expect-error' directive.
a.ts(15,7): error TS2578: Unused '@ts-expect-error' directive.

That's tsc with our lib: ["ES2021", "DOM", "DOM.Iterable"] from web-app/tsconfig.app.json, on the exact block from this PR. So yarn build (tsc -b) fails. The direct evidence is in useMediaQuery.ts itself — it makes the same two calls with no suppression at all and has compiled for as long as it's existed.

Please drop both directives. If the deprecation lint bothers you locally, // eslint-disable-next-line @typescript-eslint/no-deprecated is the tool for that, not @ts-expect-error.

Worth flagging that this went unnoticed because we don't currently run tsc/vitest on pull requests — the empty check list on this PR isn't a green one. Not your problem to fix, but it does mean the review has to catch it.

2. Third copy of the same helper

attachMediaListener (and the MediaQueryCallback type) now exists verbatim in both useMediaQuery.ts and use-mobile.ts. Please export it from useMediaQuery.ts — or lift it into a small shared module — and import it here, so the next browser quirk gets fixed in one place instead of two.

Everything else — the tests, the comment explaining why the fallback exists, keeping the file's existing quote style — is good. Once 1 is fixed I'm happy to approve; 2 is a strong preference, not a blocker if you'd rather keep the diff local. Thanks again for digging into a compatibility path most people never think about. 🧭

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.

2 participants