Skip to content

Commit 68a11e5

Browse files
committed
Adjust range start/end based on the duration and delay of the animation
1 parent 6377903 commit 68a11e5

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

+35-10
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,7 @@ function animateGesture(
18761876
// keyframe. Otherwise it applies to every keyframe.
18771877
moveOldFrameIntoViewport(keyframes[0]);
18781878
}
1879+
// TODO: Reverse the reverse if the original direction is reverse.
18791880
const reverse = rangeStart > rangeEnd;
18801881
targetElement.animate(keyframes, {
18811882
pseudoElement: pseudoElement,
@@ -1886,7 +1887,7 @@ function animateGesture(
18861887
// from scroll bouncing.
18871888
easing: 'linear',
18881889
// We fill in both direction for overscroll.
1889-
fill: 'both',
1890+
fill: 'both', // TODO: Should we preserve the fill instead?
18901891
// We play all gestures in reverse, except if we're in reverse direction
18911892
// in which case we need to play it in reverse of the reverse.
18921893
direction: reverse ? 'normal' : 'reverse',
@@ -1931,18 +1932,32 @@ export function startGestureTransition(
19311932
// up if they exist later.
19321933
const foundGroups: Set<string> = new Set();
19331934
const foundNews: Set<string> = new Set();
1935+
// Collect the longest duration of any view-transition animation including delay.
1936+
let longestDuration = 0;
19341937
for (let i = 0; i < animations.length; i++) {
1938+
const effect: KeyframeEffect = (animations[i].effect: any);
19351939
// $FlowFixMe
1936-
const pseudoElement: ?string = animations[i].effect.pseudoElement;
1940+
const pseudoElement: ?string = effect.pseudoElement;
19371941
if (pseudoElement == null) {
1938-
} else if (pseudoElement.startsWith('::view-transition-group')) {
1939-
foundGroups.add(pseudoElement.slice(23));
1940-
} else if (pseudoElement.startsWith('::view-transition-new')) {
1941-
// TODO: This is not really a sufficient detection because if the new
1942-
// pseudo element might exist but have animations disabled on it.
1943-
foundNews.add(pseudoElement.slice(21));
1942+
} else if (pseudoElement.startsWith('::view-transition')) {
1943+
const timing = effect.getTiming();
1944+
const duration =
1945+
typeof timing.duration === 'number' ? timing.duration : 0;
1946+
const durationWithDelay = timing.delay + duration;
1947+
if (durationWithDelay > longestDuration) {
1948+
longestDuration = durationWithDelay;
1949+
}
1950+
if (pseudoElement.startsWith('::view-transition-group')) {
1951+
foundGroups.add(pseudoElement.slice(23));
1952+
} else if (pseudoElement.startsWith('::view-transition-new')) {
1953+
// TODO: This is not really a sufficient detection because if the new
1954+
// pseudo element might exist but have animations disabled on it.
1955+
foundNews.add(pseudoElement.slice(21));
1956+
}
19441957
}
19451958
}
1959+
const durationToRangeMultipler =
1960+
(rangeEnd - rangeStart) / longestDuration;
19461961
for (let i = 0; i < animations.length; i++) {
19471962
const anim = animations[i];
19481963
if (anim.playState !== 'running') {
@@ -1982,14 +1997,24 @@ export function startGestureTransition(
19821997
}
19831998
// TODO: If this has only an old state and no new state,
19841999
}
2000+
// Adjust the range based on how long the animation would've ran as time based.
2001+
// Since we're running animations in reverse from how they normally would run,
2002+
// therefore the timing is from the rangeEnd to the start.
2003+
const timing = effect.getTiming();
2004+
const duration =
2005+
typeof timing.duration === 'number' ? timing.duration : 0;
2006+
const adjustedRangeStart =
2007+
rangeEnd - (duration + timing.delay) * durationToRangeMultipler;
2008+
const adjustedRangeEnd =
2009+
rangeEnd - timing.delay * durationToRangeMultipler;
19852010
animateGesture(
19862011
effect.getKeyframes(),
19872012
// $FlowFixMe: Always documentElement atm.
19882013
effect.target,
19892014
pseudoElement,
19902015
timeline,
1991-
rangeStart,
1992-
rangeEnd,
2016+
adjustedRangeStart,
2017+
adjustedRangeEnd,
19932018
isGeneratedGroupAnim,
19942019
isExitGroupAnim,
19952020
);

0 commit comments

Comments
 (0)