DN

Dnd

@hello-pangea/dnd

Beautiful and accessible drag and drop for lists with React

Tables
drag and drop
dnd
sortable
reorder
reorderable
react
react.js
natural
beautiful
accessible
Stars
4.0k
Weekly
2.0M
Open issues
144
Bundle
~25 kB

Installation

npm install @hello-pangea/dnd

Code example

import { DragDropContext, Droppable, Draggable } from "@hello-pangea/dnd";
import { useState } from "react";

export function Example() {
  const [items, setItems] = useState(["One", "Two", "Three"]);

  return (
    <DragDropContext
      onDragEnd={(result) => {
        if (!result.destination) return;
        const next = [...items];
        const [moved] = next.splice(result.source.index, 1);
        next.splice(result.destination.index, 0, moved);
        setItems(next);
      }}
    >
      <Droppable droppableId="list">
        {(provided) => (
          <ul ref={provided.innerRef} {...provided.droppableProps}>
            {items.map((item, index) => (
              <Draggable draggableId={item} index={index} key={item}>
                {(drag) => (
                  <li ref={drag.innerRef} {...drag.draggableProps} {...drag.dragHandleProps}>
                    {item}
                  </li>
                )}
              </Draggable>
            ))}
            {provided.placeholder}
          </ul>
        )}
      </Droppable>
    </DragDropContext>
  );
}

Pros & cons

Pros

  • Actively maintained by Alex Reardon
  • 2.0M weekly downloads. Battle tested
  • First-class TypeScript definitions
  • Apache-2.0 licensed

Considerations

  • Adds roughly ~25 kB to your bundle
  • Needs client-only rendering in SSR frameworks
  • Not supported in React Native
  • 144 open issues on GitHub

FAQs

Alternatives & similar libraries