Przeglądaj źródła

Allow injecting intial insets

Janic Duplessis 5 lat temu
rodzic
commit
ea2185ac93
2 zmienionych plików z 14 dodań i 4 usunięć
  1. 4
    0
      README.md
  2. 10
    4
      src/index.tsx

+ 4
- 0
README.md Wyświetl plik

@@ -134,6 +134,10 @@ function SomeComponent() {
134 134
 }
135 135
 ```
136 136
 
137
+### Web SSR
138
+
139
+If you are doing server side rendering on the web you can use `initialSafeAreaInsets` to inject insets value based on the device the user has, or simply pass zero values. Since insets measurement is async it will break rendering your page content otherwise.
140
+
137 141
 ## Resources
138 142
 
139 143
 - Great article about how this library can be used: https://dev.to/brunolemos/adding-notch-support-to-your-react-native-android-app-3ci3

+ 10
- 4
src/index.tsx Wyświetl plik

@@ -7,23 +7,29 @@ export const SafeAreaContext = React.createContext<EdgeInsets | null>(null);
7 7
 
8 8
 export interface SafeAreaViewProps {
9 9
   children?: React.ReactNode;
10
+  initialSafeAreaInsets?: EdgeInsets | null;
10 11
 }
11 12
 
12
-export function SafeAreaProvider({ children }: SafeAreaViewProps) {
13
+export function SafeAreaProvider({
14
+  children,
15
+  initialSafeAreaInsets,
16
+}: SafeAreaViewProps) {
13 17
   let parentInsets = useParentSafeArea();
14
-  const [insets, setInsets] = React.useState<EdgeInsets | null>(null);
18
+  const [insets, setInsets] = React.useState<EdgeInsets | null | undefined>(
19
+    initialSafeAreaInsets,
20
+  );
15 21
   const onInsetsChange = React.useCallback((event: InsetChangedEvent) => {
16 22
     setInsets(event.nativeEvent.insets);
17 23
   }, []);
18 24
 
19 25
   // If a provider is nested inside of another provider then we can just use
20 26
   // the parent insets, without rendering another native safe area view
21
-  if (parentInsets) {
27
+  if (parentInsets != null) {
22 28
     return <View style={styles.fill}>{children}</View>;
23 29
   } else {
24 30
     return (
25 31
       <NativeSafeAreaView style={styles.fill} onInsetsChange={onInsetsChange}>
26
-        {insets !== null ? (
32
+        {insets != null ? (
27 33
           <SafeAreaContext.Provider value={insets}>
28 34
             {children}
29 35
           </SafeAreaContext.Provider>