Browse Source

Allow injecting intial insets

Janic Duplessis 5 years ago
parent
commit
ea2185ac93
2 changed files with 14 additions and 4 deletions
  1. 4
    0
      README.md
  2. 10
    4
      src/index.tsx

+ 4
- 0
README.md View File

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
 ## Resources
141
 ## Resources
138
 
142
 
139
 - Great article about how this library can be used: https://dev.to/brunolemos/adding-notch-support-to-your-react-native-android-app-3ci3
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 View File

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