Angular animations
The @angular/animations package's trigger/state/transition model, and the newer native-CSS alternative to it.
Angular animations
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- trigger/state/transition describe an element's animation as a state machine — named states, and the transitions that animate between them, rather than hand-written keyframe timing.
- A trigger is bound to an element like any other binding ([@triggerName]="expression"), and Angular animates automatically whenever the bound expression's value changes to a different named state.
- Newer, native CSS-driven animation (transitions and the @starting-style-based animate.enter/animate.leave) increasingly covers the same ground with less package weight and no animation-specific API to learn beyond CSS itself.
Modeling an animation as states and transitions, not keyframe timing
@angular/animations doesn’t ask you to think in terms of keyframes and
durations directly — it asks you to describe an element’s possible states, and
the transitions between them, the same mental model a state machine uses:
trigger('openClose', [
state('open', style({ height: '*', opacity: 1 })),
state('closed', style({ height: '0px', opacity: 0 })),
transition('open <=> closed', animate('300ms ease-in-out')),
])
state() defines what an element should look like in each named condition —
open, closed. transition() defines how to animate between two named
states, with <=> meaning “either direction.” Nothing here says anything about
when this should actually run; that’s a separate step, binding the trigger to
an element the same way you’d bind any other property:
<div [@openClose]="isOpen ? 'open' : 'closed'">...</div>
Change isOpen, and the bound expression’s value flips between the two named
states — Angular notices, matches the change against the transition() rules,
and plays the configured animation automatically. You never manually start or
stop anything; the animation is a direct consequence of the state your own
data already changed to.
Why this earns its own package, and its own learning curve
This state-machine framing genuinely pays off for animations with more than two
simple states, or ones that need to coordinate multiple elements, stagger
timing across a list, or sequence several steps together — @angular/animations
supports all of that directly, in a declarative, Angular-native way that would
be considerably more code to hand-roll with raw CSS transitions and JavaScript
timers. The cost is real too: it’s a separate package, with its own API
surface, contributing its own weight to your bundle, for what’s sometimes a
genuinely simple visual effect that didn’t need a state machine to describe.
The newer, lighter path: native CSS
For a lot of real cases — fade in, slide in, a simple hover transition — modern
CSS alone covers the same ground, with no separate package and no
animation-specific API beyond CSS itself: ordinary CSS transition/animation
properties, plus Angular’s newer animate.enter/animate.leave directives
(built on the CSS @starting-style feature) for entrance and exit animations
specifically. The practical guidance worth taking from this: reach for
@angular/animations’ full state-machine model when you genuinely have
complex, multi-step, or coordinated animation logic that benefits from it;
reach for native CSS first for anything simpler, since it costs nothing extra
to ship and asks nothing extra of you to learn.
Where the state-machine model earns its keep: lists and sequences
The clearest case for @angular/animations over hand-rolled CSS is animating a
list of elements with staggered timing — each item fading in slightly after
the one before it, say, as a list populates. query() and stagger() let a
single trigger reach into a component’s child elements and apply a delay that
increases per item:
transition('* => *', [
query(':enter', [
style({ opacity: 0 }),
stagger('50ms', animate('200ms ease-out', style({ opacity: 1 }))),
], { optional: true }),
])
:enter matches elements that are newly added to the DOM on this change;
stagger('50ms', ...) applies the same animation to each matched element, but
delays each one by an additional 50ms relative to the last. Reproducing this
with plain CSS means either a fixed number of hand-written nth-child delay
rules (brittle the moment the list’s length changes) or JavaScript timing code
of your own — exactly the coordination problem the state-machine model was
built to absorb declaratively. This is also the mechanism behind route
transition animations, where entering and leaving components are matched by
:enter/:leave the same way.
This lesson’s video, from a well-regarded Angular specialist, covers the trigger/state/transition basics directly; the linked “deeper” video, from the same creator, makes the direct case for replacing that approach with pure CSS keyframes where it fits — worth watching as the natural second half of this lesson’s framing, once you’ve seen here where the heavier tool still wins.
Check yourself
In `trigger('openClose', [state('open', style({ height: '*' })), state('closed', style({ height: '0px' })), transition('open <=> closed', animate('300ms ease-in-out'))])`, what causes the animation to actually play?
Binding `[@openClose]="isOpen ? 'open' : 'closed'"` on an element, and then changing the bound expression's value — the moment it flips between `'open'` and `'closed'`, Angular matches that to the `transition('open <=> closed', ...)` rule and plays the configured animation between the two states' styles, with no manual timing code required.
What's the practical argument for reaching for native CSS transitions or the newer animate.enter/animate.leave syntax instead of @angular/animations for a simple case?
They accomplish the same visual effect without pulling in the separate @angular/animations package at all, using only CSS the browser already understands natively — less bundle weight, and nothing animation-specific to learn beyond CSS you likely already know, for the cases that don't need the more elaborate state-machine modeling @angular/animations provides.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.