react-native-nitro-image-pipeline 1.2.0 → 1.2.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.
Files changed (41) hide show
  1. package/README.md +72 -5
  2. package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/HybridNitroImagePipeline.kt +17 -15
  3. package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/ResizeTransformation.kt +1 -2
  4. package/ios/HybridNitroImagePipeline.swift +54 -38
  5. package/ios/RoundedCornersProcessor.swift +1 -1
  6. package/lib/commonjs/NitroImagePipeline.js +9 -0
  7. package/lib/commonjs/NitroImagePipeline.js.map +1 -0
  8. package/lib/commonjs/PipelineImage.js +111 -0
  9. package/lib/commonjs/PipelineImage.js.map +1 -0
  10. package/lib/commonjs/index.js +40 -88
  11. package/lib/commonjs/index.js.map +1 -1
  12. package/lib/commonjs/resizeForStyle.js +68 -0
  13. package/lib/commonjs/resizeForStyle.js.map +1 -0
  14. package/lib/commonjs/useImage.js +95 -0
  15. package/lib/commonjs/useImage.js.map +1 -0
  16. package/lib/module/NitroImagePipeline.js +5 -0
  17. package/lib/module/NitroImagePipeline.js.map +1 -0
  18. package/lib/module/PipelineImage.js +107 -0
  19. package/lib/module/PipelineImage.js.map +1 -0
  20. package/lib/module/index.js +4 -86
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/resizeForStyle.js +62 -0
  23. package/lib/module/resizeForStyle.js.map +1 -0
  24. package/lib/module/useImage.js +91 -0
  25. package/lib/module/useImage.js.map +1 -0
  26. package/lib/typescript/src/NitroImagePipeline.d.ts +3 -0
  27. package/lib/typescript/src/NitroImagePipeline.d.ts.map +1 -0
  28. package/lib/typescript/src/PipelineImage.d.ts +57 -0
  29. package/lib/typescript/src/PipelineImage.d.ts.map +1 -0
  30. package/lib/typescript/src/index.d.ts +5 -45
  31. package/lib/typescript/src/index.d.ts.map +1 -1
  32. package/lib/typescript/src/resizeForStyle.d.ts +30 -0
  33. package/lib/typescript/src/resizeForStyle.d.ts.map +1 -0
  34. package/lib/typescript/src/useImage.d.ts +53 -0
  35. package/lib/typescript/src/useImage.d.ts.map +1 -0
  36. package/package.json +1 -1
  37. package/src/NitroImagePipeline.ts +6 -0
  38. package/src/PipelineImage.tsx +156 -0
  39. package/src/index.ts +9 -139
  40. package/src/resizeForStyle.ts +96 -0
  41. package/src/useImage.ts +149 -0
package/README.md CHANGED
@@ -38,32 +38,69 @@ bun add react-native-nitro-image-pipeline react-native-nitro-modules react-nativ
38
38
 
39
39
  ## Usage
40
40
 
41
+ ### `<PipelineImage>` component
42
+
43
+ The zero-math way to load an image in a component — no manual `PixelRatio` conversions:
44
+
45
+ ```tsx
46
+ import { PipelineImage } from 'react-native-nitro-image-pipeline';
47
+
48
+ function MyComponent() {
49
+ return (
50
+ <PipelineImage
51
+ url="https://example.com/photo.jpg"
52
+ style={styles.photo} // bitmap is sized to this layout × PixelRatio.get()
53
+ blur={2} // points, like style
54
+ />
55
+ );
56
+ }
57
+
58
+ const styles = StyleSheet.create({
59
+ photo: { width: 300, height: 200, borderRadius: 12 }, // baked into the bitmap
60
+ });
61
+ ```
62
+
63
+ Numeric `width`/`height` in `style` load immediately; percentage or flex-based sizes wait for the
64
+ first `onLayout` before fetching, so the full-size image is never requested just to be squeezed
65
+ into a small view. `blur` is in points **on this component only** — it's converted to bitmap
66
+ pixels internally, unlike the pixel-based values used everywhere else in this library.
67
+ `cornerRadius` works the same way, but if you don't pass it, it's derived instead from `style`'s
68
+ `borderRadius` (or the per-corner `borderTopLeftRadius`/etc. properties, e.g. a "ticket" shape) —
69
+ so a style that already rounds the view rounds the bitmap too, with no separate prop. Pass
70
+ `cornerRadius` explicitly to override that. `onLoad`/`onError` callbacks are supported, and every
71
+ other prop (`resizeMode`, `recyclingKey`, `testID`, …) is passed straight through to
72
+ `NativeNitroImage`.
73
+
41
74
  ### `useImage` hook
42
75
 
43
76
  The simplest way to load an image in a component:
44
77
 
45
78
  ```tsx
46
- import { useImage } from 'react-native-nitro-image-pipeline';
79
+ import { PixelRatio, useImage, resizeForStyle } from 'react-native-nitro-image-pipeline';
47
80
 
48
81
  function MyComponent() {
49
- const px = PixelRatio.get();
50
82
  const { image, error } = useImage({
51
83
  url: 'https://example.com/photo.jpg',
52
84
  blur: 4, // Gaussian sigma in bitmap pixels — same result on iOS and Android
53
85
  // Resize to the size you display (points × screen scale) so the corner
54
86
  // radii apply 1:1 to what you see instead of the full-resolution source.
55
- resize: { width: 300 * px, height: 200 * px },
56
- cornerRadius: 12 * px,
87
+ resize: resizeForStyle(styles.image), // display size × PixelRatio.get()
88
+ cornerRadius: 12 * PixelRatio.get(), // bitmap pixels
57
89
  });
58
90
 
59
91
  if (error) return <Text>Failed to load image</Text>;
60
92
  if (!image) return <ActivityIndicator />;
61
93
 
62
94
  // use `image` with react-native-nitro-image
63
- return <NitroImage source={image} />;
95
+ return <NitroImage image={image} style={styles.image} />;
64
96
  }
97
+
98
+ const styles = StyleSheet.create({ image: { width: 300, height: 200 } });
65
99
  ```
66
100
 
101
+ Pass `enabled: false` to defer the request — used internally by `<PipelineImage>` to wait for
102
+ layout before it has a size to resize to.
103
+
67
104
  ### Direct API
68
105
 
69
106
  ```ts
@@ -115,6 +152,36 @@ Loads an image from a URL and returns a `Promise<Image>`.
115
152
  | `cornerRadius` | `number \| CornerRadii` | `0` | Corner radius in pixels of the produced bitmap — a single number for all four corners, or `{ topLeft?, topRight?, bottomLeft?, bottomRight? }` for independent per-corner radii (omitted corners stay square). Pair with `resize` for radii that match your layout |
116
153
  | `cache` | `'memory' \| 'disk' \| 'none'` | platform default | Caching strategy |
117
154
 
155
+ ### `<PipelineImage>`
156
+
157
+ | Prop | Type | Default | Description |
158
+ |---|---|---|---|
159
+ | `url` | `string` | — | Image URL to load |
160
+ | `style` | `StyleProp<ViewStyle>` | — | Layout style; also determines the resize target (see [`resizeForStyle`](#resizeforstyle-style--resizeforlayoutwidth-height)) and, if `cornerRadius` is omitted, the corner radius (see [`cornerRadiusForStyle`](#cornerradiusforstylestyle)) |
161
+ | `blur` | `number` | `0` | Gaussian blur strength, in **points** (converted to bitmap pixels internally) |
162
+ | `cornerRadius` | `number \| CornerRadii` | derived from `style` | Corner radius, in **points** (converted to bitmap pixels internally). When omitted, derived from `style`'s `borderRadius`/`borderTopLeftRadius`/etc.; square if neither is set |
163
+ | `cache` | `'memory' \| 'disk' \| 'none'` | platform default | Caching strategy |
164
+ | `onLoad` | `(image: Image) => void` | — | Called when the image finishes loading |
165
+ | `onError` | `(error: Error) => void` | — | Called if loading fails |
166
+ | `onLayout` | `(event: LayoutChangeEvent) => void` | — | Standard `View` layout callback; also drives the deferred resize for non-numeric sizes |
167
+ | `…NativeNitroImage props` | — | — | Everything else (`resizeMode`, `recyclingKey`, `testID`, …) is passed through to `NativeNitroImage` |
168
+
169
+ ### `resizeForStyle(style)` / `resizeForLayout(width, height)`
170
+
171
+ Converts a layout size in points to a bitmap `resize` option in pixels. Returns
172
+ `{ width, height }` in whole pixels via `PixelRatio.getPixelSizeForLayoutSize`, or `undefined` for
173
+ non-numeric sizes (e.g. `'100%'`, `undefined`) — `resizeForStyle` reads `style.width`/`style.height`,
174
+ `resizeForLayout` takes explicit numbers.
175
+
176
+ ### `cornerRadiusForStyle(style)`
177
+
178
+ Converts a view style's `borderRadius`/`borderTopLeftRadius`/`borderTopRightRadius`/
179
+ `borderBottomLeftRadius`/`borderBottomRightRadius` (in points) into a `cornerRadius` option — a
180
+ plain number for uniform `borderRadius` alone, or a `CornerRadii` object once any per-corner
181
+ property is set (falling back to `borderRadius` for the corners left unset). Returns `undefined`
182
+ when none are set. This is what `<PipelineImage>` uses internally when its `cornerRadius` prop is
183
+ omitted.
184
+
118
185
  ### `preLoadImage(url)`
119
186
 
120
187
  Prefetches a single image into the cache. Returns `Promise<void>`.
@@ -54,21 +54,23 @@ class HybridNitroImagePipeline : HybridNitroImagePipelineSpec() {
54
54
  // they run on, so they must see the final size.
55
55
  resize?.let { (width, height) -> add(ResizeTransformation(width, height)) }
56
56
  if (blur > 0f) add(BlurTransformation(context, blur))
57
- options?.cornerRadius?.match(
58
- first = { radius ->
59
- if (radius > 0.0) add(RoundedCornersTransformation(radius.toFloat()))
60
- },
61
- second = { radii ->
62
- // RoundedCornersTransformation rejects negative radii; treat them as square.
63
- val topLeft = (radii.topLeft?.toFloat() ?: 0f).coerceAtLeast(0f)
64
- val topRight = (radii.topRight?.toFloat() ?: 0f).coerceAtLeast(0f)
65
- val bottomLeft = (radii.bottomLeft?.toFloat() ?: 0f).coerceAtLeast(0f)
66
- val bottomRight = (radii.bottomRight?.toFloat() ?: 0f).coerceAtLeast(0f)
67
- if (topLeft > 0f || topRight > 0f || bottomLeft > 0f || bottomRight > 0f) {
68
- add(RoundedCornersTransformation(topLeft, topRight, bottomLeft, bottomRight))
69
- }
70
- },
71
- )
57
+ options
58
+ ?.cornerRadius
59
+ ?.match(
60
+ first = { radius ->
61
+ if (radius > 0.0) add(RoundedCornersTransformation(radius.toFloat()))
62
+ },
63
+ second = { radii ->
64
+ // RoundedCornersTransformation rejects negative radii; treat them as square.
65
+ val topLeft = (radii.topLeft?.toFloat() ?: 0f).coerceAtLeast(0f)
66
+ val topRight = (radii.topRight?.toFloat() ?: 0f).coerceAtLeast(0f)
67
+ val bottomLeft = (radii.bottomLeft?.toFloat() ?: 0f).coerceAtLeast(0f)
68
+ val bottomRight = (radii.bottomRight?.toFloat() ?: 0f).coerceAtLeast(0f)
69
+ if (topLeft > 0f || topRight > 0f || bottomLeft > 0f || bottomRight > 0f) {
70
+ add(RoundedCornersTransformation(topLeft, topRight, bottomLeft, bottomRight))
71
+ }
72
+ },
73
+ )
72
74
  }
73
75
  val request =
74
76
  ImageRequest.Builder(context)
@@ -31,8 +31,7 @@ class ResizeTransformation(
31
31
  else input
32
32
  if (softwareInput.width == width && softwareInput.height == height) return softwareInput
33
33
 
34
- val scale =
35
- max(width.toFloat() / softwareInput.width, height.toFloat() / softwareInput.height)
34
+ val scale = max(width.toFloat() / softwareInput.width, height.toFloat() / softwareInput.height)
36
35
  val matrix =
37
36
  Matrix().apply {
38
37
  setScale(scale, scale)
@@ -103,52 +103,68 @@ class HybridNitroImagePipeline: HybridNitroImagePipelineSpec {
103
103
  private var pipeline: ImagePipeline { Self.sharedPipeline }
104
104
  private var prefetcher: ImagePrefetcher { Self.sharedPrefetcher }
105
105
 
106
+ private static func cacheOptions(for cache: CacheOption?) -> ImageRequest.Options {
107
+ switch cache {
108
+ case .memory: [.disableDiskCache]
109
+ case .disk: [.disableMemoryCache]
110
+ case .none?: [.disableDiskCache, .disableMemoryCache]
111
+ default: []
112
+ }
113
+ }
114
+
115
+ /// The processor that bakes `cornerRadius` into the bitmap, or `nil` when
116
+ /// no corner is actually rounded.
117
+ private static func cornerRadiusProcessor(
118
+ for cornerRadius: Variant_Double_CornerRadii?
119
+ ) -> (any ImageProcessing)? {
120
+ switch cornerRadius {
121
+ case .first(let radius):
122
+ guard radius > 0 else { return nil }
123
+ // `unit: .pixels` is required: Nuke defaults to `.points`, which
124
+ // multiplies the radius by the screen scale. The radius is
125
+ // documented — and implemented on Android and in
126
+ // RoundedCornersProcessor — as bitmap pixels.
127
+ return ImageProcessors.RoundedCorners(radius: radius, unit: .pixels)
128
+ case .second(let radii):
129
+ let roundedCorners = RoundedCornersProcessor(radii: radii)
130
+ return roundedCorners.hasRounding ? roundedCorners : nil
131
+ case nil:
132
+ return nil
133
+ }
134
+ }
135
+
136
+ private static func processors(for options: Options?) -> [any ImageProcessing] {
137
+ var processors: [any ImageProcessing] = []
138
+ // Resize first: blur sigma and corner radii are defined in pixels
139
+ // of the bitmap they run on, so they must see the final size.
140
+ if let resize = options?.resize, resize.width > 0, resize.height > 0 {
141
+ processors.append(ImageProcessors.Resize(
142
+ size: CGSize(width: resize.width, height: resize.height),
143
+ unit: .pixels,
144
+ contentMode: .aspectFill,
145
+ crop: true,
146
+ upscale: true
147
+ ))
148
+ }
149
+ if let blur = options?.blur, blur > 0 {
150
+ processors.append(GaussianBlurProcessor(sigma: blur))
151
+ }
152
+ if let roundedCorners = cornerRadiusProcessor(for: options?.cornerRadius) {
153
+ processors.append(roundedCorners)
154
+ }
155
+ return processors
156
+ }
157
+
106
158
  func loadImage(url: String, options: Options?) throws -> Promise<any HybridImageSpec> {
107
159
  return Promise.async {
108
160
  guard let imageUrl = URL(string: url) else {
109
161
  throw RuntimeError.error(withMessage: "Invalid URL: \(url)")
110
162
  }
111
163
 
112
- let cacheOptions: ImageRequest.Options = switch options?.cache {
113
- case .memory: [.disableDiskCache]
114
- case .disk: [.disableMemoryCache]
115
- case .none?: [.disableDiskCache, .disableMemoryCache]
116
- default: []
117
- }
118
-
119
- var processors: [any ImageProcessing] = []
120
- // Resize first: blur sigma and corner radii are defined in pixels
121
- // of the bitmap they run on, so they must see the final size.
122
- if let resize = options?.resize, resize.width > 0, resize.height > 0 {
123
- processors.append(ImageProcessors.Resize(
124
- size: CGSize(width: resize.width, height: resize.height),
125
- unit: .pixels,
126
- contentMode: .aspectFill,
127
- crop: true,
128
- upscale: true
129
- ))
130
- }
131
- if let blur = options?.blur, blur > 0 {
132
- processors.append(GaussianBlurProcessor(sigma: blur))
133
- }
134
- switch options?.cornerRadius {
135
- case .first(let radius):
136
- if radius > 0 {
137
- processors.append(.roundedCorners(radius: radius))
138
- }
139
- case .second(let radii):
140
- let roundedCorners = RoundedCornersProcessor(radii: radii)
141
- if roundedCorners.hasRounding {
142
- processors.append(roundedCorners)
143
- }
144
- case nil:
145
- break
146
- }
147
-
148
164
  let imgRequest = ImageRequest(
149
165
  url: imageUrl,
150
- processors: processors,
151
- options: cacheOptions
166
+ processors: Self.processors(for: options),
167
+ options: Self.cacheOptions(for: options?.cache)
152
168
  )
153
169
 
154
170
  let image = try await self.pipeline.image(for: imgRequest)
@@ -78,7 +78,7 @@ struct RoundedCornersProcessor: ImageProcessing {
78
78
  (rect.width, topLeft + topRight),
79
79
  (rect.width, bottomLeft + bottomRight),
80
80
  (rect.height, topLeft + bottomLeft),
81
- (rect.height, topRight + bottomRight),
81
+ (rect.height, topRight + bottomRight)
82
82
  ] where pair > edge {
83
83
  scale = min(scale, edge / pair)
84
84
  }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.NitroImagePipeline = void 0;
7
+ var _reactNativeNitroModules = require("react-native-nitro-modules");
8
+ const NitroImagePipeline = exports.NitroImagePipeline = _reactNativeNitroModules.NitroModules.createHybridObject('NitroImagePipeline');
9
+ //# sourceMappingURL=NitroImagePipeline.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNativeNitroModules","require","NitroImagePipeline","exports","NitroModules","createHybridObject"],"sourceRoot":"../../src","sources":["NitroImagePipeline.ts"],"mappings":";;;;;;AAAA,IAAAA,wBAAA,GAAAC,OAAA;AAIO,MAAMC,kBAAkB,GAAAC,OAAA,CAAAD,kBAAA,GAC7BE,qCAAY,CAACC,kBAAkB,CAAyB,oBAAoB,CAAC","ignoreList":[]}
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.PipelineImage = PipelineImage;
7
+ var _react = require("react");
8
+ var _reactNative = require("react-native");
9
+ var _reactNativeNitroImage = require("react-native-nitro-image");
10
+ var _resizeForStyle = require("./resizeForStyle");
11
+ var _useImage = require("./useImage");
12
+ var _jsxRuntime = require("react/jsx-runtime");
13
+ function sameSize(a, b) {
14
+ return a?.width === b?.width && a?.height === b?.height;
15
+ }
16
+ function scaleRadius(cornerRadius, scale) {
17
+ if (typeof cornerRadius === 'number') {
18
+ return cornerRadius * scale;
19
+ }
20
+ return {
21
+ topLeft: (cornerRadius.topLeft ?? 0) * scale,
22
+ topRight: (cornerRadius.topRight ?? 0) * scale,
23
+ bottomLeft: (cornerRadius.bottomLeft ?? 0) * scale,
24
+ bottomRight: (cornerRadius.bottomRight ?? 0) * scale
25
+ };
26
+ }
27
+
28
+ /**
29
+ * A `NativeNitroImage` that loads `url` through the pipeline at exactly the
30
+ * size it is displayed: the bitmap is resized to the view's size in points ×
31
+ * `PixelRatio.get()`, so `blur` and `cornerRadius` (both in points here) apply
32
+ * 1:1 to what is on screen and large sources are never decoded at full size.
33
+ *
34
+ * A numeric `width`/`height` in `style` starts loading immediately; otherwise
35
+ * (`'50%'`, `flex`, `aspectRatio`, …) loading waits for the first `onLayout`.
36
+ * If the layout size later changes, a new variant is loaded and swapped in
37
+ * without flashing. Without an explicit `cornerRadius` prop, `style`'s
38
+ * `borderRadius`-family properties are baked into the bitmap instead — no
39
+ * separate view-layer rounding needed.
40
+ * @example
41
+ * ```tsx
42
+ * <PipelineImage
43
+ * url="https://example.com/photo.jpg"
44
+ * style={{ width: 300, height: 200 }}
45
+ * cornerRadius={24}
46
+ * />
47
+ * ```
48
+ */
49
+ function PipelineImage({
50
+ url,
51
+ blur = 0,
52
+ cornerRadius,
53
+ cache,
54
+ onLoad,
55
+ onError,
56
+ style,
57
+ onLayout,
58
+ ...viewProps
59
+ }) {
60
+ const scale = _reactNative.PixelRatio.get();
61
+ const styleSize = (0, _resizeForStyle.resizeForStyle)(style);
62
+ const [layoutSize, setLayoutSize] = (0, _react.useState)(undefined);
63
+ // A numeric style is what the caller declared, so it wins and starts the
64
+ // request a frame earlier; the measured layout is the fallback.
65
+ const resize = styleSize ?? layoutSize;
66
+ // Same precedence: an explicit prop wins over what style implies.
67
+ const effectiveCornerRadius = cornerRadius ?? (0, _resizeForStyle.cornerRadiusForStyle)(style) ?? 0;
68
+ const {
69
+ image,
70
+ error
71
+ } = (0, _useImage.useImage)({
72
+ url,
73
+ blur: blur * scale,
74
+ cornerRadius: scaleRadius(effectiveCornerRadius, scale),
75
+ cache,
76
+ resize,
77
+ enabled: resize !== undefined
78
+ });
79
+
80
+ // Latest callbacks in refs so inline arrow props don't re-fire the effects.
81
+ const onLoadRef = (0, _react.useRef)(onLoad);
82
+ const onErrorRef = (0, _react.useRef)(onError);
83
+ (0, _react.useEffect)(() => {
84
+ onLoadRef.current = onLoad;
85
+ onErrorRef.current = onError;
86
+ });
87
+ (0, _react.useEffect)(() => {
88
+ if (image) onLoadRef.current?.(image);
89
+ }, [image]);
90
+ (0, _react.useEffect)(() => {
91
+ if (error) onErrorRef.current?.(error);
92
+ }, [error]);
93
+ const handleLayout = event => {
94
+ onLayout?.(event);
95
+ const {
96
+ width,
97
+ height
98
+ } = event.nativeEvent.layout;
99
+ const next = (0, _resizeForStyle.resizeForLayout)(width, height);
100
+ // Always record it (even when a numeric style is in charge) so a later
101
+ // switch to a non-numeric style has a size to fall back on.
102
+ setLayoutSize(prev => sameSize(prev, next) ? prev : next);
103
+ };
104
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeNitroImage.NativeNitroImage, {
105
+ ...viewProps,
106
+ style: style,
107
+ onLayout: handleLayout,
108
+ image: image
109
+ });
110
+ }
111
+ //# sourceMappingURL=PipelineImage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_react","require","_reactNative","_reactNativeNitroImage","_resizeForStyle","_useImage","_jsxRuntime","sameSize","a","b","width","height","scaleRadius","cornerRadius","scale","topLeft","topRight","bottomLeft","bottomRight","PipelineImage","url","blur","cache","onLoad","onError","style","onLayout","viewProps","PixelRatio","get","styleSize","resizeForStyle","layoutSize","setLayoutSize","useState","undefined","resize","effectiveCornerRadius","cornerRadiusForStyle","image","error","useImage","enabled","onLoadRef","useRef","onErrorRef","useEffect","current","handleLayout","event","nativeEvent","layout","next","resizeForLayout","prev","jsx","NativeNitroImage"],"sourceRoot":"../../src","sources":["PipelineImage.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AAKA,IAAAE,sBAAA,GAAAF,OAAA;AAEA,IAAAG,eAAA,GAAAH,OAAA;AAUA,IAAAI,SAAA,GAAAJ,OAAA;AAAsC,IAAAK,WAAA,GAAAL,OAAA;AAkCtC,SA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SAAS;IACbF,KAAK,EAAEA,KAAM;IACbC,QAAQ,EAAEsB,YAAa;IACvBT,KAAK,EAAEA;EAAM,CACd,CAAC;AAEN","ignoreList":[]}
@@ -3,92 +3,44 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.NitroImagePipeline = void 0;
7
- exports.useImage = useImage;
8
- var _react = require("react");
9
- var _reactNativeNitroModules = require("react-native-nitro-modules");
10
- const NitroImagePipeline = exports.NitroImagePipeline = _reactNativeNitroModules.NitroModules.createHybridObject('NitroImagePipeline');
11
- /**
12
- * A hook to asynchronously load an image from the
13
- * given {@linkcode AsyncImageSource} into memory.
14
- * @example
15
- * ```ts
16
- * const { image, error } = useImage({ filePath: '/tmp/image.jpg' })
17
- * ```
18
- */
19
- function useImage({
20
- url,
21
- blur = 0,
22
- cornerRadius = 0,
23
- resize,
24
- cache
25
- }) {
26
- const [image, setImage] = (0, _react.useState)({
27
- image: undefined,
28
- error: undefined
29
- });
30
- const loadedUrlRef = (0, _react.useRef)(url);
31
-
32
- // Split the option into primitives so an inline `{ topLeft: 24, ... }`
33
- // literal (new identity every render) doesn't re-trigger the effect.
34
- const isUniformRadius = typeof cornerRadius === 'number';
35
- const uniformRadius = isUniformRadius ? cornerRadius : 0;
36
- const {
37
- topLeft = 0,
38
- topRight = 0,
39
- bottomLeft = 0,
40
- bottomRight = 0
41
- } = isUniformRadius ? {} : cornerRadius;
42
- const resizeWidth = resize?.width ?? 0;
43
- const resizeHeight = resize?.height ?? 0;
44
- (0, _react.useEffect)(() => {
45
- let cancelled = false;
46
- // Only reset to the loading state when the URL changes; for same-URL
47
- // param tweaks (blur/cornerRadius/cache) keep showing the current image
48
- // until the new variant resolves, to avoid flashing empty.
49
- if (loadedUrlRef.current !== url) {
50
- loadedUrlRef.current = url;
51
- setImage({
52
- image: undefined,
53
- error: undefined
54
- });
55
- }
56
- (async () => {
57
- try {
58
- const result = await NitroImagePipeline.loadImage(url, {
59
- blur,
60
- cornerRadius: isUniformRadius ? uniformRadius : {
61
- topLeft,
62
- topRight,
63
- bottomLeft,
64
- bottomRight
65
- },
66
- resize: resizeWidth > 0 && resizeHeight > 0 ? {
67
- width: resizeWidth,
68
- height: resizeHeight
69
- } : undefined,
70
- cache
71
- });
72
- if (!cancelled) {
73
- setImage({
74
- image: result,
75
- error: undefined
76
- });
77
- }
78
- } catch (e) {
79
- const error = e instanceof Error ? e : new Error(`${e}`);
80
- if (!cancelled) {
81
- setImage({
82
- image: undefined,
83
- error: error
84
- });
85
- }
86
- }
87
- })();
88
- return () => {
89
- cancelled = true;
90
- };
91
- }, [url, blur, isUniformRadius, uniformRadius, topLeft, topRight, bottomLeft, bottomRight, resizeWidth, resizeHeight, cache]);
92
- return image;
93
- }
6
+ Object.defineProperty(exports, "NitroImagePipeline", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _NitroImagePipeline.NitroImagePipeline;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "PipelineImage", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _PipelineImage.PipelineImage;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "cornerRadiusForStyle", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _resizeForStyle.cornerRadiusForStyle;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "resizeForLayout", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _resizeForStyle.resizeForLayout;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "resizeForStyle", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _resizeForStyle.resizeForStyle;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "useImage", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _useImage.useImage;
40
+ }
41
+ });
42
+ var _NitroImagePipeline = require("./NitroImagePipeline");
43
+ var _PipelineImage = require("./PipelineImage");
44
+ var _resizeForStyle = require("./resizeForStyle");
45
+ var _useImage = require("./useImage");
94
46
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_react","require","_reactNativeNitroModules","NitroImagePipeline","exports","NitroModules","createHybridObject","useImage","url","blur","cornerRadius","resize","cache","image","setImage","useState","undefined","error","loadedUrlRef","useRef","isUniformRadius","uniformRadius","topLeft","topRight","bottomLeft","bottomRight","resizeWidth","width","resizeHeight","height","useEffect","cancelled","current","result","loadImage","e","Error"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,wBAAA,GAAAD,OAAA;AAYO,MAAME,kBAAkB,GAAAC,OAAA,CAAAD,kBAAA,GAC7BE,qCAAY,CAACC,kBAAkB,CAAyB,oBAAoB,CAAC;AAmB/E;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,QAAQA,CAAC;EACvBC,GAAG;EACHC,IAAI,GAAG,CAAC;EACRC,YAAY,GAAG,CAAC;EAChBC,MAAM;EACNC;AAuBF,CAAC,EAAU;EACT,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAG,IAAAC,eAAQ,EAAS;IACzCF,KAAK,EAAEG,SAAS;IAChBC,KAAK,EAAED;EACT,CAAC,CAAC;EACF,MAAME,YAAY,GAAG,IAAAC,aAAM,EAACX,GAAG,CAAC;;EAEhC;EACA;EACA,MAAMY,eAAe,GAAG,OAAOV,YAAY,KAAK,QAAQ;EACxD,MAAMW,aAAa,GAAGD,eAAe,GAAGV,YAAY,GAAG,CAAC;EACxD,MAAM;IACJY,OAAO,GAAG,CAAC;IACXC,QAAQ,GAAG,CAAC;IACZC,UAAU,GAAG,CAAC;IACdC,WAAW,GAAG;EAChB,CAAC,GAAGL,eAAe,GAAG,CAAC,CAAC,GAAGV,YAAY;EACvC,MAAMgB,WAAW,GAAGf,MAAM,EAAEgB,KAAK,IAAI,CAAC;EACtC,MAAMC,YAAY,GAAGjB,MAAM,EAAEkB,MAAM,IAAI,CAAC;EAExC,IAAAC,gBAAS,EAAC,MAAM;IACd,IAAI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K,CACN,CAAC;EAEF,OAAOC,KAAK;AACd","ignoreList":[]}
1
+ {"version":3,"names":["_NitroImagePipeline","require","_PipelineImage","_resizeForStyle","_useImage"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,mBAAA,GAAAC,OAAA;AACA,IAAAC,cAAA,GAAAD,OAAA;AACA,IAAAE,eAAA,GAAAF,OAAA;AAWA,IAAAG,SAAA,GAAAH,OAAA","ignoreList":[]}
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.cornerRadiusForStyle = cornerRadiusForStyle;
7
+ exports.resizeForLayout = resizeForLayout;
8
+ exports.resizeForStyle = resizeForStyle;
9
+ var _reactNative = require("react-native");
10
+ /**
11
+ * Converts a layout size in points (dp) to the pipeline's `resize` option in
12
+ * whole bitmap pixels using `PixelRatio.getPixelSizeForLayoutSize`. Returns
13
+ * `undefined` unless both values are positive numbers.
14
+ */
15
+ function resizeForLayout(width, height) {
16
+ if (typeof width !== 'number' || typeof height !== 'number') {
17
+ return undefined;
18
+ }
19
+ const pixelWidth = _reactNative.PixelRatio.getPixelSizeForLayoutSize(width);
20
+ const pixelHeight = _reactNative.PixelRatio.getPixelSizeForLayoutSize(height);
21
+ return pixelWidth > 0 && pixelHeight > 0 ? {
22
+ width: pixelWidth,
23
+ height: pixelHeight
24
+ } : undefined;
25
+ }
26
+
27
+ /**
28
+ * Derives the pipeline's `resize` option from a view style whose `width` and
29
+ * `height` are numeric points, so the bitmap matches the display size on
30
+ * every screen density:
31
+ * ```ts
32
+ * useImage({ url, resize: resizeForStyle(styles.image) });
33
+ * ```
34
+ * Arrays and registered styles are flattened. Returns `undefined` when either
35
+ * dimension is missing or not a number (`'50%'`, `'auto'`, flex-driven) —
36
+ * use `<PipelineImage>` for those, which measures the view instead.
37
+ */
38
+ function resizeForStyle(style) {
39
+ const flat = _reactNative.StyleSheet.flatten(style);
40
+ return resizeForLayout(flat?.width, flat?.height);
41
+ }
42
+
43
+ /**
44
+ * Derives a `cornerRadius` option from a view style's `borderRadius` /
45
+ * `borderTopLeftRadius` / `borderTopRightRadius` / `borderBottomLeftRadius` /
46
+ * `borderBottomRightRadius`, in points. Per-corner properties override
47
+ * `borderRadius` for that corner; a corner with neither set stays square.
48
+ * Non-numeric values (percentages, animated values) are ignored, like
49
+ * {@linkcode resizeForStyle}. Returns `undefined` when none are set.
50
+ */
51
+ function cornerRadiusForStyle(style) {
52
+ const flat = _reactNative.StyleSheet.flatten(style);
53
+ const base = typeof flat?.borderRadius === 'number' ? flat.borderRadius : undefined;
54
+ const topLeft = typeof flat?.borderTopLeftRadius === 'number' ? flat.borderTopLeftRadius : undefined;
55
+ const topRight = typeof flat?.borderTopRightRadius === 'number' ? flat.borderTopRightRadius : undefined;
56
+ const bottomLeft = typeof flat?.borderBottomLeftRadius === 'number' ? flat.borderBottomLeftRadius : undefined;
57
+ const bottomRight = typeof flat?.borderBottomRightRadius === 'number' ? flat.borderBottomRightRadius : undefined;
58
+ if (topLeft === undefined && topRight === undefined && bottomLeft === undefined && bottomRight === undefined) {
59
+ return base;
60
+ }
61
+ return {
62
+ topLeft: topLeft ?? base,
63
+ topRight: topRight ?? base,
64
+ bottomLeft: bottomLeft ?? base,
65
+ bottomRight: bottomRight ?? base
66
+ };
67
+ }
68
+ //# sourceMappingURL=resizeForStyle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNative","require","resizeForLayout","width","height","undefined","pixelWidth","PixelRatio","getPixelSizeForLayoutSize","pixelHeight","resizeForStyle","style","flat","StyleSheet","flatten","cornerRadiusForStyle","base","borderRadius","topLeft","borderTopLeftRadius","topRight","borderTopRightRadius","bottomLeft","borderBottomLeftRadius","bottomRight","borderBottomRightRadius"],"sourceRoot":"../../src","sources":["resizeForStyle.ts"],"mappings":";;;;;;;;AAAA,IAAAA,YAAA,GAAAC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