2018-05-30 01:17:59 -04:00
# Reusable animations
2021-05-25 13:42:25 -04:00
This topic provides some examples of how to create reusable animations.
2018-05-30 01:17:59 -04:00
2021-05-25 13:42:25 -04:00
## Prerequisites
Before continuing with this topic, you should be familiar with the following:
2018-05-30 01:17:59 -04:00
* [Introduction to Angular animations ](guide/animations )
* [Transition and triggers ](guide/transition-and-triggers )
## Creating reusable animations
2021-04-09 01:33:21 -04:00
To create a reusable animation, use the [`animation()` ](api/animations/animation ) method to define an animation in a separate `.ts` file and declare this animation definition as a `const` export variable. You can then import and reuse this animation in any of your application components using the [`useAnimation()` ](api/animations/useAnimation ) API.
2018-05-30 01:17:59 -04:00
2021-05-25 13:42:25 -04:00
< code-example path = "animations/src/app/animations.1.ts" header = "src/app/animations.ts" region = "animation-const" language = "typescript" > < / code-example >
2018-05-30 01:17:59 -04:00
In the above code snippet, `transAnimation` is made reusable by declaring it as an export variable.
< div class = "alert is-helpful" >
**Note:** The `height` , `opacity` , `backgroundColor` , and `time` inputs are replaced during runtime.
< / div >
2021-05-25 13:42:25 -04:00
You can also export a part of an animation. For example, the following snippet exports the animation `trigger` .
< code-example path = "animations/src/app/animations.1.ts" header = "src/app/animations.1.ts" region = "trigger-const" language = "typescript" > < / code-example >
From this point, you can import resuable animation variables in your component class. For example, the following code snippet imports the `transAnimation` variable for use in the `useAnimation()` method.
2018-05-30 01:17:59 -04:00
2019-07-20 13:40:17 -04:00
< code-example path = "animations/src/app/open-close.component.3.ts" header = "src/app/open-close.component.ts" region = "reusable" language = "typescript" > < / code-example >
2018-05-30 01:17:59 -04:00
## More on Angular animations
You may also be interested in the following:
* [Introduction to Angular animations ](guide/animations )
* [Transition and triggers ](guide/transition-and-triggers )
* [Complex animation Sequences ](guide/complex-animation-sequences )
2019-07-20 13:40:17 -04:00
* [Route transition animations ](guide/route-animations )