@custom-react-hooks/use-lock-body-scroll

中文文档
1.5.1发布于 2024/08/18MITTypeScriptCommonJS零依赖近 30 天下载 1.5万

useLockBodyScroll is a React hook for controlling the scroll behavior of the body element in web applications. It's particularly useful for scenarios like opening modals or overlays where background scroll needs to be disabled.

原始使用文档
文档目录9 节
  1. Features
  2. Installation
  3. Installing Only Current Hooks
  4. Installing All Hooks
  5. Importing the Hook
  6. Usage
  7. API Reference
  8. Use Cases
  9. Contributing

useLockBodyScroll Hook

useLockBodyScroll is a React hook for controlling the scroll behavior of the body element in web applications. It's particularly useful for scenarios like opening modals or overlays where background scroll needs to be disabled.

Features

  • Conditional Scroll Lock: Allows you to conditionally enable or disable the body scroll.
  • Style Preservation: Preserves the original body overflow style and restores it upon unmounting.
  • Server-Side Rendering (SSR) Compatibility: Safe for use in SSR environments by checking for the document object.
  • Synchronous Execution: Uses useLayoutEffect for synchronous updates to the DOM.

Installation

Installing Only Current Hooks

npm install @custom-react-hooks/use-lock-body-scroll

or

yarn add @custom-react-hooks/use-lock-body-scroll

Installing All Hooks

npm install @custom-react-hooks/all

or

yarn add @custom-react-hooks/all

Importing the Hook

The useLockBodyScroll hook must be imported using a named import as shown below:

Named Import:

import { useLockBodyScroll } from '@custom-react-hooks/use-lock-body';

This approach ensures that the hook integrates seamlessly into your project, maintaining consistency and predictability in how you use our package.

Usage

import React, { useState } from 'react';
import { useLockBodyScroll } from '@custom-react-hooks/all';

const LockBodyScrollComponent= () => {
  const [isModalOpen, setIsModalOpen] = useState(false);
  useLockBodyScroll(isModalOpen);

  return (
    <div>
      <p>{isModalOpen ? 'Scroll hidden' : 'When you click on the button scroll will be hidden'}</p>
      <button onClick={() => setIsModalOpen(true)}>Open Modal</button>
      {isModalOpen && (
        <div className="modal">
          <div>
            <p>Modal Content</p>
            <button onClick={() => setIsModalOpen(false)}>Close Modal</button>
          </div>
        </div>
      )}
    </div>
  );
};

export default LockBodyScrollComponent;

In this example, the hook is used to lock the body scroll when the modal is open.

API Reference

  • lock: A boolean indicating whether to lock (true) or unlock (false) the body scroll.

Use Cases

  1. Modals and Popups: This is the most common use case. When a modal or popup is opened, it's often desirable to lock the background content to prevent the user from scrolling. This keeps the focus on the modal content.

  2. Full-Screen Menus: In mobile-responsive designs or certain desktop layouts, when a full-screen menu is opened, you might want to lock the scroll to keep the user's focus on the navigation options.

  3. Onboarding Flows or Guided Tours: If your application has a step-by-step onboarding process or a guided tour, you might want to lock scrolling to ensure the user follows the flow without distractions.

  4. Image or Video Galleries: When a user clicks on an image or video to view it in a larger format (like a lightbox), locking the scroll can enhance the viewing experience by removing background distractions.

  5. Infinite Scroll Prevention: In some scenarios, you might want to temporarily disable infinite scrolling features. For example, when a user performs an action that requires attention or confirmation, you could lock the scroll to keep them focused on the task.

  6. Interactive Games or Quizzes: If you're embedding games or quizzes in your web application, you might want to lock the background scroll when these elements are in focus to enhance user engagement and prevent accidental scroll-offs.

  7. Alerts or Critical Notifications: When displaying critical alerts or notifications that require immediate attention or action, disabling the background scroll can ensure that the user addresses these alerts without distraction.

  8. Complex Forms or Surveys: In a long, multi-step form or survey, you may want to lock the scroll when displaying validation errors or important information to ensure the user sees and acknowledges these messages.

Contributing

Your contributions to improve useLockBodyScroll are welcome. Feel free to submit issues or pull requests to the repository.

版本与发布记录

@custom-react-hooks/use-lock-body-scroll 共发布 8 个版本,首次发布于 2023/12/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.1latest2024/08/182 年前
1.5.02024/08/182 年前
1.4.192023/12/092 年前
1.4.182023/12/082 年前
1.4.162023/12/072 年前
1.4.152023/12/022 年前

关于 @custom-react-hooks/use-lock-body-scroll 的常见问题

@custom-react-hooks/use-lock-body-scroll 是什么?

useLockBodyScroll is a React hook for controlling the scroll behavior of the body element in web applications. It's particularly useful for scenarios like opening modals or overlays where background scroll needs to be disabled.

如何安装 @custom-react-hooks/use-lock-body-scroll?

在项目目录运行 npm install @custom-react-hooks/use-lock-body-scroll,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@custom-react-hooks/use-lock-body-scroll 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@custom-react-hooks/use-lock-body-scroll 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@custom-react-hooks/use-lock-body-scroll 可以商用吗?

@custom-react-hooks/use-lock-body-scroll 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@custom-react-hooks/use-lock-body-scroll 的下载量有多少?

根据 npm 官方下载统计,@custom-react-hooks/use-lock-body-scroll 在 2026/10/11 – 2026/11/09 这 30 天内被下载 14,770 次,日均约 492 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@custom-react-hooks/use-lock-body-scroll 还在维护吗?

最新版本 1.5.1 发布于 2024/08/18(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/12/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。