forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutAnimationExample.js
More file actions
221 lines (198 loc) · 4.6 KB
/
LayoutAnimationExample.js
File metadata and controls
221 lines (198 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const React = require('react');
const {
LayoutAnimation,
StyleSheet,
Text,
View,
TouchableOpacity,
} = require('react-native');
class AddRemoveExample extends React.Component<{...}, $FlowFixMeState> {
state = {
views: [],
};
UNSAFE_componentWillUpdate() {
LayoutAnimation.easeInEaseOut(args =>
console.log('AddRemoveExample completed', args),
);
}
_onPressAddView = () => {
this.setState(state => ({views: [...state.views, {}]}));
};
_onPressRemoveView = () => {
this.setState(state => ({views: state.views.slice(0, -1)}));
};
render() {
const views = this.state.views.map((view, i) => (
<View key={i} style={styles.view}>
<Text>{i}</Text>
</View>
));
return (
<View style={styles.container}>
<TouchableOpacity onPress={this._onPressAddView}>
<View style={styles.button}>
<Text>Add view</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={this._onPressRemoveView}>
<View style={styles.button}>
<Text>Remove view</Text>
</View>
</TouchableOpacity>
<View style={styles.viewContainer}>{views}</View>
</View>
);
}
}
const GreenSquare = () => (
<View style={styles.greenSquare}>
<Text>Green square</Text>
</View>
);
const BlueSquare = () => (
<View style={styles.blueSquare}>
<Text>Blue square</Text>
</View>
);
class CrossFadeExample extends React.Component<{...}, $FlowFixMeState> {
state = {
toggled: false,
};
_onPressToggle = () => {
LayoutAnimation.easeInEaseOut(args =>
console.log('CrossFadeExample completed', args),
);
this.setState(state => ({toggled: !state.toggled}));
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this._onPressToggle}>
<View style={styles.button}>
<Text>Toggle</Text>
</View>
</TouchableOpacity>
<View style={styles.viewContainer}>
{this.state.toggled ? <GreenSquare /> : <BlueSquare />}
</View>
</View>
);
}
}
class LayoutUpdateExample extends React.Component<{...}, $FlowFixMeState> {
state = {
width: 200,
height: 100,
};
timeout = null;
componentWillUnmount() {
this._clearTimeout();
}
_clearTimeout = () => {
if (this.timeout !== null) {
clearTimeout(this.timeout);
this.timeout = null;
}
};
_onPressToggle = () => {
this._clearTimeout();
this.setState({width: 150});
LayoutAnimation.configureNext(
{
duration: 1000,
update: {
type: LayoutAnimation.Types.linear,
},
},
args => console.log('LayoutUpdateExample completed', args),
);
this.timeout = setTimeout(() => this.setState({width: 100}), 500);
};
render() {
const {width, height} = this.state;
return (
<View style={styles.container}>
<TouchableOpacity onPress={this._onPressToggle}>
<View style={styles.button}>
<Text>Make box square</Text>
</View>
</TouchableOpacity>
<View style={[styles.view, {width, height}]}>
<Text>
{width}x{height}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
button: {
borderRadius: 5,
backgroundColor: '#eeeeee',
padding: 10,
marginBottom: 10,
},
viewContainer: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
},
view: {
height: 54,
width: 54,
backgroundColor: 'red',
margin: 8,
alignItems: 'center',
justifyContent: 'center',
},
greenSquare: {
width: 150,
height: 150,
backgroundColor: 'green',
alignItems: 'center',
justifyContent: 'center',
},
blueSquare: {
width: 150,
height: 150,
backgroundColor: 'blue',
alignItems: 'center',
justifyContent: 'center',
},
});
exports.title = 'Layout Animation';
exports.description = 'Layout animation';
exports.examples = [
{
title: 'Add and remove views',
render(): React.Element<any> {
return <AddRemoveExample />;
},
},
{
title: 'Cross fade views',
render(): React.Element<any> {
return <CrossFadeExample />;
},
},
{
title: 'Layout update during animation',
render(): React.Element<any> {
return <LayoutUpdateExample />;
},
},
];