front/src/hooks/responsive.ts
zzh 44d39ffa26
All checks were successful
Build and Deploy Front / deploy (push) Successful in 3m31s
feat: 全局视口适配地基,官网精选线路上移
统一断点/Token 与跨端规范,窄屏收紧页面壳;精选线路区上移 50px。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-12-03 15:38:27 +08:00

35 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { onMounted, onBeforeMount, onBeforeUnmount } from 'vue';
import { useDebounceFn } from '@vueuse/core';
import { APP_BREAKPOINTS } from '@/constants/breakpoints';
import { useAppStore } from '@/store';
import { addEventListen, removeEventListen } from '@/utils/event';
/** 与 @screen-lg / --app-bp-lg 一致;https://arco.design/vue/component/grid#responsivevalue */
const WIDTH = APP_BREAKPOINTS.lg;
function queryDevice() {
const rect = document.body.getBoundingClientRect();
return rect.width - 1 < WIDTH;
}
export default function useResponsive(immediate?: boolean) {
const appStore = useAppStore();
function resizeHandler() {
if (!document.hidden) {
const isMobile = queryDevice();
appStore.toggleDevice(isMobile ? 'mobile' : 'desktop');
appStore.toggleMenu(isMobile);
}
}
const debounceFn = useDebounceFn(resizeHandler, 100);
onMounted(() => {
if (immediate) debounceFn();
});
onBeforeMount(() => {
addEventListen(window, 'resize', debounceFn);
});
onBeforeUnmount(() => {
removeEventListen(window, 'resize', debounceFn);
});
}