SPDX-License-Identifier: Apache-2.0
// Source: https://github.com/magicapple123/ResumeForge | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
// SPDX-License-Identifier: Apache-2.0
import { useCallback, useSyncExternalStore } from "react";
/**
* 订阅一条媒体查询。
*
* 用 `useSyncExternalStore` 而不是 `useState + useEffect`:媒体查询是典型的"外部状态源",
* 首帧必须**同步**读到真值,否则侧栏会先按展开态渲染一帧、再跳成折叠态(宽度闪一下)。
* 同一条路径也免掉了在 effect 里 setState 触发的 `react-hooks/set-state-in-effect` 告警。
*
* jsdom 兜底 false:测试环境的 `matchMedia` 桩恒返回 `matches: false`,等价于"宽屏、
* 侧栏展开"——这正是既有用例的前提,所以不需要为测试改断言。
*/
function isAvailable(): boolean {
return typeof window !== "undefined" && typeof window.matchMedia === "function";
}
export function useMediaQuery(query: string)
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key