1.0.36 • Published 1 year ago

react-dnd-lite v1.0.36

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

React DND Lite

Add drag and drop feature in your react app with minimal configuration using react-dnd-lite.

Installation

Using NPM

$ npm install react-dnd-lite

Using YARN

$ yarn add react-dnd-lite

Basic concepts

DNDContainer - It is a wrapping element that handles all the drag and drop state management and give access to onDrop event. It internally uses react createContext api. Wrap your parent component inside the DNDContainer like so

App.tsx

import { DNDContainer, IElementDrop, IElementDragging, IElementDraggingOver } from 'react-dnd-lite'
import { Test } from './components/Test'

function App() { const onDrop = (e: IElementDrop) => console.log(e) const onDragStart = (e: IElementDragging) => console.log(e) const onDragOver = (e: IElementDraggingOver) => console.log(e)

return ( ) }

export default App

|API|Type|Required|Example|
|---|----|--------|-------|
|onDrop|function|`false`| (e: IElementDrop) => console.log(e) |
|onDragStart|function|`false`| (e: IElementDragging) => console.log(e) |
|onDragOver|function|`false`| (e: IElementDraggingOver) => console.log(e) |

<br>

**DNDItem** - It handles all the magic. <code>DNDItem</code> converts the children to a draggable item.

> Test.tsx
```js
import { DNDItem } from "react-dnd-lite"

export const Test = () => {
  return (
    <div className="container">
      <DNDItem id="1">
        <div className="box box-1">
          <span>Box 1</span>
        </div>
      </DNDItem>
      <DNDItem id="2">
        <div className="box box-2">
          <span>Box 2</span>
        </div>
      </DNDItem>
    </div>
  )
}

*Note: DNDItem accepts only HTMLElement as direct children. It may not work properly for React component or React Fragment as direct children.

APITypeRequiredExample
idstringtrueid="some-id-1"
isDraggablebooleanfalseisDraggable={true}

Advance features

DNDHandler - It allows user to add drag handler to DNDItem. Wrap your handler element with this component like so...

Test.tsx

import { DNDHandler, DNDItem } from "react-dnd-lite"
import DragIcon from '../assets/DragIcon.svg'

export const Test = () => { return (

<div className="container">
  <DNDItem id="1">
    <div className="box box-1">
      Box 1

      <DNDHandler id="1">
        <img src={DragIcon} className="drag-icon"/>
      </DNDHandler>
    </div>
  </DNDItem>
  <DNDItem id="2">
    <div className="box box-2">
      Box 2
    </div>
  </DNDItem>
</div>

) }

![](/public/DNDHandler.gif)
> <sup>*</sup>**Note:** DNDHandler accepts only HTMLElement as direct children. It may not work properly for React component or React Fragment as direct children.

<br>

|API|Type|Required|Example|Note|
|---|----|--------|-------|----|
|id|string|`true`| id="some-id-1"|Please note that id must be same with the DNDItem's id|

<br>

**DNDIndicator** - It allows user to add drop indicator to DNDItem. Place DNDIndicator component inside the DNDItem like so...
```js
import { DNDHandler, DNDIndicator, DNDItem } from "react-dnd-lite"
import DragIcon from '../assets/DragIcon.svg'

export const Test = () => {
  return (
    <div className="container">
      <DNDItem id="1">
        <div className="box box-1">
          Box 1

          <DNDHandler id="1">
            <img src={DragIcon} className="drag-icon"/>
          </DNDHandler>
        </div>
      </DNDItem>
      <DNDItem id="2">
        <div className="box box-2">
          Box 2

          <DNDIndicator position="left" id="2" />
          <DNDIndicator position="right" id="2" />
        </div>
      </DNDItem>
    </div>
  )
}

npm.io

APITypeRequiredExampleNote
idstringtrueid="some-id-1"Please note that id must be same with the DNDItem's id
positionstringtrueposition="left"Available options are left, right, bottom, top and element, Default is element
classNamestringfalseclassName="my-dnd-indicator"
styleObjectfalsestyle={{width: '20px', background: 'red'}}
hoveredStyleObjectfalsestyle={{width: '30px', background: 'green'}}
1.0.36

1 year ago

1.0.35

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.34

1 year ago

1.0.19

1 year ago

1.0.2

1 year ago

1.0.18

1 year ago

1.0.1

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.0

1 year ago