Skip to content

Commit

Permalink
fix: Use setState callback in finishDrop to avoid occasional flicker
Browse files Browse the repository at this point in the history
  • Loading branch information
lyxell committed Jan 30, 2025
1 parent a53fc88 commit 7d6e909
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-poems-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-movable": patch
---

Use setState callback in finishDrop to avoid occasional flicker
48 changes: 25 additions & 23 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -484,31 +484,33 @@ class List<Value = string> extends React.Component<IProps<Value>> {
? -1
: Math.max(this.afterIndex, 0)
: oldIndex;
if (removeItem || hasChanged) {
this.props.onChange({
oldIndex,
newIndex,
targetRect: this.ghostRef.current!.getBoundingClientRect(),
});
}
this.props.afterDrag &&
this.props.afterDrag({
elements: this.getChildren(),
oldIndex,
newIndex,
const targetRect = this.ghostRef.current!.getBoundingClientRect();
this.setState({ itemDragged: -1, scrollingSpeed: 0 }, () => {
if (removeItem || hasChanged) {
this.props.onChange({
oldIndex,
newIndex,
targetRect: targetRect,
});
}
this.props.afterDrag &&
this.props.afterDrag({
elements: this.getChildren(),
oldIndex,
newIndex,
});
this.getChildren().forEach((item) => {
setItemTransition(item, 0);
transformItem(item, null);
(item as HTMLElement).style.touchAction = "";
});
this.getChildren().forEach((item) => {
setItemTransition(item, 0);
transformItem(item, null);
(item as HTMLElement).style.touchAction = "";
this.afterIndex = -2;
// sometimes the scroll gets messed up after the drop, fix:
if (this.lastScroll > 0) {
this.listRef.current!.scrollTop = this.lastScroll;
this.lastScroll = 0;
}
});
this.setState({ itemDragged: -1, scrollingSpeed: 0 });
this.afterIndex = -2;
// sometimes the scroll gets messed up after the drop, fix:
if (this.lastScroll > 0) {
this.listRef.current!.scrollTop = this.lastScroll;
this.lastScroll = 0;
}
};

onKeyDown = (e: React.KeyboardEvent) => {
Expand Down

0 comments on commit 7d6e909

Please sign in to comment.