/react-drag-and-drop

Implement the drag and drop feature without using external libraries.

Primary LanguageJavaScriptMIT LicenseMIT

Drag and Drop in React

Implement the drag and drop feature without using external libraries.

Preview

drag-and-drop

Getting Started

For help getting started with React, view online documentation.

2. Clone the repo

$ https://github.com/asifvora/react-drag-and-drop.git
$ cd react-drag-and-drop/

3. Install dependency

$ npm install

4. Run the app

$ npm start

5. Build the app

$ npm run build

View Demo

Demo

The key learning points are to:

  • Make an element draggable by adding the “draggable” attribute
  • Make an area droppable by implementing the “dragover” event
  • Capture the drag data by implementing the “dragstart” event
  • Capture the drop by implementing the “drop” event
  • Implement the “drag” event that is fired as the element is being dragged
  • Store the intermediate data in the dataTransfer object

Draggable functions

onDragStart = (ev, id) => {
    ev.dataTransfer.setData("id", id);
  }
onDrop = (ev, type) => {
    let id = parseInt(ev.dataTransfer.getData("id"));
  }
onDragOver = (ev) => {
    ev.preventDefault();
  }

Draggable div

<div onDragOver={(e) => this.onDragOver(e)} onDrop={(e) => this.onDrop(e, 'H')} >
   <ul >
      <li draggable onDragStart={(e) => this.onDragStart(e, 1)}>ABC</li>
   </ul>
</div>

Questions?🤔

Hit me on Twitter URL Medium LinkedIn Instagram

License

Copyright (c) Asif Vora

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.