Refresh app when switching screen format or orientation

This commit is contained in:
Alexander Zinchuk 2021-05-06 01:47:32 +03:00
parent ca4bec5bb0
commit baffa925d8

View File

@ -1,12 +1,31 @@
import { throttle } from './schedulers';
import {
MOBILE_SCREEN_LANDSCAPE_MAX_HEIGHT,
MOBILE_SCREEN_LANDSCAPE_MAX_WIDTH,
MOBILE_SCREEN_MAX_WIDTH,
} from '../config';
import { IS_MOBILE_SCREEN } from './environment';
type IDimensions = {
width: number;
height: number;
};
const IS_LANDSCAPE = IS_MOBILE_SCREEN && isLandscape();
let windowSize = updateSizes();
const handleResize = throttle(() => {
windowSize = updateSizes();
if ((isMobileScreen() !== IS_MOBILE_SCREEN) || (IS_MOBILE_SCREEN && IS_LANDSCAPE !== !isLandscape())) {
window.location.reload();
}
}, 250, true);
window.addEventListener('resize', handleResize);
window.addEventListener('orientationchange', handleResize);
export function updateSizes(): IDimensions {
const vh = window.innerHeight * 0.01;
@ -18,12 +37,15 @@ export function updateSizes(): IDimensions {
};
}
const handleResize = throttle(() => {
windowSize = updateSizes();
}, 250, true);
function isMobileScreen() {
return windowSize.width <= MOBILE_SCREEN_MAX_WIDTH || (
windowSize.width <= MOBILE_SCREEN_LANDSCAPE_MAX_WIDTH && windowSize.height <= MOBILE_SCREEN_LANDSCAPE_MAX_HEIGHT
);
}
window.addEventListener('resize', handleResize);
window.addEventListener('orientationchange', handleResize);
function isLandscape() {
return window.matchMedia('(orientation: landscape)').matches;
}
export default {
get: () => windowSize,