-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
155 lines (140 loc) · 3.61 KB
/
App.tsx
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React from 'react';
import {
KeyboardAvoidingView,
ScrollView,
StatusBar,
StyleSheet,
Text,
TextInput,
View,
useColorScheme,
} from 'react-native';
import Animated, {
KeyboardState,
SharedValue,
runOnJS,
useAnimatedKeyboard,
useAnimatedProps,
useAnimatedStyle,
} from 'react-native-reanimated';
import {
SafeAreaProvider,
SafeAreaView,
initialWindowMetrics,
useSafeAreaInsets,
} from 'react-native-safe-area-context';
import {Colors} from 'react-native/Libraries/NewAppScreen';
function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundColor = isDarkMode ? Colors.darker : Colors.lighter;
const styles = StyleSheet.create({
root: {flex: 1, backgroundColor},
container: {flex: 1},
});
return (
<SafeAreaProvider style={styles.root} initialMetrics={initialWindowMetrics}>
<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundColor}
translucent
/>
<SafeAreaView style={styles.container}>
<AppContent isDarkMode={isDarkMode} />
</SafeAreaView>
</SafeAreaProvider>
);
}
interface AppContentProps {
isDarkMode: boolean;
}
const AppContent: React.FC<AppContentProps> = ({isDarkMode}) => {
const styles = StyleSheet.create({
container: {
flex: 1,
},
content: {
flex: 1,
justifyContent: 'flex-end',
backgroundColor: isDarkMode ? Colors.black : Colors.white,
padding: 16,
},
input: {
height: 40,
paddingHorizontal: 8,
borderRadius: 4,
borderColor: 'gray',
borderWidth: 1,
},
});
const topInset = useSafeAreaInsets().top;
const keyboardVerticalOffset = topInset + 8;
return (
<ScrollView
contentInsetAdjustmentBehavior="automatic"
contentContainerStyle={styles.container}>
<View style={styles.content}>
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={keyboardVerticalOffset}>
<Toolbar />
<TextInput placeholder="Type here..." style={styles.input} />
</KeyboardAvoidingView>
</View>
</ScrollView>
);
};
const Toolbar: React.FC = () => {
const styles = StyleSheet.create({
toolbar: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: 100,
backgroundColor: 'red',
transform: [{translateY: -120}],
justifyContent: 'center',
alignItems: 'center',
},
});
const keyboard = useAnimatedKeyboard({isStatusBarTranslucentAndroid: true});
const animatedProps = useAnimatedProps(() => ({
pointerEvents: isKeyboardOpen(keyboard.state, keyboard.height)
? ('box-none' as const)
: ('none' as const),
}));
const animatedStyle = useAnimatedStyle(() => ({
opacity: isKeyboardOpen(keyboard.state, keyboard.height) ? 1 : 0,
}));
return (
<Animated.View
style={[styles.toolbar, animatedStyle]}
animatedProps={animatedProps}>
<Text>Toolbar visible when keyboard is open</Text>
</Animated.View>
);
};
/* export */
export default App;
/* utils */
function isKeyboardOpen(
state: SharedValue<KeyboardState>,
height: SharedValue<number>,
): boolean {
'worklet';
runOnJS(debug)('Keyboard State', state.value);
runOnJS(debug)('Keyboard Height', height.value);
return (
state.value === KeyboardState.OPEN || state.value === KeyboardState.OPENING
);
}
/* debug */
function debug(label: string, value: string | number): void {
console.log('🐞', label, ':', value);
}