|
1 | 1 | --- |
2 | 2 | id: apis/LayoutAnimation |
3 | 3 | title: LayoutAnimation |
4 | | -wip: true |
| 4 | +wip: false |
5 | 5 | --- |
6 | 6 |
|
| 7 | +`LayoutAnimation` API offers a simpler alternative to `Animated` API. Instead of directly manipulating values for various style props, it suffices to specify the animation to be run before the next render. Specification of the animation should happen in the reducer, before `state` is updated. |
| 8 | + |
| 9 | +## Methods |
| 10 | + |
| 11 | +- `configureNext` is the method to specify the animation, takes an argument of type `layoutAnimationConfig`. |
| 12 | + |
| 13 | +```reason |
| 14 | +configureNext(layoutAnimationConfig) |
| 15 | +``` |
| 16 | + |
| 17 | +- `configureNextWithEndCallback` is a convenience function, which allows specification of a callback function (of type `unit => unit`) to be run after the animation, in addition to `layoutAnimationConfig`. |
| 18 | +```reason |
| 19 | +configureNextWithEndCallback(layoutAnimationConfig, callback) |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +## Types |
| 24 | + |
| 25 | +`layoutAnimationConfig` can be created with the `layoutAnimationConfig` constructor |
7 | 26 | ```reason |
8 | | -type animationConfig; |
9 | | -[@bs.obj] |
10 | | -external animationConfig: |
| 27 | +layoutAnimationConfig: |
11 | 28 | ( |
12 | | - ~duration: float=?, |
13 | | - ~delay: float=?, |
14 | | - ~springDamping: float=?, |
15 | | - ~initialVelocity: float=?, |
| 29 | + ~duration: float, |
| 30 | + ~create: animationConfig=?, |
| 31 | + ~update: animationConfig=?, |
| 32 | + ~delete: animationConfig=?, |
| 33 | + unit |
| 34 | + ) |
| 35 | +``` |
| 36 | +or by means of the helper function `create` |
| 37 | + |
| 38 | +```reason |
| 39 | +create: |
| 40 | + ( |
| 41 | + ~duration: float, |
16 | 42 | ~_type: [@bs.string] [ |
17 | 43 | | `spring |
18 | 44 | | `linear |
19 | 45 | | `easeInEaseOut |
20 | 46 | | `easeIn |
21 | 47 | | `easeOut |
22 | 48 | | `keyboard |
23 | | - ] |
24 | | - =?, |
25 | | - ~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY]=?, |
26 | | - unit |
27 | | - ) => |
28 | | - animationConfig = |
29 | | - ""; |
| 49 | + ], |
| 50 | + ~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY] |
| 51 | + ) |
| 52 | +``` |
30 | 53 |
|
31 | | -type layoutAnimationConfig; |
32 | | -[@bs.obj] |
33 | | -external layoutAnimationConfig: |
34 | | - ( |
35 | | - ~duration: float, |
36 | | - ~create: animationConfig=?, |
37 | | - ~update: animationConfig=?, |
38 | | - ~delete: animationConfig=?, |
39 | | - unit |
40 | | - ) => |
41 | | - layoutAnimationConfig = |
42 | | - ""; |
43 | 54 |
|
44 | | -[@bs.module "react-native"] [@bs.scope "LayoutAnimation"] |
45 | | -external configureNext: layoutAnimationConfig => unit = ""; |
46 | 55 |
|
47 | | -[@bs.module "react-native"] [@bs.scope "LayoutAnimation"] |
48 | | -external configureNextWithEndCallback: |
49 | | - (layoutAnimationConfig, unit => unit) => unit = |
50 | | - "configureNext"; |
| 56 | +`animationConfig` can in turn be created with the `animationConfig` constructor |
51 | 57 |
|
52 | | -[@bs.module "react-native"] [@bs.scope "LayoutAnimation"] |
53 | | -external create: |
| 58 | +```reason |
| 59 | +animationConfig: |
54 | 60 | ( |
55 | | - ~duration: float, |
| 61 | + ~duration: float=?, |
| 62 | + ~delay: float=?, |
| 63 | + ~springDamping: float=?, |
| 64 | + ~initialVelocity: float=?, |
56 | 65 | ~_type: [@bs.string] [ |
57 | 66 | | `spring |
58 | 67 | | `linear |
59 | 68 | | `easeInEaseOut |
60 | 69 | | `easeIn |
61 | 70 | | `easeOut |
62 | 71 | | `keyboard |
63 | | - ], |
64 | | - ~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY] |
65 | | - ) => |
66 | | - layoutAnimationConfig = |
67 | | - ""; |
| 72 | + ] |
| 73 | + =?, |
| 74 | + ~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY]=?, |
| 75 | + unit |
| 76 | + ) |
| 77 | +``` |
68 | 78 |
|
69 | | -[@bs.module "react-native"] [@bs.scope "LayoutAnimation"] |
70 | | -external easeInEaseOut: unit => unit = ""; |
71 | 79 |
|
72 | | -[@bs.module "react-native"] [@bs.scope "LayoutAnimation"] |
73 | | -external linear: unit => unit = ""; |
74 | 80 |
|
75 | | -[@bs.module "react-native"] [@bs.scope "LayoutAnimation"] |
76 | | -external spring: unit => unit = ""; |
| 81 | +## Presets |
77 | 82 |
|
| 83 | +There are presets for `linear`, `spring` and `easeInEaseOut` transitions which allow a very straightforward way to setup animation. Presets may either be passed as ready-made `layoutAnimationConfig` to `configureNext` and `configureNextWithEndCallback` as below |
| 84 | + |
| 85 | +```reason |
| 86 | +LayoutAnimation.configureNext(LayoutAnimation.Presets.spring) |
| 87 | +``` |
| 88 | +or equivalently as already passed to `configureNext` as |
| 89 | + |
| 90 | +```reason |
| 91 | +LayoutAnimation.spring() |
| 92 | +
|
| 93 | +``` |
78 | 94 |
|
79 | | -module Presets = { |
80 | | - [@bs.module "react-native"] [@bs.scope ("LayoutAnimation", "Presets")] |
81 | | - external easeInEaseOut: layoutAnimationConfig = ""; |
82 | 95 |
|
83 | | - [@bs.module "react-native"] [@bs.scope ("LayoutAnimation", "Presets")] |
84 | | - external linear: layoutAnimationConfig = ""; |
85 | 96 |
|
86 | | - [@bs.module "react-native"] [@bs.scope ("LayoutAnimation", "Presets")] |
87 | | - external spring: layoutAnimationConfig = ""; |
| 97 | +## Example |
| 98 | + |
| 99 | +The example below illustrates animated transition (`spring`) between two views, such as registration and login forms. Animation is specified in the reducer, as below. Making use of `presets` is also illustrated (commented out). |
| 100 | + |
| 101 | + |
| 102 | +```reason |
| 103 | +open ReactNative; |
| 104 | +
|
| 105 | +let windowWidth = float_of_int(Dimensions.get(`window)##width); |
| 106 | +
|
| 107 | +type state = {register: bool}; |
| 108 | +
|
| 109 | +type action = |
| 110 | + | ToggleRegister; |
| 111 | +
|
| 112 | +[@react.component] |
| 113 | +let make = () => { |
| 114 | + let (state, dispatch) = |
| 115 | + React.useReducer( |
| 116 | + (state, action) => |
| 117 | + switch (action) { |
| 118 | + | ToggleRegister => |
| 119 | + // Animation should be specified here, before state is updated: |
| 120 | + LayoutAnimation.configureNext( |
| 121 | + LayoutAnimation.create( |
| 122 | + ~duration=500., |
| 123 | + ~_type=`spring, |
| 124 | + ~property=`opacity, |
| 125 | + ), |
| 126 | + ); |
| 127 | + // update of the state happens below: |
| 128 | + {register: !state.register}; |
| 129 | + }, |
| 130 | + {register: false}, |
| 131 | + ); |
| 132 | +
|
| 133 | + <View style={Style.style(~flex=1., ~flexDirection=`column, ())}> |
| 134 | + <View |
| 135 | + style={Style.style( |
| 136 | + ~flex=1., |
| 137 | + ~width=Style.pt(2.0 *. windowWidth), |
| 138 | + ~left= |
| 139 | + Style.pt( |
| 140 | + if (state.register) { |
| 141 | + 0.; |
| 142 | + } else { |
| 143 | + 0. -. windowWidth; |
| 144 | + }, |
| 145 | + ), |
| 146 | + ~flexDirection=`row, |
| 147 | + (), |
| 148 | + )}> |
| 149 | + <View style={Style.style(~width=Style.pt(windowWidth), ())}> |
| 150 | + <Register /> |
| 151 | + </View> |
| 152 | + <View style={Style.style(~width=Style.pt(windowWidth), ())}> |
| 153 | + <Login /> |
| 154 | + </View> |
| 155 | + </View> |
| 156 | + <Button onPress={_ => dispatch(ToggleRegister)} title={js|Toggle|js} /> |
| 157 | + </View>; |
88 | 158 | }; |
| 159 | +``` |
89 | 160 |
|
90 | | -``` |
| 161 | +Above animation specification is that of the `spring` preset. Accordingly, the animation could have been specified as |
| 162 | +```reason |
| 163 | +LayoutAnimation.configureNext(LayoutAnimation.Presets.spring); |
| 164 | +``` |
| 165 | +or equivalently as |
| 166 | +```reason |
| 167 | +LayoutAnimation.spring(); |
| 168 | +``` |
0 commit comments