Animation
Animate an element from code with animate(), or animate content in and out with <lynx-transition> and <lynx-transition-group>. import { LynxAnimation, LynxTransition, LynxTransitionGroup, afterFrame, provideAnimateEnterLeaveDisabled, type LynxAnimationOptions, type BaseLynxAnimation } from '@blotch/angular-lynx';
Usage
Animate an element from code with animate():
Animate content in and out with <lynx-transition> and CSS @keyframes:
API
animate()
A method on every element's nativeElement, which you get from viewChild() or an ElementRef. It starts the animation at once.
-
A number for
optionsis shorthand for{ duration }. -
From an event handler, call it directly, as on the web.
-
On the background thread, it returns an animation whose methods do nothing.
-
Removing the element cancels its running animations.
LynxAnimation
What animate() returns on the main thread. Don't construct it yourself.
LynxAnimation.id
A unique ID that AngularLynx generates for the animation.
LynxAnimation.play()
Resumes a paused animation.
LynxAnimation.pause()
Pauses the animation at its current frame.
LynxAnimation.cancel()
Stops the animation and removes its effect.
LynxTransition
Mounts its projected content when show turns true. Unmounts it duration milliseconds after show turns false.
Meanwhile, it adds a class to the <lynx-transition> element itself: {name}-enter on enter and {name}-leave on leave.
- Use CSS
@keyframes, nottransition, in those classes. The class is applied in the same render that mounts the content, so there's no earlier frame to transition from. - Match
durationto your CSS animation's length. Completion is timed, not read from the animation. - The first render doesn't animate. Content that starts visible appears without an enter animation.
LynxTransitionGroup
Renders a list from a child <ng-template let-item> and animates items as they're added and removed. It puts {name}-enter or {name}-leave on each item's root element.
- The first render doesn't animate. Only later changes to
eachdo. - Removed items stay in place while they leave, and the others reorder around them.
- An item that comes back while leaving keeps its view and loses the leave class.
afterFrame()
Runs callback once the screen has drawn what your app has changed so far, and the app has nothing else to do. It returns a function that cancels it.
- When it runs: on the turn after the next frame, or after a later frame if other work came between. It waits four frames at most.
- Without frames, as in a test without
requestAnimationFrame, it runs on the next turn.
Use it to start a transition that Core Animation, iOS's own animator, runs after work such as building a screen. That's a transition on an element with enable-new-animator="false". See Keep a transition moving while your app works.
iOS begins such a transition at the time of the last frame the app saw. So one started straight after a busy moment is already partway through when it's first drawn.
provideAnimateEnterLeaveDisabled()
Turns Angular's animate.enter and animate.leave off for the app. Add it to the app's providers. They then behave as they did on iOS and Android before AngularLynx supported them:
- No class is added.
- No
(animate.enter)or(animate.leave)function is called. - A leaving element is removed without waiting.
See Enter and leave transitions.
Types
LynxAnimationOptions
AnimationOptions comes from @lynx-js/types. Together they accept:
BaseLynxAnimation
What animate() returns on either thread: an id plus play(), pause() and cancel(). Animation comes from @lynx-js/types.
Related
- Animations: CSS transitions and keyframes.
- Animate from code
- Enter and leave transitions
- Threads
