|
| 1 | +import * as React from 'react'; |
| 2 | +import { View, Text, ScrollView, StyleSheet } from 'react-native'; |
| 3 | +import { |
| 4 | + SafeAreaProvider, |
| 5 | + useSafeAreaInsets, |
| 6 | + useSafeAreaFrame, |
| 7 | + initialWindowMetrics, |
| 8 | +} from 'react-native-safe-area-context'; |
| 9 | +import { DataView } from '../components/DataView'; |
| 10 | +import { Card } from '../components/Card'; |
| 11 | + |
| 12 | +function HooksExampleScreen() { |
| 13 | + const insets = useSafeAreaInsets(); |
| 14 | + const frame = useSafeAreaFrame(); |
| 15 | + |
| 16 | + return ( |
| 17 | + <View |
| 18 | + style={{ |
| 19 | + width: frame.width, |
| 20 | + height: frame.height, |
| 21 | + backgroundColor: 'red', |
| 22 | + paddingTop: insets.top - BORDER_WIDTH, |
| 23 | + paddingLeft: insets.left - BORDER_WIDTH, |
| 24 | + paddingBottom: insets.bottom - BORDER_WIDTH, |
| 25 | + paddingRight: insets.right - BORDER_WIDTH, |
| 26 | + borderColor: 'blue', |
| 27 | + borderWidth: BORDER_WIDTH, |
| 28 | + }} |
| 29 | + > |
| 30 | + <ScrollView style={{ flex: 1, backgroundColor: '#eee' }}> |
| 31 | + <Card title="Provider insets"> |
| 32 | + <DataView data={insets} /> |
| 33 | + </Card> |
| 34 | + <Card title="Provider frame"> |
| 35 | + <DataView data={frame} /> |
| 36 | + </Card> |
| 37 | + <Card title="Initial window insets"> |
| 38 | + <DataView data={initialWindowMetrics?.insets} /> |
| 39 | + </Card> |
| 40 | + <Card title="Initial window frame"> |
| 41 | + <DataView data={initialWindowMetrics?.frame} /> |
| 42 | + </Card> |
| 43 | + <Card title="Note"> |
| 44 | + <Text style={styles.note}> |
| 45 | + On desktop browsers, insets will be 0. The CSS env(safe-area-inset-*) |
| 46 | + values only report non-zero on devices with notches (e.g. iPhone in |
| 47 | + Safari). The frame values reflect the current window dimensions. |
| 48 | + </Text> |
| 49 | + </Card> |
| 50 | + </ScrollView> |
| 51 | + </View> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +const BORDER_WIDTH = 8; |
| 56 | + |
| 57 | +export default function HooksExample() { |
| 58 | + return ( |
| 59 | + <View style={{ flex: 1 }}> |
| 60 | + <SafeAreaProvider> |
| 61 | + <HooksExampleScreen /> |
| 62 | + </SafeAreaProvider> |
| 63 | + </View> |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +const styles = StyleSheet.create({ |
| 68 | + note: { |
| 69 | + fontSize: 14, |
| 70 | + lineHeight: 20, |
| 71 | + color: '#576574', |
| 72 | + }, |
| 73 | +}); |
0 commit comments