Introduction on animations for Angular applications + heads ups for some tricky bits.
Don't forget to visit the included links:
- https://medium.com/ux-in-motion/creating-usability-with-motion-the-ux-in-motion-manifesto-a87a4584ddc
- https://medium.com/frontend-coach/angular-in-motion-4-approaches-to-animation-1aa7426aae5a
- https://medium.com/frontend-coach/angular-router-animations-what-they-dont-tell-you-3d2737a7f20b
... and follow me on twitter https://twitter.com/sulco for more on Angular, TypeScript and frontend in general.
7. Setup
import {
BrowserAnimationsModule
} from '@angular/platform-browser/animations';
@NgModule({
imports: [
BrowserAnimationsModule,
]
})
export class AppModule { }
Just use BrowserAnimationsModule
8. Setup
import {
BrowserAnimationsModule
} from '@angular/platform-browser/animations';
@NgModule({
imports: [
BrowserAnimationsModule,
]
})
export class AppModule { }
Just use BrowserAnimationsModule
NoopAnimationsModule
for tests
9. Seman3cs
Trigger
func&on linking anima&on with a specific
element (i.e. an “anchor” for anima&on)
State
descrip&on of style in a discrete moment
Transi3on
descrip&on of the way styles change from
one state to another
Angular anima&ons nomenclature
state:
“on”
state:
“off”
trigger: @myFade
22. Seman3cs 102
Query
select elements (inside a trigger)
that match a query token
Stagger
a series of anima&ons applied to subsequent
found element with a delay between them
Some more anima&on nomenclature <div>"</div>
trigger: @myStagger
<div>"</div>
<div>"</div>
:enter
.item
@*
<div>"</div>
<div>"</div>
<div>"</div>
examples of query tokens:
- anima&on keyword
- css selector
- trigger name
27. Implementa3on
<RouterOutlet>
is a direc&ve (exportAs: “outlet”) with
activatedRoute & activatedRouteData
proper&es
<main [@routerAnim]="getState(outletRef)">
<router-outlet #outletRef=“outlet”>
"</router-outlet>
"</main>
We can use this to get a state value
for an anima&on trigger.
getState(outletRef: RouterOutlet) {
return outletRef.activatedRouteData.name;
}
29. Heads up!
Anima&ons within the same route
won’t work by default.
@NgModule({
imports: [RouterModule.forRoot(routes)],
providers: [{
provide: RouteReuseStrategy,
useClass: CustomReuseStrategy
}],
})
export class CustomReuseStrategy implements RouteReuseStrategy {
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return curr.component ""!== MyFruitComponent;
}
… (see the full class)
30. Heads up!
Anima&ons within the same route
won’t work by default.
@NgModule({
imports: [RouterModule.forRoot(routes)],
providers: [{
provide: RouteReuseStrategy,
useClass: CustomReuseStrategy
}],
})
export class CustomReuseStrategy implements RouteReuseStrategy {
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return curr.component ""!== MyFruitComponent;
}
… (see the full class)
31. Heads up!
Anima&ons within the same route
won’t work by default.
@NgModule({
imports: [RouterModule.forRoot(routes)],
providers: [{
provide: RouteReuseStrategy,
useClass: CustomReuseStrategy
}],
})
export class CustomReuseStrategy implements RouteReuseStrategy {
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return curr.component ""!== MyFruitComponent;
}
… (see the full class)
33. Anima3onBuilder
Anima3onBuilder
with the build() method creates
a configured Anima&onFactory
Anima3onFactory
with the create() method creates
an Anima&onPlayer
Anima3onPlayer
allows to control the anima&on
like FormBuilder… but for anima&ons
41. More on anima&ons
‣ Angular in Mo&on: 4 approaches to anima&on
‣ Angular Router Anima&ons - what they don't tell you
Ok, cheers!
h`ps://twi`er.com/sulco