forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LayoutAnimation: documentation and example (facebook#484)
- Loading branch information
Showing
1 changed file
with
133 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |