lbry-desktop/ui/effects/use-screensize.js
2022-04-15 15:29:37 -04:00

40 lines
983 B
JavaScript

// Widths are taken from "ui/scss/init/vars.scss"
import React from 'react';
function useWindowSize() {
const isWindowClient = typeof window === 'object';
const [windowSize, setWindowSize] = React.useState(isWindowClient ? window.innerWidth : undefined);
React.useEffect(() => {
function setSize() {
setWindowSize(window.innerWidth);
}
if (isWindowClient) {
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
}
}, [isWindowClient, setWindowSize]);
return windowSize;
}
export function useIsMobile() {
const windowSize = useWindowSize();
return windowSize < 901;
}
export function useIsMediumScreen() {
const windowSize = useWindowSize();
return windowSize < 1151;
}
export function useIsLargeScreen() {
const windowSize = useWindowSize();
return windowSize > 1600;
}
export function isTouch() {
return 'ontouchstart' in window || 'onmsgesturechange' in window;
}