
Product
Socket Now Protects the Chrome Extension Ecosystem
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
@hello-pangea/dnd
Advanced tools
@hello-pangea/dnd is a powerful and flexible drag-and-drop library for React. It allows developers to create complex drag-and-drop interfaces with ease, supporting features like reordering lists, moving items between lists, and more.
Reordering a List
This code demonstrates how to reorder items in a list using @hello-pangea/dnd. It sets up a drag-and-drop context, defines a reorder function, and handles the drag end event to update the state with the reordered items.
import React from 'react';
import { DragDropContext, Droppable, Draggable } from '@hello-pangea/dnd';
const reorder = (list, startIndex, endIndex) => {
const result = Array.from(list);
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
return result;
};
const App = () => {
const [items, setItems] = React.useState(['Item 1', 'Item 2', 'Item 3']);
const onDragEnd = (result) => {
if (!result.destination) return;
const reorderedItems = reorder(items, result.source.index, result.destination.index);
setItems(reorderedItems);
};
return (
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="droppable">
{(provided) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
{items.map((item, index) => (
<Draggable key={item} draggableId={item} index={index}>
{(provided) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
{item}
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
);
};
export default App;
Moving Items Between Lists
This code demonstrates how to move items between two lists using @hello-pangea/dnd. It sets up a drag-and-drop context, defines a move function, and handles the drag end event to update the state with the moved items.
import React from 'react';
import { DragDropContext, Droppable, Draggable } from '@hello-pangea/dnd';
const move = (source, destination, droppableSource, droppableDestination) => {
const sourceClone = Array.from(source);
const destClone = Array.from(destination);
const [removed] = sourceClone.splice(droppableSource.index, 1);
destClone.splice(droppableDestination.index, 0, removed);
const result = {};
result[droppableSource.droppableId] = sourceClone;
result[droppableDestination.droppableId] = destClone;
return result;
};
const App = () => {
const [state, setState] = React.useState({
droppable1: ['Item 1', 'Item 2', 'Item 3'],
droppable2: ['Item 4', 'Item 5', 'Item 6']
});
const onDragEnd = (result) => {
const { source, destination } = result;
if (!destination) return;
if (source.droppableId === destination.droppableId) return;
const movedItems = move(
state[source.droppableId],
state[destination.droppableId],
source,
destination
);
setState({
...state,
...movedItems
});
};
return (
<DragDropContext onDragEnd={onDragEnd}>
{Object.keys(state).map((listId) => (
<Droppable key={listId} droppableId={listId}>
{(provided) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
{state[listId].map((item, index) => (
<Draggable key={item} draggableId={item} index={index}>
{(provided) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
{item}
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
))}
</DragDropContext>
);
};
export default App;
react-beautiful-dnd is a popular drag-and-drop library for React, created by Atlassian. It offers a similar API and functionality to @hello-pangea/dnd, including reordering lists and moving items between lists. However, @hello-pangea/dnd is a fork of react-beautiful-dnd and may include additional features or improvements.
react-dnd is a flexible drag-and-drop library for React, built on top of the HTML5 drag-and-drop API. It provides a more customizable and lower-level API compared to @hello-pangea/dnd, making it suitable for more complex drag-and-drop interactions. However, it requires more setup and boilerplate code.
dnd-kit is a modern drag-and-drop toolkit for React. It offers a highly customizable and extensible API, with support for advanced features like multi-drag and nested drag-and-drop. Compared to @hello-pangea/dnd, dnd-kit provides more flexibility and control over the drag-and-drop interactions, but may require more effort to implement.
Alex Reardon has created a free course on egghead.io
๐ฅ (using react-beautiful-dnd) to help you get started with @hello-pangea/dnd
as quickly as possible.
<table>
reordering - table pattern<Draggable />
@atlaskit/tree
package<Droppable />
list can be a scroll container (without a scrollable parent) or be the child of a scroll container (that also does not have a scrollable parent)@hello-pangea/dnd
exists to create beautiful drag and drop for lists that anyone can use - even people who cannot see. For a good overview of the history and motivations of the project you can take a look at these external resources:
There are a lot of libraries out there that allow for drag and drop interactions within React. Most notable of these is the amazing react-dnd
. It does an incredible job at providing a great set of drag and drop primitives which work especially well with the wildly inconsistent html5 drag and drop feature. @hello-pangea/dnd
is a higher level abstraction specifically built for lists (vertical, horizontal, movement between lists, nested lists and so on). Within that subset of functionality @hello-pangea/dnd
offers a powerful, natural and beautiful drag and drop experience. However, it does not provide the breadth of functionality offered by react-dnd
. One shortcoming is that grid layouts are not supported (yet). So @hello-pangea/dnd
might not be for you depending on what your use case is.
The ways in which somebody can start and control a drag
<DragDropContext />
- Wraps the part of your application you want to have drag and drop enabled for<Droppable />
- An area that can be dropped into. Contains <Draggable />
s<Draggable />
- What can be dragged around<DragDropContext />
responders - onDragStart
, onDragUpdate
, onDragEnd
and onBeforeDragStart
<Draggable />
sinnerRef
draggableId
and droppableId
sdoctype
TypeScript
: type information<svg>
s@hello-pangea/dnd
<Draggable />
s during a drag (11.x behaviour) - โ ๏ธ Advanced<Draggable />
- Using our cloning API or your own portalAlex Reardon @alexandereardon
Alex is no longer personally maintaning this project. The other wonderful maintainers are carrying this project forward.
react-beautiful-dnd
. Atlassian is no longer involved with this project.Thanks to Chromatic for providing the visual testing platform that helps us review UI changes and catch visual regressions.
FAQs
Beautiful and accessible drag and drop for lists with React
The npm package @hello-pangea/dnd receives a total of 746,793 weekly downloads. As such, @hello-pangea/dnd popularity was classified as popular.
We found that @hello-pangea/dnd demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.ย It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.