1
1
import React , { useRef , useState } from 'react' ;
2
- import { Button , View , Dimensions , StyleSheet , SafeAreaView , Switch , TouchableOpacity , Text , ColorValue , ViewStyle } from 'react-native' ;
2
+ import { Button , View , Dimensions , StyleSheet , SafeAreaView , Switch , ColorValue } from 'react-native' ;
3
3
import Animated , {
4
4
useSharedValue ,
5
5
useAnimatedStyle ,
@@ -15,22 +15,22 @@ const BALL_SIZE = 50;
15
15
function SandboxDemoView ( { initialProperties} : { initialProperties : any } ) {
16
16
const [ isVisible , setVisible ] = useState ( false ) ;
17
17
const sandboxRef = useRef < SandboxReactNativeViewRef > ( null ) ;
18
-
18
+
19
19
return (
20
- < View style = { { flex : 1 } } >
21
- < SafeAreaView style = { { margin : 10 , flex : 1 } } >
20
+ < View style = { styles . container } >
21
+ < SafeAreaView style = { styles . safeArea } >
22
22
< View style = { styles . control } >
23
23
< Switch value = { isVisible } onValueChange = { ( ) => setVisible ( v => ! v ) } />
24
24
< Button color = { styles . button . backgroundColor } title = "Post message" onPress = { ( ) => {
25
- sandboxRef ?. current ?. postMessage ( { date : new Date ( ) . toJSON ( ) , origin : " host" } )
25
+ sandboxRef ?. current ?. postMessage ( { date : new Date ( ) . toJSON ( ) , origin : ' host' } ) ;
26
26
} } />
27
27
</ View >
28
28
{ isVisible ?
29
29
< SandboxReactNativeView
30
30
ref = { sandboxRef }
31
- jsBundleName = { " sandbox" }
32
- moduleName = { " SandboxApp" }
33
- style = { { flex : 1 , padding : 30 } }
31
+ jsBundleName = { ' sandbox' }
32
+ moduleName = { ' SandboxApp' }
33
+ style = { styles . sandboxView }
34
34
initialProperties = { initialProperties }
35
35
onMessage = { ( msg ) => {
36
36
console . log ( `Got message from ${ initialProperties . sourceName } ` , msg ) ;
@@ -52,8 +52,8 @@ function SandboxDemoView({initialProperties}: {initialProperties: any}) {
52
52
type : 'error' ,
53
53
text1 : message ,
54
54
text2 : `${ error . name } : ${ error . message } ` ,
55
- visibilityTime : 5000
56
- } )
55
+ visibilityTime : 5000 ,
56
+ } ) ;
57
57
return false ;
58
58
} }
59
59
/> : null
@@ -64,19 +64,19 @@ function SandboxDemoView({initialProperties}: {initialProperties: any}) {
64
64
}
65
65
66
66
function BouncingBall ( ) {
67
- // Position and velocity shared values
67
+ // Position and velocity shared values
68
68
const ballX = useSharedValue ( 50 ) ;
69
69
const ballY = useSharedValue ( 50 ) ;
70
70
const velocityX = useSharedValue ( 6 ) ;
71
71
const velocityY = useSharedValue ( 6 ) ;
72
72
73
- // Animation loop using derived value for continuous movement
73
+ // Animation loop using derived value for continuous movement
74
74
useDerivedValue ( ( ) => {
75
- // Update position
75
+ // Update position
76
76
ballX . value += velocityX . value ;
77
77
ballY . value += velocityY . value ;
78
78
79
- // Bounce off walls
79
+ // Bounce off walls
80
80
if ( ballX . value <= 0 || ballX . value >= screenWidth - BALL_SIZE ) {
81
81
velocityX . value *= - 1 ;
82
82
ballX . value = Math . max ( 0 , Math . min ( screenWidth - BALL_SIZE , ballX . value ) ) ;
@@ -88,7 +88,7 @@ function BouncingBall() {
88
88
}
89
89
} ) ;
90
90
91
- // Animated style for the ball
91
+ // Animated style for the ball
92
92
const animatedStyle = useAnimatedStyle ( ( ) => {
93
93
return {
94
94
transform : [
@@ -103,23 +103,25 @@ function BouncingBall() {
103
103
) ;
104
104
}
105
105
106
+ // Move toast config outside component to avoid re-creation on every render
107
+ const toastConfig = {
108
+ customColored : ( props : ToastConfigParams < { leftColor : ColorValue } > ) => (
109
+ < BaseToast
110
+ { ...props }
111
+ text2NumberOfLines = { 0 }
112
+ style = { {
113
+ borderLeftColor : props ?. props . leftColor ,
114
+ } }
115
+ />
116
+ ) ,
117
+ } ;
118
+
106
119
export default function App ( ) {
107
- const toastConfig = {
108
- customColored : ( props : ToastConfigParams < { leftColor : ColorValue } > ) => (
109
- < BaseToast
110
- { ...props }
111
- text2NumberOfLines = { 0 }
112
- style = { {
113
- borderLeftColor : props ?. props . leftColor ,
114
- } }
115
- />
116
- ) ,
117
- } ;
118
120
119
121
return (
120
- < SafeAreaView style = { styles . container } >
121
- < SandboxDemoView initialProperties = { { sourceName : " green" , backgroundColor : '#CCFFCC' } } />
122
- < SandboxDemoView initialProperties = { { sourceName : " blue" , backgroundColor : '#CCCCFF' } } />
122
+ < SafeAreaView style = { styles . appContainer } >
123
+ < SandboxDemoView initialProperties = { { sourceName : ' green' , backgroundColor : '#CCFFCC' } } />
124
+ < SandboxDemoView initialProperties = { { sourceName : ' blue' , backgroundColor : '#CCCCFF' } } />
123
125
< BouncingBall />
124
126
< Toast config = { toastConfig } />
125
127
</ SafeAreaView >
@@ -129,6 +131,17 @@ export default function App() {
129
131
const styles = StyleSheet . create ( {
130
132
container : {
131
133
flex : 1 ,
134
+ } ,
135
+ safeArea : {
136
+ margin : 10 ,
137
+ flex : 1 ,
138
+ } ,
139
+ sandboxView : {
140
+ flex : 1 ,
141
+ padding : 30 ,
142
+ } ,
143
+ appContainer : {
144
+ flex : 1 ,
132
145
flexDirection : 'column' ,
133
146
} ,
134
147
ball : {
0 commit comments