Skip to content

Commit

Permalink
Merge pull request #1 from Lumen5/add-setTimeout-fallback
Browse files Browse the repository at this point in the history
Add set timeout fallback
  • Loading branch information
nigelgutzmann authored Sep 6, 2023
2 parents ae9bd77 + 93eb2b3 commit 73f355b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-flip-move",
"version": "3.0.5",
"name": "lumen5-react-flip-move",
"version": "3.0.6",
"description": "Effortless animation between DOM changes (eg. list reordering) using the FLIP technique.",
"main": "dist/react-flip-move.cjs.js",
"module": "dist/react-flip-move.es.js",
Expand Down
22 changes: 20 additions & 2 deletions src/FlipMove.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,17 @@ class FlipMove extends Component<ConvertedProps, FlipMoveState> {
});
});

this.bindTransitionEndHandler(child);
this.bindTransitionEndHandler(child, index);
}

bindTransitionEndHandler(child: ChildData) {
bindTransitionEndHandler(child: ChildData, index: number) {
const { domNode } = this.getChildData(getKey(child));
if (!domNode) {
return;
}

let called = false;
let timeoutId;
// The onFinish callback needs to be bound to the transitionEnd event.
// We also need to unbind it when the transition completes, so this ugly
// inline function is required (we need it here so it closes over
Expand All @@ -478,20 +480,36 @@ class FlipMove extends Component<ConvertedProps, FlipMoveState> {
// but on a nested transition (eg. a hover effect). Ignore these cases.
if (ev.target !== domNode) return;

called = true;

// Remove the 'transition' inline style we added. This is cleanup.
domNode.style.transition = '';

// Trigger any applicable onFinish/onFinishAll hooks
this.triggerFinishHooks(child, domNode);

domNode.removeEventListener(transitionEnd, transitionEndHandler);
window.clearTimeout(timeoutId);

if (child.leaving) {
this.removeChildData(getKey(child));
}
};

domNode.addEventListener(transitionEnd, transitionEndHandler);

let { delay, duration } = this.props;
const { staggerDurationBy, staggerDelayBy } = this.props;

delay += index * staggerDelayBy;
duration += index * staggerDurationBy;

timeoutId = window.setTimeout(() => {
if (!called) {
const event = new Event(transitionEnd);
domNode.dispatchEvent(event);
}
}, delay + duration);
}

triggerFinishHooks(child: ChildData, domNode: HTMLElement) {
Expand Down

0 comments on commit 73f355b

Please sign in to comment.