interface: fix mediaquery watchers on safari

This commit is contained in:
Liam Fitzgerald 2021-07-06 10:15:34 +10:00
parent 6b7030d617
commit 13a9b758ec
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
2 changed files with 20 additions and 6 deletions

View File

@ -26,6 +26,7 @@ export interface LocalState {
setTutorialRef: (el: HTMLElement | null) => void;
dark: boolean;
mobile: boolean;
mdBreak: boolean;
background: BackgroundConfig;
omniboxShown: boolean;
suspendedFocus?: HTMLElement;
@ -45,6 +46,7 @@ export const selectLocalState =
const useLocalState = create<LocalStateZus>(persist((set, get) => ({
dark: false,
mobile: false,
mdBreak: false,
background: undefined,
theme: 'auto',
hideAvatars: false,

View File

@ -83,15 +83,20 @@ class App extends React.Component {
bootstrapApi();
this.props.getShallowChildren(`~${window.ship}`, 'dm-inbox');
const theme = this.getTheme();
this.themeWatcher = window.matchMedia('(prefers-color-scheme: dark)');
this.mobileWatcher = window.matchMedia(`(max-width: ${theme.breakpoints[0]})`);
this.themeWatcher.onchange = this.updateTheme;
this.mobileWatcher.onchange = this.updateMobile;
setTimeout(() => {
// Something about how the store works doesn't like changing it
// before the app has actually rendered, hence the timeout.
this.themeWatcher = window.matchMedia('(prefers-color-scheme: dark)');
this.mobileWatcher = window.matchMedia(`(max-width: ${theme.breakpoints[0]})`);
this.mediumWatcher = window.matchMedia(`(max-width: ${theme.breakpoints[1]})`);
// TODO: addListener is deprecated, but safari 13 requires it
this.themeWatcher.addListener(this.updateTheme);
this.mobileWatcher.addListener(this.updateMobile);
this.mediumWatcher.addListener(this.updateMedium);
this.updateMobile(this.mobileWatcher);
this.updateTheme(this.themeWatcher);
this.updateMedium(this.mediumWatcher);
}, 500);
this.props.getBaseHash();
this.props.getRuntimeLag(); // TODO consider polling periodically
@ -105,8 +110,9 @@ class App extends React.Component {
}
componentWillUnmount() {
this.themeWatcher.onchange = undefined;
this.mobileWatcher.onchange = undefined;
this.themeWatcher.removeListener(this.updateTheme);
this.mobileWatcher.removeListener(this.updateMobile);
this.mediumWatcher.removeListener(this.updateMedium);
}
updateTheme(e) {
@ -121,6 +127,12 @@ class App extends React.Component {
});
}
updateMedium = (e) => {
this.props.set((state) => {
state.mdBreak = e.matches;
});
}
getTheme() {
const { props } = this;
return ((props.dark && props?.display?.theme == 'auto') ||