Browse Source

Better backwards compat

Janic Duplessis 4 years ago
parent
commit
dad9cc41a6
4 changed files with 34 additions and 17 deletions
  1. 5
    0
      src/InitialWindow.ts
  2. 8
    0
      src/InitialWindow.web.ts
  3. 21
    14
      src/SafeAreaContext.tsx
  4. 0
    3
      src/initialWindowMetrics.web.ts

src/initialWindowMetrics.ts → src/InitialWindow.ts View File

10
 RNCSafeAreaViewConfig.Constants != null
10
 RNCSafeAreaViewConfig.Constants != null
11
   ? RNCSafeAreaViewConfig.Constants.initialWindowMetrics
11
   ? RNCSafeAreaViewConfig.Constants.initialWindowMetrics
12
   : null) as Metrics | null;
12
   : null) as Metrics | null;
13
+
14
+/**
15
+ * @deprecated
16
+ */
17
+export const initialWindowSafeAreaInsets = initialWindowMetrics?.insets;

+ 8
- 0
src/InitialWindow.web.ts View File

1
+import { EdgeInsets, Metrics } from './SafeArea.types';
2
+
3
+export const initialWindowMetrics: Metrics | null = null;
4
+
5
+/**
6
+ * @deprecated
7
+ */
8
+export const initialWindowSafeAreaInsets: EdgeInsets | null = null;

+ 21
- 14
src/SafeAreaContext.tsx View File

1
 import * as React from 'react';
1
 import * as React from 'react';
2
-import { StyleSheet } from 'react-native';
2
+import { StyleSheet, Dimensions } from 'react-native';
3
 import NativeSafeAreaView from './NativeSafeAreaView';
3
 import NativeSafeAreaView from './NativeSafeAreaView';
4
 import { EdgeInsets, InsetChangedEvent, Metrics, Rect } from './SafeArea.types';
4
 import { EdgeInsets, InsetChangedEvent, Metrics, Rect } from './SafeArea.types';
5
 
5
 
12
 export interface SafeAreaViewProps {
12
 export interface SafeAreaViewProps {
13
   children?: React.ReactNode;
13
   children?: React.ReactNode;
14
   initialMetrics?: Metrics | null;
14
   initialMetrics?: Metrics | null;
15
+  /**
16
+   * @deprecated
17
+   */
18
+  initialSafeAreaInsets?: EdgeInsets | null;
15
 }
19
 }
16
 
20
 
17
 export function SafeAreaProvider({
21
 export function SafeAreaProvider({
18
   children,
22
   children,
19
   initialMetrics,
23
   initialMetrics,
24
+  initialSafeAreaInsets,
20
 }: SafeAreaViewProps) {
25
 }: SafeAreaViewProps) {
21
   const parentInsets = useParentSafeAreaInsets();
26
   const parentInsets = useParentSafeAreaInsets();
22
   const parentFrame = useParentSafeAreaFrame();
27
   const parentFrame = useParentSafeAreaFrame();
23
   const [insets, setInsets] = React.useState<EdgeInsets | null>(
28
   const [insets, setInsets] = React.useState<EdgeInsets | null>(
24
-    initialMetrics?.insets ?? parentInsets ?? null,
29
+    initialMetrics?.insets ?? initialSafeAreaInsets ?? parentInsets ?? null,
25
   );
30
   );
26
-  const [frame, setFrame] = React.useState<Rect | null>(
27
-    initialMetrics?.frame ?? parentFrame ?? null,
31
+  const [frame, setFrame] = React.useState<Rect>(
32
+    initialMetrics?.frame ??
33
+      parentFrame ?? {
34
+        // Backwards compat so we render anyway if we don't have frame.
35
+        x: 0,
36
+        y: 0,
37
+        width: Dimensions.get('window').width,
38
+        height: Dimensions.get('window').height,
39
+      },
28
   );
40
   );
29
   const onInsetsChange = React.useCallback((event: InsetChangedEvent) => {
41
   const onInsetsChange = React.useCallback((event: InsetChangedEvent) => {
42
+    // Backwards compat with old native code that won't send frame.
43
+    if (event.nativeEvent.frame != null) {
44
+      setFrame(event.nativeEvent.frame);
45
+    }
30
     setInsets(event.nativeEvent.insets);
46
     setInsets(event.nativeEvent.insets);
31
-    setFrame(event.nativeEvent.frame);
32
   }, []);
47
   }, []);
33
 
48
 
34
   return (
49
   return (
35
     <NativeSafeAreaView style={styles.fill} onInsetsChange={onInsetsChange}>
50
     <NativeSafeAreaView style={styles.fill} onInsetsChange={onInsetsChange}>
36
-      {insets != null && frame != null ? (
51
+      {insets != null ? (
37
         <SafeAreaFrameContext.Provider value={frame}>
52
         <SafeAreaFrameContext.Provider value={frame}>
38
           <SafeAreaInsetsContext.Provider value={insets}>
53
           <SafeAreaInsetsContext.Provider value={insets}>
39
             {children}
54
             {children}
80
  * @deprecated
95
  * @deprecated
81
  */
96
  */
82
 export function useSafeArea(): EdgeInsets {
97
 export function useSafeArea(): EdgeInsets {
83
-  React.useEffect(() => {
84
-    console.warn('useSafeArea is deprecated, use useSafeAreaInsets instead.');
85
-  }, []);
86
   return useSafeAreaInsets();
98
   return useSafeAreaInsets();
87
 }
99
 }
88
 
100
 
92
 export function SafeAreaConsumer(
104
 export function SafeAreaConsumer(
93
   props: React.ComponentProps<typeof SafeAreaInsetsContext.Consumer>,
105
   props: React.ComponentProps<typeof SafeAreaInsetsContext.Consumer>,
94
 ) {
106
 ) {
95
-  React.useEffect(() => {
96
-    console.warn(
97
-      'SafeAreaConsumer is deprecated, use SafeAreaInsetsContext.Consumer instead.',
98
-    );
99
-  }, []);
100
   return <SafeAreaInsetsContext.Consumer {...props} />;
107
   return <SafeAreaInsetsContext.Consumer {...props} />;
101
 }
108
 }

+ 0
- 3
src/initialWindowMetrics.web.ts View File

1
-import { EdgeInsets } from './SafeArea.types';
2
-
3
-export const initialWindowMetrics: EdgeInsets | null = null;