Skip to content

Commit 33a135f

Browse files
committed
Cancel view transitions in fire-and-forget too
1 parent f9d7808 commit 33a135f

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,21 @@ export function hasInstanceAffectedParent(
15501550
return oldRect.height !== newRect.height || oldRect.width !== newRect.width;
15511551
}
15521552

1553+
function cancelAllViewTransitionAnimations(ownerDocument: Document) {
1554+
// In Safari, we need to manually cancel all manually start animations
1555+
// or it'll block or interfer with future transitions.
1556+
const animations = ownerDocument.getAnimations();
1557+
for (let i = 0; i < animations.length; i++) {
1558+
const anim = animations[i];
1559+
const effect: KeyframeEffect = (anim.effect: any);
1560+
// $FlowFixMe
1561+
const pseudo: ?string = effect.pseudoElement;
1562+
if (pseudo != null && pseudo.startsWith('::view-transition')) {
1563+
anim.cancel();
1564+
}
1565+
}
1566+
}
1567+
15531568
// How long to wait for new fonts to load before just committing anyway.
15541569
// This freezes the screen. It needs to be short enough that it doesn't cause too much of
15551570
// an issue when it's a new load and slow, yet long enough that you have a chance to load
@@ -1640,6 +1655,7 @@ export function startViewTransition(
16401655
}
16411656
transition.ready.then(spawnedWorkCallback, spawnedWorkCallback);
16421657
transition.finished.then(() => {
1658+
cancelAllViewTransitionAnimations(ownerDocument);
16431659
// $FlowFixMe[prop-missing]
16441660
if (ownerDocument.__reactViewTransition === transition) {
16451661
// $FlowFixMe[prop-missing]
@@ -1817,6 +1833,9 @@ export function startGestureTransition(
18171833
}
18181834
for (let i = 0; i < animations.length; i++) {
18191835
const anim = animations[i];
1836+
if (anim.playState !== 'running') {
1837+
continue;
1838+
}
18201839
const effect: KeyframeEffect = (anim.effect: any);
18211840
// $FlowFixMe
18221841
const pseudoElement: ?string = effect.pseudoElement;
@@ -1913,19 +1932,7 @@ export function startGestureTransition(
19131932
: readyCallback;
19141933
transition.ready.then(readyForAnimations, readyCallback);
19151934
transition.finished.then(() => {
1916-
// In Safari, we need to manually cancel all manually start animations
1917-
// or it'll block future transitions.
1918-
const documentElement: Element = (ownerDocument.documentElement: any);
1919-
const animations = documentElement.getAnimations({subtree: true});
1920-
for (let i = 0; i < animations.length; i++) {
1921-
const anim = animations[i];
1922-
const effect: KeyframeEffect = (anim.effect: any);
1923-
// $FlowFixMe
1924-
const pseudo: ?string = effect.pseudoElement;
1925-
if (pseudo != null && pseudo.startsWith('::view-transition')) {
1926-
anim.cancel();
1927-
}
1928-
}
1935+
cancelAllViewTransitionAnimations(ownerDocument);
19291936
// $FlowFixMe[prop-missing]
19301937
if (ownerDocument.__reactViewTransition === transition) {
19311938
// $FlowFixMe[prop-missing]

0 commit comments

Comments
 (0)