vue-react-dnd
Vue Drag and Drop Library based on react-dnd.
vue-react-dnd has no dependency to React itself, since it only uses
dnd-core
as dependency.
Install
npm install --save vue-react-dnd
npm install --save react-dnd-html5-backend
Usage
Make sure you are familiar with react-dnd concepts. An overview of all the concepts can be found here: http://react-dnd.github.io/react-dnd/docs-overview.html
DragSource
To make a vue component draggable you need to add the DragSource
mixin, define the dragSource
configuration and add the v-dragSource
directive to the HTML.
<!-- Use the v-dragSource directive on the element you want to drag. The directive can anywhere in the HTML and must not be the root element-->Drag me
// 1. Import DragSource Mixin // ... // 2. Add DragSource Mixin to Component mixins: DragSource { return isDragging: false } // 3. Definine type function, specs object and collect function dragSource: // Required. Returns either a string, an ES6 symbol. // Only the drop targets registered for the same type will react to the // items produced by this drag source. // Read the react-dnd documentation to learn more about the items and types. { return ItemTypesBOX } // Required. A plain JavaScript object with a few allowed methods on it. // It describes how the drag source reacts to the drag and drop events. // Read the react-dnd documentation to learn more about the drag source specification. specs: { return name: 'Drag me' } { const item = monitor const dropResult = monitor if dropResult } // Optional. The collecting function receives two parameters: connect and monitor. // It can update a components internal state. // Read the react-dnd documentation an introduction to the monitors, the connectors. { thisisDragging = monitor } // ...
DropTarget
To make a vue component a drop traget you need to add the DropTarget
mixin, define the dropTarget
configuration and add a v-dropTarget
directive to the HTML.
<!-- Use the v-dropTarget directive on the element you want to have a drop traget. The directive can be anywhere in the HTML and must not be the root element--> Drag a box here
// 1. Import DropTarget Mixin // ... // 2. Add DropTarget Mixin to Component mixins: DropTarget { return isOver: false canDrop: false } computed: { return thiscanDrop && thisisOver } // 2. Definine type function, specs object and collect function dropTarget: // Required. Returns a string, an ES6 symbol, an array of either. // This drop target will only react to the items produced by the drag sources // of the specified type or types. // Read the react-dnd documentation to learn more about the items and types. { return ItemsTypesBOX } // Required. A plain JavaScript object with a few allowed methods on it. // It describes how the drop target reacts to the drag and drop events. // See the react-dnd documentaion where the drop target specification is // described in detail. specs: { return name: 'Dustbin' } // Optional. The collecting function receives two parameters: connect and monitor. // It can update a components internal state. // Read the react-dnd documentation an introduction to the monitors, the connectors. { thisisOver = monitor thiscanDrop = monitor } // ...
DragLayer
see: docs/examples/DragAround/CustomDragLayer/CustomDragLayer.vue
DragDropContext
Use the DragDropContext
mixin to add a Drag and Drop Backend to your
application.
// ... mixins: // ...
DragDropContextProvider
As alternative to the DragDropContext
use the
DragDropContextProvider
component to add a Drag and Drop Backend to
your application.
// ... components: DragDropContextProvider { return html5Backend: HTML5Backend } // ...
Differences to react-dnd
props
parameter
Specs function without The specs functions in vue-react-dnd
do not have the props
parameter, since all values of the component can be directly accessed
using this
.
Collect Function
In react-dnd
the collect function always has to return a plain
JavaScript Object that updates the internal state. In vue-react-dnd
the internal state can be set directly using this
. Therefore the
collect function is optional in vue-react-dnd
.
Examples
Examples can be found in the docs/examples
directory. Most of the
examples are reimplementations of the examples provided by
react-dnd. A live version of the examples can be found here:
https://jenshaase.github.io/vue-react-dnd
Development
Run examples: npm run dev
Lint code: npm run lint
Publish documentation to gh-pages: npm run publish:doc
Publish new version
npm run publish:doc
npm run build:lib
npm version <newversion>
npm publish