fix useScreensize
This commit is contained in:
parent
e0f01a58d2
commit
00c9410253
2 changed files with 18 additions and 12 deletions
|
@ -2,13 +2,17 @@ import { useEffect } from 'react';
|
||||||
import debounce from 'util/debounce';
|
import debounce from 'util/debounce';
|
||||||
|
|
||||||
export function useOnResize(cb) {
|
export function useOnResize(cb) {
|
||||||
|
const isWindowClient = typeof window === 'object';
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleResize = debounce(cb, 100);
|
const handleResize = debounce(cb, 100);
|
||||||
|
|
||||||
window.addEventListener('resize', handleResize);
|
if (isWindowClient) {
|
||||||
|
window.addEventListener('resize', handleResize);
|
||||||
|
|
||||||
handleResize();
|
handleResize();
|
||||||
|
|
||||||
return () => window.removeEventListener('resize', handleResize);
|
return () => window.removeEventListener('resize', handleResize);
|
||||||
}, []);
|
}
|
||||||
|
}, [isWindowClient]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,23 @@
|
||||||
// Widths are taken from "ui/scss/init/vars.scss"
|
// Widths are taken from "ui/scss/init/vars.scss"
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
function useWindowSize() {
|
export function useWindowSize(fn) {
|
||||||
const isWindowClient = typeof window === 'object';
|
const isWindowClient = typeof window === 'object';
|
||||||
const [windowSize, setWindowSize] = React.useState(isWindowClient ? window.innerWidth : undefined);
|
const [windowSize, setWindowSize] = React.useState(isWindowClient ? window.innerWidth : undefined);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
function setSize() {
|
function setSize() {
|
||||||
setWindowSize(window.innerWidth);
|
if (fn) {
|
||||||
|
const curr = fn(window.innerWidth);
|
||||||
|
if (windowSize !== curr) setWindowSize(curr);
|
||||||
|
} else setWindowSize(window.innerWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isWindowClient) {
|
if (isWindowClient) {
|
||||||
window.addEventListener('resize', setSize);
|
window.addEventListener('resize', setSize);
|
||||||
|
|
||||||
|
setSize();
|
||||||
|
|
||||||
return () => window.removeEventListener('resize', setSize);
|
return () => window.removeEventListener('resize', setSize);
|
||||||
}
|
}
|
||||||
}, [isWindowClient, setWindowSize]);
|
}, [isWindowClient, setWindowSize]);
|
||||||
|
@ -21,16 +26,13 @@ function useWindowSize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useIsMobile() {
|
export function useIsMobile() {
|
||||||
const windowSize = useWindowSize();
|
return useWindowSize((windowSize) => windowSize < 901);
|
||||||
return windowSize < 901;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useIsMediumScreen() {
|
export function useIsMediumScreen() {
|
||||||
const windowSize = useWindowSize();
|
return useWindowSize((windowSize) => windowSize < 1151);
|
||||||
return windowSize < 1151;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useIsLargeScreen() {
|
export function useIsLargeScreen() {
|
||||||
const windowSize = useWindowSize();
|
return useWindowSize((windowSize) => windowSize > 1600);
|
||||||
return windowSize > 1600;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue