sendou.ink/app/hooks/useWindowSize.ts
Kalle 4622426c07
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Eliminate useEffects (#3273)
2026-08-03 08:53:08 +03:00

32 lines
687 B
TypeScript

import * as React from "react";
interface WindowSize {
width: number;
height: number;
}
function subscribe(listener: () => void) {
window.addEventListener("resize", listener);
return () => window.removeEventListener("resize", listener);
}
/**
* Returns the current window dimensions, re-rendering on resize. Both
* dimensions are `0` during server-side rendering and the initial hydration
* render.
*/
export function useWindowSize(): WindowSize {
const width = React.useSyncExternalStore(
subscribe,
() => window.innerWidth,
() => 0,
);
const height = React.useSyncExternalStore(
subscribe,
() => window.innerHeight,
() => 0,
);
return { width, height };
}