@react-native-ohos/react-native-safe-area-context 5.1.1-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.OpenSource +11 -0
- package/README.md +10 -0
- package/harmony/safe_area/LICENSE +21 -0
- package/harmony/safe_area/NOTICE +33 -0
- package/harmony/safe_area/README.md +270 -0
- package/harmony/safe_area/build-profile.json5 +9 -0
- package/harmony/safe_area/hvigorfile.ts +6 -0
- package/harmony/safe_area/index.ets +26 -0
- package/harmony/safe_area/oh-package.json5 +22 -0
- package/harmony/safe_area/src/main/cpp/CMakeLists.txt +9 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaBeanData.h +56 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaColumnNode.cpp +45 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaColumnNode.h +38 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaManagerMap.cpp +66 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaManagerMap.h +57 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaProviderComponentInstance.cpp +75 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaProviderComponentInstance.h +49 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaStackNode.cpp +94 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaStackNode.h +53 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaViewComponentInstance.cpp +94 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaViewComponentInstance.h +64 -0
- package/harmony/safe_area/src/main/cpp/SafeAreaViewPackage.h +60 -0
- package/harmony/safe_area/src/main/cpp/TurboModuleRequest.cpp +63 -0
- package/harmony/safe_area/src/main/cpp/TurboModuleRequest.h +33 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/BaseReactNativeSafeAreaContextPackage.h +72 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/components/BaseRNCSafeAreaProviderComponentInstance.h +18 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/components/BaseRNCSafeAreaViewComponentInstance.h +18 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/components/RNCSafeAreaProviderJSIBinder.h +27 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/components/RNCSafeAreaViewJSIBinder.h +30 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCSafeAreaContext.cpp +16 -0
- package/harmony/safe_area/src/main/cpp/generated/RNOH/generated/turbo_modules/RNCSafeAreaContext.h +16 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/ComponentDescriptors.h +25 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/EventEmitters.cpp +56 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/EventEmitters.h +59 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/Props.cpp +36 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/Props.h +88 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/ShadowNodes.cpp +20 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/ShadowNodes.h +39 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/States.cpp +18 -0
- package/harmony/safe_area/src/main/cpp/generated/react/renderer/components/react_native_safe_area_context/States.h +23 -0
- package/harmony/safe_area/src/main/ets/Logger.ts +64 -0
- package/harmony/safe_area/src/main/ets/SafeAreaProvider.ets +128 -0
- package/harmony/safe_area/src/main/ets/SafeAreaProviderModifier.ets +121 -0
- package/harmony/safe_area/src/main/ets/SafeAreaView.ets +242 -0
- package/harmony/safe_area/src/main/ets/SafeAreaViewModifier.ets +140 -0
- package/harmony/safe_area/src/main/ets/SafeAreaViewPackage.ts +46 -0
- package/harmony/safe_area/src/main/ets/SafeViewTurboModule.ts +131 -0
- package/harmony/safe_area/src/main/ets/common/SafeAreaType.ts +70 -0
- package/harmony/safe_area/src/main/module.json5 +7 -0
- package/harmony/safe_area/src/main/resources/base/element/string.json +8 -0
- package/harmony/safe_area/src/main/resources/en_US/element/string.json +8 -0
- package/harmony/safe_area/src/main/resources/zh_CN/element/string.json +8 -0
- package/harmony/safe_area/ts.ts +26 -0
- package/harmony/safe_area.har +0 -0
- package/lib/commonjs/SafeArea.types.js +6 -0
- package/lib/commonjs/SafeArea.types.js.map +1 -0
- package/lib/commonjs/SafeAreaView.js +184 -0
- package/lib/commonjs/SafeAreaView.js.map +1 -0
- package/lib/commonjs/index.js +51 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/specs/NativeSafeAreaContext.js +9 -0
- package/lib/commonjs/specs/NativeSafeAreaContext.js.map +1 -0
- package/lib/commonjs/specs/NativeSafeAreaProvider.js +10 -0
- package/lib/commonjs/specs/NativeSafeAreaProvider.js.map +1 -0
- package/lib/commonjs/specs/NativeSafeAreaView.js +12 -0
- package/lib/commonjs/specs/NativeSafeAreaView.js.map +1 -0
- package/lib/module/SafeArea.types.js +2 -0
- package/lib/module/SafeArea.types.js.map +1 -0
- package/lib/module/SafeAreaView.js +176 -0
- package/lib/module/SafeAreaView.js.map +1 -0
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/specs/NativeSafeAreaContext.js +3 -0
- package/lib/module/specs/NativeSafeAreaContext.js.map +1 -0
- package/lib/module/specs/NativeSafeAreaProvider.js +3 -0
- package/lib/module/specs/NativeSafeAreaProvider.js.map +1 -0
- package/lib/module/specs/NativeSafeAreaView.js +5 -0
- package/lib/module/specs/NativeSafeAreaView.js.map +1 -0
- package/lib/typescript/SafeArea.types.d.ts +36 -0
- package/lib/typescript/SafeArea.types.d.ts.map +1 -0
- package/lib/typescript/SafeAreaView.d.ts +6 -0
- package/lib/typescript/SafeAreaView.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +5 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/specs/NativeSafeAreaContext.d.ts +23 -0
- package/lib/typescript/specs/NativeSafeAreaContext.d.ts.map +1 -0
- package/lib/typescript/specs/NativeSafeAreaProvider.d.ts +22 -0
- package/lib/typescript/specs/NativeSafeAreaProvider.d.ts.map +1 -0
- package/lib/typescript/specs/NativeSafeAreaView.d.ts +24 -0
- package/lib/typescript/specs/NativeSafeAreaView.d.ts.map +1 -0
- package/package.json +139 -0
- package/src/SafeArea.types.ts +47 -0
- package/src/SafeAreaView.tsx +201 -0
- package/src/index.tsx +6 -0
- package/src/specs/NativeSafeAreaContext.ts +23 -0
- package/src/specs/NativeSafeAreaProvider.ts +29 -0
- package/src/specs/NativeSafeAreaView.ts +28 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type {
|
|
3
|
+
Edge,
|
|
4
|
+
EdgeMode,
|
|
5
|
+
EdgeRecord,
|
|
6
|
+
NativeSafeAreaViewInstance,
|
|
7
|
+
NativeSafeAreaViewProps,
|
|
8
|
+
} from './SafeArea.types';
|
|
9
|
+
import NativeSafeAreaView, { onSafeAreaValueChangeEvent } from './specs/NativeSafeAreaView';
|
|
10
|
+
import type * as ReactNative from "react-native";
|
|
11
|
+
import { useMemo, useState } from 'react';
|
|
12
|
+
|
|
13
|
+
const defaultEdges: Record<Edge, EdgeMode> = {
|
|
14
|
+
top: 'additive',
|
|
15
|
+
left: 'additive',
|
|
16
|
+
bottom: 'additive',
|
|
17
|
+
right: 'additive',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type SafeAreaViewProps = NativeSafeAreaViewProps;
|
|
21
|
+
|
|
22
|
+
let isOnChange = false;
|
|
23
|
+
let realTop = 0;
|
|
24
|
+
let realLeft = 0;
|
|
25
|
+
let realBottom = 0;
|
|
26
|
+
let realRight = 0;
|
|
27
|
+
let safeAreaTop = 0;
|
|
28
|
+
let safeAreaLeft = 0;
|
|
29
|
+
let safeAreaBottom = 0;
|
|
30
|
+
let safeAreaRight = 0;
|
|
31
|
+
|
|
32
|
+
function getEdgeValue(edgeMode: string, insetValue: number, edgeValue: number) {
|
|
33
|
+
if (edgeMode === 'off') {
|
|
34
|
+
return edgeValue;
|
|
35
|
+
} else if (edgeMode === 'maximum') {
|
|
36
|
+
return Math.max(insetValue, edgeValue);
|
|
37
|
+
} else {
|
|
38
|
+
return insetValue + edgeValue;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const SafeAreaView = React.forwardRef<
|
|
43
|
+
NativeSafeAreaViewInstance,
|
|
44
|
+
SafeAreaViewProps
|
|
45
|
+
>(({ edges, ...props }, ref) => {
|
|
46
|
+
const nativeEdges = useMemo(() => {
|
|
47
|
+
if (edges == null) {
|
|
48
|
+
return defaultEdges;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const edgesObj = Array.isArray(edges)
|
|
52
|
+
? edges.reduce<EdgeRecord>((acc, edge: Edge) => {
|
|
53
|
+
acc[edge] = 'additive';
|
|
54
|
+
return acc;
|
|
55
|
+
}, {})
|
|
56
|
+
: // ts has trouble with refining readonly arrays.
|
|
57
|
+
(edges as EdgeRecord);
|
|
58
|
+
|
|
59
|
+
// make sure that we always pass all edges, required for fabric
|
|
60
|
+
const requiredEdges: Record<Edge, EdgeMode> = {
|
|
61
|
+
top: edgesObj.top ?? 'off',
|
|
62
|
+
right: edgesObj.right ?? 'off',
|
|
63
|
+
bottom: edgesObj.bottom ?? 'off',
|
|
64
|
+
left: edgesObj.left ?? 'off',
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return requiredEdges;
|
|
68
|
+
}, [edges]);
|
|
69
|
+
|
|
70
|
+
const [isFirstRefresh, setIsFirstRefresh] = useState(
|
|
71
|
+
false
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
let safeAreaStyle = [props.style]
|
|
75
|
+
let rawPadding = 0;
|
|
76
|
+
let rawMargin = 0;
|
|
77
|
+
let rawPaddingTop: number | undefined = undefined;
|
|
78
|
+
let rawPaddingLeft: number | undefined = undefined;
|
|
79
|
+
let rawPaddingBottom: number | undefined = undefined;
|
|
80
|
+
let rawPaddingRight: number | undefined = undefined;
|
|
81
|
+
let rawMarginTop: number | undefined = undefined;
|
|
82
|
+
let rawMarginLeft: number | undefined = undefined;
|
|
83
|
+
let rawMarginBottom: number | undefined = undefined;
|
|
84
|
+
let rawMarginRight: number | undefined = undefined;
|
|
85
|
+
|
|
86
|
+
const safeAreaChange = (e: ReactNative.NativeSyntheticEvent<onSafeAreaValueChangeEvent>) => {
|
|
87
|
+
if (e.nativeEvent.safeAreaViewInsetsValue?.top) {
|
|
88
|
+
safeAreaTop = e.nativeEvent.safeAreaViewInsetsValue?.top;
|
|
89
|
+
}
|
|
90
|
+
if (e.nativeEvent.safeAreaViewInsetsValue?.right) {
|
|
91
|
+
safeAreaRight = e.nativeEvent.safeAreaViewInsetsValue?.right;
|
|
92
|
+
}
|
|
93
|
+
if (e.nativeEvent.safeAreaViewInsetsValue?.bottom) {
|
|
94
|
+
safeAreaBottom = e.nativeEvent.safeAreaViewInsetsValue?.bottom;
|
|
95
|
+
}
|
|
96
|
+
if (e.nativeEvent.safeAreaViewInsetsValue?.left) {
|
|
97
|
+
safeAreaLeft = e.nativeEvent.safeAreaViewInsetsValue?.left;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!isOnChange) {
|
|
101
|
+
setIsFirstRefresh(true);
|
|
102
|
+
}
|
|
103
|
+
isOnChange = true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
let rawProp = props.style as ReactNative.ViewStyle[];
|
|
107
|
+
let i: number;
|
|
108
|
+
if (rawProp && rawProp.length > 0) {
|
|
109
|
+
for (i = 0; i < rawProp.length; i++) {
|
|
110
|
+
if (rawProp[i]?.padding) {
|
|
111
|
+
rawPadding = rawProp[i]?.padding as number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (rawProp[i]?.margin) {
|
|
115
|
+
rawMargin = rawProp[i]?.margin as number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (rawProp[i]?.paddingTop) {
|
|
119
|
+
rawPaddingTop = rawProp[i]?.paddingTop as number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (rawProp[i]?.paddingLeft) {
|
|
123
|
+
rawPaddingLeft = rawProp[i]?.paddingLeft as number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (rawProp[i]?.paddingRight) {
|
|
127
|
+
rawPaddingRight = rawProp[i]?.paddingRight as number;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (rawProp[i]?.paddingBottom) {
|
|
131
|
+
rawPaddingBottom = rawProp[i]?.paddingBottom as number;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (rawProp[i]?.marginTop) {
|
|
135
|
+
rawMarginTop = rawProp[i]?.marginTop as number;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (rawProp[i]?.marginLeft) {
|
|
139
|
+
rawMarginLeft = rawProp[i]?.marginLeft as number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (rawProp[i]?.marginRight) {
|
|
143
|
+
rawMarginRight = rawProp[i]?.marginRight as number;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (rawProp[i]?.marginBottom) {
|
|
147
|
+
rawMarginBottom = rawProp[i]?.marginBottom as number;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!rawPaddingTop) {
|
|
153
|
+
rawPaddingTop = rawPadding;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!rawPaddingLeft) {
|
|
157
|
+
rawPaddingLeft = rawPadding;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!rawPaddingRight) {
|
|
161
|
+
rawPaddingRight = rawPadding;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!rawPaddingBottom) {
|
|
165
|
+
rawPaddingBottom = rawPadding;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!rawMarginTop) {
|
|
169
|
+
rawMarginTop = rawMargin;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (!rawMarginLeft) {
|
|
173
|
+
rawMarginLeft = rawMargin;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!rawMarginRight) {
|
|
177
|
+
rawMarginRight = rawMargin;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (!rawMarginBottom) {
|
|
181
|
+
rawMarginBottom = rawMargin;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (isOnChange) {
|
|
185
|
+
if (props.mode === 'margin') {
|
|
186
|
+
realTop = getEdgeValue(nativeEdges.top, safeAreaTop, rawMarginTop)
|
|
187
|
+
realLeft = getEdgeValue(nativeEdges.left, safeAreaLeft, rawMarginLeft)
|
|
188
|
+
realBottom = getEdgeValue(nativeEdges.bottom, safeAreaBottom, rawMarginBottom)
|
|
189
|
+
realRight = getEdgeValue(nativeEdges.right, safeAreaRight, rawMarginRight)
|
|
190
|
+
safeAreaStyle = [props.style, { marginTop: realTop, marginLeft: realLeft, marginBottom: realBottom, marginRight: realRight }]
|
|
191
|
+
} else {
|
|
192
|
+
realTop = getEdgeValue(nativeEdges.top, safeAreaTop, rawPaddingTop)
|
|
193
|
+
realLeft = getEdgeValue(nativeEdges.left, safeAreaLeft, rawPaddingLeft)
|
|
194
|
+
realBottom = getEdgeValue(nativeEdges.bottom, safeAreaBottom, rawPaddingBottom)
|
|
195
|
+
realRight = getEdgeValue(nativeEdges.right, safeAreaRight, rawPaddingRight)
|
|
196
|
+
safeAreaStyle = [props.style, { paddingTop: realTop, paddingLeft: realLeft, paddingBottom: realBottom, paddingRight: realRight }]
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return <NativeSafeAreaView {...props} style={safeAreaStyle} edges={nativeEdges} isFirstRefresh={isFirstRefresh} onSafeAreaValueChange={safeAreaChange} ref={ref} />;
|
|
201
|
+
});
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TurboModule, TurboModuleRegistry } from 'react-native';
|
|
2
|
+
import type { Double } from 'react-native/Libraries/Types/CodegenTypes';
|
|
3
|
+
|
|
4
|
+
export interface Spec extends TurboModule {
|
|
5
|
+
getConstants: () => {
|
|
6
|
+
initialWindowMetrics?: {
|
|
7
|
+
insets: {
|
|
8
|
+
top: Double;
|
|
9
|
+
right: Double;
|
|
10
|
+
bottom: Double;
|
|
11
|
+
left: Double;
|
|
12
|
+
};
|
|
13
|
+
frame: {
|
|
14
|
+
x: Double;
|
|
15
|
+
y: Double;
|
|
16
|
+
width: Double;
|
|
17
|
+
height: Double;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default TurboModuleRegistry.get<Spec>('RNCSafeAreaContext');
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DirectEventHandler,
|
|
3
|
+
Double,
|
|
4
|
+
} from 'react-native/Libraries/Types/CodegenTypes';
|
|
5
|
+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
|
|
6
|
+
import type { ViewProps, HostComponent } from 'react-native';
|
|
7
|
+
|
|
8
|
+
export type Event = Readonly<{
|
|
9
|
+
insets: Readonly<{
|
|
10
|
+
top: Double;
|
|
11
|
+
right: Double;
|
|
12
|
+
bottom: Double;
|
|
13
|
+
left: Double;
|
|
14
|
+
}>;
|
|
15
|
+
frame: Readonly<{
|
|
16
|
+
x: Double;
|
|
17
|
+
y: Double;
|
|
18
|
+
width: Double;
|
|
19
|
+
height: Double;
|
|
20
|
+
}>;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
export interface NativeProps extends ViewProps {
|
|
24
|
+
onInsetsChange?: DirectEventHandler<Event>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default codegenNativeComponent<NativeProps>(
|
|
28
|
+
'RNCSafeAreaProvider',
|
|
29
|
+
) as HostComponent<NativeProps>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
|
|
2
|
+
import type { WithDefault, Float, DirectEventHandler } from 'react-native/Libraries/Types/CodegenTypes';
|
|
3
|
+
import type { ViewProps, HostComponent } from 'react-native';
|
|
4
|
+
|
|
5
|
+
export type onSafeAreaValueChangeEvent = Readonly<{
|
|
6
|
+
safeAreaViewInsetsValue?: {
|
|
7
|
+
top?: Float;
|
|
8
|
+
left?: Float;
|
|
9
|
+
bottom?: Float
|
|
10
|
+
right?: Float
|
|
11
|
+
};
|
|
12
|
+
}>
|
|
13
|
+
|
|
14
|
+
export interface NativeProps extends ViewProps {
|
|
15
|
+
mode?: WithDefault<'padding' | 'margin', 'padding'>;
|
|
16
|
+
edges?: Readonly<{
|
|
17
|
+
top: string;
|
|
18
|
+
right: string;
|
|
19
|
+
bottom: string;
|
|
20
|
+
left: string;
|
|
21
|
+
}>;
|
|
22
|
+
isFirstRefresh: boolean;
|
|
23
|
+
onSafeAreaValueChange: DirectEventHandler<onSafeAreaValueChangeEvent>,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default codegenNativeComponent<NativeProps>('RNCSafeAreaView', {
|
|
27
|
+
interfaceOnly: true,
|
|
28
|
+
}) as HostComponent<NativeProps>;
|