|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | import type {ViewProps} from '../../../../Libraries/Components/View/ViewPropTypes'; |
| 12 | +import type { |
| 13 | + SafeAreaInsets, |
| 14 | + SafeAreaInsetsChangeEvent, |
| 15 | +} from '../../../../Libraries/Types/CoreEventTypes'; |
| 16 | +import type {HostInstance} from '../../types/HostInstance'; |
12 | 17 |
|
13 | 18 | import View from '../../../../Libraries/Components/View/View'; |
14 | | -import UIManager from '../../../../Libraries/ReactNative/UIManager'; |
15 | | -import Platform from '../../../../Libraries/Utilities/Platform'; |
| 19 | +import I18nManager from '../../../../Libraries/ReactNative/I18nManager'; |
| 20 | +import Dimensions from '../../../../Libraries/Utilities/Dimensions'; |
16 | 21 | import * as React from 'react'; |
| 22 | +import {useCallback, useMemo, useState} from 'react'; |
17 | 23 |
|
18 | | -const exported: component( |
19 | | - ref?: React.RefSetter<React.ElementRef<typeof View>>, |
20 | | - ...ViewProps |
21 | | -) = Platform.select({ |
22 | | - ios: require('../../../../src/private/components/safeareaview/specs/RCTSafeAreaViewNativeComponent') |
23 | | - .default, |
24 | | - android: UIManager.hasViewManagerConfig('RCTSafeAreaView') |
25 | | - ? require('../../../../src/private/components/safeareaview/specs/RCTSafeAreaViewNativeComponent') |
26 | | - .default |
27 | | - : View, |
28 | | - default: View, |
29 | | -}); |
| 24 | +/** |
| 25 | + * Renders its children within the safe area of the device, by applying the part |
| 26 | + * of the view that is covered by the system UI as padding. |
| 27 | + * |
| 28 | + * This is the internal counterpart of `react-native-safe-area-context`, for the |
| 29 | + * few surfaces React Native renders itself (LogBox, the element inspector, ...) |
| 30 | + * which cannot take a dependency on it. Everything else should use the library. |
| 31 | + */ |
| 32 | +component SafeAreaView( |
| 33 | + ref?: React.RefSetter<HostInstance>, |
| 34 | + ...props: ViewProps |
| 35 | +) { |
| 36 | + const {style, experimental_onSafeAreaInsetsChange, ...otherProps} = props; |
| 37 | + // Seeded with the window insets so the first frame is already padded; the |
| 38 | + // synchronous event then keeps them correct relative to this view. The seed |
| 39 | + // is only exact for views aligned with the window edges, which the internal |
| 40 | + // surfaces using this component (LogBox, the element inspector) are. |
| 41 | + const [insets, setInsets] = useState<?SafeAreaInsets>( |
| 42 | + () => Dimensions.get('window').experimental_safeAreaInsets, |
| 43 | + ); |
| 44 | + |
| 45 | + const handleSafeAreaInsetsChange = useCallback( |
| 46 | + (event: SafeAreaInsetsChangeEvent) => { |
| 47 | + setInsets(event.nativeEvent.insets); |
| 48 | + experimental_onSafeAreaInsetsChange?.(event); |
| 49 | + }, |
| 50 | + [experimental_onSafeAreaInsetsChange], |
| 51 | + ); |
| 52 | + |
| 53 | + const paddingStyle = useMemo(() => { |
| 54 | + if (insets == null) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + // Insets are physical edges, but Yoga remaps paddingLeft/paddingRight to |
| 58 | + // start/end when I18nManager's swapLeftAndRightInRTL is on, which would |
| 59 | + // pad the mirror-image edge in RTL. Swap the values so the physical edge |
| 60 | + // keeps its inset. |
| 61 | + const {isRTL, doLeftAndRightSwapInRTL} = I18nManager.getConstants(); |
| 62 | + const swap = isRTL && doLeftAndRightSwapInRTL; |
| 63 | + return { |
| 64 | + paddingTop: insets.top, |
| 65 | + paddingRight: swap ? insets.left : insets.right, |
| 66 | + paddingBottom: insets.bottom, |
| 67 | + paddingLeft: swap ? insets.right : insets.left, |
| 68 | + }; |
| 69 | + }, [insets]); |
| 70 | + |
| 71 | + return ( |
| 72 | + <View |
| 73 | + {...otherProps} |
| 74 | + ref={ref} |
| 75 | + experimental_onSafeAreaInsetsChange={handleSafeAreaInsetsChange} |
| 76 | + style={[style, paddingStyle]} |
| 77 | + /> |
| 78 | + ); |
| 79 | +} |
30 | 80 |
|
31 | | -export default exported; |
| 81 | +export default SafeAreaView; |
0 commit comments