Files
yx-react-template/src/course/orientation/useOrientation.ts
T
2026-09-08 14:07:42 +08:00

23 lines
665 B
TypeScript

import { useEffect, useState } from "react";
function getIsPortrait() {
if (typeof window === "undefined") return false;
return window.innerHeight > window.innerWidth;
}
export function useOrientation() {
const [isPortrait, setIsPortrait] = useState(getIsPortrait);
useEffect(() => {
const updateOrientation = () => setIsPortrait(getIsPortrait());
window.addEventListener("resize", updateOrientation);
window.addEventListener("orientationchange", updateOrientation);
return () => {
window.removeEventListener("resize", updateOrientation);
window.removeEventListener("orientationchange", updateOrientation);
};
}, []);
return isPortrait;
}