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

react-dragula-hoc

v0.1.6

Published

A higher-order React component wrapper for Dragula, drag-and-drop.

Downloads

10

Readme

react-dragula-hoc

This is not production-ready. It probably contains bugs and the API will change.

A React Higher-Order Component wrapper for the Dragula drag-and-drop library.

react-dragula-hoc hides away the imperative details of the Dragula API and presents a more declarative API, inspired by ReactDnD. ReactDnD is extremely powerful but much more low-level.

Install

npm install --save react-dragula-hoc

[email protected] and [email protected] are listed as peerDependencies (i.e. they're expected to be in your project) but it will probably work with other versions.

Make a component draggable

import { dndElement } from 'react-dragula-hoc';

const Item = ({ text }) =>
  <li className="item">
    <span className="item__handle" />
    <span>This item has some text: {text}</span>
  </li>

const DraggableItem = dndElement({
  // name of the prop used to identify the container
  // defaults to 'id' if not specified:
  idProp: 'itemId',
  // an element with some type can only be dropped into
  // containers with a matching acceptType prop
  type: 'item'
})(Item);

Make a container that accepts draggable components

import { dndContainer } from 'react-dragula-hoc';

const Items = ({ children }) => <ul className="list__items">{children}</ul>;

const DropItems = dndContainer({
  // name of the prop used to identify the container
  // defaults to 'id' if not specified:
  idProp: 'listId',
  // the type of the container (any string):
  containerType: 'list',
  // the type that the container accepts (any string)
  // needs to match the 'type' of the DraggableItem
  acceptType: 'item',
  // optional, only start dragging if click is registered
  // on element with the provided className:
  handleClassName: 'item__handle',
  // optional, defaults to 'vertical'
  direction: 'vertical',
  // optional, if container is fixed height or width, dragging outside the container
  // will cause it to scroll its contents:
  scrollContainerAtBoundaries: true,
  // optional, set the speed of the scrolling (number from 0 to 1):
  containerScrollRate: 0.2
})(Items);

Use the container in your app

const ListOfItems = ({ listId, items, onChange }) =>
  // note: listId is specified here
  // because it is the idProp used in dndContainer
  <DropItems listId={listId} onChange={onChange} >
    {
      // render items as children inside the draggable container
      // (you could also render non-draggable things here...)
      items.map(
        // note: 'itemId' is specified here
        // because it is the idProp used in dndElement
        item => <Item item={item} itemId={item.id} key={item.id} />
      )
    }
  </DropItems>

An example onChange handler

// this fires whenever there is a drop even that results
// in a change of contents or order of contents within
// a container
const onChange = ({ source, target }) => {
  const sourceList = source.id; // the id of the source container
  const targetList = target.id; // the id of the target container (can be same as source)
  const sourceListItems = source.elements; // Array of ids in the source container ["item1", "item2" ...]
  const targetListItems = target.elements; // Array of ids in the target container

  // For example, update two lists in state (where state is keyed by list ID):
  this.setState({
    [sourceList]: {
      ...this.state[sourceList],
      items: sourceListItems
    },
    [targetList]: {
      ...this.state[targetList],
      items: targetListItems
    }
  });
};

TODOs

  • wrap the remaining Dragula options and events
  • decide on good public API
  • write unit tests
  • create an example app