百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-drag-to-select

> 前端框架
开源

一个添加拖动选择功能的 React 库,可用于您的应用程序

194 stars0 点赞5 次浏览
访问官网GitHub

工具介绍

一个添加拖动选择功能的 React 库,可用于您的应用程序

React drag-to-select

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

✨ 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

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

Usage

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

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

  return (
    

    
  )
}

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

useSelectionContainer arguments

Name Required Type Default Description
onSelectionStart No () => void Method called when selection starts (mouse is down and moved)
onSelectionEnd No () => void Method called when selection ends (mouse is up)
onSelectionChange Yes (box: Box) => void Method called when selection moves
isEnabled No boolean true If false, selection does not fire
eventsElement No Window, HTMLElement or null window Element to listen mouse events
selectionProps No React.HTMLAttributes Props of selection - you can pass style here as shown below
shouldStartSelecting No () => boolean undefined If 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:

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

The default style for the selection box is

typescript
{
  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.

…

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 `` 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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptdragreactselect

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架