@granite-js/image 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @granite-js/image
2
2
 
3
+ ## 0.1.3
4
+
5
+ ### Patch Changes
6
+
7
+ - @granite-js/native@0.1.3
8
+
9
+ ## 0.1.2
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [e1384cf]
14
+ - @granite-js/native@0.1.2
15
+
3
16
  ## 0.1.1
4
17
 
5
18
  ### Patch Changes
package/NOTICE CHANGED
@@ -45,7 +45,13 @@ Third-Party Libraries:
45
45
  - License Text: https://opensource.org/licenses/MIT
46
46
  - Repository: git+https://github.com/facebook/react-native.git
47
47
 
48
- 5. **typescript**
48
+ 5. **react-simplikit**
49
+
50
+ - License: MIT
51
+ - License Text: https://opensource.org/licenses/MIT
52
+ - Repository: git+https://github.com/toss/react-simplikit.git
53
+
54
+ 6. **typescript**
49
55
 
50
56
  - License: Apache-2.0
51
57
  - License Text: https://www.apache.org/licenses/LICENSE-2.0
@@ -0,0 +1,66 @@
1
+ import { FastImageProps, Source as FastImageSource } from '@granite-js/native/react-native-fast-image';
2
+ type Source = {
3
+ uri?: string;
4
+ cache?: FastImageSource['cache'];
5
+ };
6
+ export interface ImageProps extends Omit<FastImageProps, 'source'> {
7
+ source?: Source;
8
+ }
9
+ /**
10
+ * @public
11
+ * @category UI
12
+ * @name Image
13
+ * @description You can use the `Image` component to load and render bitmap images (such as PNG, JPG) or vector images (SVG). It automatically renders with the appropriate method depending on the image format.
14
+ * @link https://github.com/DylanVann/react-native-fast-image/tree/v8.6.3/README.md
15
+ *
16
+ * @param {object} [props] - The `props` object passed to the component.
17
+ * @param {object} [props.style] - An object that defines the style for the image component. It can include layout-related properties like `width` and `height`.
18
+ * @param {object} [props.source] - An object containing information about the image resource to load.
19
+ * @param {string} [props.source.uri] - The URI address representing the image resource to load.
20
+ * @param {'immutable' | 'web' | 'cacheOnly'} [props.source.cache = 'immutable'] - An option to set the image caching strategy. This applies only to bitmap images. The default value is `immutable`.
21
+ * @param {() => void} [props.onLoadStart] - A callback function that is called when image loading starts.
22
+ * @param {() => void} [props.onLoadEnd] - A callback function that is called when image loading finishes.
23
+ * @param {() => void} [props.onError] - A callback function that is called when an error occurs during image loading.
24
+ *
25
+ * @example
26
+ * ### Example: Loading and rendering an image
27
+ *
28
+ * The following example shows how to load bitmap and vector image resources, and how to print an error message to `console.log` if an error occurs.
29
+ *
30
+ * ```tsx
31
+ * import { Image } from '@granite-js/react-native';
32
+ * import { View } from 'react-native';
33
+ *
34
+ * export function ImageExample() {
35
+ * return (
36
+ * <View>
37
+ * <Image
38
+ * source={{ uri: 'my-image-link' }}
39
+ * style={{
40
+ * width: 300,
41
+ * height: 300,
42
+ * borderWidth: 1,
43
+ * }}
44
+ * onError={() => {
45
+ * console.log('Failed to load image');
46
+ * }}
47
+ * />
48
+ *
49
+ * <Image
50
+ * source={{ uri: 'my-svg-link' }}
51
+ * style={{
52
+ * width: 300,
53
+ * height: 300,
54
+ * borderWidth: 1,
55
+ * }}
56
+ * onError={() => {
57
+ * console.log('Failed to load image');
58
+ * }}
59
+ * />
60
+ * </View>
61
+ * );
62
+ * }
63
+ * ```
64
+ */
65
+ declare function Image(props: ImageProps): import("react/jsx-runtime").JSX.Element;
66
+ export { Image };
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './SvgImage';
1
+ export { Image, type ImageProps } from './Image';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@granite-js/image",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "scripts": {
5
5
  "prepack": "yarn build",
6
6
  "typecheck": "tsc --noEmit",
@@ -17,7 +17,7 @@
17
17
  "./package.json": "./package.json"
18
18
  },
19
19
  "devDependencies": {
20
- "@granite-js/native": "0.1.1",
20
+ "@granite-js/native": "0.1.3",
21
21
  "@types/react": "18.3.3",
22
22
  "eslint": "^9.7.0",
23
23
  "react": "18.2.0",
@@ -29,5 +29,8 @@
29
29
  "@types/react": "*",
30
30
  "react": "*",
31
31
  "react-native": "*"
32
+ },
33
+ "dependencies": {
34
+ "react-simplikit": "^0.0.40"
32
35
  }
33
36
  }
package/src/Image.tsx ADDED
@@ -0,0 +1,93 @@
1
+ import FastImage, { FastImageProps, Source as FastImageSource } from '@granite-js/native/react-native-fast-image';
2
+ import { StyleSheet } from 'react-native';
3
+ import { SvgImage } from './SvgImage';
4
+
5
+ type Source = {
6
+ uri?: string;
7
+ cache?: FastImageSource['cache'];
8
+ };
9
+
10
+ export interface ImageProps extends Omit<FastImageProps, 'source'> {
11
+ source?: Source;
12
+ }
13
+
14
+ /**
15
+ * @public
16
+ * @category UI
17
+ * @name Image
18
+ * @description You can use the `Image` component to load and render bitmap images (such as PNG, JPG) or vector images (SVG). It automatically renders with the appropriate method depending on the image format.
19
+ * @link https://github.com/DylanVann/react-native-fast-image/tree/v8.6.3/README.md
20
+ *
21
+ * @param {object} [props] - The `props` object passed to the component.
22
+ * @param {object} [props.style] - An object that defines the style for the image component. It can include layout-related properties like `width` and `height`.
23
+ * @param {object} [props.source] - An object containing information about the image resource to load.
24
+ * @param {string} [props.source.uri] - The URI address representing the image resource to load.
25
+ * @param {'immutable' | 'web' | 'cacheOnly'} [props.source.cache = 'immutable'] - An option to set the image caching strategy. This applies only to bitmap images. The default value is `immutable`.
26
+ * @param {() => void} [props.onLoadStart] - A callback function that is called when image loading starts.
27
+ * @param {() => void} [props.onLoadEnd] - A callback function that is called when image loading finishes.
28
+ * @param {() => void} [props.onError] - A callback function that is called when an error occurs during image loading.
29
+ *
30
+ * @example
31
+ * ### Example: Loading and rendering an image
32
+ *
33
+ * The following example shows how to load bitmap and vector image resources, and how to print an error message to `console.log` if an error occurs.
34
+ *
35
+ * ```tsx
36
+ * import { Image } from '@granite-js/react-native';
37
+ * import { View } from 'react-native';
38
+ *
39
+ * export function ImageExample() {
40
+ * return (
41
+ * <View>
42
+ * <Image
43
+ * source={{ uri: 'my-image-link' }}
44
+ * style={{
45
+ * width: 300,
46
+ * height: 300,
47
+ * borderWidth: 1,
48
+ * }}
49
+ * onError={() => {
50
+ * console.log('Failed to load image');
51
+ * }}
52
+ * />
53
+ *
54
+ * <Image
55
+ * source={{ uri: 'my-svg-link' }}
56
+ * style={{
57
+ * width: 300,
58
+ * height: 300,
59
+ * borderWidth: 1,
60
+ * }}
61
+ * onError={() => {
62
+ * console.log('Failed to load image');
63
+ * }}
64
+ * />
65
+ * </View>
66
+ * );
67
+ * }
68
+ * ```
69
+ */
70
+ function Image(props: ImageProps) {
71
+ if (typeof props.source === 'object' && props.source.uri?.endsWith('.svg')) {
72
+ const style = StyleSheet.flatten(props.style);
73
+ const width = style?.width;
74
+ const height = style?.height;
75
+
76
+ return (
77
+ <SvgImage
78
+ testID={props.testID}
79
+ url={props.source.uri!}
80
+ width={width}
81
+ height={height}
82
+ style={props.style}
83
+ onLoadStart={props.onLoadStart}
84
+ onLoadEnd={props.onLoadEnd}
85
+ onError={props.onError}
86
+ />
87
+ );
88
+ }
89
+
90
+ return <FastImage {...props} />;
91
+ }
92
+
93
+ export { Image };
package/src/SvgImage.tsx CHANGED
@@ -1,7 +1,7 @@
1
1
  import { SvgUri, SvgXml } from '@granite-js/native/react-native-svg';
2
2
  import { createElement, useEffect, useCallback, useState } from 'react';
3
3
  import { View, type ViewStyle, type StyleProp } from 'react-native';
4
- import { usePreservedCallback } from './hooks/usePreservedCallback';
4
+ import { usePreservedCallback } from 'react-simplikit';
5
5
  import type { DimensionValue, NumberValue } from './types';
6
6
 
7
7
  export interface SvgImageProps {
package/src/index.ts CHANGED
@@ -1 +1 @@
1
- export * from './SvgImage';
1
+ export { Image, type ImageProps } from './Image';
@@ -1 +0,0 @@
1
- export declare function usePreservedCallback<Callback extends (...args: any[]) => any>(callback: Callback): Callback;
@@ -1,16 +0,0 @@
1
- import { useCallback, useEffect, useRef } from 'react';
2
-
3
- export function usePreservedCallback<Callback extends (...args: any[]) => any>(callback: Callback) {
4
- const callbackRef = useRef<Callback>(callback);
5
-
6
- useEffect(() => {
7
- callbackRef.current = callback;
8
- }, [callback]);
9
-
10
- return useCallback(
11
- (...args: any[]) => {
12
- return callbackRef.current(...args);
13
- },
14
- [callbackRef]
15
- ) as Callback;
16
- }