I would like to fire two animations at the same time on click. The animation triggers use the same state, one is placed on an outer parent div and the other is nested within this div. The style changes are made, but the transition is applied only on the parent component. I've used animateChild within my parent animation with no luck. How can I apply an animation both a parent and child element?
animations.ts
import {
trigger,
state,
style,
transition,
animate,
query,
group,
animateChild,
} from "@angular/animations";
export const Animations = {
animations: [
trigger("expansionTrigger", [
state(
"true",
style({
height: "*"
})
),
state(
"false",
style({
height: "0",
display: "none"
})
),
transition("false <=> true", [
group([
query("@colExpansion", [animateChild()]),
animate("3s ease")
])
])
]),
trigger("colExpansion", [
state(
"true",
style({
"-webkit-box-flex": "0",
flex: "0 0 66.66667%",
"max-width": "66.66667%"
})
),
state(
"false",
style({
"flex-basis": "0",
"-webkit-box-flex": "1",
"flex-grow": "1",
"max-width": "100%"
})
),
transition("false <=> true", animate(3))
])
]
};
body.component.ts
import { Component, Input } from '@angular/core';
import { Animations } from '../animations';
@Component({
selector: 'app-body',
templateUrl: './body.component.html',
styleUrls: ['./body.component.css'],
animations: Animations.animations
})
export class BodyComponent {
@Input() isExpanded: string;
}
body.component.html
<div [@expansionTrigger]="isExpanded === 'true' ? 'true' : 'false'">
<div [@colExpansion]="isExpanded === 'true' ? 'true' : 'false'">
</div>
</div>
I really struggled with this for a long time. I finally managed to make it work and I can share the syntax I used.
In your setup above I would suggest switching this block:
to this:
i.e. include the
animate("3s ease")
block into the array that defines how the child should be animated as well as defining it in the block for the parent.You can see that there are now two specifications - one for the parent and one for the child (which happen to have the same timing).
I can't vouch that that code will work as I haven't run it but I can share mine. You'll see that I don't actually use the
@
selector to target the animation, I just target a class calledname
and I supply the animation directly but I can confirm that the following syntax does work: