Skip to content

Commit 89f0900

Browse files
Luna Weifacebook-github-bot
authored andcommitted
Add a pointermove with AnimatedEvent example
Summary: Changelog: [Internal] - Add an example of pointermove using AnimatedEvent Reviewed By: vincentriemer Differential Revision: D37769263 fbshipit-source-id: 25de0ae88b55148d23fa388768707ea859e9ae4d
1 parent 4167c4f commit 89f0900

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
* @format
8+
* @flow
9+
*/
10+
11+
import * as React from 'react';
12+
import {Text, Animated, StyleSheet} from 'react-native';
13+
import type {RNTesterModuleExample} from '../../../types/RNTesterTypes';
14+
import ToggleNativeDriver from '../../Animated/utils/ToggleNativeDriver';
15+
16+
const WIDTH = 200;
17+
const HEIGHT = 250;
18+
19+
const styles = StyleSheet.create({
20+
container: {
21+
backgroundColor: 'black',
22+
marginTop: 20,
23+
width: WIDTH,
24+
height: HEIGHT,
25+
alignSelf: 'center',
26+
},
27+
text: {
28+
color: 'white',
29+
position: 'absolute',
30+
top: HEIGHT / 2,
31+
},
32+
animatingBox: {
33+
backgroundColor: 'blue',
34+
width: 1,
35+
height: 1,
36+
},
37+
});
38+
39+
function CompatibilityAnimatedPointerMove(): React.Node {
40+
const xCoord = React.useRef(new Animated.Value(0)).current;
41+
const yCoord = React.useRef(new Animated.Value(0)).current;
42+
const [useNativeDriver, setUseNativeDriver] = React.useState(true);
43+
44+
return (
45+
<>
46+
<ToggleNativeDriver
47+
style={{paddingHorizontal: 30}}
48+
value={useNativeDriver}
49+
onValueChange={setUseNativeDriver}
50+
/>
51+
<Animated.View
52+
onPointerMove={Animated.event(
53+
[{nativeEvent: {offsetX: xCoord, offsetY: yCoord}}],
54+
{useNativeDriver},
55+
)}
56+
pointerEvents="box-only"
57+
style={styles.container}>
58+
<Text style={styles.text}>Move pointer over me</Text>
59+
<Animated.View
60+
style={{
61+
backgroundColor: 'blue',
62+
width: 1,
63+
height: 1,
64+
transform: [
65+
{
66+
translateX: xCoord.interpolate({
67+
inputRange: [0, WIDTH],
68+
outputRange: ([0, WIDTH / 2]: number[]),
69+
}),
70+
},
71+
{
72+
translateY: yCoord.interpolate({
73+
inputRange: [0, HEIGHT],
74+
outputRange: ([0, HEIGHT / 2]: number[]),
75+
}),
76+
},
77+
{
78+
scaleX: xCoord.interpolate({
79+
inputRange: [0, WIDTH],
80+
outputRange: ([0, WIDTH]: number[]),
81+
}),
82+
},
83+
{
84+
scaleY: yCoord.interpolate({
85+
inputRange: [0, HEIGHT],
86+
outputRange: ([0, HEIGHT]: number[]),
87+
}),
88+
},
89+
],
90+
}}
91+
/>
92+
</Animated.View>
93+
</>
94+
);
95+
}
96+
97+
export default ({
98+
name: 'compatibility_animatedevent_pointer_move',
99+
description:
100+
'An AnimatedEvent example on onPointerMove. The blue box should scale to pointer event offset values within black box',
101+
title: 'AnimatedEvent with pointermove',
102+
render(): React.Node {
103+
return <CompatibilityAnimatedPointerMove />;
104+
},
105+
}: RNTesterModuleExample);

packages/rn-tester/js/examples/Experimental/W3CPointerEventsExample.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTyp
1515

1616
import PointerEventAttributesHoverablePointers from './W3CPointerEventPlatformTests/PointerEventAttributesHoverablePointers';
1717
import PointerEventPointerMove from './W3CPointerEventPlatformTests/PointerEventPointerMove';
18+
import CompatibilityAnimatedPointerMove from './Compatibility/CompatibilityAnimatedPointerMove';
1819

1920
function EventfulView(props: {|
2021
name: string,
@@ -246,5 +247,6 @@ export default {
246247
return <PointerEventPointerMove />;
247248
},
248249
},
250+
CompatibilityAnimatedPointerMove,
249251
],
250252
};

0 commit comments

Comments
 (0)