npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@air/react-drag-to-select

v5.0.10

Published

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

Downloads

31,339

Readme

✨ 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

|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:

  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.