|
| 1 | +- Start Date: 2023-07-04 |
| 2 | +- RFC PR: (leave this empty) |
| 3 | +- React Issue: (leave this empty) |
| 4 | + |
| 5 | +# Summary |
| 6 | + |
| 7 | +Introduce a built-in component for animations in React. |
| 8 | + |
| 9 | +# Basic example |
| 10 | + |
| 11 | +Proposed is the inclusion of a built-in `Animation` component that would handle various types of animations effectively. |
| 12 | + |
| 13 | +Please refer to this example for a demonstration: https://codesandbox.io/p/sandbox/o1mplp |
| 14 | + |
| 15 | +```jsx |
| 16 | +<Animation |
| 17 | + duration={500} |
| 18 | + fill="both" |
| 19 | + direction={show ? "normal" : "reverse"} |
| 20 | + keyframes={[ |
| 21 | + { opacity: 0, transform: "translateX(0px)" }, |
| 22 | + { opacity: 1, transform: "translateX(500px)" }, |
| 23 | + ]} |
| 24 | +> |
| 25 | + {show && <Card />} |
| 26 | +</Animation> |
| 27 | +``` |
| 28 | + |
| 29 | +The best part of in the above example is `show` state, the `show` state determines whether the `Card` component is displayed on the page. When `show` is set to `false`, the Card component performs the animation and disappears after completion. |
| 30 | + |
| 31 | +Animations can be composed together as shown in this example: https://codesandbox.io/p/sandbox/0rm9xk |
| 32 | + |
| 33 | +```jsx |
| 34 | +<Animation |
| 35 | + duration={2000} |
| 36 | + iterations={Infinity} |
| 37 | + keyframes={[ |
| 38 | + { borderRadius: 0, transform: "scale(0) rotate(0turn)" }, |
| 39 | + { borderRadius: "50%", transform: "scale(1) rotate(1turn)" }, |
| 40 | +]}> |
| 41 | + <Animation |
| 42 | + duration={1000} |
| 43 | + iterations={Infinity} |
| 44 | + keyframes={[{ backgroundColor: "red" }, { backgroundColor: "blue" }]} |
| 45 | + > |
| 46 | + <Card /> |
| 47 | + </Animation> |
| 48 | +</Animation> |
| 49 | +``` |
| 50 | + |
| 51 | +# Motivation |
| 52 | + |
| 53 | +In React projects, animations are often handled by external packages, each with their own advantages and disadvantages. Having a built-in component for animations in React would be beneficial as it can directly interact with low-level React APIs. |
| 54 | + |
| 55 | +# Detailed design |
| 56 | + |
| 57 | +The proposed built-in Animation component would handle animations on child components by utilizing DOM access. that's mean it dosen't need to re-render child components. Therefore, child components should use `forwardRef` to enable this feature. The advantage of this approach is that child components can be used with or without animation without requiring any changes. |
| 58 | + |
| 59 | +# Drawbacks |
| 60 | + |
| 61 | +Why should we *not* do this? Please consider: |
| 62 | + |
| 63 | +- Potential increase in the React package size |
| 64 | +- Possible argument that animations are not directly related to React itself |
| 65 | +- Existing third-party packages already available for handling animations |
| 66 | + |
| 67 | +# Alternatives |
| 68 | + |
| 69 | +For testing purposes, an alternative package called Reactima has been developed: https://www.npmjs.com/package/reactima |
| 70 | + |
| 71 | +# Adoption strategy |
| 72 | + |
| 73 | +Introducing the proposed built-in `Animation` component would not introduce any breaking changes and would have no impact on existing React features. |
| 74 | + |
| 75 | +# How we teach this |
| 76 | + |
| 77 | +All prop and method names are based on the `Web Animation API`, which can be referenced for further guidance: |
| 78 | + |
| 79 | +https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API |
0 commit comments