MCPcopy Create free account
hub / github.com/msgbyte/tailchat / useWindowSize

Function useWindowSize

client/web/src/hooks/useWindowSize.ts:6–33  ·  view source on GitHub ↗
(
  initialWidth = Infinity,
  initialHeight = Infinity
)

Source from the content-addressed store, hash-verified

4// Reference: https://github.com/streamich/react-use/blob/master/src/useWindowSize.ts
5
6export const useWindowSize = (
7 initialWidth = Infinity,
8 initialHeight = Infinity
9) => {
10 const [state, setState] = useRafState<{ width: number; height: number }>({
11 width: isBrowser ? window.innerWidth : initialWidth,
12 height: isBrowser ? window.innerHeight : initialHeight,
13 });
14
15 useEffect((): (() => void) | void => {
16 if (isBrowser) {
17 const handler = () => {
18 setState({
19 width: window.innerWidth,
20 height: window.innerHeight,
21 });
22 };
23
24 window.addEventListener('resize', handler);
25
26 return () => {
27 window.removeEventListener('resize', handler);
28 };
29 }
30 }, []);
31
32 return state;
33};

Callers 1

useIsMobileFunction · 0.90

Calls 1

useRafStateFunction · 0.85

Tested by

no test coverage detected