useSystemTheme: wrap addEventListener in try-catch for old iOS Safari

This commit is contained in:
Alex Gleason 2022-06-20 12:03:33 -05:00
parent 01b46e249b
commit 2acd0abab0
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -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';