Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 2x 47x 47x 23x 23x 47x 47x 47x 47x 47x 47x 23x 23x 23x 23x 4x 4x 2x 4x 4x 23x 4x 23x 23x 23x 23x 23x 23x 47x 47x 47x 47x 27x 27x 27x 7x 27x 27x 47x 23x 23x 24x 24x 24x 18x 18x 127x 127x 127x 127x 127x 24x 24x 24x 6x 6x 6x 24x 24x 24x 6x 6x 6x 24x 24x 24x 23x 23x 23x 23x 4x 4x 4x 4x 4x 19x 19x 47x | import { useState, useEffect, useRef } from "react";
import { Logger } from "@shared/logger";
import {
getResponsiveLayoutMode,
RESPONSIVE_MEDIA_QUERIES,
type ResponsiveLayoutMode,
} from "@/lib/responsive-layout";
import type { MobilePanel } from "@/components/features/mobile-layout";
const logger = new Logger("MobileLayout");
export function useMobileLayout() {
const isClient = globalThis.window !== undefined;
const getInitialMode = (): ResponsiveLayoutMode => {
Iif (!isClient) return "desktop";
return getResponsiveLayoutMode(
globalThis.matchMedia(RESPONSIVE_MEDIA_QUERIES.mobile).matches,
globalThis.matchMedia(RESPONSIVE_MEDIA_QUERIES.tablet).matches,
);
};
const [layoutMode, setLayoutMode] = useState<ResponsiveLayoutMode>(getInitialMode);
const [mobilePanel, setMobilePanel] = useState<MobilePanel>("code");
const previousModeRef = useRef<ResponsiveLayoutMode>(layoutMode);
const [headerHeight, setHeaderHeight] = useState<number>(40);
const [overlayZ, setOverlayZ] = useState<number>(30);
// Keep all layout boundaries in sync with the shared responsive contract.
useEffect(() => {
Iif (!isClient) return;
const mobileQuery = globalThis.matchMedia(RESPONSIVE_MEDIA_QUERIES.mobile);
const tabletQuery = globalThis.matchMedia(RESPONSIVE_MEDIA_QUERIES.tablet);
const applyLayoutMode = (mobileMatches: boolean, tabletMatches: boolean) => {
const nextMode = getResponsiveLayoutMode(
mobileMatches,
tabletMatches,
);
if (nextMode === "mobile" && previousModeRef.current !== "mobile") {
setMobilePanel("code");
}
previousModeRef.current = nextMode;
setLayoutMode(nextMode);
};
const onMobileChange = (event: MediaQueryListEvent) => {
applyLayoutMode(event.matches, tabletQuery.matches);
};
const onTabletChange = (event: MediaQueryListEvent) => {
applyLayoutMode(mobileQuery.matches, event.matches);
};
mobileQuery.addEventListener("change", onMobileChange);
tabletQuery.addEventListener("change", onTabletChange);
return () => {
mobileQuery.removeEventListener("change", onMobileChange);
tabletQuery.removeEventListener("change", onTabletChange);
};
}, [isClient]);
const isMobile = layoutMode === "mobile";
const isTablet = layoutMode === "tablet";
const isDesktop = layoutMode === "desktop";
// Prevent document scroll while the mobile workspace owns the viewport.
useEffect(() => {
Iif (!isClient) return;
const prev = document.body.style.overflow;
if (isMobile) {
document.body.style.overflow = "hidden";
}
return () => {
document.body.style.overflow = prev || "";
};
}, [isMobile, isClient]);
// Compute header height and overlay z-index
useEffect(() => {
Iif (!isClient) return;
const measure = () => {
// First try to find our mobile header by data attribute
let hdr: Element | null = document.querySelector("[data-mobile-header]");
// Fallback to <header> tag
hdr ??= document.querySelector("header");
if (!hdr) {
const all = Array.from<HTMLElement>(
document.body.querySelectorAll<HTMLElement>("*"),
);
hdr =
all.find((el) => {
Iif (!el) return false;
// ignore html/body
Iif (el === document.body || el === document.documentElement)
return false;
const style = getComputedStyle(el);
Eif (
style.display === "none" ||
style.visibility === "hidden" ||
Number(style.opacity) === 0
)
return false;
const r = el.getBoundingClientRect();
// must be near the top and reasonably small (not full-page)
if (r.top < -5 || r.top > 48) return false;
if (r.height < 24 || r.height > globalThis.innerHeight / 2)
return false;
return true;
}) || null;
}
Iif (hdr === document.body || hdr === document.documentElement) hdr = null;
let h = 40;
if (hdr) {
const rect = (hdr as HTMLElement).getBoundingClientRect();
Eif (rect.height > 0 && rect.height < globalThis.innerHeight / 2)
h = Math.ceil(rect.height);
}
setHeaderHeight(h);
let z = 0;
if (hdr) {
const zStr = getComputedStyle(hdr as HTMLElement).zIndex;
const zNum = Number.parseInt(zStr || "", 10);
z = Number.isFinite(zNum) ? zNum : 0;
}
const chosenZ = z > 0 ? Math.max(z - 1, 5) : 30;
setOverlayZ(chosenZ);
logger.debug(
`[mobile overlay] header detect: ${hdr?.tagName ?? "null"} headerHeight=${h} overlayZ=${chosenZ}`,
);
};
measure();
globalThis.addEventListener("resize", measure);
const hdr = document.querySelector("header");
if (hdr) {
const obs = new MutationObserver(measure);
obs.observe(hdr, { attributes: true, childList: true, subtree: true });
return () => {
globalThis.removeEventListener("resize", measure);
obs.disconnect();
};
}
return () => {
globalThis.removeEventListener("resize", measure);
};
}, [isClient]);
return {
isMobile,
isTablet,
isDesktop,
layoutMode,
mobilePanel,
setMobilePanel,
headerHeight,
overlayZ,
};
}
|