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

drag-n-sort

v1.4.1

Published

A React component designed to easily allow elements to be dragged and dropped within one container for sorting purposes.

Downloads

22

Readme

Drag-N-Sort

Drag-N-Sort is a React DOM package designed to effortlessly add Drag and Drop functionality to both flex and grid containers, enabling innovative content sorting capabilities.

Drag-N-Sort Demo


Installation

To install Drag-N-Sort, simply run:

npm i drag-n-drop

in your project directory.

Usage

To implement Drag-N-Sort, utilize the Draggable component within your container div. Wrap your actual component with the Draggable component, ensuring uniformity of the section_id across all relevant Draggable objects.

<div className="flex relative gap-02" ref={ref}>
  {array.map((element, _index) => (
    <Draggable
      parent_ref={ref}
      onDrop={(id: number, direction: PlacementMarker) => console.log(id, direction)}
      section_id={"array_one"}
      vertical={false}
      drag_only_button={false}
      key={element.id}
    >
      <Component/>
    </Draggable>
  ))}
</div>

Upon rendering the page, users can click and hold to drag and drop items within the container (based on the children).

Differentiating Between Containers

To segregate different groups of Draggable elements, utilize the section_id parameter to group Draggable elements, ensuring they interact only with elements sharing the same key.

<div className="flex relative gap-02" ref={ref}>
  {array_of_array.map((element) => (
    {element.array.map((sub_element) => ( 
      <Draggable
        parent_ref={ref}
        onDrop={(id: number, direction: PlacementMarker) => console.log(id, direction)}
        section_id={element.key}
        vertical={false}
        drag_only_button={false}
        key={element.key}
      >
        <Component {...sub_element}/>
      </Draggable>
    )
  ))}
</div>

Changing Click Area

To customize the click area, utilize the drag_only_button and drag_button parameters. By default, the click area targets the top right of the Draggable object, though this will change in future versions.

<div className="flex relative gap-02" ref={ref}>
  {array.map((element, _index) => (
    <Draggable
      parent_ref={ref}
      onDrop={(id: number, direction: PlacementMarker) => console.log(id, direction)}
      section_id={"array_one"}
      vertical={false}
      drag_only_button={true}
      drag_button={<img src="/icons/move.svg" alt="drag" className="icon" draggable={false}/>}
      key={element.id}
    >
      <Component/>
    </Draggable>
  ))}
</div>

Orientation

Depending on the website layout, you may prefer vertical drag and drop functionality over traditional horizontal. Toggle this using the vertical parameter.

<div className="flex column relative gap-02" ref={ref}>
  {array.map((element, _index) => (
    <Draggable
      parent_ref={ref}
      onDrop={(id: number, direction: PlacementMarker) => console.log(id, direction)}
      section_id={"array_one"}
      vertical={true}
      drag_only_button={true}
      drag_button={<img src="/icons/move.svg" alt="drag" className="icon" draggable={false}/>}
      key={element.id}
    >
      <Component/>
    </Draggable>
  ))}
</div>

Types

PlacementMarker

This simple type is passed into the onDrop callback, describing the element's position, especially when vertical is set to true.

export type PlacementMarker = 'left' | 'right' | 'top' | 'bottom'

DraggableProps

DraggableProps is a simple prop type for the Draggable component.

export type DraggableProps = {
  children: ReactNode,
  parent_ref: RefObject<HTMLElement>,
  vertical?: boolean,
  onDrop?: (index: number, position: PlacementMarker) => void
  section_id?: string,
  index?: number
  drag_only_button?: boolean
  drag_button?: ReactNode
}

Features

  • Seamless integration with existing components and containers.
  • Efficient Drag and Drop functionality for sorting elements on a page.
  • Easy-to-use callback function.

Limitations

  • Not yet optimized for mobile (planned for future updates).
  • Right-clicking may cause tracking issues.
  • Drag button styling is not adjustable (planned for future updates).

Links