Skip to content

Commit

Permalink
LayoutAnimation: documentation and example (facebook#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgny authored and MoOx committed May 8, 2019
1 parent 11621a3 commit ed5396f
Showing 1 changed file with 133 additions and 55 deletions.
188 changes: 133 additions & 55 deletions reason-react-native/src/apis/LayoutAnimation.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,168 @@
---
id: apis/LayoutAnimation
title: LayoutAnimation
wip: true
wip: false
---

`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.

## Methods

- `configureNext` is the method to specify the animation, takes an argument of type `layoutAnimationConfig`.

```reason
configureNext(layoutAnimationConfig)
```

- `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`.
```reason
configureNextWithEndCallback(layoutAnimationConfig, callback)
```


## Types

`layoutAnimationConfig` can be created with the `layoutAnimationConfig` constructor
```reason
type animationConfig;
[@bs.obj]
external animationConfig:
layoutAnimationConfig:
(
~duration: float=?,
~delay: float=?,
~springDamping: float=?,
~initialVelocity: float=?,
~duration: float,
~create: animationConfig=?,
~update: animationConfig=?,
~delete: animationConfig=?,
unit
)
```
or by means of the helper function `create`

```reason
create:
(
~duration: float,
~_type: [@bs.string] [
| `spring
| `linear
| `easeInEaseOut
| `easeIn
| `easeOut
| `keyboard
]
=?,
~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY]=?,
unit
) =>
animationConfig =
"";
],
~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY]
)
```

type layoutAnimationConfig;
[@bs.obj]
external layoutAnimationConfig:
(
~duration: float,
~create: animationConfig=?,
~update: animationConfig=?,
~delete: animationConfig=?,
unit
) =>
layoutAnimationConfig =
"";

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

[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
external configureNextWithEndCallback:
(layoutAnimationConfig, unit => unit) => unit =
"configureNext";
`animationConfig` can in turn be created with the `animationConfig` constructor

[@bs.module "react-native"] [@bs.scope "LayoutAnimation"]
external create:
```reason
animationConfig:
(
~duration: float,
~duration: float=?,
~delay: float=?,
~springDamping: float=?,
~initialVelocity: float=?,
~_type: [@bs.string] [
| `spring
| `linear
| `easeInEaseOut
| `easeIn
| `easeOut
| `keyboard
],
~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY]
) =>
layoutAnimationConfig =
"";
]
=?,
~property: [@bs.string] [ | `opacity | `scaleX | `scaleY | `scaleXY]=?,
unit
)
```

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

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

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

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

```reason
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring)
```
or equivalently as already passed to `configureNext` as

```reason
LayoutAnimation.spring()
```

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

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

[@bs.module "react-native"] [@bs.scope ("LayoutAnimation", "Presets")]
external spring: layoutAnimationConfig = "";
## Example

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).


```reason
open ReactNative;
let windowWidth = float_of_int(Dimensions.get(`window)##width);
type state = {register: bool};
type action =
| ToggleRegister;
[@react.component]
let make = () => {
let (state, dispatch) =
React.useReducer(
(state, action) =>
switch (action) {
| ToggleRegister =>
// Animation should be specified here, before state is updated:
LayoutAnimation.configureNext(
LayoutAnimation.create(
~duration=500.,
~_type=`spring,
~property=`opacity,
),
);
// update of the state happens below:
{register: !state.register};
},
{register: false},
);
<View style={Style.style(~flex=1., ~flexDirection=`column, ())}>
<View
style={Style.style(
~flex=1.,
~width=Style.pt(2.0 *. windowWidth),
~left=
Style.pt(
if (state.register) {
0.;
} else {
0. -. windowWidth;
},
),
~flexDirection=`row,
(),
)}>
<View style={Style.style(~width=Style.pt(windowWidth), ())}>
<Register />
</View>
<View style={Style.style(~width=Style.pt(windowWidth), ())}>
<Login />
</View>
</View>
<Button onPress={_ => dispatch(ToggleRegister)} title={js|Toggle|js} />
</View>;
};
```

```
Above animation specification is that of the `spring` preset. Accordingly, the animation could have been specified as
```reason
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
```
or equivalently as
```reason
LayoutAnimation.spring();
```

0 comments on commit ed5396f

Please sign in to comment.