# AnimationsContext

`AnimationsContext` holds the animations the SDK's components use, starting with their layout transitions. Set them with `WithAnimations`, and read them with `useAnimationsContext()`. Without a `WithAnimations`, every component uses the defaults.

A layout transition animates a view from its old position and size to its new one, for example when the composer grows because an attachment was added. Each one belongs to a use case, such as `composer` or `messageList`, and you can replace it or turn it off.

## Best practices

- Declare the `overrides` object once, at module scope. `WithAnimations` reads it once, when it mounts.
- Type it with `satisfies AnimationOverrides`, so a misspelled key is a type error and your editor suggests the keys.
- Turn a transition off with `null`. A key you leave out, or set to `undefined`, keeps the value from the `WithAnimations` above.
- Put `WithAnimations` above the components it should reach. Around `OverlayProvider`, it covers the whole chat.

## Basic usage

Turn off the composer's transitions and keep the rest:

```tsx title="src/AppAnimations.tsx"
import type { PropsWithChildren } from "react";
import { WithAnimations } from "stream-chat-react-native";
import type { AnimationOverrides } from "stream-chat-react-native";

// Declared once, at module scope.
const animationOverrides = {
  transitions: {
    composer: { layout: null },
    composerContent: { layout: null },
  },
} satisfies AnimationOverrides;

export const AppAnimations = ({ children }: PropsWithChildren) => (
  <WithAnimations overrides={animationOverrides}>{children}</WithAnimations>
);
```

### Turning off every transition

`noTransitions` sets every transition to `null`:

```tsx
import { noTransitions, WithAnimations } from "stream-chat-react-native";

<WithAnimations overrides={noTransitions}>
  <Chat client={client}>{/* ... */}</Chat>
</WithAnimations>;
```

### Using your own transition

A transition is any Reanimated layout transition, so it can match the motion in the rest of your app:

```tsx
import { LinearTransition } from "react-native-reanimated";
import type { AnimationOverrides } from "stream-chat-react-native";

const animationOverrides = {
  transitions: {
    attachmentUploadPreviews: {
      layout: LinearTransition.springify().damping(18),
    },
  },
} satisfies AnimationOverrides;
```

### Reading a transition in your own component

A component of your own can use the same transition as the part of the SDK it sits next to:

```tsx title="src/ComposerBanner.tsx"
import Animated from "react-native-reanimated";
import { useAnimationsContext } from "stream-chat-react-native";

export const ComposerBanner = () => {
  const { transitions } = useAnimationsContext();

  return (
    <Animated.View layout={transitions.composerContent.layout}>
      {/* ... */}
    </Animated.View>
  );
};
```

## Transitions

Every transition defaults to `LinearTransition.duration(200)`, except `pollComposerOption`, which uses `LinearTransition.duration(200).springify()`.

| Key                        | What it animates                                                                                                                                                          |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `messageList`              | The message list itself. When it's off, `MessageList` renders a plain `FlatList`, the same as `animateLayout={false}`.                                                    |
| `scrollToBottomButton`     | The container of the scroll to bottom button.                                                                                                                             |
| `suggestionList`           | The container the autocomplete suggestion list is positioned in.                                                                                                          |
| `suggestionListContent`    | The autocomplete suggestion list inside its container.                                                                                                                    |
| `composer`                 | The outer container of the message composer.                                                                                                                              |
| `composerContent`          | The views inside the message composer: the input box, its header, the leading and trailing views, the command chip, link previews and the audio recording lock indicator. |
| `attachmentUploadPreviews` | The attachment previews in the message composer.                                                                                                                          |
| `notificationList`         | The notification list.                                                                                                                                                    |
| `pollComposer`             | The sections of the poll creation screen.                                                                                                                                 |
| `pollComposerOption`       | Each option on the poll creation screen.                                                                                                                                  |

Each key holds a `layout` field. `MessageList`'s own `animateLayout` prop still wins when you pass it.

## WithAnimations

| Prop        | Type                 | Default  | Description                                                                                                                                                                                                   |
| ----------- | -------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `overrides` | `AnimationOverrides` | required | The animations to set for everything below. They're merged over the values from a `WithAnimations` above, field by field, and the closest one wins. Unknown keys are a type error. Read once, when it mounts. |

`WithAnimations` also renders its `children`.

## useAnimationsContext

`useAnimationsContext()` returns an `Animations` object: every transition, with the overrides in effect merged over the defaults. A transition that's turned off is `undefined`, which is what Reanimated's `layout` prop expects for no transition. It never throws. Outside every `WithAnimations`, it returns the defaults.

## Types

| Type                 | What it is                                                                                               |
| -------------------- | -------------------------------------------------------------------------------------------------------- |
| `AnimationOverrides` | The type of `overrides`. Every key is optional at every level, and every transition also accepts `null`. |
| `Animations`         | The type `useAnimationsContext()` returns. Every key is defined.                                         |
| `Transitions`        | The transitions by use case, as in the table above.                                                      |
| `TransitionConfig`   | The transitions of one use case. For now, `layout`.                                                      |
| `LayoutTransition`   | A Reanimated layout transition, the type of an animated view's `layout` prop.                            |

`DEFAULT_ANIMATIONS` holds the defaults, and `noTransitions` is the override that turns every transition off.

## Related

- [Keyboard and insets](https://getstream.io/chat/docs/sdk/react-native/ui/layout/keyboard-and-insets/#layout-transitions-and-the-keyboard) shows when to turn transitions off for a keyboard view that moves the chat on every frame.
- [ComponentsContext](https://getstream.io/chat/docs/sdk/react-native/component-reference/channel/componentscontext/) works the same way for the components the SDK renders.

---

For the most recent version of this documentation, visit [https://getstream.io/chat/docs/sdk/react-native/component-reference/channel/animationscontext/](https://getstream.io/chat/docs/sdk/react-native/component-reference/channel/animationscontext/).