30
loading...
This website collects cookies to deliver better user experience
react-beautiful-dnd
, whilst it has great support and demos, I hit a few stumbling blocks along to way. It's footprint was also a hard sell to add into the application for a single use-case. I also encountered this issue whilst I was investigating a different use case where I wanted horizontal and vertical re-ordering in a grid.dnd kit is currently in beta. Issues and bugs should be expected for early releases. The core concepts are stable, but some of the APIs may change in the future.
dnd-kit
is to install the packages you need. The library is separated into small micro-libraries, which each provide additional utilities.@dnd-kit/core
(~11KB gzipped).dnd-kit
provides:@dnd-kit/sortable
- building blocks to build sortable interfaces .(~3KB gzipped)@dnd-kit/utilities
- dnd-kit utilities to help with integration. (~1.3KB gzipped)@dnd-kit/modifiers
- modifiers to change behavior of drag and drop, for example restricting the drag and drop movement to vertical or horizontal. (~3.4KB gzipped)@dnd-kit/accessibility
- Hooks to assist with accessibility for example screen reader announcements. (~499B gzipped)@dnd-kit/core
which is used inside of @dnd-kit/sortable
. Aside from adding to bloat, if this dependency is not de-duplicated the DndContext
provided by dnd-kit
will not work. If you run into this you can force it use the same copy of the core library, using an alias in your webpack.config.js.resolve: {
alias: {
// without this webpack dupes the dependencies when other dnd-kit libs use it which breaks context
"@dnd-kit/core": path.resolve(__dirname, "node_modules/@dnd-kit/core"),
}
}
DragOverlay
component, which you can use to render a custom react component to visualise your dragged item differently when it is being dragged, for example adding a drop shadow, which you'll notice in my sortable table example a little later. It also offers you the opportunity to change how the original rendered item is rendered when it is being dragged by using the isDragging
property from useSortable
(or useDraggable
), for example change it's opacity or color to help indicate the drop area to the user, this is also shown in my up-coming example.react-table
from their examples here: https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/basic and then added in drag and drop support, let's have a look how.dnd-kit
packages: @dnd-kit/core, @dnd-kit/modifiers, @dnd-kit/sortable, @dnd-kit/utilities.DndContext
and SortableContext
.SortableContext
to allow items to be sorted.useSortable
hook passing in the id of the row.DragOverlay
for a custom view of the row when dragging.isDragging
property, to act as a "slot" for the user to drop into.dnd-kit
and react-table
: https://codesandbox.io/s/react-table-drag-and-drop-sort-rows-with-dnd-kit-btpy9