Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom keyframe animations for web #5277

Closed
wants to merge 35 commits into from
Closed

Conversation

m-bert
Copy link
Contributor

@m-bert m-bert commented Oct 19, 2023

Caution

This PR was suppressed by #6135

Summary

This PR adds support for custom layout animations created via Keyframe on web.

Known limitations

Currently originX and originY don't work because in CSS there's only transform-origin property which needs both of those values at the same time (current approach doesn't make it easy to merge them).

Test plan

Tested on example app and the following code snippet:

Test code
import { StyleSheet, View, Text, Pressable } from 'react-native';

import React, { useState } from 'react';
import Animated, { Easing, Keyframe } from 'react-native-reanimated';

const entering = new Keyframe({
  0: {
    transform: [
      { translateX: -500 },
      { translateY: -300 },
      { scale: 1.25 },
      { skewY: '25deg' },
    ],
    opacity: 0.3,
    easing: Easing.cubic,
  },

  70: {
    transform: [{ translateX: 250 }, { translateY: 100 }, { scale: 1.25 }],
    opacity: 0.7,
  },
});
const exiting = new Keyframe({
  0: {
    easing: Easing.exp,
  },
  100: {
    transform: [
      { translateX: 700 },
      { translateY: 250 },
      { scale: 0.3 },
      { rotate: '225deg' },
    ],
    opacity: 0,
  },
});

export default function EmptyExample() {
  const [show, setShow] = useState(true);
  return (
    <View style={styles.container}>
      {show && (
        <Animated.View
          entering={entering.duration(1000)}
          exiting={exiting}
          style={styles.box}
        />
      )}

      <Pressable onPress={() => setShow((prev) => !prev)} style={styles.button}>
        <Text style={{ color: 'white' }}> Click me! </Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 250,
    height: 250,
    backgroundColor: '#782aeb',
  },
  button: {
    width: 100,
    height: 50,
    backgroundColor: '#57b495',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'space-around',
    borderRadius: 15,

    position: 'absolute',
    top: 10,
    left: 10,
  },
});

@m-bert m-bert marked this pull request as ready for review March 11, 2024 10:19
@m-bert m-bert requested a review from piaskowyk March 11, 2024 10:19
Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

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

Solid PR 🤝
Have you thought about implementing OriginX/Y as position absolute?

github-merge-queue bot pushed a commit that referenced this pull request May 29, 2024
## Summary

A while ago I've noticed that layout animations on mobile throw warning
if one tries to add animation into `View` that has `transform` property.
Now, while working on #5277 I've decided to remove code responsible for
applying existing transform on web. Here are the reasons why I believe
it is good idea:

1. It unifies behavior with mobile platforms, which are main target of
`reanimated`.
2. Some of `transforms` were not applied correctly and required
additional calculations to be implemented (like `skew`).
3. Removing existing transforms means removing a lot of `ifs` and
unnecessary code.

## Test plan

Tested on example app on `LA` examples.
@m-bert
Copy link
Contributor Author

m-bert commented Jun 18, 2024

I'm closing this PR since there are some conflicts after introducing monorepo structure (also my workspace in vscode froze while trying to merge current main into this branch).

These changes will be added in #6135.

@m-bert m-bert closed this Jun 18, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jun 26, 2024
## Summary

This PR adds support for custom layout animations created via `Keyframe`
on web.

> [!IMPORTANT]
> This PR replaces #5277 

## Test plan

Tested on example app and the following code snippet:

<details>
<summary> Test code </summary>

```jsx
import { StyleSheet, View, Text, Pressable } from 'react-native';

import React, { useState } from 'react';
import Animated, { Easing, Keyframe } from 'react-native-reanimated';

const entering = new Keyframe({
  0: {
    transform: [
      { translateX: -500 },
      { translateY: -300 },
      { scale: 1.25 },
      { skewY: '25deg' },
    ],
    opacity: 0.3,
    easing: Easing.cubic,
  },

  70: {
    transform: [{ translateX: 250 }, { translateY: 100 }, { scale: 1.25 }],
    opacity: 0.7,
  },
});
const exiting = new Keyframe({
  0: {
    easing: Easing.exp,
  },
  100: {
    transform: [
      { translateX: 700 },
      { translateY: 250 },
      { scale: 0.3 },
      { rotate: '225deg' },
    ],
    opacity: 0,
  },
});

export default function EmptyExample() {
  const [show, setShow] = useState(true);
  return (
    <View style={styles.container}>
      {show && (
        <Animated.View
          entering={entering.duration(1000)}
          exiting={exiting}
          style={styles.box}
        />
      )}

      <Pressable onPress={() => setShow((prev) => !prev)} style={styles.button}>
        <Text style={{ color: 'white' }}> Click me! </Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 250,
    height: 250,
    backgroundColor: '#782aeb',
  },
  button: {
    width: 100,
    height: 50,
    backgroundColor: '#57b495',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'space-around',
    borderRadius: 15,

    position: 'absolute',
    top: 10,
    left: 10,
  },
});

```

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants