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

@badasukerubin/react-simple-drag-selection

v1.1.1

Published

A react library that provides an efficient and effective drag selection logic.

Downloads

5

Readme

react-simple-drag-selection

A simple and configurable React library that provides an efficient and effective drag selection logic.

Setup

  1. Install the repo

    npm install @badasukerubin/react-simple-drag-selection
  2. Import the component, hooks and wrap your component with the DragSelectionContext provider

    import {
      DragSelectionContext,
      SelectionBox,
      useDragSelection,
    } from "@badasukerubin/react-simple-drag-selection";
    
    ...
    
    const selectContainerRef = useRef<HTMLDivElement | null>(null);
    const selectBoxRef = useRef<HTMLDivElement | null>(null);
    const selection = useDragSelection({
      containerRef: selectContainerRef,
      boxRef: selectBoxRef,
    });
    
    <DragSelectionContext.Provider value={selection}>
      <div ref={selectContainerRef} className={`overflow-${axis}`}>
         <SelectionBox ref={selectBoxRef} />
    
         <Boxes boxes={boxes} />
       </div>
    </DragSelectionContext.Provider>;
  3. Use the useDragSelected hook to get the selection state in the component you want to use it in (Box).

    import {
      DragSelectionContext,
      useDragSelected,
    } from "@badasukerubin/react-simple-drag-selection";
    
    ...
    
    export default memo(function Box() {
      const [color] = useState(getRandomColor());
    
      const boxRef = useRef<HTMLDivElement | null>(null);
    
      const selection = useContext(DragSelectionContext);
      const isSelected = useDragSelected({
         elementRef: boxRef,
         selection
      });
    
      return (
         <div
         ref={boxRef}
         className="box"
         style={{ backgroundColor: isSelected ? "black" : color }}
         ></div>
      );
    });
    
     ...

Available props

  1. DragSelectionContext is the context provider that provides the selection state to the components that need it.

  2. SelectionBox is the selection box component that is rendered when the selection starts. You can customise this component by passing values for className, style or any other prop that you'd pass to a div element.

  3. useDragSelection has the following options:

    • containerRef: React ref to the container element
    • boxRef: React ref to the selection box element
    • onSelectionStart: Optional callback function that is called when the selection starts
    • onSelection: Optional callback function that is called when the selection changes
    • onSelectionEnd: Optional callback function that is called when the selection ends
  4. useDragSelected has the following options:

    • elementRef: React ref to the element that you want to check if it is selected
    • selection: The selection object that is provided by the DragSelectionContext
    • onSelected: Optional callback function that is called when the element is selected
  5. You can also ignore elements from having the selection logic interfere with their default behaviour (or that of their descendants) by adding the ignore-drag-selection class to them.

    <div className="ignore-drag-selection">
      <div className="box" draggable={true} ondragstart={dragFunction}></div>
    </div>

Example

A full example can be found in the example directory.

Roadmap

  • [ ] Unselect/Select using Shift and Meta keys
  • [ ] Add tests
  • [ ] Mobile/touch selection support