Skip to content

[Web LA] Adjust animation cleanup to slower devices - #7933

Merged
m-bert merged 2 commits into
mainfrom
@mbert/web-la-fix
Jul 29, 2025
Merged

m-bert merged 2 commits into
mainfrom
@mbert/web-la-fix

Conversation

@m-bert

@m-bert m-bert commented Jul 29, 2025 •

Copy link
Copy Markdown
Contributor

Summary

Turns out that on slower devices when animation lags, it can be removed from DOM before it finishes. This results in callbacks not being called and dummy being left instead of removed.

This PR:

  1. Adjusts timeout value for slower devices,
  2. Removes dummy elements when their animation is removed from DOM

Test plan

Tested on the following code, with 20x throttling enabled in chrome dev tools:
import { useState } from 'react';
import { Button, View } from 'react-native';
import Animated, { Keyframe, Easing } from 'react-native-reanimated';

function Square() {
  const [visible, setVisible] = useState(false);

  const easing = Easing.bezier(0.76, 0.0, 0.24, 1.0).factory();
  const animation = new Keyframe({
    from: { transform: [{ translateX: '0%' }] },
    to: {
      transform: [{ translateX: '100%' }],
      easing,
    },
  });

  return (
    <View
      style={{
        padding: 20,
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
      }}>
      <Button
        title="Toggle"
        onPress={() => setVisible((visible) => !visible)}
      />
      {visible && (
        <Animated.View
          style={{
            backgroundColor: 'red',
            width: 200,
            height: 200,
            flexDirection: 'row',
            flexWrap: 'wrap',
          }}
          exiting={animation
            .duration(300)
            .withCallback(() => console.log('callback!'))}>
          {Array(10000)
            .fill(0)
            .map((_, i) => (
              <View
                key={i}
                style={{ backgroundColor: 'yellow', width: 2, height: 2 }}
              />
            ))}
        </Animated.View>
      )}
    </View>
  );
}

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <Square />
    </View>
  );
}

@MatiPl01 MatiPl01 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@m-bert
m-bert added this pull request to the merge queue Jul 29, 2025
Merged via the queue into main with commit 3a8df89 Jul 29, 2025
@m-bert
m-bert deleted the @mbert/web-la-fix branch July 29, 2025 13:50
github-merge-queue Bot pushed a commit that referenced this pull request Jul 30, 2025
… already (#7940)

## Summary

Follows up
#7933

Currently when the web animation is removed in the timeout
(scheduleAnimationCleanup) before it has started, neither of the
end/cancel events are fired, and so the callback is never called.

The PR also ensures the callback is called always exactly once.

## Test plan

20x CPU throttling, check if the callback is called every time. also I'd
recommend setting timeoutScale to a lower value temporarily to help
reproducing the issue (the timeout has to run before the animation is
started by the browser)

<details>
<summary>Code</summary>

```tsx
import { useState } from 'react';
import { Button, View } from 'react-native';
import Animated, { Keyframe, Easing } from 'react-native-reanimated';

function Square() {
  const [visible, setVisible] = useState(false);

  const easing = Easing.bezier(0.76, 0.0, 0.24, 1.0).factory();
  const animation = new Keyframe({
    from: { transform: [{ translateX: '0%' }] },
    to: {
      transform: [{ translateX: '100%' }],
      easing,
    },
  });

  return (
    <View
      style={{
        padding: 20,
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
      }}>
      <Button
        title="Toggle"
        onPress={() => setVisible((visible) => !visible)}
      />
      {visible && (
        <Animated.View
          style={{
            backgroundColor: 'red',
            width: 200,
            height: 200,
            flexDirection: 'row',
            flexWrap: 'wrap',
          }}
          exiting={animation
            .duration(300)
            .withCallback(() => console.log('callback!'))}>
          {Array(10000)
            .fill(0)
            .map((_, i) => (
              <View
                key={i}
                style={{ backgroundColor: 'yellow', width: 2, height: 2 }}
              />
            ))}
        </Animated.View>
      )}
    </View>
  );
}

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <Square />
    </View>
  );
}
```
</details>
@MatiPl01 MatiPl01 mentioned this pull request Aug 4, 2025
10 tasks done
MatiPl01 pushed a commit that referenced this pull request Aug 5, 2025
## Summary

Turns out that on slower devices when animation lags, it can be removed
from DOM before it finishes. This results in callbacks not being called
and `dummy` being left instead of removed.

This PR:

1. Adjusts timeout value for slower devices,
2. Removes dummy elements when their animation is removed from DOM

## Test plan

<details>
<summary>Tested on the following code, with 20x throttling enabled in
chrome dev tools:</summary>

```jsx
import { useState } from 'react';
import { Button, View } from 'react-native';
import Animated, { Keyframe, Easing } from 'react-native-reanimated';

function Square() {
  const [visible, setVisible] = useState(false);

  const easing = Easing.bezier(0.76, 0.0, 0.24, 1.0).factory();
  const animation = new Keyframe({
    from: { transform: [{ translateX: '0%' }] },
    to: {
      transform: [{ translateX: '100%' }],
      easing,
    },
  });

  return (
    <View
      style={{
        padding: 20,
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
      }}>
      <Button
        title="Toggle"
        onPress={() => setVisible((visible) => !visible)}
      />
      {visible && (
        <Animated.View
          style={{
            backgroundColor: 'red',
            width: 200,
            height: 200,
            flexDirection: 'row',
            flexWrap: 'wrap',
          }}
          exiting={animation
            .duration(300)
            .withCallback(() => console.log('callback!'))}>
          {Array(10000)
            .fill(0)
            .map((_, i) => (
              <View
                key={i}
                style={{ backgroundColor: 'yellow', width: 2, height: 2 }}
              />
            ))}
        </Animated.View>
      )}
    </View>
  );
}

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <Square />
    </View>
  );
}
```

</details>
MatiPl01 pushed a commit that referenced this pull request Aug 5, 2025
… already (#7940)

## Summary

Follows up
#7933

Currently when the web animation is removed in the timeout
(scheduleAnimationCleanup) before it has started, neither of the
end/cancel events are fired, and so the callback is never called.

The PR also ensures the callback is called always exactly once.

## Test plan

20x CPU throttling, check if the callback is called every time. also I'd
recommend setting timeoutScale to a lower value temporarily to help
reproducing the issue (the timeout has to run before the animation is
started by the browser)

<details>
<summary>Code</summary>

```tsx
import { useState } from 'react';
import { Button, View } from 'react-native';
import Animated, { Keyframe, Easing } from 'react-native-reanimated';

function Square() {
  const [visible, setVisible] = useState(false);

  const easing = Easing.bezier(0.76, 0.0, 0.24, 1.0).factory();
  const animation = new Keyframe({
    from: { transform: [{ translateX: '0%' }] },
    to: {
      transform: [{ translateX: '100%' }],
      easing,
    },
  });

  return (
    <View
      style={{
        padding: 20,
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
      }}>
      <Button
        title="Toggle"
        onPress={() => setVisible((visible) => !visible)}
      />
      {visible && (
        <Animated.View
          style={{
            backgroundColor: 'red',
            width: 200,
            height: 200,
            flexDirection: 'row',
            flexWrap: 'wrap',
          }}
          exiting={animation
            .duration(300)
            .withCallback(() => console.log('callback!'))}>
          {Array(10000)
            .fill(0)
            .map((_, i) => (
              <View
                key={i}
                style={{ backgroundColor: 'yellow', width: 2, height: 2 }}
              />
            ))}
        </Animated.View>
      )}
    </View>
  );
}

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <Square />
    </View>
  );
}
```
</details>
MatiPl01 added a commit that referenced this pull request Aug 5, 2025
)

Cherry-pick of the #7933

Co-authored-by: Michał Bert <63123542+m-bert@users.noreply.github.com>
MatiPl01 pushed a commit that referenced this pull request Aug 5, 2025
… already (#7940)

## Summary

Follows up
#7933

Currently when the web animation is removed in the timeout
(scheduleAnimationCleanup) before it has started, neither of the
end/cancel events are fired, and so the callback is never called.

The PR also ensures the callback is called always exactly once.

## Test plan

20x CPU throttling, check if the callback is called every time. also I'd
recommend setting timeoutScale to a lower value temporarily to help
reproducing the issue (the timeout has to run before the animation is
started by the browser)

<details>
<summary>Code</summary>

```tsx
import { useState } from 'react';
import { Button, View } from 'react-native';
import Animated, { Keyframe, Easing } from 'react-native-reanimated';

function Square() {
  const [visible, setVisible] = useState(false);

  const easing = Easing.bezier(0.76, 0.0, 0.24, 1.0).factory();
  const animation = new Keyframe({
    from: { transform: [{ translateX: '0%' }] },
    to: {
      transform: [{ translateX: '100%' }],
      easing,
    },
  });

  return (
    <View
      style={{
        padding: 20,
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
      }}>
      <Button
        title="Toggle"
        onPress={() => setVisible((visible) => !visible)}
      />
      {visible && (
        <Animated.View
          style={{
            backgroundColor: 'red',
            width: 200,
            height: 200,
            flexDirection: 'row',
            flexWrap: 'wrap',
          }}
          exiting={animation
            .duration(300)
            .withCallback(() => console.log('callback!'))}>
          {Array(10000)
            .fill(0)
            .map((_, i) => (
              <View
                key={i}
                style={{ backgroundColor: 'yellow', width: 2, height: 2 }}
              />
            ))}
        </Animated.View>
      )}
    </View>
  );
}

export default function App() {
  return (
    <View style={{ flex: 1 }}>
      <Square />
    </View>
  );
}
```
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants