"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]);
}
