App.tsx 2.3KB

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