lbry-desktop/ui/effects/use-screensize.js
AsadUmar 50423111af
Fix/use has window width changed enough (#894)
* fix render of toggle.

* Fix useHasWindowWidthChangedEnough

* fix flow

Co-authored-by: Asad Umar <au@visuary.fr>
2022-02-16 14:08:57 -05:00

64 lines
1.8 KiB
JavaScript

// @flow
// Widths are taken from "ui/scss/init/vars.scss"
import React, { useRef } from 'react';
const DEFAULT_SCREEN_SIZE = 1080;
export function useWindowSize() {
const isWindowClient = typeof window === 'object';
const [windowSize, setWindowSize] = React.useState(isWindowClient ? window.innerWidth : DEFAULT_SCREEN_SIZE);
React.useEffect(() => {
function setSize() {
setWindowSize(window.innerWidth);
}
if (isWindowClient) {
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
}
}, [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);
React.useEffect(() => {
function setSize() {
const curr = comparisonFn(window.innerWidth);
if (prev.current !== curr) {
setWindowSize(curr);
prev.current = curr;
}
}
if (isWindowClient) {
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
}
}, [isWindowClient]);
return windowSize;
}
export function useIsMobile() {
return useHasWindowWidthChangedEnough((windowSize) => windowSize < 901);
}
export function useIsMediumScreen() {
return useHasWindowWidthChangedEnough((windowSize) => windowSize < 1151);
}
export function useIsLargeScreen() {
return useHasWindowWidthChangedEnough((windowSize) => windowSize > 1600);
}
export function isTouch() {
return 'ontouchstart' in window || 'onmsgesturechange' in window;
}