Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@air/react-drag-to-select

Package Overview
Dependencies
Maintainers
19
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@air/react-drag-to-select

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

  • 5.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
19
Created
Source

React drag-to-select

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

size e2e unit size

✨ Features

  • 60 fps, tested thoroughly in 6x CPU slowdown for optimal performance
  • 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. 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.

Used by

MIT License

FAQs

Package last updated on 11 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc