sendou.ink/app/hooks/useMainContentWidth.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

49 lines
1.1 KiB
TypeScript

import * as React from "react";
import { useWindowSize } from "./useWindowSize";
const MOBILE_BREAKPOINT = 600;
const DESKTOP_BREAKPOINT = 1000;
type LayoutSize = "mobile" | "tablet" | "desktop";
export function useLayoutSize(): LayoutSize {
const { width } = useWindowSize();
if (width === 0) return "desktop";
if (width < MOBILE_BREAKPOINT) return "mobile";
if (width < DESKTOP_BREAKPOINT) return "tablet";
return "desktop";
}
const listeners = new Set<() => void>();
let observer: ResizeObserver | null = null;
function subscribe(listener: () => void) {
listeners.add(listener);
if (!observer) {
observer = new ResizeObserver(() => {
for (const notify of listeners) {
notify();
}
});
const main = document.querySelector("main");
if (main) observer.observe(main);
}
return () => {
listeners.delete(listener);
if (listeners.size === 0) {
observer?.disconnect();
observer = null;
}
};
}
function getSnapshot() {
return document.querySelector("main")?.clientWidth ?? 0;
}
export function useMainContentWidth() {
return React.useSyncExternalStore(subscribe, getSnapshot, () => 0);
}