@air/react-drag-to-select

中文文档
5.0.11发布于 2025/06/25MITTypeScriptESM + CJS近 30 天下载 17.8万

A performant React library which adds drag to select to your app

原始使用文档
文档目录7 节
  1. ✨ Features
  2. Install
  3. Usage
  4. useSelectionContainer arguments
  5. Selection styling
  6. Disabling selecting in certain areas
  7. Scrolling

React drag-to-select

A highly-performant React library which adds drag-to-select to your app.

size e2e unit size

✨ Features

  • Near 60 fps in 6x CPU slowdown on 2.3 GHz Quad-Core Intel Core i7
  • Simple API. It doesn't actually select items; just draws the selection box and passes you coordinates so you can determine that (we provided a utility to help though)
  • Fully built in TypeScript
  • Unit and e2e tested
  • Actively battle-tested in a production-scale application

Install

npm install --save @air/react-drag-to-select
yarn add @air/react-drag-to-select

Usage

import { useSelectionContainer } from '@air/react-drag-to-select'

const App = () => {
  const { DragSelection } = useSelectionContainer();

  return (
    <div>
      <DragSelection/>
      <div>Selectable element</div>
    </div>
  )
}

Check out this codesandbox for a complete working example: https://codesandbox.io/s/billowing-lake-rzhid4

useSelectionContainer arguments

NameRequiredTypeDefaultDescription
onSelectionStartNo() => voidMethod called when selection starts (mouse is down and moved)
onSelectionEndNo() => voidMethod called when selection ends (mouse is up)
onSelectionChangeYes(box: Box) => voidMethod called when selection moves
isEnabledNobooleantrueIf false, selection does not fire
eventsElementNoWindow, HTMLElement or nullwindowElement to listen mouse events
selectionPropsNoReact.HTMLAttributesProps of selection - you can pass style here as shown below
shouldStartSelectingNo() => booleanundefinedIf supplied, this callback is fired on mousedown and can be used to prevent selection from starting. This is useful when you want to prevent certain areas of your application from being able to be selected. Returning true will enable selection and returning false will prevent selection from starting.

Selection styling

To style the selection box, pass selectionProps: { style } prop:

  const { DragSelection } = useSelectionContainer({
    ...,
    selectionProps: {
      style: {
        border: '2px dashed purple',
        borderRadius: 4,
        backgroundColor: 'brown',
        opacity: 0.5,
      },
    },
  });

The default style for the selection box is

{
  border: '1px solid #4C85D8',
  background: 'rgba(155, 193, 239, 0.4)',
  position: `absolute`,
  zIndex: 99,
}

Disabling selecting in certain areas

Sometimes you want to disable a user being able to start selecting in a certain area. You can use the shouldStartSelecting prop for this.

const { DragSelection } = useSelectionContainer({
  shouldStartSelecting: (target) => {
    /**
     * In this example, we're preventing users from selecting in elements
     * that have a data-disableselect attribute on them or one of their parents
     */
    if (target instanceof HTMLElement) {
      let el = target;
      while (el.parentElement && !el.dataset.disableselect) {
        el = el.parentElement;
      }
      return el.dataset.disableselect !== "true";
    }

    /**
    * If the target doesn't exist, return false
    * This would most likely not happen. It's really a TS safety check
    */
    return false;
  }
});

See full example here: https://codesandbox.io/s/exciting-rubin-xxf6r0

Scrolling

Because we use the mouse position to calculate the selection box's coordinates, if your <DragSelection /> is inside of an area that scrolls, you'll need to make some adjustments on your end. Our library can't inherently know which parent is being scrolled nor of it's position inside of the scrolling parent (if there are other sibling elements above it).

How this is solved on your end is modifiying the left (for horizontal scrolling) and top (for vertical scrolling) of the selectionBox that is passed to handleSelectionChange. See the onSelectionChange in the example for an idea of how to do this.

MIT License

版本与发布记录

@air/react-drag-to-select 共发布 25 个版本,首次发布于 2021/10/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.11latest2025/06/251.4 年前
5.0.11-rc.22025/06/251.4 年前
5.0.11-rc.12025/06/251.4 年前
5.0.102024/08/142 年前
5.0.82023/06/153 年前
5.0.62023/04/173 年前

关于 @air/react-drag-to-select 的常见问题

@air/react-drag-to-select 是什么?

A performant React library which adds drag to select to your app

如何安装 @air/react-drag-to-select?

在项目目录运行 npm install @air/react-drag-to-select,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(16 - 19)、react-dom(16 - 19)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@air/react-drag-to-select 支持 TypeScript 吗?

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

@air/react-drag-to-select 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@air/react-drag-to-select 可以商用吗?

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

@air/react-drag-to-select 的下载量有多少?

根据 npm 官方下载统计,@air/react-drag-to-select 在 2026/10/05 – 2026/11/03 这 30 天内被下载 178,065 次,日均约 5,936 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@air/react-drag-to-select 还在维护吗?

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