Skip to content

Commit

Permalink
Add Animated Color example
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[Internal] - Add an example to demo Animated colors with both JS and native drivers

Reviewed By: mdvacca

Differential Revision: D34153047

fbshipit-source-id: 9b61fd4e5f597b0440bed7ff1a33716e50ec34e5
  • Loading branch information
genkikondo authored and facebook-github-bot committed Feb 11, 2022
1 parent 201f355 commit 1aec881
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/rn-tester/js/examples/Animated/AnimatedIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ComposeAnimationsWithEasingExample from './ComposeAnimationsWithEasingExa
import TransformBounceExample from './TransformBounceExample';
import ComposingExample from './ComposingExample';
import TransformStylesExample from './TransformStylesExample';
import ColorStylesExample from './ColorStylesExample';

export default ({
framework: 'React',
Expand All @@ -31,6 +32,7 @@ export default ({
showIndividualExamples: true,
examples: [
TransformStylesExample,
ColorStylesExample,
FadeInViewExample,
ComposingExample,
EasingExample,
Expand Down
125 changes: 125 additions & 0 deletions packages/rn-tester/js/examples/Animated/ColorStylesExample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import * as React from 'react';
import {Animated, View, StyleSheet} from 'react-native';
import RNTConfigurationBlock from '../../components/RNTConfigurationBlock';
import RNTesterButton from '../../components/RNTesterButton';
import ToggleNativeDriver from './utils/ToggleNativeDriver';

function AnimatedView({useNativeDriver}: {useNativeDriver: boolean}) {
const animations = [];

const animatedViewStyle = {
backgroundColor: new Animated.Color('blue'),
borderColor: new Animated.Color('orange'),
};
animations.push(
Animated.timing(animatedViewStyle.backgroundColor, {
toValue: new Animated.Color('red'),
duration: 1000,
useNativeDriver,
}),
);
animations.push(
Animated.timing(animatedViewStyle.borderColor, {
toValue: new Animated.Color('purple'),
duration: 1000,
useNativeDriver,
}),
);

const animatedTextStyle = {
color: new Animated.Color('blue'),
};
animations.push(
Animated.timing(animatedTextStyle.color, {
toValue: new Animated.Color('red'),
duration: 1000,
useNativeDriver,
}),
);

const animatedImageStyle = {
tintColor: new Animated.Color('blue'),
};
animations.push(
Animated.timing(animatedImageStyle.tintColor, {
toValue: new Animated.Color('red'),
duration: 1000,
useNativeDriver,
}),
);

const animation = Animated.parallel(animations);

return (
<>
<RNTesterButton
onPress={() => {
animation.reset();
animation.start();
}}>
Press to animate
</RNTesterButton>
<Animated.View style={[styles.animatedView, animatedViewStyle]} />
<Animated.Text style={[styles.animatedText, animatedTextStyle]}>
Hello World
</Animated.Text>
<Animated.Image
style={[styles.animatedImage, animatedImageStyle]}
source={require('../../assets/bunny.png')}
/>
</>
);
}

function AnimatedColorStyleExample(): React.Node {
const [useNativeDriver, setUseNativeDriver] = React.useState(false);

return (
<View>
<RNTConfigurationBlock>
<ToggleNativeDriver
value={useNativeDriver}
onValueChange={setUseNativeDriver}
/>
</RNTConfigurationBlock>
<AnimatedView
key={`animated-view-use-${useNativeDriver ? 'native' : 'js'}-driver`}
useNativeDriver={useNativeDriver}
/>
</View>
);
}

const styles = StyleSheet.create({
animatedView: {
height: 100,
width: 100,
borderWidth: 10,
},
animatedText: {
fontSize: 20,
fontWeight: 'bold',
},
animatedImage: {
height: 100,
width: 100,
},
});

export default ({
title: 'Color Styles',
name: 'colorStyles',
description: 'Animations of color styles.',
render: () => <AnimatedColorStyleExample />,
}: RNTesterModuleExample);

0 comments on commit 1aec881

Please sign in to comment.