Skip to content

Commit ed5396f

Browse files
sgnyMoOx
authored andcommitted
LayoutAnimation: documentation and example (#484)
1 parent 11621a3 commit ed5396f

1 file changed

Lines changed: 133 additions & 55 deletions

File tree

Lines changed: 133 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,168 @@
11
---
22
id: apis/LayoutAnimation
33
title: LayoutAnimation
4-
wip: true
4+
wip: false
55
---
66

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
726
```reason
8-
type animationConfig;
9-
[@bs.obj]
10-
external animationConfig:
27+
layoutAnimationConfig:
1128
(
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,
1642
~_type: [@bs.string] [
1743
| `spring
1844
| `linear
1945
| `easeInEaseOut
2046
| `easeIn
2147
| `easeOut
2248
| `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+
```
3053

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-
"";
4354

44-
[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
45-
external configureNext: layoutAnimationConfig => unit = "";
4655

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
5157

52-
[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
53-
external create:
58+
```reason
59+
animationConfig:
5460
(
55-
~duration: float,
61+
~duration: float=?,
62+
~delay: float=?,
63+
~springDamping: float=?,
64+
~initialVelocity: float=?,
5665
~_type: [@bs.string] [
5766
| `spring
5867
| `linear
5968
| `easeInEaseOut
6069
| `easeIn
6170
| `easeOut
6271
| `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+
```
6878

69-
[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
70-
external easeInEaseOut: unit => unit = "";
7179

72-
[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
73-
external linear: unit => unit = "";
7480

75-
[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
76-
external spring: unit => unit = "";
81+
## Presets
7782

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+
```
7894

79-
module Presets = {
80-
[@bs.module "react-native"] [@bs.scope ("LayoutAnimation", "Presets")]
81-
external easeInEaseOut: layoutAnimationConfig = "";
8295

83-
[@bs.module "react-native"] [@bs.scope ("LayoutAnimation", "Presets")]
84-
external linear: layoutAnimationConfig = "";
8596

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>;
88158
};
159+
```
89160

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

Comments
 (0)