Have you ever wondered if it's possible to integrate custom parameters into an Angular animation by passing them through a function, and then proceed to use the resulting animation in a component?
To exemplify this concept, consider this demo where the animation is defined separately in animations.ts
as shown below:
import {
trigger,
state,
style,
transition,
animate,
group,
} from '@angular/animations';
export const SlideInOutAnimation = [
trigger('slideInOut', [
state(
'in',
style({
width: '50%',
opacity: '1',
visibility: 'visible',
})
),
state(
'out',
style({
width: '50px',
opacity: 0.3,
visibility: 'visible',
})
),
transition('in => out', [
group([
animate(
'600ms ease-in-out',
style({
width: '50px',
})
),
]),
]),
transition('out => in', [
group([
animate(
'1ms ease-in-out',
style({
visibility: 'visible',
})
),
animate(
'600ms ease-in-out',
style({
width: '50%',
})
),
animate(
'800ms ease-in-out',
style({
opacity: '1',
})
),
]),
]),
]),
];
The next step involves importing and utilizing this animation within a component by declaring it in the animations
property of the @Component
decorator.
However, what happens when we need to incorporate an @Input
property value and utilize it within the animation?
For instance, if we have something like:
@Input()
width: 50%;
How can we transmit this parameter to the animation effectively?