Strategies for effectively passing parameters to animation step functions

By breaking down an animation function into reusable parts, I created the following structure:

const enterLeaveAnimation = (
  triggerName: string,
  step1: AnimationMetadata | AnimationMetadata[],
  step2: AnimationMetadata | AnimationMetadata[]
) =>
trigger(triggerName, [
  transition(':enter', step1, { params: { delay: 0 } }),
  transition(':leave', step2, { params: { delay: 0 } })
])

const fadeEnter = [style({ opacity: 0 }), animate('600ms {{delay}}ms ease', style({ opacity: 1 }))]
const fadeLeave = [animate('600ms {{delay}}ms ease', style({ opacity: 0 }))]

const fadeAnimation = enterLeaveAnimation('fade', fadeEnter, fadeLeave);

The function enterLeaveAnimation is then used as follows:

...
animations: [fadeAnimation],

And in the template:

<p @fade *ngIf="visible">
...

Everything appears to be functioning correctly. A working demo can be found here.

However, setting a custom delay value seems to be an issue, as it defaults to 0.

I have attempted different approaches, like:

...[@fade]="{value:true,params: {delay: 200}}"

Unfortunately, these attempts are unsuccessful and I am unsure how to pass the delay parameter to the animations fadeEnter and fadeLeave.

You can see another demo of this attempt here.

My ultimate goal is to have separate delay values for each step, but for now, I am focused on passing at least one value successfully.

Is there a way to achieve this?

Answer №1

The parameters must be incorporated into the steps. While default values will still be in place, they can also be supplied by the trigger:

const animateInOut = (triggerName: string, step1: AnimationMetadata | AnimationMetadata[],
    step2: AnimationMetadata | AnimationMetadata[], parameters?: AnimationOptions | null | undefined
) =>
    trigger(triggerName, [
        transition(':enter', step1, parameters),
        transition(':leave', step2, parameters)
    ])

const fadeIn = animation([style({ opacity: 0 }), animate('600ms {{delay1}}ms ease-out', style({ opacity: 1 }))], { params: { delay1: 0 } })
const fadeOut = animation([animate('600ms {{delay2}}ms ease', style({ opacity: 0 }))], { params: { delay2: 0 } })

Example of usage:

...[@fadeInOut]="{value:true,params: {delay1: 200, delay2: 300}}"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message in Vue3 with TypeScript: When using global components, you may encounter the error "JSX element type '___' does not have any construct or call signatures.ts(2604)."

Each globally registered component suddenly displays a red TypeScript squiggle in the markup! Surprisingly, the app continues to function without any visible errors! This issue arises with all components, whether they are custom-made or third-party libr ...

Dynamic module declaration - Typescript 3.5

In TypeScript, I am able to declare a module like this: declare module '*.svg' { const content: string export default content } After declaring the module, I can import it using import svg from './src/file.svg' without encount ...

Accessing the URL causes malfunctioning of the dynamic routing in Angular 2

I am currently working on implementing dynamic routing functionality in my Angular application. So far, I have successfully achieved the following functionalities: Addition of routing to an existing angular component based on user input Removal of routin ...

Testing inherit from a parent class in a unit test for Angular 2

Trying to create a unit test that checks if the method from the base class is being called This is the base class: export abstract class Animal{ protected eatFood() { console.log("EAT FOOD!") } } Here is the class under test: export ...

Choose an option from a selection and showcase it

I need to implement a modal that displays a list of different sounds for the user to choose from. Once they select a sound, it should be displayed on the main page. Here is the code snippet for the modal page: <ion-content text-center> <ion-ca ...

utilize a modal button in Angular to showcase images

I am working on a project where I want to display images upon clicking a button. How can I set up the openModal() method to achieve this functionality? The images will be fetched from the assets directory and will change depending on the choice made (B1, ...

The ViewChild property encountered an error: "The child element cannot be found"

I am facing an issue in Angular 7 where I have a parent component with a child component. I would like to obtain a reference to the child component within the parent component so that I can access its functions and properties. Child Component : @Componen ...

Utilizing *ngIf within a loop to alternate the visibility of table rows with a designated class upon clicking on rows with a different class in Angular 2

I have created a table that displays data, and within this table there are 2 tr elements with the classes default and toggle-row. When I click on a tr element with the class default, it should only toggle the corresponding tr element with the class toggle- ...

Ways to verify if an item is an Express object?

Currently, I am utilizing typescript to verify whether an app returned by the Express() function is indeed an instance of Express. This is how I am attempting to accomplish this: import Express from "express" const app = Express() console.log( ...

Encountering a problem during npm installation on Windows 10 while trying to install the Angular CLI globally using the

node -v v4.5.0 npm -v 5.0.1 Is there anybody who encountered a similar problem when trying to install angular-cli on Windows 10? ...

Why does the error "property undefined" keep popping up even though I've already declared it?

Currently, I am facing an issue with toggling the theme in my project. I am encountering difficulties with the types involved, and I am unsure whether the problem lies within my TypeScript configuration or my actual code itself. I attempted to replicate t ...

Troubleshooting why Angular2 is failing to load external styles in styleUrls

Currently in the process of developing an angular2 application and had envisioned being able to implement distinct styles for public content versus admin content. To my disappointment, it appears that Angular is not accommodating external styles that are l ...

Angular 8 is throwing an error stating that the property 'token' is not recognized on the 'Object' data type

As a newcomer to Angular, I have been following the MEAN Stack - Mean Auth App Tutorial in Angular 2. However, since I am using Angular 8, some of the code is deprecated due to recent updates. I have encountered an issue with the code bel ...

Define class attributes within a TypeScript callback function

I have encountered an issue with my method involving a subscription to an event from a pub sub messaging service. The problem arises when I attempt to define a class property within the callback function, only to find that the property returns as undefin ...

atom-typescript - What could be causing the unrecognized Typescript configuration options?

I'm puzzled as to why I am encountering the errors depicted in the screenshot below. Atom is indicating that my tsconfig.json file has 'project file contains invalid options' for allowJs, buildOnSave, and compileOnSave. However, according ...

Combine all JavaScript files into a single file for an Angular 2 application

I've been working on an Angular 2 application. While I can successfully minify the TS/JS files using "uglify", I'm wondering if there's a way to combine all JS files into a single file for faster rendering. https://i.sstatic.net/EVBxU.jpg ...

Guide to creating a generic that captures the prop types of a given component

Is there a way to create a function that accepts a component and uses its prop type as the type of the second parameter? For example, if I provide a component with the type React.FunctionComponent<IMovieShowcase> How would I go about extracting the ...

Enhancing Angular with Plotly: Implementing click events on bar chart legends

I'm currently working on implementing color pickers for my plotly-plot charts within an Angular template. I am looking to add a function that triggers when the chart legend is clicked. How can I achieve this and get a click event for the chart legends ...

When using TypeScript, how can I effectively utilize the Component property obtained from connect()?

Software Development Environment TypeScript 2.8 React 16 Redux Foo.tsx interface IFooProps{ userId:number } class Foo extends Component<IFooProps>{ render(){ return <p>foo...</p> } } const mapStateToProps = (state: I ...

Can we link together two separate ngfor loops in any manner?

<div class="gl-w-100 gl-md-w-auto gl-po-rel list"> <div class="signin_wrpr gl-w-100 gl-po-rel gl-d-flex gl-fd-column gl-bg-white gl-lg-w-auto gl-md-w-auto gl-h-100 gl-md-ta-c"> <div class="head g ...