From 2acd0abab022061bc19363c7c37df625c8e3ad37 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 20 Jun 2022 12:03:33 -0500 Subject: [PATCH] useSystemTheme: wrap addEventListener in try-catch for old iOS Safari --- app/soapbox/hooks/useSystemTheme.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/soapbox/hooks/useSystemTheme.ts b/app/soapbox/hooks/useSystemTheme.ts index 3258851f5d..d4d24c9eea 100644 --- a/app/soapbox/hooks/useSystemTheme.ts +++ b/app/soapbox/hooks/useSystemTheme.ts @@ -11,10 +11,22 @@ export const useSystemTheme = (): SystemTheme => { setDark(event.matches); }; + // Older versions of Safari on iOS don't support these events, + // so try-catch and do nothing. useEffect(() => { - query.addEventListener('change', handleChange); + try { + query.addEventListener('change', handleChange); + } catch (e) { + // do nothing + } - return () => query.removeEventListener('change', handleChange); + return () => { + try { + query.removeEventListener('change', handleChange); + } catch (e) { + // do nothing + } + }; }, []); return dark ? 'dark' : 'light';