Преглед на файлове

Add a SafeAreaView component (#15)

* Add a SafeAreaView component

* Update README.md
Satyajit Sahoo преди 4 години
родител
ревизия
6a376e0678
променени са 2 файла, в които са добавени 38 реда и са изтрити 2 реда
  1. 15
    1
      README.md
  2. 23
    1
      src/index.tsx

+ 15
- 1
README.md Целия файл

@@ -120,6 +120,20 @@ class ClassComponent extends React.Component {
120 120
 }
121 121
 ```
122 122
 
123
+Usage with `SafeAreaView`:
124
+
125
+```js
126
+import { SafeAreaView } from 'react-native-safe-area-context';
127
+
128
+function SomeComponent() {
129
+  return (
130
+    <SafeAreaView>
131
+      <View />
132
+    </SafeAreaView>
133
+  );
134
+}
135
+```
136
+
123 137
 ## Resources
124 138
 
125
-- Great article about how this libary can be used: https://dev.to/brunolemos/adding-notch-support-to-your-react-native-android-app-3ci3
139
+- Great article about how this library can be used: https://dev.to/brunolemos/adding-notch-support-to-your-react-native-android-app-3ci3

+ 23
- 1
src/index.tsx Целия файл

@@ -1,5 +1,5 @@
1 1
 import * as React from 'react';
2
-import { StyleSheet, View } from 'react-native';
2
+import { StyleSheet, View, ViewProps } from 'react-native';
3 3
 import { EdgeInsets, InsetChangedEvent } from './SafeArea.types';
4 4
 import NativeSafeAreaView from './NativeSafeAreaView';
5 5
 
@@ -53,4 +53,26 @@ export function useSafeArea(): EdgeInsets {
53 53
   return safeArea;
54 54
 }
55 55
 
56
+export function SafeAreaView({
57
+  style,
58
+  ...rest
59
+}: ViewProps & { children: React.ReactNode }) {
60
+  const insets = useSafeArea();
61
+
62
+  return (
63
+    <View
64
+      style={[
65
+        {
66
+          paddingTop: insets.top,
67
+          paddingRight: insets.right,
68
+          paddingBottom: insets.bottom,
69
+          paddingLeft: insets.left,
70
+        },
71
+        style,
72
+      ]}
73
+      {...rest}
74
+    />
75
+  );
76
+}
77
+
56 78
 export type EdgeInsets = EdgeInsets;