@amazon-devices/expo-image 2.0.1758683737

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 (289) hide show
  1. package/.eslintrc.js +2 -0
  2. package/CHANGELOG.md +285 -0
  3. package/CMakeLists.txt +91 -0
  4. package/LICENSE +7 -0
  5. package/README.kepler.md +123 -0
  6. package/README.md +54 -0
  7. package/THIRD_PARTY_NOTICES.txt +24 -0
  8. package/android/build.gradle +124 -0
  9. package/android/proguard-rules.pro +28 -0
  10. package/android/src/main/AndroidManifest.xml +15 -0
  11. package/android/src/main/java/expo/modules/image/Exceptions.kt +9 -0
  12. package/android/src/main/java/expo/modules/image/ExpoImageAppGlideModule.kt +19 -0
  13. package/android/src/main/java/expo/modules/image/ExpoImageModule.kt +192 -0
  14. package/android/src/main/java/expo/modules/image/ExpoImageView.kt +220 -0
  15. package/android/src/main/java/expo/modules/image/ExpoImageViewWrapper.kt +652 -0
  16. package/android/src/main/java/expo/modules/image/GlideModel.kt +73 -0
  17. package/android/src/main/java/expo/modules/image/ImageUtils.kt +50 -0
  18. package/android/src/main/java/expo/modules/image/ImageViewWrapperTarget.kt +313 -0
  19. package/android/src/main/java/expo/modules/image/ResourceIdHelper.kt +47 -0
  20. package/android/src/main/java/expo/modules/image/ThumbnailRequestCoordinatorExtension.kt +21 -0
  21. package/android/src/main/java/expo/modules/image/YogaUtils.kt +20 -0
  22. package/android/src/main/java/expo/modules/image/blurhash/BlurHashFetcher.kt +32 -0
  23. package/android/src/main/java/expo/modules/image/blurhash/BlurhashDecoder.kt +189 -0
  24. package/android/src/main/java/expo/modules/image/blurhash/BlurhashModelLoader.kt +30 -0
  25. package/android/src/main/java/expo/modules/image/blurhash/BlurhashModelLoaderFactory.kt +14 -0
  26. package/android/src/main/java/expo/modules/image/blurhash/BlurhashModule.kt +17 -0
  27. package/android/src/main/java/expo/modules/image/dataurls/Base64DataFetcher.kt +27 -0
  28. package/android/src/main/java/expo/modules/image/dataurls/Base64ModelLoader.kt +24 -0
  29. package/android/src/main/java/expo/modules/image/dataurls/Base64ModelLoaderFactory.kt +12 -0
  30. package/android/src/main/java/expo/modules/image/dataurls/Base64Module.kt +16 -0
  31. package/android/src/main/java/expo/modules/image/drawing/OutlineProvider.kt +201 -0
  32. package/android/src/main/java/expo/modules/image/enums/ContentFit.kt +89 -0
  33. package/android/src/main/java/expo/modules/image/enums/ImageCacheType.kt +14 -0
  34. package/android/src/main/java/expo/modules/image/enums/Priority.kt +15 -0
  35. package/android/src/main/java/expo/modules/image/events/GlideRequestListener.kt +64 -0
  36. package/android/src/main/java/expo/modules/image/events/OkHttpProgressListener.kt +26 -0
  37. package/android/src/main/java/expo/modules/image/okhttp/ExpoImageOkHttpClientGlideModule.kt +80 -0
  38. package/android/src/main/java/expo/modules/image/okhttp/GlideUrlWrapperLoader.kt +55 -0
  39. package/android/src/main/java/expo/modules/image/records/CachePolicy.kt +10 -0
  40. package/android/src/main/java/expo/modules/image/records/ContentPosition.kt +89 -0
  41. package/android/src/main/java/expo/modules/image/records/ImageTransition.kt +8 -0
  42. package/android/src/main/java/expo/modules/image/records/SourceMap.kt +160 -0
  43. package/android/src/main/java/expo/modules/image/records/events.kt +25 -0
  44. package/android/src/main/java/expo/modules/image/svg/SVGDecoder.kt +33 -0
  45. package/android/src/main/java/expo/modules/image/svg/SVGDrawableTranscoder.kt +37 -0
  46. package/android/src/main/java/expo/modules/image/svg/SVGModule.kt +26 -0
  47. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashDecoder.kt +365 -0
  48. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashFetcher.kt +31 -0
  49. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashModelLoader.kt +26 -0
  50. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashModelLoaderFactory.kt +14 -0
  51. package/android/src/main/java/expo/modules/image/thumbhash/ThumbhashModule.kt +17 -0
  52. package/build/ExpoImage.d.ts +15 -0
  53. package/build/ExpoImage.d.ts.map +1 -0
  54. package/build/ExpoImage.js +75 -0
  55. package/build/ExpoImage.js.map +1 -0
  56. package/build/ExpoImage.kepler.d.ts +23 -0
  57. package/build/ExpoImage.kepler.d.ts.map +1 -0
  58. package/build/ExpoImage.kepler.js +71 -0
  59. package/build/ExpoImage.kepler.js.map +1 -0
  60. package/build/ExpoImage.web.d.ts +9 -0
  61. package/build/ExpoImage.web.d.ts.map +1 -0
  62. package/build/ExpoImage.web.js +90 -0
  63. package/build/ExpoImage.web.js.map +1 -0
  64. package/build/ExpoImageModules.d.ts +3 -0
  65. package/build/ExpoImageModules.d.ts.map +1 -0
  66. package/build/ExpoImageModules.js +4 -0
  67. package/build/ExpoImageModules.js.map +1 -0
  68. package/build/ExpoImageModules.kepler.d.ts +6 -0
  69. package/build/ExpoImageModules.kepler.d.ts.map +1 -0
  70. package/build/ExpoImageModules.kepler.js +5 -0
  71. package/build/ExpoImageModules.kepler.js.map +1 -0
  72. package/build/Image.d.ts +30 -0
  73. package/build/Image.d.ts.map +1 -0
  74. package/build/Image.js +49 -0
  75. package/build/Image.js.map +1 -0
  76. package/build/Image.types.d.ts +374 -0
  77. package/build/Image.types.d.ts.map +1 -0
  78. package/build/Image.types.js +2 -0
  79. package/build/Image.types.js.map +1 -0
  80. package/build/ImageBackground.d.ts +6 -0
  81. package/build/ImageBackground.d.ts.map +1 -0
  82. package/build/ImageBackground.js +13 -0
  83. package/build/ImageBackground.js.map +1 -0
  84. package/build/ImageViewNativeComponent.d.ts +20 -0
  85. package/build/ImageViewNativeComponent.d.ts.map +1 -0
  86. package/build/ImageViewNativeComponent.js +42 -0
  87. package/build/ImageViewNativeComponent.js.map +1 -0
  88. package/build/NativeImageLoaderKepler.d.ts +18 -0
  89. package/build/NativeImageLoaderKepler.d.ts.map +1 -0
  90. package/build/NativeImageLoaderKepler.js +3 -0
  91. package/build/NativeImageLoaderKepler.js.map +1 -0
  92. package/build/fabric/registerNativeComponent.d.ts +15 -0
  93. package/build/fabric/registerNativeComponent.d.ts.map +1 -0
  94. package/build/fabric/registerNativeComponent.js +47 -0
  95. package/build/fabric/registerNativeComponent.js.map +1 -0
  96. package/build/index.d.ts +4 -0
  97. package/build/index.d.ts.map +1 -0
  98. package/build/index.js +4 -0
  99. package/build/index.js.map +1 -0
  100. package/build/utils/AssetSourceResolver.web.d.ts +24 -0
  101. package/build/utils/AssetSourceResolver.web.d.ts.map +1 -0
  102. package/build/utils/AssetSourceResolver.web.js +67 -0
  103. package/build/utils/AssetSourceResolver.web.js.map +1 -0
  104. package/build/utils/blurhash/base83.d.ts +3 -0
  105. package/build/utils/blurhash/base83.d.ts.map +1 -0
  106. package/build/utils/blurhash/base83.js +103 -0
  107. package/build/utils/blurhash/base83.js.map +1 -0
  108. package/build/utils/blurhash/decode.d.ts +7 -0
  109. package/build/utils/blurhash/decode.d.ts.map +1 -0
  110. package/build/utils/blurhash/decode.js +92 -0
  111. package/build/utils/blurhash/decode.js.map +1 -0
  112. package/build/utils/blurhash/error.d.ts +4 -0
  113. package/build/utils/blurhash/error.d.ts.map +1 -0
  114. package/build/utils/blurhash/error.js +8 -0
  115. package/build/utils/blurhash/error.js.map +1 -0
  116. package/build/utils/blurhash/useBlurhash.d.ts +8 -0
  117. package/build/utils/blurhash/useBlurhash.d.ts.map +1 -0
  118. package/build/utils/blurhash/useBlurhash.js +64 -0
  119. package/build/utils/blurhash/useBlurhash.js.map +1 -0
  120. package/build/utils/blurhash/utils.d.ts +5 -0
  121. package/build/utils/blurhash/utils.d.ts.map +1 -0
  122. package/build/utils/blurhash/utils.js +21 -0
  123. package/build/utils/blurhash/utils.js.map +1 -0
  124. package/build/utils/resolveAssetSource.d.ts +3 -0
  125. package/build/utils/resolveAssetSource.d.ts.map +1 -0
  126. package/build/utils/resolveAssetSource.js +3 -0
  127. package/build/utils/resolveAssetSource.js.map +1 -0
  128. package/build/utils/resolveAssetSource.web.d.ts +9 -0
  129. package/build/utils/resolveAssetSource.web.d.ts.map +1 -0
  130. package/build/utils/resolveAssetSource.web.js +31 -0
  131. package/build/utils/resolveAssetSource.web.js.map +1 -0
  132. package/build/utils/resolveHashString.d.ts +14 -0
  133. package/build/utils/resolveHashString.d.ts.map +1 -0
  134. package/build/utils/resolveHashString.js +31 -0
  135. package/build/utils/resolveHashString.js.map +1 -0
  136. package/build/utils/resolveHashString.web.d.ts +20 -0
  137. package/build/utils/resolveHashString.web.d.ts.map +1 -0
  138. package/build/utils/resolveHashString.web.js +31 -0
  139. package/build/utils/resolveHashString.web.js.map +1 -0
  140. package/build/utils/resolveSources.d.ts +8 -0
  141. package/build/utils/resolveSources.d.ts.map +1 -0
  142. package/build/utils/resolveSources.js +45 -0
  143. package/build/utils/resolveSources.js.map +1 -0
  144. package/build/utils/thumbhash/thumbhash.d.ts +66 -0
  145. package/build/utils/thumbhash/thumbhash.d.ts.map +1 -0
  146. package/build/utils/thumbhash/thumbhash.js +338 -0
  147. package/build/utils/thumbhash/thumbhash.js.map +1 -0
  148. package/build/utils.d.ts +17 -0
  149. package/build/utils.d.ts.map +1 -0
  150. package/build/utils.js +85 -0
  151. package/build/utils.js.map +1 -0
  152. package/build/web/AnimationManager.d.ts +37 -0
  153. package/build/web/AnimationManager.d.ts.map +1 -0
  154. package/build/web/AnimationManager.js +155 -0
  155. package/build/web/AnimationManager.js.map +1 -0
  156. package/build/web/ColorTintFilter.d.ts +12 -0
  157. package/build/web/ColorTintFilter.d.ts.map +1 -0
  158. package/build/web/ColorTintFilter.js +30 -0
  159. package/build/web/ColorTintFilter.js.map +1 -0
  160. package/build/web/ImageWrapper.d.ts +5 -0
  161. package/build/web/ImageWrapper.d.ts.map +1 -0
  162. package/build/web/ImageWrapper.js +44 -0
  163. package/build/web/ImageWrapper.js.map +1 -0
  164. package/build/web/ImageWrapper.types.d.ts +29 -0
  165. package/build/web/ImageWrapper.types.d.ts.map +1 -0
  166. package/build/web/ImageWrapper.types.js +2 -0
  167. package/build/web/ImageWrapper.types.js.map +1 -0
  168. package/build/web/getImageWrapperEventHandler.d.ts +8 -0
  169. package/build/web/getImageWrapperEventHandler.d.ts.map +1 -0
  170. package/build/web/getImageWrapperEventHandler.js +25 -0
  171. package/build/web/getImageWrapperEventHandler.js.map +1 -0
  172. package/build/web/hooks.d.ts +13 -0
  173. package/build/web/hooks.d.ts.map +1 -0
  174. package/build/web/hooks.js +58 -0
  175. package/build/web/hooks.js.map +1 -0
  176. package/build/web/imageStyles.d.ts +2 -0
  177. package/build/web/imageStyles.d.ts.map +1 -0
  178. package/build/web/imageStyles.js +137 -0
  179. package/build/web/imageStyles.js.map +1 -0
  180. package/build/web/positioning.d.ts +11 -0
  181. package/build/web/positioning.d.ts.map +1 -0
  182. package/build/web/positioning.js +35 -0
  183. package/build/web/positioning.js.map +1 -0
  184. package/build/web/useSourceSelection.d.ts +14 -0
  185. package/build/web/useSourceSelection.d.ts.map +1 -0
  186. package/build/web/useSourceSelection.js +98 -0
  187. package/build/web/useSourceSelection.js.map +1 -0
  188. package/expo-module.config.json +9 -0
  189. package/ios/Blurhash.swift +326 -0
  190. package/ios/BlurhashLoader.swift +52 -0
  191. package/ios/ContentFit.swift +55 -0
  192. package/ios/ContentPosition.swift +108 -0
  193. package/ios/ExpoImage.podspec +30 -0
  194. package/ios/ImageCachePolicy.swift +24 -0
  195. package/ios/ImageCacheType.swift +21 -0
  196. package/ios/ImageModule.swift +127 -0
  197. package/ios/ImagePriority.swift +22 -0
  198. package/ios/ImageSource.swift +44 -0
  199. package/ios/ImageTransition.swift +67 -0
  200. package/ios/ImageUtils.swift +227 -0
  201. package/ios/ImageView.swift +469 -0
  202. package/ios/Loaders/PhotoLibraryAssetLoader.swift +146 -0
  203. package/ios/Thumbhash.swift +550 -0
  204. package/ios/ThumbhashLoader.swift +53 -0
  205. package/jest.config.js +3 -0
  206. package/kepler/ExpoImage.cpp +63 -0
  207. package/kepler/ExpoImage.h +29 -0
  208. package/kepler/ExpoImageCacheHandler.cpp +15 -0
  209. package/kepler/ExpoImageCacheHandler.h +28 -0
  210. package/kepler/ExpoImageComponent.cpp +579 -0
  211. package/kepler/ExpoImageComponent.h +199 -0
  212. package/kepler/ImageWrapper/BlurhashImageWrapper.cpp +37 -0
  213. package/kepler/ImageWrapper/BlurhashImageWrapper.h +23 -0
  214. package/kepler/ImageWrapper/CacheImageWrapper.cpp +80 -0
  215. package/kepler/ImageWrapper/CacheImageWrapper.h +41 -0
  216. package/kepler/ImageWrapper/HashImageWrapper.cpp +21 -0
  217. package/kepler/ImageWrapper/HashImageWrapper.h +22 -0
  218. package/kepler/ImageWrapper/ImageWrapper.cpp +24 -0
  219. package/kepler/ImageWrapper/ImageWrapper.h +33 -0
  220. package/kepler/ImageWrapper/LocalImageWrapper.cpp +61 -0
  221. package/kepler/ImageWrapper/LocalImageWrapper.h +30 -0
  222. package/kepler/ImageWrapper/ThumbhashImageWrapper.cpp +33 -0
  223. package/kepler/ImageWrapper/ThumbhashImageWrapper.h +20 -0
  224. package/kepler/KeplerImageViewComponent.h +64 -0
  225. package/kepler/KeplerInit.cpp +35 -0
  226. package/kepler/blurhash/common.h +32 -0
  227. package/kepler/blurhash/decode.cpp +187 -0
  228. package/kepler/blurhash/decode.h +58 -0
  229. package/kepler/contentposition/ImageContentPosition.cpp +86 -0
  230. package/kepler/contentposition/ImageContentPosition.h +48 -0
  231. package/kepler/enums/ImageContentFit.h +17 -0
  232. package/kepler/enums/ImagePriority.h +17 -0
  233. package/kepler/managers/ExpoImageManager.cpp +38 -0
  234. package/kepler/managers/ExpoImageManager.h +23 -0
  235. package/kepler/test/ExpoImage.cpp +73 -0
  236. package/kepler/test/ExpoImageComponent.cpp +290 -0
  237. package/kepler/test/blurhash/decode.cpp +39 -0
  238. package/kepler/test/contentposition/ImageContentPosition.cpp +100 -0
  239. package/kepler/test/managers/ExpoImageManager.cpp +39 -0
  240. package/kepler/test/thumbhash/decode.cpp +18 -0
  241. package/kepler/test/utils/LoaderDelegate.cpp +130 -0
  242. package/kepler/thumbhash/decode.cpp +166 -0
  243. package/kepler/thumbhash/decode.h +22 -0
  244. package/kepler/utils/PlaceholderImageLoaderDelegate.cpp +26 -0
  245. package/kepler/utils/PlaceholderImageLoaderDelegate.h +26 -0
  246. package/kepler/utils/PrefetchImageLoaderDelegate.cpp +55 -0
  247. package/kepler/utils/PrefetchImageLoaderDelegate.h +35 -0
  248. package/kepler/utils/SourceImageLoaderDelegate.cpp +47 -0
  249. package/kepler/utils/SourceImageLoaderDelegate.h +42 -0
  250. package/kepler/utils/SourceImageLoaderDelegateEmitter.cpp +15 -0
  251. package/kepler/utils/SourceImageLoaderDelegateEmitter.h +28 -0
  252. package/kepler-compatibility.json +8 -0
  253. package/package.json +1 -0
  254. package/react-native.config.js +22 -0
  255. package/src/ExpoImage.kepler.tsx +119 -0
  256. package/src/ExpoImage.tsx +119 -0
  257. package/src/ExpoImage.web.tsx +155 -0
  258. package/src/ExpoImageModules.kepler.ts +7 -0
  259. package/src/ExpoImageModules.ts +4 -0
  260. package/src/Image.tsx +82 -0
  261. package/src/Image.types.ts +450 -0
  262. package/src/ImageBackground.tsx +18 -0
  263. package/src/ImageViewNativeComponent.ts +59 -0
  264. package/src/NativeImageLoaderKepler.ts +22 -0
  265. package/src/fabric/registerNativeComponent.ts +70 -0
  266. package/src/index.ts +3 -0
  267. package/src/utils/AssetSourceResolver.web.ts +88 -0
  268. package/src/utils/blurhash/base83.ts +104 -0
  269. package/src/utils/blurhash/decode.ts +114 -0
  270. package/src/utils/blurhash/error.ts +7 -0
  271. package/src/utils/blurhash/useBlurhash.tsx +81 -0
  272. package/src/utils/blurhash/utils.ts +21 -0
  273. package/src/utils/resolveAssetSource.tsx +3 -0
  274. package/src/utils/resolveAssetSource.web.ts +40 -0
  275. package/src/utils/resolveHashString.tsx +36 -0
  276. package/src/utils/resolveHashString.web.tsx +33 -0
  277. package/src/utils/resolveSources.tsx +48 -0
  278. package/src/utils/thumbhash/thumbhash.ts +387 -0
  279. package/src/utils.ts +109 -0
  280. package/src/web/AnimationManager.tsx +224 -0
  281. package/src/web/ColorTintFilter.tsx +36 -0
  282. package/src/web/ImageWrapper.tsx +85 -0
  283. package/src/web/ImageWrapper.types.ts +36 -0
  284. package/src/web/getImageWrapperEventHandler.ts +29 -0
  285. package/src/web/hooks.ts +72 -0
  286. package/src/web/imageStyles.tsx +136 -0
  287. package/src/web/positioning.ts +48 -0
  288. package/src/web/useSourceSelection.ts +151 -0
  289. package/tsconfig.json +9 -0
@@ -0,0 +1,224 @@
1
+ import React from 'react';
2
+
3
+ import { ImageTransition } from '../Image.types';
4
+
5
+ type Callbacks = {
6
+ onReady?: (() => void) | null;
7
+ onAnimationFinished?: (() => void) | null;
8
+ onMount?: (() => void) | null;
9
+ onError?: (() => void) | null;
10
+ };
11
+
12
+ export type AnimationManagerNode = [
13
+ key: string,
14
+ renderFunction: (
15
+ renderProps: NonNullable<Callbacks>
16
+ ) => (className: string, style: React.CSSProperties) => React.ReactElement,
17
+ ];
18
+
19
+ const SUPPORTED_ANIMATIONS: ImageTransition['effect'][] = [
20
+ 'cross-dissolve',
21
+ 'flip-from-left',
22
+ 'flip-from-right',
23
+ 'flip-from-top',
24
+ 'flip-from-bottom',
25
+ ];
26
+
27
+ type NodeStatus = 'mounted' | 'in' | 'active' | 'out' | 'errored';
28
+
29
+ function useAnimationManagerNode(node: AnimationManagerNode | null, initialStatus?: NodeStatus) {
30
+ const newNode = React.useMemo(() => {
31
+ if (!node) {
32
+ return null;
33
+ }
34
+ const [animationKey, renderFunction] = node;
35
+ // key, ReactElement, ref, callbacks
36
+ return {
37
+ animationKey,
38
+ persistedElement: renderFunction,
39
+ status: (initialStatus || 'mounted') as NodeStatus,
40
+ };
41
+ }, [node?.[0]]);
42
+ return newNode;
43
+ }
44
+
45
+ function validateTimingFunctionForAnimation(
46
+ animationClass: ImageTransition['effect'],
47
+ timingFunction: ImageTransition['timing']
48
+ ) {
49
+ if (animationClass?.includes('flip')) {
50
+ if (timingFunction?.includes('ease')) {
51
+ return 'ease-in-out';
52
+ }
53
+ return 'linear';
54
+ }
55
+ return timingFunction || null;
56
+ }
57
+
58
+ function validateAnimationClass(effect: ImageTransition['effect']) {
59
+ if (SUPPORTED_ANIMATIONS.includes(effect)) {
60
+ return effect;
61
+ }
62
+ return 'cross-dissolve';
63
+ }
64
+
65
+ export function getAnimatorFromTransition(transition: ImageTransition | null | undefined) {
66
+ if (!transition?.duration) {
67
+ return null;
68
+ }
69
+ const animationClass = validateAnimationClass(transition.effect);
70
+ if (!animationClass) {
71
+ return {
72
+ startingClass: '',
73
+ animateInClass: '',
74
+ animateOutClass: '',
75
+ containerClass: '',
76
+ timingFunction: 'linear',
77
+ animationClass: '',
78
+ duration: 0,
79
+ };
80
+ }
81
+
82
+ const timingFunction = validateTimingFunctionForAnimation(animationClass, transition.timing);
83
+ const timingClass = `image-timing-${timingFunction}`;
84
+
85
+ return {
86
+ startingClass: `${animationClass}-start`,
87
+ animateInClass: [animationClass, 'transitioning', `${animationClass}-active`, timingClass].join(
88
+ ' '
89
+ ),
90
+ animateOutClass: [animationClass, `${animationClass}-end`, timingClass].join(' '),
91
+ containerClass: `${animationClass}-container`,
92
+ timingFunction,
93
+ animationClass,
94
+ duration: transition?.duration || 0,
95
+ };
96
+ }
97
+
98
+ type MountedAnimationNode = {
99
+ animationKey: string;
100
+ persistedElement: (
101
+ renderProps: Callbacks
102
+ ) => (className: string, style: React.CSSProperties) => React.ReactElement;
103
+ status: NodeStatus;
104
+ };
105
+
106
+ export default function AnimationManager({
107
+ children: renderFunction,
108
+ initial,
109
+ transition,
110
+ recyclingKey,
111
+ }: {
112
+ children: AnimationManagerNode;
113
+ initial: AnimationManagerNode | null;
114
+ transition: ImageTransition | null | undefined;
115
+ recyclingKey?: string | null | undefined;
116
+ }) {
117
+ const animation = getAnimatorFromTransition(transition);
118
+
119
+ const initialNode = useAnimationManagerNode(initial, 'active');
120
+
121
+ const [nodes, setNodes] = React.useState<MountedAnimationNode[]>(
122
+ initialNode ? [initialNode] : []
123
+ );
124
+
125
+ const [prevRecyclingKey, setPrevRecyclingKey] = React.useState<string>(recyclingKey ?? '');
126
+ if (prevRecyclingKey !== (recyclingKey ?? '')) {
127
+ setPrevRecyclingKey(recyclingKey ?? '');
128
+ setNodes(initialNode ? [initialNode] : []);
129
+ }
130
+
131
+ const removeAllNodesOfKeyExceptShowing = (key?: string) => {
132
+ setNodes((n) =>
133
+ n.filter(
134
+ (node) =>
135
+ (key ? node.animationKey !== key : false) ||
136
+ node.status === 'in' ||
137
+ node.status === 'active'
138
+ )
139
+ );
140
+ };
141
+
142
+ const newNode = useAnimationManagerNode(renderFunction);
143
+
144
+ React.useEffect(() => {
145
+ setNodes((n) => {
146
+ if (!newNode) {
147
+ return n;
148
+ }
149
+ const existingNodeIndex = n.findIndex((node) => node.animationKey === newNode.animationKey);
150
+ if (existingNodeIndex >= 0) {
151
+ if (animation) {
152
+ return n.map((n2) =>
153
+ n2.animationKey === newNode.animationKey
154
+ ? { ...newNode, status: 'in' }
155
+ : { ...n2, status: 'out' }
156
+ );
157
+ } else {
158
+ return [{ ...newNode, status: 'in' }];
159
+ }
160
+ }
161
+ return [...n, newNode];
162
+ });
163
+ }, [newNode]);
164
+
165
+ function wrapNodeWithCallbacks(node: MountedAnimationNode) {
166
+ if (renderFunction[0] === node.animationKey) {
167
+ return renderFunction[1]({
168
+ onReady: () => {
169
+ if (animation) {
170
+ setNodes((nodes) =>
171
+ nodes.map((n) => (n === newNode ? { ...n, status: 'in' } : { ...n, status: 'out' }))
172
+ );
173
+ } else {
174
+ setNodes([{ ...node, status: 'in' }]);
175
+ }
176
+ },
177
+ onAnimationFinished: () => {
178
+ setNodes([{ ...node, status: 'in' }]);
179
+ },
180
+ onError: () => {
181
+ setNodes((nodes) => nodes.map((n) => (n === node ? { ...n, status: 'errored' } : n)));
182
+ },
183
+ });
184
+ }
185
+ if (initial?.[0] === node.animationKey) {
186
+ return initial[1]({
187
+ onAnimationFinished: () => {
188
+ if (node.status === 'out') {
189
+ removeAllNodesOfKeyExceptShowing(node.animationKey);
190
+ }
191
+ },
192
+ onError: () => {
193
+ setNodes((nodes) => nodes.map((n) => (n === node ? { ...n, status: 'errored' } : n)));
194
+ },
195
+ });
196
+ }
197
+ return node.persistedElement({
198
+ onAnimationFinished: () => {
199
+ removeAllNodesOfKeyExceptShowing(node.animationKey);
200
+ },
201
+ });
202
+ }
203
+ const styles = {
204
+ transitionDuration: `${animation?.duration || 0}ms`,
205
+ transitionTimingFunction: animation?.timingFunction || 'linear',
206
+ };
207
+ const classes = {
208
+ in: animation?.animateInClass,
209
+ out: animation?.animateOutClass,
210
+ mounted: animation?.startingClass,
211
+ };
212
+
213
+ return (
214
+ <>
215
+ {[...nodes]
216
+ .filter((n) => n.status !== 'errored')
217
+ .map((n) => (
218
+ <div className={animation?.containerClass} key={n.animationKey}>
219
+ {wrapNodeWithCallbacks(n)(classes[n.status], styles)}
220
+ </div>
221
+ ))}
222
+ </>
223
+ );
224
+ }
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+ import { StyleSheet } from 'react-native';
3
+
4
+ export function getTintColorStyle(tintColor?: string | null) {
5
+ if (!tintColor) {
6
+ return {};
7
+ }
8
+ return {
9
+ filter: `url(#expo-image-tint-${tintColor})`,
10
+ };
11
+ }
12
+
13
+ type TintColorFilterProps = { tintColor?: string | null };
14
+
15
+ export default function TintColorFilter({ tintColor }: TintColorFilterProps) {
16
+ if (!tintColor) {
17
+ return null;
18
+ }
19
+ return (
20
+ <svg style={styles.svg}>
21
+ <defs>
22
+ <filter id={`expo-image-tint-${tintColor}`} x="0" y="0" width="0" height="0">
23
+ <feFlood floodColor={tintColor} floodOpacity="1" result="flood" />
24
+ <feComposite in="flood" in2="SourceAlpha" operator="in" />
25
+ </filter>
26
+ </defs>
27
+ </svg>
28
+ );
29
+ }
30
+
31
+ const styles = StyleSheet.create({
32
+ svg: {
33
+ width: 0,
34
+ height: 0,
35
+ },
36
+ });
@@ -0,0 +1,85 @@
1
+ import React, { useEffect, Ref } from 'react';
2
+
3
+ import ColorTintFilter, { getTintColorStyle } from './ColorTintFilter';
4
+ import { ImageWrapperProps } from './ImageWrapper.types';
5
+ import { getImageWrapperEventHandler } from './getImageWrapperEventHandler';
6
+ import { useHeaders, useImageHashes } from './hooks';
7
+ import { absoluteFilledPosition, getObjectPositionFromContentPositionObject } from './positioning';
8
+ import { SrcSetSource } from './useSourceSelection';
9
+ import { ImageNativeProps, ImageSource } from '../Image.types';
10
+
11
+ function getFetchPriorityFromImagePriority(priority: ImageNativeProps['priority'] = 'normal') {
12
+ return priority && ['low', 'high'].includes(priority) ? priority : 'auto';
13
+ }
14
+
15
+ function getImgPropsFromSource(source: ImageSource | SrcSetSource | null | undefined) {
16
+ if (source && 'srcset' in source) {
17
+ return {
18
+ srcSet: source.srcset,
19
+ sizes: source.sizes,
20
+ };
21
+ }
22
+ return {};
23
+ }
24
+
25
+ const ImageWrapper = React.forwardRef(
26
+ (
27
+ {
28
+ source,
29
+ events,
30
+ contentPosition,
31
+ hashPlaceholderContentPosition,
32
+ priority,
33
+ style,
34
+ hashPlaceholderStyle,
35
+ tintColor,
36
+ className,
37
+ accessibilityLabel,
38
+ cachePolicy,
39
+ ...props
40
+ }: ImageWrapperProps,
41
+ ref: Ref<HTMLImageElement>
42
+ ) => {
43
+ useEffect(() => {
44
+ events?.onMount?.forEach((e) => e?.());
45
+ }, []);
46
+
47
+ // Thumbhash uri always has to start with 'thumbhash:/'
48
+ const { resolvedSource, isImageHash } = useImageHashes(source);
49
+ const objectPosition = getObjectPositionFromContentPositionObject(
50
+ isImageHash ? hashPlaceholderContentPosition : contentPosition
51
+ );
52
+
53
+ const sourceWithHeaders = useHeaders(resolvedSource, cachePolicy, events?.onError);
54
+ if (!sourceWithHeaders) {
55
+ return null;
56
+ }
57
+ return (
58
+ <>
59
+ <ColorTintFilter tintColor={tintColor} />
60
+ <img
61
+ ref={ref}
62
+ alt={accessibilityLabel}
63
+ className={className}
64
+ src={sourceWithHeaders?.uri || undefined}
65
+ key={source?.uri}
66
+ style={{
67
+ objectPosition,
68
+ ...absoluteFilledPosition,
69
+ ...getTintColorStyle(tintColor),
70
+ ...style,
71
+ ...(isImageHash ? hashPlaceholderStyle : {}),
72
+ }}
73
+ // @ts-ignore
74
+ // eslint-disable-next-line react/no-unknown-property
75
+ fetchpriority={getFetchPriorityFromImagePriority(priority || 'normal')}
76
+ {...getImageWrapperEventHandler(events, sourceWithHeaders)}
77
+ {...getImgPropsFromSource(source)}
78
+ {...props}
79
+ />
80
+ </>
81
+ );
82
+ }
83
+ );
84
+
85
+ export default ImageWrapper;
@@ -0,0 +1,36 @@
1
+ import { CSSProperties, SyntheticEvent } from 'react';
2
+
3
+ import { SrcSetSource } from './useSourceSelection';
4
+ import { ImageContentPositionObject, ImageProps, ImageSource } from '../Image.types';
5
+
6
+ export type OnErrorEvent =
7
+ | (({ source }: { source: ImageSource | null }) => void)
8
+ | undefined
9
+ | null;
10
+ export type OnLoadEvent =
11
+ | ((event: SyntheticEvent<HTMLImageElement, Event>) => void)
12
+ | undefined
13
+ | null;
14
+ export type OnTransitionEndEvent = (() => void) | undefined | null;
15
+ export type OnMountEvent = (() => void) | undefined | null;
16
+
17
+ export type ImageWrapperEvents = {
18
+ onLoad?: OnLoadEvent[];
19
+ onError?: OnErrorEvent[];
20
+ onTransitionEnd?: OnTransitionEndEvent[];
21
+ onMount?: OnMountEvent[];
22
+ };
23
+
24
+ export type ImageWrapperProps = {
25
+ source?: ImageSource | SrcSetSource | null;
26
+ events?: ImageWrapperEvents;
27
+ contentPosition?: ImageContentPositionObject;
28
+ hashPlaceholderContentPosition?: ImageContentPositionObject;
29
+ priority?: string | null;
30
+ style: CSSProperties;
31
+ tintColor?: string | null;
32
+ hashPlaceholderStyle?: CSSProperties;
33
+ className?: string;
34
+ accessibilityLabel?: string;
35
+ cachePolicy?: ImageProps['cachePolicy'];
36
+ };
@@ -0,0 +1,29 @@
1
+ import { ImageWrapperEvents } from './ImageWrapper.types';
2
+ import { ImageSource } from '../Image.types';
3
+ import { isBlurhashString } from '../utils/resolveSources';
4
+
5
+ export function getImageWrapperEventHandler(
6
+ events: ImageWrapperEvents | undefined,
7
+ source: ImageSource
8
+ ) {
9
+ return {
10
+ onLoad: (event) => {
11
+ if (typeof window !== 'undefined') {
12
+ // this ensures the animation will run, since the starting class is applied at least 1 frame before the target class set in the onLoad event callback
13
+ window.requestAnimationFrame(() => {
14
+ events?.onLoad?.forEach((e) => e?.(event));
15
+ });
16
+ } else {
17
+ events?.onLoad?.forEach((e) => e?.(event));
18
+ }
19
+ },
20
+ onTransitionEnd: () => events?.onTransitionEnd?.forEach((e) => e?.()),
21
+ onError: () => {
22
+ // A temporary workaround for blurhash blobs throwing opaque errors when used in an img tag.
23
+ if (source?.uri && isBlurhashString(source?.uri)) {
24
+ return;
25
+ }
26
+ events?.onError?.forEach((e) => e?.({ source: source || null }));
27
+ },
28
+ };
29
+ }
@@ -0,0 +1,72 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+
3
+ import { OnErrorEvent } from './ImageWrapper.types';
4
+ import { ImageNativeProps, ImageSource } from '../Image.types';
5
+ import { useBlurhash } from '../utils/blurhash/useBlurhash';
6
+ import { isThumbhashString } from '../utils/resolveSources';
7
+ import { thumbHashStringToDataURL } from '../utils/thumbhash/thumbhash';
8
+
9
+ export function useThumbhash(source: ImageSource | null | undefined) {
10
+ const isThumbhash = isThumbhashString(source?.uri || '');
11
+ const strippedThumbhashString = source?.uri?.replace(/thumbhash:\//, '') ?? '';
12
+ const thumbhashSource = useMemo(
13
+ () => (isThumbhash ? { uri: thumbHashStringToDataURL(strippedThumbhashString) } : null),
14
+ [strippedThumbhashString, isThumbhash]
15
+ );
16
+ return thumbhashSource;
17
+ }
18
+
19
+ export function useImageHashes(source: ImageSource | null | undefined) {
20
+ const thumbhash = useThumbhash(source);
21
+ const blurhash = useBlurhash(source);
22
+ return useMemo(
23
+ () => ({
24
+ resolvedSource: blurhash ?? thumbhash ?? source,
25
+ isImageHash: !!blurhash || !!thumbhash,
26
+ }),
27
+ [blurhash, thumbhash]
28
+ );
29
+ }
30
+
31
+ export function useHeaders(
32
+ source: ImageSource | null | undefined,
33
+ cachePolicy: ImageNativeProps['cachePolicy'],
34
+ onError?: OnErrorEvent[]
35
+ ): ImageSource | null | undefined {
36
+ const [objectURL, setObjectURL] = useState<string | null>(null);
37
+ useEffect(() => {
38
+ (async () => {
39
+ if (!source?.headers || !source.uri) {
40
+ return;
41
+ }
42
+ try {
43
+ const result = await fetch(source.uri, {
44
+ headers: source.headers,
45
+ cache: cachePolicy === 'none' ? 'no-cache' : 'default',
46
+ redirect: 'follow',
47
+ });
48
+ if (!result.ok) {
49
+ throw new Error(`Failed to fetch image: ${result.status} ${result.statusText}`);
50
+ }
51
+ const blob = await result.blob();
52
+ setObjectURL((prevObjURL) => {
53
+ if (prevObjURL) {
54
+ URL.revokeObjectURL(prevObjURL);
55
+ }
56
+ return URL.createObjectURL(blob);
57
+ });
58
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
59
+ } catch (error) {
60
+ onError?.forEach((e) => e?.({ source }));
61
+ }
62
+ })();
63
+ }, [source]);
64
+ if (!source?.headers) {
65
+ return source;
66
+ }
67
+ if (!objectURL) {
68
+ // Avoid fetching a URL without headers if we have headers
69
+ return null;
70
+ }
71
+ return { ...source, uri: objectURL };
72
+ }
@@ -0,0 +1,136 @@
1
+ const style = `
2
+ [data-expoimage] .cross-dissolve {
3
+ transition-property: opacity;
4
+ animation-fill-mode: forwards;
5
+ }
6
+ [data-expoimage] .cross-dissolve-start:not(.transitioning) {
7
+ opacity: 0;
8
+ }
9
+ [data-expoimage] .cross-dissolve-active {
10
+ opacity: 1;
11
+ }
12
+ [data-expoimage] .cross-dissolve-end {
13
+ opacity: 0;
14
+ }
15
+ [data-expoimage] .flip-from-left {
16
+ transition-property: transform, opacity;
17
+ transition-timing-function: var(--expo-image-timing,linear), steps(2, jump-none) !important;
18
+ transform-origin: center;
19
+
20
+ }
21
+ [data-expoimage] .flip-from-left-container {
22
+ width: 100%;
23
+ height: 100%;
24
+ position: absolute;
25
+ left: 0;
26
+ top: 0;
27
+ perspective: 1000px;
28
+ }
29
+ [data-expoimage] .flip-from-left-start:not(.transitioning) {
30
+ transform: translateZ(calc(var(--expo-image-width,1000px) * -1.25)) rotateY(-180deg);
31
+ opacity: 0;
32
+ }
33
+ [data-expoimage] .flip-from-left-active {
34
+ transform: translateZ(0px) rotateY(0) ;
35
+ opacity:1;
36
+ }
37
+ [data-expoimage] .flip-from-left-end {
38
+ transform: translateZ(calc(var(--expo-image-width,1000px) * -1.25)) rotateY(180deg);
39
+ opacity: 0;
40
+ }
41
+ [data-expoimage] .flip-from-right {
42
+ transition-property: transform, opacity;
43
+ transition-timing-function: var(--expo-image-timing,linear), steps(2, jump-none) !important;
44
+ transform-origin: center;
45
+ }
46
+ [data-expoimage] .flip-from-right-container {
47
+ width: 100%;
48
+ height: 100%;
49
+ position: absolute;
50
+ left: 0;
51
+ top: 0;
52
+ perspective: 1000px;
53
+ }
54
+ [data-expoimage] .flip-from-right-start:not(.transitioning) {
55
+ transform: translateZ(calc(var(--expo-image-width,1000px) * -1.25)) rotateY(180deg);
56
+ opacity: 0;
57
+ }
58
+ [data-expoimage] .flip-from-right-active {
59
+ transform: translateZ(0px) rotateY(0) ;
60
+ opacity:1;
61
+ }
62
+ [data-expoimage] .flip-from-right-end {
63
+ transform: translateZ(calc(var(--expo-image-width,1000px) * -1.25)) rotateY(-180deg);
64
+ opacity: 0;
65
+ }
66
+ [data-expoimage] .flip-from-top {
67
+ transition-property: transform, opacity;
68
+ transition-timing-function: var(--expo-image-timing,linear), steps(2, jump-none) !important;
69
+ transform-origin: center;
70
+ }
71
+ [data-expoimage] .flip-from-top-container {
72
+ width: 100%;
73
+ height: 100%;
74
+ position: absolute;
75
+ left: 0;
76
+ top: 0;
77
+ perspective: 1000px;
78
+ }
79
+ [data-expoimage] .flip-from-top-start:not(.transitioning) {
80
+ transform: translateZ(calc(var(--expo-image-height,1000px) * -1.5)) rotateX(180deg);
81
+ opacity: 0;
82
+ }
83
+ [data-expoimage] .flip-from-top-active {
84
+ transform: translateZ(0px) rotateX(0) ;
85
+ opacity:1;
86
+ }
87
+ [data-expoimage] .flip-from-top-end {
88
+ transform: translateZ(calc(var(--expo-image-height,1000px) * -1.5)) rotateX(-180deg);
89
+ opacity: 0;
90
+ }
91
+ [data-expoimage] .flip-from-bottom {
92
+ transition-property: transform, opacity;
93
+ transition-timing-function: var(--expo-image-timing,linear), steps(2, jump-none) !important;
94
+ transform-origin: center;
95
+ }
96
+ [data-expoimage] .flip-from-bottom-container {
97
+ width: 100%;
98
+ height: 100%;
99
+ position: absolute;
100
+ left: 0;
101
+ top: 0;
102
+ perspective: 1000px;
103
+ }
104
+ [data-expoimage] .flip-from-bottom-start:not(.transitioning) {
105
+ transform: translateZ(calc(var(--expo-image-height,1000px) * -1.25)) rotateX(-180deg);
106
+ opacity: 0;
107
+ }
108
+ [data-expoimage] .flip-from-bottom-active {
109
+ transform: translateZ(0px) rotateX(0) ;
110
+ opacity:1;
111
+ }
112
+ [data-expoimage] .flip-from-bottom-end {
113
+ transform: translateZ(calc(var(--expo-image-height,1000px) * -1.25)) rotateX(180deg);
114
+ opacity: 0;
115
+ }
116
+ [data-expoimage] .image-timing-linear {
117
+ --expo-image-timing: linear;
118
+ }
119
+ [data-expoimage] .image-timing-ease-in {
120
+ --expo-image-timing: ease-in;
121
+ }
122
+ [data-expoimage] .image-timing-ease-out {
123
+ --expo-image-timing: ease-out;
124
+ }
125
+ [data-expoimage] .image-timing-ease-in-out {
126
+ --expo-image-timing: ease-in-out;
127
+ }
128
+ `;
129
+ export default function loadStyle() {
130
+ if (typeof document !== 'undefined') {
131
+ const styleTag = document.createElement('style');
132
+ styleTag.innerHTML = style;
133
+ styleTag.id = 'expo-image-styles';
134
+ document.head.appendChild(styleTag);
135
+ }
136
+ }
@@ -0,0 +1,48 @@
1
+ import { ImageContentPositionObject, ImageContentPositionValue } from '../Image.types';
2
+
3
+ export function ensureValueIsWebUnits(value: string | number) {
4
+ const trimmedValue = String(value).trim();
5
+ if (trimmedValue.endsWith('%')) {
6
+ return trimmedValue;
7
+ }
8
+ return `${trimmedValue}px`;
9
+ }
10
+
11
+ type KeysOfUnion<T> = T extends T ? keyof T : never;
12
+
13
+ export const absoluteFilledPosition = {
14
+ width: '100%',
15
+ height: '100%',
16
+ position: 'absolute',
17
+ left: 0,
18
+ top: 0,
19
+ } as const;
20
+
21
+ export function getObjectPositionFromContentPositionObject(
22
+ contentPosition?: ImageContentPositionObject
23
+ ): string {
24
+ const resolvedPosition = { ...contentPosition } as Record<
25
+ KeysOfUnion<ImageContentPositionObject>,
26
+ ImageContentPositionValue
27
+ >;
28
+ if (!resolvedPosition) {
29
+ return '50% 50%';
30
+ }
31
+ if (resolvedPosition.top == null && resolvedPosition.bottom == null) {
32
+ resolvedPosition.top = '50%';
33
+ }
34
+ if (resolvedPosition.left == null && resolvedPosition.right == null) {
35
+ resolvedPosition.left = '50%';
36
+ }
37
+
38
+ return (
39
+ ['top', 'bottom', 'left', 'right']
40
+ .map((key) => {
41
+ if (key in resolvedPosition) {
42
+ return `${key} ${ensureValueIsWebUnits(resolvedPosition[key])}`;
43
+ }
44
+ return '';
45
+ })
46
+ .join(' ') || '50% 50%'
47
+ );
48
+ }