mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-06-23 21:22:04 -05:00
39 lines
926 B
TypeScript
39 lines
926 B
TypeScript
import { useState } from "react";
|
|
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
|
|
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";
|
|
}
|
|
|
|
export function useMainContentWidth() {
|
|
const [width, setWidth] = useState(0);
|
|
|
|
useIsomorphicLayoutEffect(() => {
|
|
const main = document.querySelector("main");
|
|
if (!main) return;
|
|
|
|
setWidth(main.clientWidth);
|
|
|
|
const observer = new ResizeObserver((entries) => {
|
|
setWidth(entries[0]?.contentRect.width);
|
|
});
|
|
|
|
observer.observe(main);
|
|
|
|
return () => observer.disconnect();
|
|
}, []);
|
|
|
|
return width;
|
|
}
|