refactor: 横屏提示与缩放改为组件标签(OrientationGuard)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a98b1d383e
commit
ce9567d1da
12 files changed
+50
-70
No files matched your search
@@ -1,6 +1,4 @@
|
||||
export * from "./assets";
|
||||
export * from "./lazyRoute";
|
||||
export * from "./tailwind";
|
||||
export * from "./useOrientation";
|
||||
export * from "./useScale";
|
||||
export * from "./yiban";
|
||||
@@ -1,22 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { appConfig } from "@/app.config";
|
||||
|
||||
export const DESIGN_WIDTH = appConfig.design.width;
|
||||
export const DESIGN_HEIGHT = appConfig.design.height;
|
||||
|
||||
function getViewportSize() {
|
||||
if (typeof window === "undefined") {
|
||||
return { width: DESIGN_WIDTH, height: DESIGN_HEIGHT };
|
||||
}
|
||||
return {
|
||||
width: window.visualViewport?.width ?? window.innerWidth,
|
||||
height: window.visualViewport?.height ?? window.innerHeight,
|
||||
};
|
||||
}
|
||||
|
||||
export function useScale() {
|
||||
const calculateScale = useCallback(() => {
|
||||
const viewport = getViewportSize();
|
||||
return Math.min(
|
||||
viewport.width / DESIGN_WIDTH,
|
||||
viewport.height / DESIGN_HEIGHT,
|
||||
appConfig.scale.max,
|
||||
);
|
||||
}, []);
|
||||
const [scale, setScale] = useState(calculateScale);
|
||||
|
||||
useEffect(() => {
|
||||
const updateScale = () => setScale(calculateScale());
|
||||
window.addEventListener("resize", updateScale);
|
||||
window.addEventListener("orientationchange", updateScale);
|
||||
window.visualViewport?.addEventListener("resize", updateScale);
|
||||
window.visualViewport?.addEventListener("scroll", updateScale);
|
||||
return () => {
|
||||
window.removeEventListener("resize", updateScale);
|
||||
window.removeEventListener("orientationchange", updateScale);
|
||||
window.visualViewport?.removeEventListener("resize", updateScale);
|
||||
window.visualViewport?.removeEventListener("scroll", updateScale);
|
||||
};
|
||||
}, [calculateScale]);
|
||||
|
||||
return scale;
|
||||
}
|
||||
Reference in new issue
Block a user