scroll

React drag and drop framework with inbuilt virtualizing scrollbars

May 10, 2019
React drag and drop framework with inbuilt virtualizing scrollbars

react-virtualized-dnd

react-virtualized-dnd is a React-based, fully virtualized drag-and-drop framework, enabling the the cross over of great user interaction and great performance. This project was made in response to the large amount of issues experienced trying to use virtualization libraries together with drag and drop libraries - react-virtualized-dnd does it all for you!

View Demo Download Source

Install

npm install --save react-virtualized-dnd

Usage

React-Virtualized-DnD utilizes a three part abstraction for drag and drop:

  • A DragDropContext, which controls the overall flow and events of the drag and drop.
  • Draggables, which are wrappers for the elements you want to drag around.
  • Droppables, which indicate a drop zone that Draggables can be dropped on, and create the virtualizing container.
    Draggables and Droppables can be organized in groups.

Droppables use an internal scrollbar to virtualize its children, and the DragDropContext offers the option to include a horizontal scrollbar.

React-virtualized-dnd places a placeholder in droppables during drag, which is placed after the draggable element hovered over during drag. The placeholderId represents the id of the element it was placed after.
On drag end, the DragDropContext returns the placeholderId.

Example code can be seen below. A live example can be found at: https://forecast-it.github.io/react-virtualized-dnd/

import React, {Component} from 'react'; import ExampleBoard from 'react-virtualized-dnd'; class Example extends Component { render() { const name = 'my-group'; const elemsToRender = [... your data here ...]; return ( <DragDropContext dragAndDropGroup={name} onDragEnd={this.onDragEnd.bind(this)} horizontalScroll={true}> <div className={'your-drag-container'}> {elemsToRender.map((elem, index) => ( <div className={'your-droppable-container'}> <Droppable dragAndDropGroup={name} droppableId={elem.droppableId} key={elem.droppableId}> {elem.items.map(item => ( <Draggable dragAndDropGroup={name} draggableId={item.id}> <div className='your-draggable-element'> <p> {item.name} </p> </div> </Draggable> ))} </Droppable> </div> ))} </div> </DragDropContext> ); } }

Documentation & API

DragDropContext

Props

PropTypeRequiredDescription
dragAndDropGroupstringyesUnique identifier for the drag and drop group the context uses
outerScrollBarbooleannoEnables or disables global outer scrolling of the context (triggered by dragging)
scrollYSpeednumbernoCustom scroll speed for global page scrolling (y-axis)
scrollXSpeednumbernoCustom scroll speed for global page scrolling (x-axis)
scrollContainerHeightnumberno, yes with outerScrollBarHeight of the outer scrollable container
scrollContainerMinHeightnumbernoMinimum height of the outer scrollable container
onDragEndfunctionnoFunction fired on drag end with the source object, the droppableId of the destination, and the ID of the placeholder dropped on as params
onDragCancelfunctionnoFunction fired on drag end if the drop did not occur inside a droppable with the draggableId of the dragged element as params
onDragStartfunctionnoFunction fired on drag start with the draggableId of the dragged element as params

The placeholder ID can be used to determine where to place the dragged element on drag end. The placeholderID returns the string "END_OF_LIST" if dragged below the last element of a droppable.

Draggable

Props

PropTypeRequiredDescription
dragAndDropGroupstringyesUnique identifier for the drag and drop group the context uses
draggableIdstringyesUnique identifier for the draggable
dragActiveClassstringnoCSS class applied to a draggable element during an active drag
dragDisabledboolnoFlag to disabled dragging of element

Draggables will ignore drags started by clicking on any element with the "no-drag" css class. This can be used to control drag interactions with interactive elements, such as inputs or buttons.

Droppable

Props

PropTypeRequiredDescription
dragAndDropGroupstringyesUnique identifier for the drag and drop group the context uses
droppableIdstringyesUnique identifier for the droppable
placeholderStyleobjectnoStyle object for placeholder. If included, remember to include placeholder height.
containerHeightnumberyesHeight of the virtualizing scroll container
containerMinHeightnumbernoMinimum height of the virtualizing scroll container (if larger than total height of elems)
rowHeightnumbernoHeight of each row with borders. Default is 50px.
disableScrollbooleannoFlag to disable scrollbars. This disables virtualization as well
listHeaderHTML elementnoElement to use as header for a droppable list, to react to drops on top of the list.
listHeaderHeightNumberno (yes with listHeader)Height of the header element, necessary for calculations.
activeHeaderClassstringnoCSS class added to the header when an active drag is hovering over the list header
hideListbooleannoHides (unrenders) all droppable elements in the list
dynamicElemHeightbooleannoFlag to indicate differing/dynamicly changing heights of children elements.*

*Enabling dynamic element height currently disables virtualization, but allows usage of the framework for DnD/Autoscroll purposes. Virtualization for dynamicly sized elements will come in a future update.

GitHub

Recommended