react-dragula

repository·master·Indexed 21 days ago

https://github.com/bevacqua/react-dragula

A React wrapper for the dragula library that provides simple drag-and-drop capabilities. It includes the reactDragula function to initialize dragula instances while automatically cleaning up React-specific metadata from cloned elements to prevent interference with drag-and-drop behavior.

Tokens
858
Snippets
4
Records
4
Agent score
27%

What's inside react-dragula

  1. Use react-dragula with React components

    master

    The react-dragula API is identical to the original dragula API, with minor adjustments to ensure compatibility with React. To use it, you pass an array of DOM elements (containers) to the dragula function. These containers define the areas where items can be dragged and dropped.

    var React = require('react');
    var dragula = require('react-dragula');
    var App = React.createClass({
      render: function () {
        return <div className='container'>
          <div>Item 1</div>
          <div>Item 2</div>
        </div>;
      },
      componentDidMount: function () {
        var container = React.findDOMNode(this);
        dragula([container]);
      }
    });
  2. Implement react-dragula using ES2015 refs

    master

    For modern React applications, you can use the ref callback attribute to initialize Dragula. This allows you to capture the underlying DOM element (the componentBackingInstance) and pass it to the Dragula function along with an options object.

    import * as React from "react";
    import * as ReactDOM from 'react-dom';
    import Dragula from 'react-dragula';
    
    export class App extends React.Component {
      render () {
        return <div className='container' ref={this.dragulaDecorator}>
          <div>Item 1</div>
          <div>Item 2</div>
        </div>;
      },
      dragulaDecorator = (componentBackingInstance) => {
        if (componentBackingInstance) {
          let options = { };
          Dragula([componentBackingInstance], options);
        }
      };
    }
    
    ReactDOM.render(<App />, document.getElementById('examples'));
  3. Use reactDragula to wrap dragula in React

    master

    The reactDragula function acts as a wrapper for the dragula library. It initializes a dragula instance while automatically cleaning up React-specific metadata from cloned elements. This prevents React's internal data-reactid attributes from interfering with the drag-and-drop behavior when elements are cloned during a drag operation.

    To use it, call reactDragula() with the same arguments you would pass to the standard dragula() function.

    var reactDragula = require('react-dragula');
    
    // Initialize dragula using the wrapper
    var drake = reactDragula(container1, container2, options);