@amazon-devices/react-native-safe-area-context 2.0.1759135970 → 8.0.9000000000-rn-83

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.
Files changed (260) hide show
  1. package/README.md +6 -302
  2. package/SystemBundles/920f0b3a0c43cc93db1621b4c3a47e06a0d4ed32175c793e9934ad9c764fbc25.bundle +7374 -0
  3. package/SystemBundles/920f0b3a0c43cc93db1621b4c3a47e06a0d4ed32175c793e9934ad9c764fbc25.map +2780 -0
  4. package/SystemModules/amzn__react-native-safe-area-context-8.modules.txt +7 -0
  5. package/SystemModules/amzn__react-native-safe-area-context-8.private.modules.txt +61 -0
  6. package/SystemModules/keplerscript-system-bundle-config.json +3 -3
  7. package/android/build.gradle +103 -25
  8. package/android/gradle.properties +1 -0
  9. package/android/spotless.gradle +24 -0
  10. package/android/src/fabric/java/com/th3rdwave/safeareacontext/InsetsChangeEvent.kt +25 -0
  11. package/android/src/fabric/java/com/th3rdwave/safeareacontext/UIManagerHelperCompat.kt +14 -0
  12. package/android/src/main/AndroidManifest.xml +1 -2
  13. package/android/src/main/java/com/th3rdwave/safeareacontext/EdgeInsets.kt +3 -0
  14. package/android/src/main/java/com/th3rdwave/safeareacontext/Rect.kt +3 -0
  15. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaContextModule.kt +32 -0
  16. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaContextPackage.kt +41 -0
  17. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaProvider.kt +47 -0
  18. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaProviderManager.kt +41 -0
  19. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaUtils.kt +102 -0
  20. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.kt +155 -0
  21. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewEdges.kt +16 -0
  22. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewLocalData.kt +7 -0
  23. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewManager.kt +73 -0
  24. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewMode.kt +6 -0
  25. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewShadowNode.kt +170 -0
  26. package/android/src/main/java/com/th3rdwave/safeareacontext/SerializationUtils.kt +39 -0
  27. package/android/src/main/jni/CMakeLists.txt +87 -0
  28. package/android/src/main/jni/safeareacontext.h +17 -0
  29. package/android/src/paper/java/com/facebook/react/viewmanagers/RNCSafeAreaProviderManagerDelegate.java +31 -0
  30. package/android/src/paper/java/com/facebook/react/viewmanagers/RNCSafeAreaProviderManagerInterface.java +16 -0
  31. package/android/src/paper/java/com/facebook/react/viewmanagers/RNCSafeAreaViewManagerDelegate.java +41 -0
  32. package/android/src/paper/java/com/facebook/react/viewmanagers/RNCSafeAreaViewManagerInterface.java +20 -0
  33. package/android/src/paper/java/com/th3rdwave/safeareacontext/InsetsChangeEvent.kt +29 -0
  34. package/android/src/paper/java/com/th3rdwave/safeareacontext/NativeSafeAreaContextSpec.java +56 -0
  35. package/android/src/paper/java/com/th3rdwave/safeareacontext/UIManagerHelperCompat.kt +20 -0
  36. package/common/cpp/react/renderer/components/safeareacontext/RNCSafeAreaViewComponentDescriptor.h +25 -0
  37. package/common/cpp/react/renderer/components/safeareacontext/RNCSafeAreaViewShadowNode.cpp +131 -0
  38. package/common/cpp/react/renderer/components/safeareacontext/RNCSafeAreaViewShadowNode.h +36 -0
  39. package/common/cpp/react/renderer/components/safeareacontext/RNCSafeAreaViewState.cpp +23 -0
  40. package/common/cpp/react/renderer/components/safeareacontext/RNCSafeAreaViewState.h +56 -0
  41. package/ios/Fabric/RNCSafeAreaProviderComponentView.h +13 -0
  42. package/ios/Fabric/RNCSafeAreaProviderComponentView.mm +129 -0
  43. package/ios/Fabric/RNCSafeAreaViewComponentView.h +11 -0
  44. package/ios/Fabric/RNCSafeAreaViewComponentView.mm +153 -0
  45. package/ios/RNCSafeAreaContext.h +9 -0
  46. package/ios/RNCSafeAreaContext.mm +85 -0
  47. package/ios/{SafeAreaView/RNCSafeAreaProvider.h → RNCSafeAreaProvider.h} +4 -0
  48. package/ios/RNCSafeAreaProvider.m +102 -0
  49. package/ios/RNCSafeAreaProviderManager.m +20 -0
  50. package/ios/{SafeAreaView/RNCSafeAreaShadowView.m → RNCSafeAreaShadowView.m} +50 -36
  51. package/ios/RNCSafeAreaUtils.h +22 -0
  52. package/ios/RNCSafeAreaUtils.m +41 -0
  53. package/ios/{SafeAreaView/RNCSafeAreaView.h → RNCSafeAreaView.h} +8 -4
  54. package/ios/RNCSafeAreaView.m +168 -0
  55. package/ios/RNCSafeAreaViewEdgeMode.h +12 -0
  56. package/ios/RNCSafeAreaViewEdgeMode.m +16 -0
  57. package/ios/RNCSafeAreaViewEdges.h +15 -0
  58. package/ios/RNCSafeAreaViewEdges.m +36 -0
  59. package/ios/{SafeAreaView/RNCSafeAreaViewLocalData.h → RNCSafeAreaViewLocalData.h} +12 -1
  60. package/ios/{SafeAreaView/RNCSafeAreaViewLocalData.m → RNCSafeAreaViewLocalData.m} +4 -0
  61. package/ios/{SafeAreaView/RNCSafeAreaViewManager.m → RNCSafeAreaViewManager.m} +5 -1
  62. package/ios/RNCSafeAreaViewMode.m +15 -0
  63. package/ios/{SafeAreaView.xcodeproj → RNSafeAreaContext.xcodeproj}/project.pbxproj +70 -56
  64. package/jest/mock.tsx +59 -0
  65. package/kepler-compatibility.json +2 -2
  66. package/lib/commonjs/CompatNativeSafeAreaProvider.js +7 -8
  67. package/lib/commonjs/CompatNativeSafeAreaProvider.js.map +1 -1
  68. package/lib/commonjs/InitialWindow.js.map +1 -1
  69. package/lib/commonjs/InitialWindow.kepler.js +1 -1
  70. package/lib/commonjs/InitialWindow.kepler.js.map +1 -1
  71. package/lib/commonjs/InitialWindow.native.js +4 -4
  72. package/lib/commonjs/InitialWindow.native.js.map +1 -1
  73. package/lib/commonjs/NativeSafeAreaProvider.js +7 -125
  74. package/lib/commonjs/NativeSafeAreaProvider.js.map +1 -1
  75. package/lib/commonjs/NativeSafeAreaProvider.kepler.js +6 -2
  76. package/lib/commonjs/NativeSafeAreaProvider.kepler.js.map +1 -1
  77. package/lib/commonjs/NativeSafeAreaProvider.web.js +135 -0
  78. package/lib/commonjs/NativeSafeAreaProvider.web.js.map +1 -0
  79. package/lib/commonjs/NativeSafeAreaProvider.windows.js +6 -2
  80. package/lib/commonjs/NativeSafeAreaProvider.windows.js.map +1 -1
  81. package/lib/commonjs/SafeArea.types.js.map +1 -1
  82. package/lib/commonjs/SafeAreaContext.js +56 -36
  83. package/lib/commonjs/SafeAreaContext.js.map +1 -1
  84. package/lib/commonjs/SafeAreaView.js +40 -73
  85. package/lib/commonjs/SafeAreaView.js.map +1 -1
  86. package/lib/commonjs/SafeAreaView.kepler.js.map +1 -1
  87. package/lib/commonjs/SafeAreaView.web.js +91 -0
  88. package/lib/commonjs/SafeAreaView.web.js.map +1 -0
  89. package/lib/commonjs/SafeAreaView.windows.js.map +1 -1
  90. package/lib/commonjs/index.js +1 -0
  91. package/lib/commonjs/index.js.map +1 -1
  92. package/lib/commonjs/package.json +1 -0
  93. package/lib/commonjs/specs/NativeSafeAreaContext.js +9 -0
  94. package/lib/commonjs/specs/NativeSafeAreaContext.js.map +1 -0
  95. package/lib/commonjs/specs/NativeSafeAreaProvider.js +10 -0
  96. package/lib/commonjs/specs/NativeSafeAreaProvider.js.map +1 -0
  97. package/lib/commonjs/specs/NativeSafeAreaView.js +12 -0
  98. package/lib/commonjs/specs/NativeSafeAreaView.js.map +1 -0
  99. package/lib/module/CompatNativeSafeAreaProvider.js +8 -5
  100. package/lib/module/CompatNativeSafeAreaProvider.js.map +1 -1
  101. package/lib/module/InitialWindow.js +2 -0
  102. package/lib/module/InitialWindow.js.map +1 -1
  103. package/lib/module/InitialWindow.kepler.js +3 -1
  104. package/lib/module/InitialWindow.kepler.js.map +1 -1
  105. package/lib/module/InitialWindow.native.js +5 -4
  106. package/lib/module/InitialWindow.native.js.map +1 -1
  107. package/lib/module/NativeSafeAreaProvider.js +3 -122
  108. package/lib/module/NativeSafeAreaProvider.js.map +1 -1
  109. package/lib/module/NativeSafeAreaProvider.kepler.js +3 -2
  110. package/lib/module/NativeSafeAreaProvider.kepler.js.map +1 -1
  111. package/lib/module/NativeSafeAreaProvider.web.js +130 -0
  112. package/lib/module/NativeSafeAreaProvider.web.js.map +1 -0
  113. package/lib/module/NativeSafeAreaProvider.windows.js +3 -2
  114. package/lib/module/NativeSafeAreaProvider.windows.js.map +1 -1
  115. package/lib/module/SafeArea.types.js +2 -0
  116. package/lib/module/SafeArea.types.js.map +1 -1
  117. package/lib/module/SafeAreaContext.js +57 -33
  118. package/lib/module/SafeAreaContext.js.map +1 -1
  119. package/lib/module/SafeAreaView.js +38 -69
  120. package/lib/module/SafeAreaView.js.map +1 -1
  121. package/lib/module/SafeAreaView.kepler.js +2 -0
  122. package/lib/module/SafeAreaView.kepler.js.map +1 -1
  123. package/lib/module/SafeAreaView.web.js +86 -0
  124. package/lib/module/SafeAreaView.web.js.map +1 -0
  125. package/lib/module/SafeAreaView.windows.js +2 -0
  126. package/lib/module/SafeAreaView.windows.js.map +1 -1
  127. package/lib/module/index.js +3 -0
  128. package/lib/module/index.js.map +1 -1
  129. package/lib/module/specs/NativeSafeAreaContext.js +5 -0
  130. package/lib/module/specs/NativeSafeAreaContext.js.map +1 -0
  131. package/lib/module/specs/NativeSafeAreaProvider.js +5 -0
  132. package/lib/module/specs/NativeSafeAreaProvider.js.map +1 -0
  133. package/lib/module/specs/NativeSafeAreaView.js +7 -0
  134. package/lib/module/specs/NativeSafeAreaView.js.map +1 -0
  135. package/lib/typescript/CompatNativeSafeAreaProvider.d.ts +4 -0
  136. package/lib/typescript/CompatNativeSafeAreaProvider.d.ts.map +1 -0
  137. package/lib/typescript/{src/InitialWindow.d.ts → InitialWindow.d.ts} +2 -1
  138. package/lib/typescript/InitialWindow.d.ts.map +1 -0
  139. package/lib/typescript/{src/InitialWindow.kepler.d.ts → InitialWindow.kepler.d.ts} +1 -0
  140. package/lib/typescript/InitialWindow.kepler.d.ts.map +1 -0
  141. package/lib/typescript/{src/InitialWindow.native.d.ts → InitialWindow.native.d.ts} +2 -1
  142. package/lib/typescript/InitialWindow.native.d.ts.map +1 -0
  143. package/lib/typescript/NativeSafeAreaProvider.d.ts +3 -0
  144. package/lib/typescript/NativeSafeAreaProvider.d.ts.map +1 -0
  145. package/lib/typescript/NativeSafeAreaProvider.kepler.d.ts +2 -0
  146. package/lib/typescript/NativeSafeAreaProvider.kepler.d.ts.map +1 -0
  147. package/lib/typescript/NativeSafeAreaProvider.web.d.ts +4 -0
  148. package/lib/typescript/NativeSafeAreaProvider.web.d.ts.map +1 -0
  149. package/lib/typescript/NativeSafeAreaProvider.windows.d.ts +2 -0
  150. package/lib/typescript/NativeSafeAreaProvider.windows.d.ts.map +1 -0
  151. package/lib/typescript/SafeArea.types.d.ts +36 -0
  152. package/lib/typescript/SafeArea.types.d.ts.map +1 -0
  153. package/lib/typescript/{src/SafeAreaContext.d.ts → SafeAreaContext.d.ts} +9 -6
  154. package/lib/typescript/SafeAreaContext.d.ts.map +1 -0
  155. package/lib/typescript/SafeAreaView.d.ts +5 -0
  156. package/lib/typescript/SafeAreaView.d.ts.map +1 -0
  157. package/lib/typescript/{src/SafeAreaView.kepler.d.ts → SafeAreaView.kepler.d.ts} +1 -0
  158. package/lib/typescript/SafeAreaView.kepler.d.ts.map +1 -0
  159. package/lib/typescript/SafeAreaView.web.d.ts +4 -0
  160. package/lib/typescript/SafeAreaView.web.d.ts.map +1 -0
  161. package/lib/typescript/SafeAreaView.windows.d.ts +3 -0
  162. package/lib/typescript/SafeAreaView.windows.d.ts.map +1 -0
  163. package/lib/typescript/{src/index.d.ts → index.d.ts} +1 -0
  164. package/lib/typescript/index.d.ts.map +1 -0
  165. package/lib/typescript/specs/NativeSafeAreaContext.d.ts +23 -0
  166. package/lib/typescript/specs/NativeSafeAreaContext.d.ts.map +1 -0
  167. package/lib/typescript/specs/NativeSafeAreaProvider.d.ts +22 -0
  168. package/lib/typescript/specs/NativeSafeAreaProvider.d.ts.map +1 -0
  169. package/lib/typescript/specs/NativeSafeAreaView.d.ts +14 -0
  170. package/lib/typescript/specs/NativeSafeAreaView.d.ts.map +1 -0
  171. package/oss-attribution/attribution.txt +0 -0
  172. package/oss-attribution/licenseInfos.json +1 -0
  173. package/package.json +137 -1
  174. package/react-native-safe-area-context.podspec +26 -4
  175. package/react-native.config.js +8 -0
  176. package/src/CompatNativeSafeAreaProvider.tsx +2 -3
  177. package/src/InitialWindow.kepler.ts +1 -1
  178. package/src/InitialWindow.native.ts +5 -11
  179. package/src/InitialWindow.ts +1 -1
  180. package/src/NativeSafeAreaProvider.kepler.tsx +1 -3
  181. package/src/NativeSafeAreaProvider.tsx +2 -128
  182. package/src/NativeSafeAreaProvider.web.tsx +127 -0
  183. package/src/NativeSafeAreaProvider.windows.tsx +1 -3
  184. package/src/SafeArea.types.ts +15 -11
  185. package/src/SafeAreaContext.tsx +66 -43
  186. package/src/SafeAreaView.tsx +47 -82
  187. package/src/SafeAreaView.web.tsx +123 -0
  188. package/src/SafeAreaView.windows.tsx +2 -1
  189. package/src/index.tsx +2 -0
  190. package/src/specs/NativeSafeAreaContext.ts +23 -0
  191. package/src/specs/NativeSafeAreaProvider.ts +29 -0
  192. package/src/specs/NativeSafeAreaView.ts +17 -0
  193. package/SystemBundles/e3c0cdeab56b8b622d4f0fd5a80812eafe199fa2287d0e3a09c5e573a1035350.bundle +0 -11031
  194. package/SystemBundles/e3c0cdeab56b8b622d4f0fd5a80812eafe199fa2287d0e3a09c5e573a1035350.map +0 -1
  195. package/SystemModules/amzn__react-native-safe-area-context-2.modules.txt +0 -55
  196. package/SystemModules/amzn__react-native-safe-area-context-2.private.modules.txt +0 -91
  197. package/THIRD_PARTY_NOTICES.txt +0 -24
  198. package/android/src/main/java/com/th3rdwave/safeareacontext/EdgeInsets.java +0 -22
  199. package/android/src/main/java/com/th3rdwave/safeareacontext/InsetsChangeEvent.java +0 -33
  200. package/android/src/main/java/com/th3rdwave/safeareacontext/Rect.java +0 -22
  201. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaContextPackage.java +0 -31
  202. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaProvider.java +0 -66
  203. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaProviderManager.java +0 -94
  204. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaUtils.java +0 -82
  205. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.java +0 -159
  206. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewEdges.java +0 -8
  207. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewLocalData.java +0 -27
  208. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewManager.java +0 -72
  209. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewMode.java +0 -6
  210. package/android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewShadowNode.java +0 -201
  211. package/android/src/main/java/com/th3rdwave/safeareacontext/SerializationUtils.java +0 -52
  212. package/ios/SafeAreaView/RCTView+SafeAreaCompat.h +0 -16
  213. package/ios/SafeAreaView/RCTView+SafeAreaCompat.m +0 -57
  214. package/ios/SafeAreaView/RNCSafeAreaProvider.m +0 -65
  215. package/ios/SafeAreaView/RNCSafeAreaProviderManager.m +0 -61
  216. package/ios/SafeAreaView/RNCSafeAreaView.m +0 -118
  217. package/ios/SafeAreaView/RNCSafeAreaViewEdges.h +0 -9
  218. package/ios/SafeAreaView/RNCSafeAreaViewEdges.m +0 -13
  219. package/ios/SafeAreaView/RNCSafeAreaViewMode.m +0 -11
  220. package/lib/commonjs/NativeSafeAreaProvider.macos.js +0 -9
  221. package/lib/commonjs/NativeSafeAreaProvider.macos.js.map +0 -1
  222. package/lib/commonjs/NativeSafeAreaProvider.native.js +0 -9
  223. package/lib/commonjs/NativeSafeAreaProvider.native.js.map +0 -1
  224. package/lib/commonjs/SafeAreaView.macos.js +0 -9
  225. package/lib/commonjs/SafeAreaView.macos.js.map +0 -1
  226. package/lib/commonjs/SafeAreaView.native.js +0 -9
  227. package/lib/commonjs/SafeAreaView.native.js.map +0 -1
  228. package/lib/commonjs/useWindowDimensions.js +0 -32
  229. package/lib/commonjs/useWindowDimensions.js.map +0 -1
  230. package/lib/module/NativeSafeAreaProvider.macos.js +0 -3
  231. package/lib/module/NativeSafeAreaProvider.macos.js.map +0 -1
  232. package/lib/module/NativeSafeAreaProvider.native.js +0 -3
  233. package/lib/module/NativeSafeAreaProvider.native.js.map +0 -1
  234. package/lib/module/SafeAreaView.macos.js +0 -3
  235. package/lib/module/SafeAreaView.macos.js.map +0 -1
  236. package/lib/module/SafeAreaView.native.js +0 -3
  237. package/lib/module/SafeAreaView.native.js.map +0 -1
  238. package/lib/module/useWindowDimensions.js +0 -26
  239. package/lib/module/useWindowDimensions.js.map +0 -1
  240. package/lib/typescript/src/CompatNativeSafeAreaProvider.d.ts +0 -2
  241. package/lib/typescript/src/NativeSafeAreaProvider.d.ts +0 -2
  242. package/lib/typescript/src/NativeSafeAreaProvider.kepler.d.ts +0 -2
  243. package/lib/typescript/src/NativeSafeAreaProvider.macos.d.ts +0 -2
  244. package/lib/typescript/src/NativeSafeAreaProvider.native.d.ts +0 -3
  245. package/lib/typescript/src/NativeSafeAreaProvider.windows.d.ts +0 -2
  246. package/lib/typescript/src/SafeArea.types.d.ts +0 -31
  247. package/lib/typescript/src/SafeAreaView.d.ts +0 -2
  248. package/lib/typescript/src/SafeAreaView.macos.d.ts +0 -2
  249. package/lib/typescript/src/SafeAreaView.native.d.ts +0 -2
  250. package/lib/typescript/src/SafeAreaView.windows.d.ts +0 -2
  251. package/lib/typescript/src/useWindowDimensions.d.ts +0 -8
  252. package/src/NativeSafeAreaProvider.macos.tsx +0 -3
  253. package/src/NativeSafeAreaProvider.native.tsx +0 -6
  254. package/src/SafeAreaView.macos.tsx +0 -3
  255. package/src/SafeAreaView.native.tsx +0 -6
  256. package/src/useWindowDimensions.tsx +0 -35
  257. /package/ios/{SafeAreaView/RNCSafeAreaProviderManager.h → RNCSafeAreaProviderManager.h} +0 -0
  258. /package/ios/{SafeAreaView/RNCSafeAreaShadowView.h → RNCSafeAreaShadowView.h} +0 -0
  259. /package/ios/{SafeAreaView/RNCSafeAreaViewManager.h → RNCSafeAreaViewManager.h} +0 -0
  260. /package/ios/{SafeAreaView/RNCSafeAreaViewMode.h → RNCSafeAreaViewMode.h} +0 -0
@@ -0,0 +1,127 @@
1
+ /* eslint-env browser */
2
+
3
+ import * as React from 'react';
4
+ import { View } from 'react-native';
5
+ import type { NativeSafeAreaProviderProps } from './SafeArea.types';
6
+
7
+ /**
8
+ * TODO:
9
+ * Currently insets and frame are based on the window and are not
10
+ * relative to the provider view. This is inconsistent with iOS and Android.
11
+ * However in most cases if the provider view covers the screen this is not
12
+ * an issue.
13
+ */
14
+
15
+ const CSSTransitions: Record<string, string> = {
16
+ WebkitTransition: 'webkitTransitionEnd',
17
+ Transition: 'transitionEnd',
18
+ MozTransition: 'transitionend',
19
+ MSTransition: 'msTransitionEnd',
20
+ OTransition: 'oTransitionEnd',
21
+ };
22
+
23
+ export function NativeSafeAreaProvider({
24
+ children,
25
+ style,
26
+ onInsetsChange,
27
+ }: NativeSafeAreaProviderProps) {
28
+ React.useEffect(() => {
29
+ // Skip for SSR.
30
+ if (typeof document === 'undefined') {
31
+ return;
32
+ }
33
+
34
+ const element = createContextElement();
35
+ document.body.appendChild(element);
36
+ const onEnd = () => {
37
+ const { paddingTop, paddingBottom, paddingLeft, paddingRight } =
38
+ window.getComputedStyle(element);
39
+
40
+ const insets = {
41
+ top: paddingTop ? parseInt(paddingTop, 10) : 0,
42
+ bottom: paddingBottom ? parseInt(paddingBottom, 10) : 0,
43
+ left: paddingLeft ? parseInt(paddingLeft, 10) : 0,
44
+ right: paddingRight ? parseInt(paddingRight, 10) : 0,
45
+ };
46
+ const frame = {
47
+ x: 0,
48
+ y: 0,
49
+ width: document.documentElement.offsetWidth,
50
+ height: document.documentElement.offsetHeight,
51
+ };
52
+ // @ts-ignore: missing properties
53
+ onInsetsChange({ nativeEvent: { insets, frame } });
54
+ };
55
+ element.addEventListener(getSupportedTransitionEvent(), onEnd);
56
+ onEnd();
57
+ return () => {
58
+ document.body.removeChild(element);
59
+ element.removeEventListener(getSupportedTransitionEvent(), onEnd);
60
+ };
61
+ }, [onInsetsChange]);
62
+
63
+ return <View style={style}>{children}</View>;
64
+ }
65
+
66
+ let _supportedTransitionEvent: string | null | undefined = null;
67
+ function getSupportedTransitionEvent(): string {
68
+ if (_supportedTransitionEvent != null) {
69
+ return _supportedTransitionEvent;
70
+ }
71
+ const element = document.createElement('invalidtype');
72
+
73
+ _supportedTransitionEvent = CSSTransitions.Transition;
74
+ for (const key in CSSTransitions) {
75
+ if (element.style[key as keyof CSSStyleDeclaration] !== undefined) {
76
+ _supportedTransitionEvent = CSSTransitions[key];
77
+ break;
78
+ }
79
+ }
80
+ return _supportedTransitionEvent as string;
81
+ }
82
+
83
+ type CssEnv = 'constant' | 'env';
84
+
85
+ let _supportedEnv: CssEnv | null = null;
86
+ function getSupportedEnv(): CssEnv {
87
+ if (_supportedEnv !== null) {
88
+ return _supportedEnv;
89
+ }
90
+ const { CSS } = window;
91
+ if (
92
+ CSS &&
93
+ CSS.supports &&
94
+ CSS.supports('top: constant(safe-area-inset-top)')
95
+ ) {
96
+ _supportedEnv = 'constant';
97
+ } else {
98
+ _supportedEnv = 'env';
99
+ }
100
+ return _supportedEnv;
101
+ }
102
+
103
+ function getInset(side: string): string {
104
+ return `${getSupportedEnv()}(safe-area-inset-${side})`;
105
+ }
106
+
107
+ function createContextElement(): HTMLElement {
108
+ const element = document.createElement('div');
109
+ const { style } = element;
110
+ style.position = 'fixed';
111
+ style.left = '0';
112
+ style.top = '0';
113
+ style.width = '0';
114
+ style.height = '0';
115
+ style.zIndex = '-1';
116
+ style.overflow = 'hidden';
117
+ style.visibility = 'hidden';
118
+ // Bacon: Anything faster than this and the callback will be invoked too early with the wrong insets
119
+ style.transitionDuration = '0.05s';
120
+ style.transitionProperty = 'padding';
121
+ style.transitionDelay = '0s';
122
+ style.paddingTop = getInset('top');
123
+ style.paddingBottom = getInset('bottom');
124
+ style.paddingLeft = getInset('left');
125
+ style.paddingRight = getInset('right');
126
+ return element;
127
+ }
@@ -1,3 +1 @@
1
- import { CompatNativeSafeAreaProvider } from './CompatNativeSafeAreaProvider';
2
-
3
- export default CompatNativeSafeAreaProvider;
1
+ export { CompatNativeSafeAreaProvider as NativeSafeAreaProvider } from './CompatNativeSafeAreaProvider';
@@ -1,11 +1,12 @@
1
- import {
2
- NativeSyntheticEvent,
3
- StyleProp,
4
- ViewProps,
5
- ViewStyle,
6
- } from 'react-native';
1
+ import type * as React from 'react';
2
+ import type { NativeSyntheticEvent, ViewProps } from 'react-native';
3
+ import NativeSafeAreaView from './specs/NativeSafeAreaView';
7
4
 
8
5
  export type Edge = 'top' | 'right' | 'bottom' | 'left';
6
+ export type EdgeMode = 'off' | 'additive' | 'maximum';
7
+
8
+ export type EdgeRecord = Partial<Record<Edge, EdgeMode>>;
9
+ export type Edges = readonly Edge[] | Readonly<EdgeRecord>;
9
10
 
10
11
  export interface EdgeInsets {
11
12
  top: number;
@@ -30,14 +31,17 @@ export type InsetChangedEvent = NativeSyntheticEvent<Metrics>;
30
31
 
31
32
  export type InsetChangeNativeCallback = (event: InsetChangedEvent) => void;
32
33
 
33
- export interface NativeSafeAreaProviderProps {
34
+ export interface NativeSafeAreaProviderProps extends ViewProps {
34
35
  children?: React.ReactNode;
35
- style?: StyleProp<ViewStyle>;
36
36
  onInsetsChange: InsetChangeNativeCallback;
37
37
  }
38
38
 
39
- export type NativeSafeAreaViewProps = ViewProps & {
39
+ export interface NativeSafeAreaViewProps extends ViewProps {
40
40
  children?: React.ReactNode;
41
41
  mode?: 'padding' | 'margin';
42
- edges?: ReadonlyArray<Edge>;
43
- };
42
+ edges?: Edges;
43
+ }
44
+
45
+ export type NativeSafeAreaViewInstance = InstanceType<
46
+ typeof NativeSafeAreaView
47
+ >;
@@ -1,24 +1,34 @@
1
1
  import * as React from 'react';
2
- import { Dimensions, StyleProp, StyleSheet, ViewStyle } from 'react-native';
3
- import NativeSafeAreaProvider from './NativeSafeAreaProvider';
4
- import { EdgeInsets, InsetChangedEvent, Metrics, Rect } from './SafeArea.types';
2
+ import { Dimensions, StyleSheet, type ViewProps } from 'react-native';
3
+ import { NativeSafeAreaProvider } from './NativeSafeAreaProvider';
4
+ import type {
5
+ EdgeInsets,
6
+ InsetChangedEvent,
7
+ Metrics,
8
+ Rect,
9
+ } from './SafeArea.types';
10
+
11
+ const isDev = process.env.NODE_ENV !== 'production';
5
12
 
6
13
  export const SafeAreaInsetsContext = React.createContext<EdgeInsets | null>(
7
14
  null,
8
15
  );
9
- SafeAreaInsetsContext.displayName = 'SafeAreaInsetsContext';
16
+ if (isDev) {
17
+ SafeAreaInsetsContext.displayName = 'SafeAreaInsetsContext';
18
+ }
10
19
 
11
20
  export const SafeAreaFrameContext = React.createContext<Rect | null>(null);
12
- SafeAreaFrameContext.displayName = 'SafeAreaFrameContext';
21
+ if (isDev) {
22
+ SafeAreaFrameContext.displayName = 'SafeAreaFrameContext';
23
+ }
13
24
 
14
- export interface SafeAreaViewProps {
25
+ export interface SafeAreaProviderProps extends ViewProps {
15
26
  children?: React.ReactNode;
16
27
  initialMetrics?: Metrics | null;
17
28
  /**
18
29
  * @deprecated
19
30
  */
20
31
  initialSafeAreaInsets?: EdgeInsets | null;
21
- style?: StyleProp<ViewStyle>;
22
32
  }
23
33
 
24
34
  export function SafeAreaProvider({
@@ -26,7 +36,8 @@ export function SafeAreaProvider({
26
36
  initialMetrics,
27
37
  initialSafeAreaInsets,
28
38
  style,
29
- }: SafeAreaViewProps) {
39
+ ...others
40
+ }: SafeAreaProviderProps) {
30
41
  const parentInsets = useParentSafeAreaInsets();
31
42
  const parentFrame = useParentSafeAreaFrame();
32
43
  const [insets, setInsets] = React.useState<EdgeInsets | null>(
@@ -42,40 +53,46 @@ export function SafeAreaProvider({
42
53
  height: Dimensions.get('window').height,
43
54
  },
44
55
  );
45
- const onInsetsChange = React.useCallback(
46
- (event: InsetChangedEvent) => {
47
- const {
48
- nativeEvent: { frame: nextFrame, insets: nextInsets },
49
- } = event;
56
+ const onInsetsChange = React.useCallback((event: InsetChangedEvent) => {
57
+ const {
58
+ nativeEvent: { frame: nextFrame, insets: nextInsets },
59
+ } = event;
50
60
 
61
+ setFrame((curFrame) => {
51
62
  if (
52
63
  // Backwards compat with old native code that won't send frame.
53
64
  nextFrame &&
54
- (nextFrame.height !== frame.height ||
55
- nextFrame.width !== frame.width ||
56
- nextFrame.x !== frame.x ||
57
- nextFrame.y !== frame.y)
65
+ (nextFrame.height !== curFrame.height ||
66
+ nextFrame.width !== curFrame.width ||
67
+ nextFrame.x !== curFrame.x ||
68
+ nextFrame.y !== curFrame.y)
58
69
  ) {
59
- setFrame(nextFrame);
70
+ return nextFrame;
71
+ } else {
72
+ return curFrame;
60
73
  }
74
+ });
61
75
 
76
+ setInsets((curInsets) => {
62
77
  if (
63
- !insets ||
64
- nextInsets.bottom !== insets.bottom ||
65
- nextInsets.left !== insets.left ||
66
- nextInsets.right !== insets.right ||
67
- nextInsets.top !== insets.top
78
+ !curInsets ||
79
+ nextInsets.bottom !== curInsets.bottom ||
80
+ nextInsets.left !== curInsets.left ||
81
+ nextInsets.right !== curInsets.right ||
82
+ nextInsets.top !== curInsets.top
68
83
  ) {
69
- setInsets(nextInsets);
84
+ return nextInsets;
85
+ } else {
86
+ return curInsets;
70
87
  }
71
- },
72
- [frame, insets],
73
- );
88
+ });
89
+ }, []);
74
90
 
75
91
  return (
76
92
  <NativeSafeAreaProvider
77
93
  style={[styles.fill, style]}
78
94
  onInsetsChange={onInsetsChange}
95
+ {...others}
79
96
  >
80
97
  {insets != null ? (
81
98
  <SafeAreaFrameContext.Provider value={frame}>
@@ -100,34 +117,40 @@ function useParentSafeAreaFrame(): Rect | null {
100
117
  return React.useContext(SafeAreaFrameContext);
101
118
  }
102
119
 
120
+ const NO_INSETS_ERROR =
121
+ 'No safe area value available. Make sure you are rendering `<SafeAreaProvider>` at the top of your app.';
122
+
103
123
  export function useSafeAreaInsets(): EdgeInsets {
104
- const safeArea = React.useContext(SafeAreaInsetsContext);
105
- if (safeArea == null) {
106
- throw new Error(
107
- 'No safe area insets value available. Make sure you are rendering `<SafeAreaProvider>` at the top of your app.',
108
- );
124
+ const insets = React.useContext(SafeAreaInsetsContext);
125
+ if (insets == null) {
126
+ throw new Error(NO_INSETS_ERROR);
109
127
  }
110
- return safeArea;
128
+ return insets;
111
129
  }
112
130
 
113
131
  export function useSafeAreaFrame(): Rect {
114
132
  const frame = React.useContext(SafeAreaFrameContext);
115
133
  if (frame == null) {
116
- throw new Error(
117
- 'No safe area frame value available. Make sure you are rendering `<SafeAreaProvider>` at the top of your app.',
118
- );
134
+ throw new Error(NO_INSETS_ERROR);
119
135
  }
120
136
  return frame;
121
137
  }
122
138
 
139
+ export type WithSafeAreaInsetsProps = {
140
+ insets: EdgeInsets;
141
+ };
142
+
123
143
  export function withSafeAreaInsets<T>(
124
- WrappedComponent: React.ComponentType<T>,
125
- ) {
126
- return React.forwardRef((props: T, ref: React.Ref<T>) => (
127
- <SafeAreaConsumer>
128
- {(insets) => <WrappedComponent {...props} insets={insets} ref={ref} />}
129
- </SafeAreaConsumer>
130
- ));
144
+ WrappedComponent: React.ComponentType<
145
+ (React.PropsWithoutRef<T> | T) & WithSafeAreaInsetsProps
146
+ >,
147
+ ): React.ForwardRefExoticComponent<
148
+ React.PropsWithoutRef<T> & React.RefAttributes<unknown>
149
+ > {
150
+ return React.forwardRef<unknown, T>((props, ref) => {
151
+ const insets = useSafeAreaInsets();
152
+ return <WrappedComponent {...props} insets={insets} ref={ref} />;
153
+ });
131
154
  }
132
155
 
133
156
  /**
@@ -1,85 +1,50 @@
1
1
  import * as React from 'react';
2
- import { View, StyleSheet } from 'react-native';
3
- import { useSafeAreaInsets } from './SafeAreaContext';
4
- import { Edge, NativeSafeAreaViewProps } from './SafeArea.types';
5
-
6
- // prettier-ignore
7
- const TOP = 0b1000,
8
- RIGHT = 0b0100,
9
- BOTTOM = 0b0010,
10
- LEFT = 0b0001,
11
- ALL = 0b1111;
12
-
13
- /* eslint-disable no-bitwise */
14
-
15
- const edgeBitmaskMap: Record<Edge, number> = {
16
- top: TOP,
17
- right: RIGHT,
18
- bottom: BOTTOM,
19
- left: LEFT,
2
+ import type {
3
+ Edge,
4
+ EdgeMode,
5
+ EdgeRecord,
6
+ NativeSafeAreaViewInstance,
7
+ NativeSafeAreaViewProps,
8
+ } from './SafeArea.types';
9
+ import NativeSafeAreaView from './specs/NativeSafeAreaView';
10
+ import { useMemo } from 'react';
11
+
12
+ const defaultEdges: Record<Edge, EdgeMode> = {
13
+ top: 'additive',
14
+ left: 'additive',
15
+ bottom: 'additive',
16
+ right: 'additive',
20
17
  };
21
18
 
22
- export function SafeAreaView({
23
- style = {},
24
- mode,
25
- edges,
26
- ...rest
27
- }: NativeSafeAreaViewProps) {
28
- const insets = useSafeAreaInsets();
29
-
30
- const edgeBitmask =
31
- edges != null
32
- ? edges.reduce((accum, edge) => accum | edgeBitmaskMap[edge], 0)
33
- : ALL;
34
-
35
- const appliedStyle = React.useMemo(() => {
36
- const insetTop = edgeBitmask & TOP ? insets.top : 0;
37
- const insetRight = edgeBitmask & RIGHT ? insets.right : 0;
38
- const insetBottom = edgeBitmask & BOTTOM ? insets.bottom : 0;
39
- const insetLeft = edgeBitmask & LEFT ? insets.left : 0;
40
-
41
- const flatStyle = StyleSheet.flatten(style) as Record<string, number>;
42
-
43
- if (mode === 'margin') {
44
- const {
45
- margin = 0,
46
- marginVertical = margin,
47
- marginHorizontal = margin,
48
- marginTop = marginVertical,
49
- marginRight = marginHorizontal,
50
- marginBottom = marginVertical,
51
- marginLeft = marginHorizontal,
52
- } = flatStyle;
53
-
54
- const marginStyle = {
55
- marginTop: marginTop + insetTop,
56
- marginRight: marginRight + insetRight,
57
- marginBottom: marginBottom + insetBottom,
58
- marginLeft: marginLeft + insetLeft,
59
- };
60
-
61
- return [style, marginStyle];
62
- } else {
63
- const {
64
- padding = 0,
65
- paddingVertical = padding,
66
- paddingHorizontal = padding,
67
- paddingTop = paddingVertical,
68
- paddingRight = paddingHorizontal,
69
- paddingBottom = paddingVertical,
70
- paddingLeft = paddingHorizontal,
71
- } = flatStyle;
72
-
73
- const paddingStyle = {
74
- paddingTop: paddingTop + insetTop,
75
- paddingRight: paddingRight + insetRight,
76
- paddingBottom: paddingBottom + insetBottom,
77
- paddingLeft: paddingLeft + insetLeft,
78
- };
79
-
80
- return [style, paddingStyle];
81
- }
82
- }, [style, insets, mode, edgeBitmask]);
83
-
84
- return <View style={appliedStyle} {...rest} />;
85
- }
19
+ export type SafeAreaViewProps = NativeSafeAreaViewProps;
20
+
21
+ export const SafeAreaView = React.forwardRef<
22
+ NativeSafeAreaViewInstance,
23
+ SafeAreaViewProps
24
+ >(({ edges, ...props }, ref) => {
25
+ const nativeEdges = useMemo(() => {
26
+ if (edges == null) {
27
+ return defaultEdges;
28
+ }
29
+
30
+ const edgesObj = Array.isArray(edges)
31
+ ? edges.reduce<EdgeRecord>((acc, edge: Edge) => {
32
+ acc[edge] = 'additive';
33
+ return acc;
34
+ }, {})
35
+ : // ts has trouble with refining readonly arrays.
36
+ (edges as EdgeRecord);
37
+
38
+ // make sure that we always pass all edges, required for fabric
39
+ const requiredEdges: Record<Edge, EdgeMode> = {
40
+ top: edgesObj.top ?? 'off',
41
+ right: edgesObj.right ?? 'off',
42
+ bottom: edgesObj.bottom ?? 'off',
43
+ left: edgesObj.left ?? 'off',
44
+ };
45
+
46
+ return requiredEdges;
47
+ }, [edges]);
48
+
49
+ return <NativeSafeAreaView {...props} edges={nativeEdges} ref={ref} />;
50
+ });
@@ -0,0 +1,123 @@
1
+ import * as React from 'react';
2
+ import { StyleSheet, View } from 'react-native';
3
+ import {
4
+ Edge,
5
+ EdgeMode,
6
+ EdgeRecord,
7
+ NativeSafeAreaViewInstance,
8
+ NativeSafeAreaViewProps,
9
+ } from './SafeArea.types';
10
+ import { useSafeAreaInsets } from './SafeAreaContext';
11
+
12
+ const defaultEdges: Record<Edge, EdgeMode> = {
13
+ top: 'additive',
14
+ left: 'additive',
15
+ bottom: 'additive',
16
+ right: 'additive',
17
+ };
18
+
19
+ function getEdgeValue(
20
+ inset: number,
21
+ current: number,
22
+ mode: EdgeMode | undefined,
23
+ ) {
24
+ switch (mode) {
25
+ case 'off':
26
+ return current;
27
+ case 'maximum':
28
+ return Math.max(current, inset);
29
+ case 'additive':
30
+ default:
31
+ return current + inset;
32
+ }
33
+ }
34
+
35
+ export const SafeAreaView = React.forwardRef<
36
+ NativeSafeAreaViewInstance,
37
+ NativeSafeAreaViewProps
38
+ >(({ style = {}, mode, edges, ...rest }, ref) => {
39
+ const insets = useSafeAreaInsets();
40
+
41
+ const edgesRecord = React.useMemo(() => {
42
+ if (edges == null) {
43
+ return defaultEdges;
44
+ }
45
+
46
+ return Array.isArray(edges)
47
+ ? edges.reduce<EdgeRecord>((acc, edge: Edge) => {
48
+ acc[edge] = 'additive';
49
+ return acc;
50
+ }, {})
51
+ : // ts has trouble with refining readonly arrays.
52
+ (edges as EdgeRecord);
53
+ }, [edges]);
54
+
55
+ const appliedStyle = React.useMemo(() => {
56
+ const flatStyle = StyleSheet.flatten(style) as Record<string, number>;
57
+
58
+ if (mode === 'margin') {
59
+ const {
60
+ margin = 0,
61
+ marginVertical = margin,
62
+ marginHorizontal = margin,
63
+ marginTop = marginVertical,
64
+ marginRight = marginHorizontal,
65
+ marginBottom = marginVertical,
66
+ marginLeft = marginHorizontal,
67
+ } = flatStyle;
68
+
69
+ const marginStyle = {
70
+ marginTop: getEdgeValue(insets.top, marginTop, edgesRecord.top),
71
+ marginRight: getEdgeValue(insets.right, marginRight, edgesRecord.right),
72
+ marginBottom: getEdgeValue(
73
+ insets.bottom,
74
+ marginBottom,
75
+ edgesRecord.bottom,
76
+ ),
77
+ marginLeft: getEdgeValue(insets.left, marginLeft, edgesRecord.left),
78
+ };
79
+
80
+ return [style, marginStyle];
81
+ } else {
82
+ const {
83
+ padding = 0,
84
+ paddingVertical = padding,
85
+ paddingHorizontal = padding,
86
+ paddingTop = paddingVertical,
87
+ paddingRight = paddingHorizontal,
88
+ paddingBottom = paddingVertical,
89
+ paddingLeft = paddingHorizontal,
90
+ } = flatStyle;
91
+
92
+ const paddingStyle = {
93
+ paddingTop: getEdgeValue(insets.top, paddingTop, edgesRecord.top),
94
+ paddingRight: getEdgeValue(
95
+ insets.right,
96
+ paddingRight,
97
+ edgesRecord.right,
98
+ ),
99
+ paddingBottom: getEdgeValue(
100
+ insets.bottom,
101
+ paddingBottom,
102
+ edgesRecord.bottom,
103
+ ),
104
+ paddingLeft: getEdgeValue(insets.left, paddingLeft, edgesRecord.left),
105
+ };
106
+
107
+ return [style, paddingStyle];
108
+ }
109
+ }, [
110
+ edgesRecord.bottom,
111
+ edgesRecord.left,
112
+ edgesRecord.right,
113
+ edgesRecord.top,
114
+ insets.bottom,
115
+ insets.left,
116
+ insets.right,
117
+ insets.top,
118
+ mode,
119
+ style,
120
+ ]);
121
+
122
+ return <View style={appliedStyle} {...rest} ref={ref} />;
123
+ });
@@ -1,3 +1,4 @@
1
1
  import { View } from 'react-native';
2
+ import type { NativeSafeAreaViewInstance } from './SafeArea.types';
2
3
 
3
- export const SafeAreaView = View;
4
+ export const SafeAreaView = View as unknown as NativeSafeAreaViewInstance;
package/src/index.tsx CHANGED
@@ -1,3 +1,5 @@
1
+ 'use client';
2
+
1
3
  export * from './SafeAreaContext';
2
4
  export * from './SafeAreaView';
3
5
  export * from './InitialWindow';
@@ -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>;