-
I'm using dnd-kit in my project which allows to delete / add items to a draggable list. Deleting an item would animate the items below upwards: Screen.Recording.2021-02-28.at.12.54.33.movAdding an item would animate the items below downwards: Screen.Recording.2021-02-28.at.13.06.45.movI dove into the code but am unsure how to approach this. Would appreciate some guidance on how this could be accomplished @clauderic. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 16 replies
This comment has been hidden.
This comment has been hidden.
-
For anyone stumbling on this discussion, animating insertion / deletion of items is now possible by combining the import {MeasuringStrategy} from '@dnd-kit/core';
const measuringConfig = {
droppable: {
strategy: MeasuringStrategy.Always,
}
};
function App() {
return (
<DndContext measuring={measuringConfig}>
{/* ... */}
</DndContext>
} import {useSortable, defaultAnimateLayoutChanges} from '@dnd-kit/sortable';
function animateLayoutChanges(args) {
const {isSorting, wasSorting} = args;
if (isSorting || wasSorting) {
return defaultAnimateLayoutChanges(args);
}
return true;
}
function Sortable(props) {
const {
attributes,
listeners,
setNodeRef,
transform,
transition,
} = useSortable({
animateLayoutChanges,
id: props.id,
});
const style = {
transition,
transform: CSS.Translate.toString(transform),
};
return (
<button ref={setNodeRef} style={style} {...listeners} {...attributes} />
)
} Kapture.2021-03-23.at.21.56.00.mp4Kapture.2021-03-22.at.10.31.28.mp4Refer to these stories to see it in action: https://5fc05e08a4a65d0021ae0bf2-mhplerkrjw.chromatic.com/?path=/story/examples-pages-layout--grid |
Beta Was this translation helpful? Give feedback.
-
"LayoutMeasuringStrategy" and "defaultAnimateLayoutChanges" are not found in @dnd-kit/core version 5.0.3 . Which version should I use? these are the packages I have installed
|
Beta Was this translation helpful? Give feedback.
For anyone stumbling on this discussion, animating insertion / deletion of items is now possible by combining the
layoutMeasuring
prop of<DndContext>
and theanimateLayoutChanges
argument ofuseSortable
: