mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
853 B
TypeScript
34 lines
853 B
TypeScript
import { isBrowser, useRafState } from 'tailchat-shared';
|
|
import { useEffect } from 'react';
|
|
|
|
// Reference: https://github.com/streamich/react-use/blob/master/src/useWindowSize.ts
|
|
|
|
export const useWindowSize = (
|
|
initialWidth = Infinity,
|
|
initialHeight = Infinity
|
|
) => {
|
|
const [state, setState] = useRafState<{ width: number; height: number }>({
|
|
width: isBrowser ? window.innerWidth : initialWidth,
|
|
height: isBrowser ? window.innerHeight : initialHeight,
|
|
});
|
|
|
|
useEffect((): (() => void) | void => {
|
|
if (isBrowser) {
|
|
const handler = () => {
|
|
setState({
|
|
width: window.innerWidth,
|
|
height: window.innerHeight,
|
|
});
|
|
};
|
|
|
|
window.addEventListener('resize', handler);
|
|
|
|
return () => {
|
|
window.removeEventListener('resize', handler);
|
|
};
|
|
}
|
|
}, []);
|
|
|
|
return state;
|
|
};
|