Design a React drag-and-drop interface with dnd-kit combined with a virtual list. Handle reordering items that aren't currently rendered in the DOM due to scrolling.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Designing drag-and-drop with virtual list handling
The plan uses react-window's FixedSizeList wrapped in dnd-kit's SortableContext, combining useSortable for items and a pointer sensor to track drag moves. To handle reordering items outside the viewport (not in the DOM), it computes the target index based on the draggable item's bounding rect relative to the scroll container on drag move or end, bypassing reliance on visible droppables. This enables smooth reordering via manual index calculation and array reordering even when drop targets aren't rendered.Finalizing virtualized drag-and-drop code
The code finalizes a React component combining dnd-kit and react-window to support drag-and-drop sorting with virtualization. It handles drops outside rendered items by computing the target index from the draggable item's bounding rect relative to the scroll container. The drag move event updates a projected index used as fallback when the drop target isn't in the DOM. This approach ensures reliable reordering across the full list despite virtualization's limited DOM presence.
A virtual list only mounts the rows that are in view, so over will be null when you drop on an item that is not rendered. The trick is to derive the target index from the pointer position + scroll offset instead of relying on a DOM Droppable.
Below is a minimal example using dnd-kit + react-window with fixed‑height rows. When over is null we compute the index from the drag rectangle and the list’s scroll position, so you can reorder to any position in the list even if the target row isn’t mounted.
Only the visible rows are mounted by react-window, each wrapped in useSortable.
When you drag over a visible row, over is set normally and reordering works as usual.
When you drag beyond the rendered range (over is null), handleDragMove computes a “virtual” target index using the pointer’s clientY (or the drag rect) plus the list’s scroll offset: index = floor((y + scrollTop) / ROW_HEIGHT).
On drop we use over.id if present, otherwise the computed virtualOverIndex, so you can drop anywhere in the list, even on rows that are not currently rendered.
Add styling/indicators as you like (e.g. render a placeholder at virtualOverIndex) to give users feedback while dragging.