Files
writer-work-flow/apps/web/lib/ui/useBodyScrollLock.ts

21 lines
570 B
TypeScript
Raw Permalink 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.

"use client";
import { useEffect } from "react";
// 弹层Drawer / CommandPalette / Modal打开时锁定 body 滚动,关闭后恢复原值。
// SSR 守卫:服务端无 document直接跳过。
export function useBodyScrollLock(active: boolean): void {
useEffect(() => {
if (!active) return;
if (typeof document === "undefined") return;
const { body } = document;
const previousOverflow = body.style.overflow;
body.style.overflow = "hidden";
return () => {
body.style.overflow = previousOverflow;
};
}, [active]);
}