28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
/**
|
||
* 窄屏(触摸档)判定:和 theme/css-vars.ts 注入的那条 `@media (max-width:560px)` 同一个断点。
|
||
*
|
||
* 为什么非要有这个 hook:控件高度有两条落地路径——
|
||
* 自绘区域读 CSS 变量(media query 自己会切档);
|
||
* antd 控件读 ThemeConfig 里的 JS 数字(media query 管不到)。
|
||
* 只在 CSS 侧切档的话,手机上一屏会同时出现 36/40(自绘)和 31/38(antd)两把尺子——
|
||
* 这是 430/560 首跑体检量出来的 61 项。所以断点要在 JS 里再判一次,把触摸档喂给 antd。
|
||
* 两边都从 metrics.breakpoints.narrow / metrics.controlHeight 取数,值不重复定义。
|
||
*/
|
||
import { useEffect, useState } from 'react'
|
||
import { breakpoints } from '../theme/metrics'
|
||
|
||
const QUERY = `(max-width:${breakpoints.narrow}px)`
|
||
|
||
/** 当前视口是否落在触摸档(≤560px)内 */
|
||
export function useNarrow(): boolean {
|
||
const [narrow, setNarrow] = useState(() => window.matchMedia(QUERY).matches)
|
||
useEffect(() => {
|
||
const mq = window.matchMedia(QUERY)
|
||
const sync = () => setNarrow(mq.matches)
|
||
sync()
|
||
mq.addEventListener('change', sync)
|
||
return () => mq.removeEventListener('change', sync)
|
||
}, [])
|
||
return narrow
|
||
}
|