SimpleExample.tsx 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import * as React from 'react';
  2. import { View, Text, StatusBar, ScrollView } from 'react-native';
  3. import {
  4. SafeAreaProvider,
  5. useSafeAreaInsets,
  6. initialWindowMetrics,
  7. useSafeAreaFrame,
  8. } from 'react-native-safe-area-context';
  9. const DataView = ({ data }: { data: object | null | undefined }) => (
  10. <Text style={{ fontSize: 16, lineHeight: 24, color: '#292929' }}>
  11. {data == null
  12. ? 'null'
  13. : Object.entries(data)
  14. .map(([key, value]) => `${key}: ${value}`)
  15. .join('\n')}
  16. </Text>
  17. );
  18. const Card = ({
  19. title,
  20. children,
  21. }: {
  22. title: string;
  23. children: React.ReactNode;
  24. }) => (
  25. <View style={{ padding: 16, backgroundColor: 'white', marginBottom: 4 }}>
  26. <Text
  27. style={{
  28. fontSize: 20,
  29. fontWeight: 'bold',
  30. marginBottom: 16,
  31. color: '#292929',
  32. }}
  33. >
  34. {title}
  35. </Text>
  36. {children}
  37. </View>
  38. );
  39. const BORDER_WIDTH = 8;
  40. function SimpleExampleScreen() {
  41. const insets = useSafeAreaInsets();
  42. const frame = useSafeAreaFrame();
  43. return (
  44. <>
  45. <StatusBar barStyle="dark-content" backgroundColor="transparent" />
  46. <View
  47. style={{
  48. width: frame.width,
  49. height: frame.height,
  50. backgroundColor: 'red',
  51. paddingTop: insets.top - BORDER_WIDTH,
  52. paddingLeft: insets.left - BORDER_WIDTH,
  53. paddingBottom: insets.bottom - BORDER_WIDTH,
  54. paddingRight: insets.right - BORDER_WIDTH,
  55. borderColor: 'blue',
  56. borderWidth: BORDER_WIDTH,
  57. }}
  58. >
  59. <ScrollView style={{ flex: 1, backgroundColor: '#eee' }}>
  60. <Card title="Provider insets">
  61. <DataView data={insets} />
  62. </Card>
  63. <Card title="Provider frame">
  64. <DataView data={frame} />
  65. </Card>
  66. <Card title="Initial window insets">
  67. <DataView data={initialWindowMetrics?.insets} />
  68. </Card>
  69. <Card title="Initial window frame">
  70. <DataView data={initialWindowMetrics?.frame} />
  71. </Card>
  72. </ScrollView>
  73. </View>
  74. </>
  75. );
  76. }
  77. export default function SimpleExample() {
  78. return (
  79. <View style={{ marginTop: 0, flex: 1 }}>
  80. <SafeAreaProvider>
  81. <SimpleExampleScreen />
  82. </SafeAreaProvider>
  83. </View>
  84. );
  85. }