• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Drag and drop with react-native made simple

Build Status NPM Version MIT license

React native easy DnD Demo

Installation

npm install --save react-native-easy-dnd

# or using yarn

yarn add react-native-easy-dnd

Usage

First, you need to import createDndContext. This function creates the context for storing the data for the draggable and droppable child components.

import { createDndContext } from "react-native-easy-dnd";

const { Provider, Droppable, Draggable } = createDndContext();

Provider

Wrap the part of your application that you want to enable drag and drop for inside Provider.

<Provider>
   <View>
      {/*  */}
   </View>
</Provider>

Draggable

Add a Draggable component with a function as a child. The element that you want to make draggable needs to be Animated.View whose props must extend viewProps passed in by the render prop function.

import {Animated} from 'react-native';

// ...
<Draggable
  onDragStart={() => {
    console.log('Started draggging');
  }}
  onDragEnd={() => {
    console.log('Ended draggging');
  }}
  payload="my-draggable-item"
>
  {({ viewProps }) => {
    return (
      <Animated.View
        {...viewProps}
        style={[viewProps.style, { width: 200, height: 200, backgroundColor: "red" }]}
      >
        <Text style={{ color: "#fff", fontWeight: "bold" }}>
          Drag me
        </Text>
      </Animated.View>
    );
  }}
</Draggable>

Props

Prop Type Description
onDragStart Function Callback that is triggerd when user starts dragging the draggable element
onDragStart Function Callback that is triggerd when user ends dragging the draggable element
payload any An arbitrary value (often) unique to this draggable that can later be used to determine which draggable item was dropped onto a droppable

Droppable

Add a Droppable component with a function as a child. Similarly, the element that you want to make droppable needs to be Animated.View whose props must extend viewProps passed in by the render prop function.

import {Animated} from 'react-native';

// ...

<Droppable
  onEnter={() => {
    console.log('Draggable entered');
  }}
  onLeave={() => {
    console.log('Draggable left');
  }}
  onDrop={({ payload }) => {
    console.log('Draggable with the following payload was dropped', payload);
  }}
>
  {({ active, viewProps }) => {
    return (
      <Animated.View
        {...viewProps}
        style={[
          {
            width: 300,
            height: 200,
            backgroundColor: active
              ? "blue"
              : "green"
          },
          viewProps.style,
        ]}
      >
        <Text style={{ fontWeight: "bold", color: "white" }}>Drop here</Text>
      </Animated.View>
    );
  }}
</Droppable>

Props

Prop Type Description
onEnter Function Callback that is triggerd when a draggable enters the droppable area
onLeave Function Callback that is triggerd when a draggable leaves the droppable area
onDrop Function Callback that is triggerd when a draggable is dropped onto the droppable area

Fun Fact!

I wrote most of the code on a flight from Toronto to St. John's in March 2019. ✈

License

Licensed under the MIT License, Copyright © 2019 Mohamad Mohebifar.

More Repositories

1

react-native-copilot

Step-by-step walkthrough tooltip for your react native app
TypeScript
2,002
star
2

made-in-iran

A list of cool projects made in Iran
TypeScript
833
star
3

vidact

A compiler that converts React-compatible codes to VanillaJS with no Virtual DOM
TypeScript
764
star
4

konsul

A react renderer for browser's dev console
JavaScript
615
star
5

grafgiti

Vandalize your github contributions wall.
JavaScript
412
star
6

react-native-loader

A collection of animated spinners for React Native
JavaScript
393
star
7

racket

A complete starting app for developing universal React/Redux web apps with generators, best practices and more
JavaScript
290
star
8

angular-persian

🌏 Persian tools for angular.js
CoffeeScript
98
star
9

lithree.js

🔮 Lightweight 3D WebGL framework written in ES6
JavaScript
42
star
10

react-native-ximage

An alternative to react native's Image component with cache support.
JavaScript
31
star
11

react-use-context-selector

Context selector hook for React
TypeScript
29
star
12

openbabel-node

OpenBabel Bindings for Node.js
C++
26
star
13

php-jalali-extension

A php extension for Jalali dates
Shell
20
star
14

DateTimeBundle

A Symfony2 Bundle to format DateTime to string according to a Calendar and a Datepicker FormType.
JavaScript
16
star
15

telegram-vanilla-client

TypeScript
15
star
16

react-markdown-native

Render Markdown as React native components
JavaScript
14
star
17

volley

A Web-Based Volleyball game
JavaScript
12
star
18

mol3d

A chemical visualization tool on top of WebGL
JavaScript
10
star
19

Razi

A new scientific chemistry library for java.
Java
10
star
20

pasoor

♠️ A simple API for game cards.
JavaScript
9
star
21

molcanvas.js

A WebGL molecule renderer and editor
JavaScript
7
star
22

goron

Yet another graphical database management tool [WIP]
JavaScript
7
star
23

muzik

A simple music player made with react. I made it for an "interview" test but never joined the company.
JavaScript
7
star
24

vscode-shopify-i18n

TypeScript
6
star
25

shelem

♥️ Shelem (a perisan card game) made with react native
JavaScript
6
star
26

mohebifar.ir

Mohamad Mohebifar's blog
HTML
4
star
27

openmm-transformer

A cleaner way to manage custom forces in OpenMM
Python
4
star
28

muniranians

JavaScript
4
star
29

GayPI

:octocat: Isomorphic JavaScript API wrapper around the Github API v3.
JavaScript
4
star
30

truck-cli

JavaScript
3
star
31

openmm-buckingham-plugin

C++
3
star
32

chem.js

A molecular modeling JavaScript framework.
JavaScript
3
star
33

CalendarBundle

A Symfony2 Bundle to show times with any type of calendar (Persian هجری, Gregorian, ...)
PHP
2
star
34

fix-pdb

A simple script for fixing broken PDB files
Python
2
star
35

pybel-orca

Python code around pybel to write inputs and read outputs of the ab initio package, ORCA
Python
2
star
36

cssTransit

Javascript CSS Transition
JavaScript
1
star
37

react-konsole

1
star
38

node-parsi-payment

A Node.js module providing access to the Iran's Shetab Payment API
JavaScript
1
star
39

smirnoff

The SMIRks Native Open Force Field (SMIRNOFF)
Jupyter Notebook
1
star
40

janshop

Simple RESTful django content management framework
HTML
1
star