> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Animations

Animate your UI with:

- CSS transitions and CSS `@keyframes`
- an element's `animate()` method
- for content that appears and disappears, Angular's `animate.enter` and `animate.leave`, or the `LynxTransition` component


## Choose an approach

| You want to                                                    | Use                                 | Details                                                         |
| -------------------------------------------------------------- | ----------------------------------- | --------------------------------------------------------------- |
| Change a color, size or position smoothly when state changes   | CSS `transition`                    | [Below](#css-transitions)                                       |
| Loop an animation, or play a fixed sequence (spinner, pulse)   | CSS `@keyframes`                    | [Below](#css-keyframes)                                         |
| Start an animation from code, with keyframes chosen at runtime | The element's `animate()` method    | [Animate from code](/guide/animations/programmatic.md)          |
| Animate content as it appears and disappears                   | `animate.enter` and `animate.leave` | [Enter and leave transitions](/guide/animations/enter-leave.md) |
| Animate content in and out with a timing you set               | `LynxTransition`                    | [`LynxTransition`](/guide/animations/lynx-transition.md)        |
| Move an element with a finger, frame by frame                  | A main-thread script                | [Main-thread scripts](/guide/main-thread/index.md)              |

:::info Angular's animation APIs
Angular's `animate.enter` and `animate.leave` work, with CSS classes or with a function of your own: see [Enter and leave transitions](/guide/animations/enter-leave.md). Its older `@angular/animations` package (`trigger()`, `state()`, `transition()`) isn't supported. Use the approaches on this page instead.

[Lynx elements](/guide/glossary.md#element) do have an `animate()` method that looks like the browser's [Web Animations API](https://developer.mozilla.org/docs/Web/API/Element/animate). It's Lynx's own version: it returns a handle with only `play()`, `pause()` and `cancel()`. See [Animate from code](/guide/animations/programmatic.md).
:::

## CSS transitions

Put `transition` on an element, then change a property by toggling a class from a signal:

```typescript title="src/app/fade.ts"
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-fade',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="box" [class.active]="active()" (bindtap)="toggle()">
      <text>Tap me</text>
    </view>
  `,
  styles: `
    .box {
      width: 100px;
      height: 100px;
      background-color: #a1a1aa;
      transition: background-color 0.5s ease-in-out; /* [!code highlight] */
    }
    .active {
      background-color: #6366f1;
    }
  `,
})
export class Fade {
  readonly active = signal(false);

  toggle(): void {
    this.active.update((v) => !v);
  }
}
```

- The handler only sets a signal, which is always safe in an event handler. Angular updates the class after it returns.
- A `transition` runs when a property changes on an element that's already on screen. It doesn't run when the element first appears: use [`animate.enter`](/guide/animations/enter-leave.md) or [`LynxTransition`](/guide/animations/lynx-transition.md) for that.
- Only [some properties](#what-can-animate) animate.
- Write time values with a unit: `0ms`, not `0`. Lynx ignores a bare `0`.

### Keep a transition moving while your app works

Lynx works out each frame of an animation itself, on the thread your Angular code runs on. So a frame Angular spends building a screen is a frame nothing moves.

Put `enable-new-animator="false"` on an element to hand its transitions and `@keyframes` to the platform instead:

- **iOS** uses Core Animation, which keeps them moving whatever the app is doing.
- **Android** uses its animators.

Add `flatten="false"` too: Android only animates a `transform` on an element with a view of its own.

```angular-html
<view
  enable-new-animator="false"
  flatten="false"
  class="drawer"
  [class.open]="open()"
></view>
```

- The element's `animate()` method does nothing then: move it with `transition` and `@keyframes`.
- On iOS, `transform`, `opacity` and `background-color` animate, and the element's position and size, though its background and children take the new size at once: move it with `transform` and `opacity`.
- Start the transition with [`afterFrame()`](/api/angular-lynx/animation.md#afterframe) after work such as building a screen. iOS begins it at the time of the last frame the app saw, so one started straight after a busy moment is partway through when it's first drawn.
- `<lynx-stack-outlet>` moves its screens this way, and the [Bottom Sheet](/dolan/bottom-sheet.md) its sheet. The [Spinner](/dolan/spinner.md), [Skeleton](/dolan/skeleton.md) and indeterminate [Progress](/dolan/progress.md) run their loops on the platform too, so they keep going while a screen builds, which is when they show.
- The [web preview](/guide/glossary.md#web-preview) ignores the attribute: the browser runs the transition.

## CSS @keyframes

Define `@keyframes` in your component styles, and start the animation by adding a class:

```typescript title="src/app/spin.ts"
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-spin',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="spinner" [class.spinning]="spinning()" (bindtap)="toggle()">
      <text>Spin</text>
    </view>
  `,
  styles: `
    @keyframes rotate {
      from {
        transform: rotate(0deg);
      }
      to {
        transform: rotate(360deg);
      }
    }
    .spinner {
      width: 80px;
      height: 80px;
      background-color: #f97316;
    }
    .spinning {
      animation: rotate 1s linear infinite; /* [!code highlight] */
    }
  `,
})
export class Spin {
  readonly spinning = signal(false);

  toggle(): void {
    this.spinning.update((v) => !v);
  }
}
```

- Adding the `spinning` class starts the animation from its first frame, and removing it stops the animation.
- `infinite` in the `animation` shorthand loops it. Use a number to play it that many times.
- A loop that shows while the app works, such as a spinner, stops while Angular is busy unless the platform runs it: give its element `enable-new-animator="false"` and `flatten="false"` (see [above](#keep-a-transition-moving-while-your-app-works)). A `rotate()` from `0deg` to `360deg` turns there too.

## Animate from code

Call `animate()` on an element you get with [`viewChild()`](https://angular.dev/api/core/viewChild):

```typescript title="src/app/pulse.ts"
import { Component, type ElementRef, viewChild } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-pulse',
  imports: [LYNX_ELEMENTS],
  template: `
    <view #box (bindtap)="pulse()">
      <text>Tap to pulse</text>
    </view>
  `,
})
export class Pulse {
  readonly box = viewChild.required<ElementRef>('box');

  pulse(): void {
    this.box().nativeElement.animate(
      [
        { transform: 'scale(1)' },
        { transform: 'scale(1.2)' },
        { transform: 'scale(1)' },
      ],
      { duration: 300, easing: 'ease-out' },
    );
  }
}
```

[Animate from code](/guide/animations/programmatic.md) covers the options and how to pause or cancel.

## Enter and leave

Give an element in an `@if` or `@for` Angular's `animate.enter` and `animate.leave` classes. See [Enter and leave transitions](/guide/animations/enter-leave.md).

Or wrap content in `<lynx-transition>` to animate it in when `show` turns `true`, and out before it's removed:

```angular-html
<lynx-transition [show]="visible()" name="fade" [duration]="300">
  <view class="panel">
    <text>I animate in and out</text>
  </view>
</lynx-transition>
```

The `fade-enter` and `fade-leave` classes each need a `@keyframes` animation. See [Enter and leave with `LynxTransition`](/guide/animations/lynx-transition.md).

## What can animate

Both `transition` and `@keyframes` animate only these properties:

| Group      | Properties                                                                                         |
| ---------- | -------------------------------------------------------------------------------------------------- |
| Position   | `left`, `right`, `top`, `bottom`                                                                   |
| Size       | `width`, `height`, `min-width`, `max-width`, `min-height`, `max-height`, `flex-basis`, `flex-grow` |
| Spacing    | `padding-top`, `padding-right`, `padding-bottom`, `padding-left`, and the same four for `margin`   |
| Borders    | `border-top-width` and the other three sides, `border-top-color` and the other three sides         |
| Appearance | `opacity`, `background-color`, `color`, `filter`                                                   |
| Transform  | `transform`, `transform-origin`                                                                    |

Any other property, such as `border-radius`, `font-size` or `letter-spacing`, doesn't animate.

## Related

- [Animate from code](/guide/animations/programmatic.md)
- [Enter and leave transitions](/guide/animations/enter-leave.md)
- [Enter and leave with `LynxTransition`](/guide/animations/lynx-transition.md)
- [Animation API reference](/api/angular-lynx/animation.md)
- [CSS on Lynx](/guide/styling/index.md)
- [Animations example](/examples/animations.md)
