Skip to content

Commit f7eaf63

Browse files
Biki-dasfacebook-github-bot
authored andcommitted
- added combining animted values example to the animated api example list (#43435)
Summary: Added an example to add ```combining``` values with the animated API ## Changelog: [General] [Added] - Add examples for Combining Animated Values with the Animated API Pull Request resolved: #43435 Test Plan: https://github.com/facebook/react-native/assets/72331432/c4a0c1e2-df9d-4e73-8602-e4550a22b8dc https://github.com/facebook/react-native/assets/72331432/d2f1196e-68b0-49d4-8d3d-2c53ee30652b Reviewed By: cortinico Differential Revision: D55917557 Pulled By: hoxyq fbshipit-source-id: d4214323f2a8823b1b3fe0c8d90a7518d0953b48
1 parent 244eb42 commit f7eaf63

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

packages/rn-tester/js/examples/Animated/AnimatedIndex.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {RNTesterModule} from '../../types/RNTesterTypes';
1212

1313
import ColorStylesExample from './ColorStylesExample';
14+
import CombineExample from './CombineExample';
1415
import ComposeAnimationsWithEasingExample from './ComposeAnimationsWithEasingExample';
1516
import ComposingExample from './ComposingExample';
1617
import ContinuousInteractionsExample from './ContinuousInteractionsExample';
@@ -43,5 +44,6 @@ export default ({
4344
TransformBounceExample,
4445
LoopingExample,
4546
ContinuousInteractionsExample,
47+
CombineExample,
4648
],
4749
}: RNTesterModule);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
12+
import type {Numeric} from 'react-native/Libraries/Animated/AnimatedImplementation';
13+
14+
import RNTesterButton from '../../components/RNTesterButton';
15+
import * as React from 'react';
16+
import {Animated, StyleSheet, Text, TextInput, View} from 'react-native';
17+
export default ({
18+
title: 'Combine Example',
19+
name: 'Combine View',
20+
description:
21+
'Change the opacity of the view by combining different Animated.Values.',
22+
render: () => <CombineExample />,
23+
}: RNTesterModuleExample);
24+
25+
const CombineExample = () => {
26+
const [aValue, setAValue] = React.useState('0.4');
27+
const [bValue, setBValue] = React.useState('0.5');
28+
const a = new Animated.Value(parseFloat(aValue));
29+
const b = new Animated.Value(parseFloat(bValue));
30+
const add = Animated.add(a, b);
31+
const subtract = Animated.subtract(b, a);
32+
const mult = Animated.multiply(a, b);
33+
const divide =
34+
parseFloat(aValue) !== 0 ? Animated.divide(b, a) : new Animated.Value(1);
35+
const mod = Animated.modulo(a, parseFloat(bValue));
36+
const [animation, setAnimation] = React.useState<Numeric>(add);
37+
38+
return (
39+
<View>
40+
<TextInput
41+
style={styles.input}
42+
value={aValue}
43+
onChangeText={text => setAValue(text)}
44+
placeholder="Enter value for a"
45+
keyboardType="numeric"
46+
/>
47+
<TextInput
48+
style={styles.input}
49+
value={bValue}
50+
onChangeText={text => setBValue(text)}
51+
placeholder="Enter value for b"
52+
keyboardType="numeric"
53+
/>
54+
<Animated.View style={[styles.content, {opacity: animation}]}>
55+
<Text>Change Opacity</Text>
56+
</Animated.View>
57+
<RNTesterButton onPress={() => setAnimation(add)}>Add</RNTesterButton>
58+
<RNTesterButton onPress={() => setAnimation(subtract)}>
59+
Subtract
60+
</RNTesterButton>
61+
<RNTesterButton onPress={() => setAnimation(mult)}>
62+
Multiply
63+
</RNTesterButton>
64+
<RNTesterButton onPress={() => setAnimation(divide)}>
65+
Divide
66+
</RNTesterButton>
67+
<RNTesterButton onPress={() => setAnimation(mod)}>Modulo</RNTesterButton>
68+
</View>
69+
);
70+
};
71+
72+
const styles = StyleSheet.create({
73+
content: {
74+
backgroundColor: 'deepskyblue',
75+
borderWidth: 1,
76+
borderColor: 'dodgerblue',
77+
padding: 20,
78+
margin: 20,
79+
borderRadius: 10,
80+
alignItems: 'center',
81+
},
82+
input: {
83+
height: 40,
84+
borderColor: 'gray',
85+
borderWidth: 1,
86+
paddingLeft: 10,
87+
margin: 10,
88+
borderRadius: 5,
89+
},
90+
});

0 commit comments

Comments
 (0)