Skip to content

Commit 45d9fec

Browse files
committed
fix(animations): always change to desired animation state even if no transition fires
Fixes #16947
1 parent 1f9a3dd commit 45d9fec

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎packages/animations/browser/src/render/transition_animation_engine.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ export class TransitionAnimationEngine {
734734

735735
// if a unmatched transition is queued to go then it SHOULD NOT render
736736
// an animation and cancel the previously running animations.
737-
if (entry.isFallbackTransition && !instruction.isRemovalTransition) {
738-
eraseStyles(element, instruction.fromStyles);
737+
if (entry.isFallbackTransition) {
738+
player.onStart(() => eraseStyles(element, instruction.fromStyles));
739739
player.onDestroy(() => setStyles(element, instruction.toStyles));
740740
skippedPlayers.push(player);
741741
return;
@@ -790,6 +790,14 @@ export class TransitionAnimationEngine {
790790
}
791791
});
792792

793+
skippedPlayers.forEach(player => {
794+
const element = player.element;
795+
const previousPlayers =
796+
this._getPreviousPlayers(element, false, player.namespaceId, player.triggerName, null);
797+
previousPlayers.forEach(
798+
prevPlayer => { getOrSetAsInMap(allPreviousPlayersMap, element, []).push(prevPlayer); });
799+
});
800+
793801
allPreviousPlayersMap.forEach(players => players.forEach(player => player.destroy()));
794802

795803
const leaveNodes: any[] = bodyNode && allPostStyleElements.size ?
@@ -919,8 +927,8 @@ export class TransitionAnimationEngine {
919927
afterFlushAnimationsDone(callback: () => any) { this._whenQuietFns.push(callback); }
920928

921929
private _getPreviousPlayers(
922-
element: string, instruction: AnimationTransitionInstruction, isQueriedElement: boolean,
923-
namespaceId?: string, triggerName?: string): TransitionAnimationPlayer[] {
930+
element: string, isQueriedElement: boolean, namespaceId?: string, triggerName?: string,
931+
toStateValue?: any): TransitionAnimationPlayer[] {
924932
let players: TransitionAnimationPlayer[] = [];
925933
if (isQueriedElement) {
926934
const queriedElementPlayers = this.playersByQueriedElement.get(element);
@@ -930,10 +938,10 @@ export class TransitionAnimationEngine {
930938
} else {
931939
const elementPlayers = this.playersByElement.get(element);
932940
if (elementPlayers) {
933-
const isRemovalAnimation = instruction.toState == VOID_VALUE;
941+
const isRemovalAnimation = !toStateValue || toStateValue == VOID_VALUE;
934942
elementPlayers.forEach(player => {
935943
if (player.queued) return;
936-
if (!isRemovalAnimation && player.triggerName != instruction.triggerName) return;
944+
if (!isRemovalAnimation && player.triggerName != triggerName) return;
937945
players.push(player);
938946
});
939947
}
@@ -943,7 +951,7 @@ export class TransitionAnimationEngine {
943951
if (namespaceId && namespaceId != player.namespaceId) return false;
944952
if (triggerName && triggerName != player.triggerName) return false;
945953
return true;
946-
})
954+
});
947955
}
948956
return players;
949957
}
@@ -970,7 +978,7 @@ export class TransitionAnimationEngine {
970978
const isQueriedElement = element !== rootElement;
971979
const players = getOrSetAsInMap(allPreviousPlayersMap, element, []);
972980
const previousPlayers = this._getPreviousPlayers(
973-
element, instruction, isQueriedElement, targetNameSpaceId, targetTriggerName);
981+
element, isQueriedElement, targetNameSpaceId, targetTriggerName, instruction.toState);
974982
previousPlayers.forEach(player => {
975983
const realPlayer = player.getRealPlayer() as any;
976984
if (realPlayer.beforeDestroy) {

‎packages/core/test/animation/animation_integration_spec.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export function main() {
223223
]);
224224
});
225225

226-
it('should not cancel the previous transition if a follow-up transition is not matched',
226+
it('should always cancel the previous transition if a follow-up transition is not matched',
227227
fakeAsync(() => {
228228
@Component({
229229
selector: 'if-cmp',
@@ -290,7 +290,7 @@ export function main() {
290290
fixture.detectChanges();
291291
engine.flush();
292292

293-
expect(engine.players.length).toEqual(1);
293+
expect(engine.players.length).toEqual(0);
294294
expect(getLog().length).toEqual(0);
295295

296296
flushMicrotasks();
@@ -299,7 +299,7 @@ export function main() {
299299
expect(cmp.startEvent.toState).toEqual('c');
300300
expect(cmp.startEvent.totalTime).toEqual(0);
301301

302-
expect(completed).toBe(false);
302+
expect(completed).toBe(true);
303303
}));
304304

305305
it('should only turn a view removal as into `void` state transition', () => {

‎packages/core/test/animation/animation_query_integration_spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ export function main() {
984984
expect(count).toEqual(8);
985985
});
986986

987-
it('should not cancel inner queried animations if a trigger state value changes, but isn\'t detected as a valid transition',
987+
it('should cancel inner queried animations if a trigger state value changes, but isn\'t detected as a valid transition',
988988
() => {
989989
@Component({
990990
selector: 'ani-cmp',
@@ -1031,7 +1031,7 @@ export function main() {
10311031
fixture.detectChanges();
10321032
engine.flush();
10331033

1034-
expect(count).toEqual(0);
1034+
expect(count).toEqual(5);
10351035
});
10361036

10371037
it('should allow for queried items to restore their styling back to the original state via animate(time, "*")',

0 commit comments

Comments
 (0)