mobile mode

This commit is contained in:
ari committed 2022-05-16 21:50:05 -04:00
1 parent b342088b96
commit f50f22190e
4 files changed
+110 -15

No files matched your search

+16 -12
View File
@@ -5,27 +5,24 @@ interface Size {
width: number;
height: number;
ratioWbh: number;
isMobile: boolean;
}
// Hook
export default function useWindowSize(): Size {
export default function useWindowSize(mobileRatioInd: number = 0.8): Size {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
const [windowSize, setWindowSize] = useState<Size>({
width: window.innerWidth,
height: window.innerHeight,
ratioWbh: window.innerWidth / window.innerHeight,
});
const [windowSize, setWindowSize] = useState<Size>(makeUpdate(window, mobileRatioInd));
// alias
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
ratioWbh: window.innerWidth / window.innerHeight,
});
setWindowSize(makeUpdate(window, mobileRatioInd));
}
// Add event listener
@@ -39,4 +36,11 @@ export default function useWindowSize(): Size {
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
}
}
const makeUpdate = (w: Window, mobileRatioInd: number): Size => ({
width: w.innerWidth,
height: w.innerHeight,
ratioWbh: w.innerWidth / w.innerHeight,
isMobile: w.innerWidth / w.innerHeight <= mobileRatioInd,
})