Skip to content

Commit

Permalink
Cancel view transitions in fire-and-forget too
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Mar 7, 2025
1 parent f9d7808 commit 33a135f
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,21 @@ export function hasInstanceAffectedParent(
return oldRect.height !== newRect.height || oldRect.width !== newRect.width;
}

function cancelAllViewTransitionAnimations(ownerDocument: Document) {
// In Safari, we need to manually cancel all manually start animations
// or it'll block or interfer with future transitions.
const animations = ownerDocument.getAnimations();
for (let i = 0; i < animations.length; i++) {
const anim = animations[i];
const effect: KeyframeEffect = (anim.effect: any);
// $FlowFixMe
const pseudo: ?string = effect.pseudoElement;
if (pseudo != null && pseudo.startsWith('::view-transition')) {
anim.cancel();
}
}
}

// How long to wait for new fonts to load before just committing anyway.
// This freezes the screen. It needs to be short enough that it doesn't cause too much of
// an issue when it's a new load and slow, yet long enough that you have a chance to load
Expand Down Expand Up @@ -1640,6 +1655,7 @@ export function startViewTransition(
}
transition.ready.then(spawnedWorkCallback, spawnedWorkCallback);
transition.finished.then(() => {
cancelAllViewTransitionAnimations(ownerDocument);
// $FlowFixMe[prop-missing]
if (ownerDocument.__reactViewTransition === transition) {
// $FlowFixMe[prop-missing]
Expand Down Expand Up @@ -1817,6 +1833,9 @@ export function startGestureTransition(
}
for (let i = 0; i < animations.length; i++) {
const anim = animations[i];
if (anim.playState !== 'running') {
continue;
}
const effect: KeyframeEffect = (anim.effect: any);
// $FlowFixMe
const pseudoElement: ?string = effect.pseudoElement;
Expand Down Expand Up @@ -1913,19 +1932,7 @@ export function startGestureTransition(
: readyCallback;
transition.ready.then(readyForAnimations, readyCallback);
transition.finished.then(() => {
// In Safari, we need to manually cancel all manually start animations
// or it'll block future transitions.
const documentElement: Element = (ownerDocument.documentElement: any);
const animations = documentElement.getAnimations({subtree: true});
for (let i = 0; i < animations.length; i++) {
const anim = animations[i];
const effect: KeyframeEffect = (anim.effect: any);
// $FlowFixMe
const pseudo: ?string = effect.pseudoElement;
if (pseudo != null && pseudo.startsWith('::view-transition')) {
anim.cancel();
}
}
cancelAllViewTransitionAnimations(ownerDocument);
// $FlowFixMe[prop-missing]
if (ownerDocument.__reactViewTransition === transition) {
// $FlowFixMe[prop-missing]
Expand Down

0 comments on commit 33a135f

Please sign in to comment.