This code is currently working for me but I got to this solution from trial and error and playing with the stagger()
method.
Is there a cleaner way to implement this?
Ideally I'd just use an *ngIf
but the elements are image heavy and the opacity animation is choppy from loading the images each time the element is rendered.
Ultimately what I am trying to accomplish is:
display: none + opacity: 0 -> display block + opacity: 1;
However I know that display
can't be animated. So I am trying to stagger it to change to none
after the opacity: 0
. And then trigger display: block
before the opacity is animated to opacity: 1
trigger('fade', [
state('hidden', style({ display: 'none', opacity: 0 })),
state('shown', style({ display: 'block'})),
transition('shown => hidden', [
query(':self', [
animate('250ms ease')
])
]),
transition('hidden => shown', [
query(':self', [
animate('250ms ease'),
stagger(250, [
animate('250ms ease', style({ opacity: 1 }))
])
])
])
])
I am calling it in the template with:
[@fade]="displayState === 'shown' ? 'shown' : 'hidden'"