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 🙏

© 2025 – Pkg Stats / Ryan Hefner

dragdropcontainer

v0.0.1

Published

React-based drag-drop for dragging an element and dropping on a target

Downloads

4

Readme

DragDropContainer

A simple component for dragging an element and dropping it on a target.

Works on mouse and touch devices.

Setup

npm install

And if you want to look at the demo page:

npm start

Then point your browser at http://localhost:3002/

Approach

  • Wrap the draggable element in DragDropContainer.

  • Use properties to specify custom event names for the drop, enter, and leave events.

  • On target element(s), add listeners for the custom-named events, e.g. on the enter event you might highlight the target and then unhighlight on
    leave, and then of course you'll have some code to handle the drop event.

  • Alternatively, if you don't like the custom-event-name approach, add listeners for the default events (see below) and then use the event.details to make sure that this is a draggable item you're interested in.

Example

Drag container:

<h1>Drag the Bananas to the Monkey</h1>
<DragDropContainer
    dragData={ data about the bananas that you want to pass on to the monkey }
    dragEnterEventName="bananas_entered"
    dragLeaveEventName="bananas_left"
    dropEventName="bananas_dropped"
    >
    <img src="bananas.jpg" />
</DragDropContainer>

Target element(s)

And then in the target element's React code, get the DOM element and then add listeners to it. For example, lets assume the target has ref="monkey"...

  componentDidMount() {
    var elem = this.refs.monkey;
    elem.addEventListener('bananas_entered', this.highlight, false);
    elem.addEventListener('bananas_left', this.unhighlight, false);
    elem.addEventListener('bananas_dropped', (ev) => {this.dropOn(ev)}, false);
  }

The dragData is in the detail.data property of the event, so you might handle the drop event like this:

 dropOn(ev) {
   var newBananas = ev.detail.data.bananaCount;
   this.setState({numBananas: this.state.numBananas + newBananas});
 }

Also, the dragged element itself is referenced in the detail.dragElement property of the event. So for example if you wanted the bananas to disappear when they're dropped on the monkey, your dropOn method might look like this:

 dropOn(ev) {
   ev.detail.dragElement.style.display = 'none'
   alert('Thanks for the bananas. They were delicious!')
 }

Options

All passed as properties

dragData (required)

Data about the dragged item that you want to pass to the target. This gets inserted as event.detail.data for the enter, leave, and drop events.

dragEnterEventName, dragLeaveEventName, dropEventName

Custom names for the three events.

They default to "dragEnter", "dragLeave", and "dropOn", respectively

dragGhost

If a DOM node is provided, we'll drag it instead of the actual object (which will remain in place).

Example:

var ghost = <div class="drag_elem">Drag Me</div>;

<DragDropContainer dragGhost={ghost}>

returnToBase

Defaults to true. If false, then dragged item stays where you put it when you drop.

zIndex

The z-index for the dragged item defaults to 1000 (so that it floats over the target). If that doesn't work for you, change it here.