|
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'; |
16 | 20 | import * as React from 'react'; |
| 21 | +import {useCallback, useMemo, useState} from 'react'; |
17 | 22 |
|
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 | | -}); |
| 23 | +/** |
| 24 | + * Renders its children within the safe area of the device, by applying the part |
| 25 | + * of the view that is covered by the system UI as padding. |
| 26 | + */ |
| 27 | +component SafeAreaView( |
| 28 | + ref?: React.RefSetter<HostInstance>, |
| 29 | + ...props: ViewProps |
| 30 | +) { |
| 31 | + const {style, experimental_onSafeAreaInsetsChange, ...otherProps} = props; |
| 32 | + const [insets, setInsets] = useState<?SafeAreaInsets>(null); |
| 33 | + |
| 34 | + const handleSafeAreaInsetsChange = useCallback( |
| 35 | + (event: SafeAreaInsetsChangeEvent) => { |
| 36 | + setInsets(event.nativeEvent.insets); |
| 37 | + experimental_onSafeAreaInsetsChange?.(event); |
| 38 | + }, |
| 39 | + [experimental_onSafeAreaInsetsChange], |
| 40 | + ); |
| 41 | + |
| 42 | + const paddingStyle = useMemo(() => { |
| 43 | + if (insets == null) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + // Insets are physical edges, but Yoga remaps paddingLeft/paddingRight to |
| 47 | + // start/end when I18nManager's swapLeftAndRightInRTL is on, which would |
| 48 | + // pad the mirror-image edge in RTL. Swap the values so the physical edge |
| 49 | + // keeps its inset. |
| 50 | + const {isRTL, doLeftAndRightSwapInRTL} = I18nManager.getConstants(); |
| 51 | + const swap = isRTL && doLeftAndRightSwapInRTL; |
| 52 | + return { |
| 53 | + paddingTop: insets.top, |
| 54 | + paddingRight: swap ? insets.left : insets.right, |
| 55 | + paddingBottom: insets.bottom, |
| 56 | + paddingLeft: swap ? insets.right : insets.left, |
| 57 | + }; |
| 58 | + }, [insets]); |
| 59 | + |
| 60 | + return ( |
| 61 | + <View |
| 62 | + {...otherProps} |
| 63 | + ref={ref} |
| 64 | + experimental_onSafeAreaInsetsChange={handleSafeAreaInsetsChange} |
| 65 | + style={[style, paddingStyle]} |
| 66 | + /> |
| 67 | + ); |
| 68 | +} |
30 | 69 |
|
31 | | -export default exported; |
| 70 | +export default SafeAreaView; |
0 commit comments