lbry-desktop/ui/effects/use-screensize.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-02-05 16:34:51 +01:00
// @flow
2020-08-21 17:49:13 +02:00
// Widths are taken from "ui/scss/init/vars.scss"
2022-02-04 19:37:46 +01:00
import React, { useRef } from 'react';
import { getWindowAngle, isWindowLandscapeForAngle } from 'component/fileRenderFloating/helper-functions';
2022-02-05 16:34:51 +01:00
const DEFAULT_SCREEN_SIZE = 1080;
2020-08-21 17:49:13 +02:00
2022-02-05 16:34:51 +01:00
export function useWindowSize() {
2020-08-21 17:49:13 +02:00
const isWindowClient = typeof window === 'object';
2022-02-05 16:34:51 +01:00
const [windowSize, setWindowSize] = React.useState(isWindowClient ? window.innerWidth : DEFAULT_SCREEN_SIZE);
2020-08-21 17:49:13 +02:00
React.useEffect(() => {
function setSize() {
2022-02-05 16:34:51 +01:00
setWindowSize(window.innerWidth);
2020-08-21 17:49:13 +02:00
}
if (isWindowClient) {
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
}
2022-02-05 16:34:51 +01:00
}, [isWindowClient]);
return windowSize;
}
function useHasWindowWidthChangedEnough(comparisonFn: (windowSize: number) => boolean) {
const isWindowClient = typeof window === 'object';
const initialState: boolean = isWindowClient ? comparisonFn(window.innerWidth) : comparisonFn(DEFAULT_SCREEN_SIZE);
const [windowSize, setWindowSize] = React.useState<boolean>(initialState);
const prev = useRef<boolean>(initialState);
2022-02-05 16:34:51 +01:00
React.useEffect(() => {
function setSize() {
const curr = comparisonFn(window.innerWidth);
if (prev.current !== curr) {
2022-02-05 16:34:51 +01:00
setWindowSize(curr);
prev.current = curr;
}
}
if (isWindowClient) {
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
}
}, [isWindowClient]);
2020-08-21 17:49:13 +02:00
return windowSize;
}
2020-08-10 22:47:39 +02:00
export function useIsMobile() {
2022-02-05 16:34:51 +01:00
return useHasWindowWidthChangedEnough((windowSize) => windowSize < 901);
2020-08-10 22:47:39 +02:00
}
export function useIsMobileLandscape() {
const isMobile = useIsMobile();
2022-04-06 23:01:26 +02:00
const isLandscapeScreen = useIsLandscapeScreen();
return isMobile && isLandscapeScreen;
}
export function useIsLandscapeScreen() {
const isWindowClient = typeof window === 'object';
const windowAngle = getWindowAngle();
2022-04-06 23:01:26 +02:00
const isLandscape = isWindowLandscapeForAngle(windowAngle);
const [landscape, setLandscape] = React.useState<boolean>(isLandscape);
React.useEffect(() => {
function handleResize() {
const currAngle = getWindowAngle();
2022-04-06 23:01:26 +02:00
const isCurrLandscape = isWindowLandscapeForAngle(currAngle);
if (landscape !== isCurrLandscape) {
setLandscape(isCurrLandscape);
}
}
if (isWindowClient) {
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}
}, [isWindowClient, landscape]);
return landscape;
}
2020-08-10 22:47:39 +02:00
export function useIsMediumScreen() {
2022-02-05 16:34:51 +01:00
return useHasWindowWidthChangedEnough((windowSize) => windowSize < 1151);
2020-08-21 17:49:13 +02:00
}
export function useIsLargeScreen() {
2022-02-05 16:34:51 +01:00
return useHasWindowWidthChangedEnough((windowSize) => windowSize > 1600);
2020-08-10 22:47:39 +02:00
}