useSystemTheme: wrap addEventListener in try-catch for old iOS Safari
This commit is contained in:
parent
01b46e249b
commit
2acd0abab0
1 changed files with 14 additions and 2 deletions
|
@ -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';
|
||||
|
|
Loading…
Reference in a new issue