@react-native-vector-icons/common 12.4.1 → 13.0.0

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 (54) hide show
  1. package/README.md +30 -19
  2. package/lib/commonjs/create-icon-set.js +25 -17
  3. package/lib/commonjs/create-icon-set.js.map +1 -1
  4. package/lib/commonjs/create-icon-source-cache.js +3 -26
  5. package/lib/commonjs/create-icon-source-cache.js.map +1 -1
  6. package/lib/commonjs/dynamicLoading/dynamic-font-loading.js +0 -1
  7. package/lib/commonjs/dynamicLoading/dynamic-font-loading.js.map +1 -1
  8. package/lib/commonjs/dynamicLoading/dynamic-loading-setting.js +3 -0
  9. package/lib/commonjs/dynamicLoading/dynamic-loading-setting.js.map +1 -1
  10. package/lib/commonjs/get-image-library.js +8 -7
  11. package/lib/commonjs/get-image-library.js.map +1 -1
  12. package/lib/commonjs/get-image-source.js +39 -45
  13. package/lib/commonjs/get-image-source.js.map +1 -1
  14. package/lib/module/create-icon-set.js +24 -13
  15. package/lib/module/create-icon-set.js.map +1 -1
  16. package/lib/module/create-icon-source-cache.js +3 -26
  17. package/lib/module/create-icon-source-cache.js.map +1 -1
  18. package/lib/module/dynamicLoading/dynamic-font-loading.js +0 -1
  19. package/lib/module/dynamicLoading/dynamic-font-loading.js.map +1 -1
  20. package/lib/module/dynamicLoading/dynamic-loading-setting.js +3 -0
  21. package/lib/module/dynamicLoading/dynamic-loading-setting.js.map +1 -1
  22. package/lib/module/get-image-library.js +8 -8
  23. package/lib/module/get-image-library.js.map +1 -1
  24. package/lib/module/get-image-source.js +39 -45
  25. package/lib/module/get-image-source.js.map +1 -1
  26. package/lib/typescript/commonjs/src/create-icon-set.d.ts +9 -5
  27. package/lib/typescript/commonjs/src/create-icon-set.d.ts.map +1 -1
  28. package/lib/typescript/commonjs/src/create-icon-source-cache.d.ts +18 -15
  29. package/lib/typescript/commonjs/src/create-icon-source-cache.d.ts.map +1 -1
  30. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.d.ts.map +1 -1
  31. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-loading-setting.d.ts +21 -2
  32. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-loading-setting.d.ts.map +1 -1
  33. package/lib/typescript/commonjs/src/get-image-library.d.ts +7 -3
  34. package/lib/typescript/commonjs/src/get-image-library.d.ts.map +1 -1
  35. package/lib/typescript/commonjs/src/get-image-source.d.ts +7 -8
  36. package/lib/typescript/commonjs/src/get-image-source.d.ts.map +1 -1
  37. package/lib/typescript/module/src/create-icon-set.d.ts +9 -5
  38. package/lib/typescript/module/src/create-icon-set.d.ts.map +1 -1
  39. package/lib/typescript/module/src/create-icon-source-cache.d.ts +18 -15
  40. package/lib/typescript/module/src/create-icon-source-cache.d.ts.map +1 -1
  41. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.d.ts.map +1 -1
  42. package/lib/typescript/module/src/dynamicLoading/dynamic-loading-setting.d.ts +21 -2
  43. package/lib/typescript/module/src/dynamicLoading/dynamic-loading-setting.d.ts.map +1 -1
  44. package/lib/typescript/module/src/get-image-library.d.ts +7 -3
  45. package/lib/typescript/module/src/get-image-library.d.ts.map +1 -1
  46. package/lib/typescript/module/src/get-image-source.d.ts +7 -8
  47. package/lib/typescript/module/src/get-image-source.d.ts.map +1 -1
  48. package/package.json +6 -2
  49. package/src/create-icon-set.tsx +41 -26
  50. package/src/create-icon-source-cache.ts +27 -28
  51. package/src/dynamicLoading/dynamic-font-loading.ts +0 -1
  52. package/src/dynamicLoading/dynamic-loading-setting.ts +25 -2
  53. package/src/get-image-library.ts +19 -9
  54. package/src/get-image-source.ts +36 -54
@@ -1,5 +1,9 @@
1
- export declare const ensureGetImageAvailable: () => typeof import("@react-native-vector-icons/get-image") | {
2
- getImageForFont: (fontReference: string, glyph: string, size: number, color: number) => Promise<string>;
3
- getImageForFontSync: () => never;
1
+ import type { ImageOptions } from '@react-native-vector-icons/get-image';
2
+ import type { ImageResult } from './create-icon-source-cache';
3
+ type GetImageAPI = {
4
+ getImageForFont(glyph: string, options: ImageOptions): Promise<ImageResult>;
5
+ getImageForFontSync(glyph: string, options: ImageOptions): ImageResult;
4
6
  };
7
+ export declare const ensureGetImageAvailable: () => GetImageAPI;
8
+ export {};
5
9
  //# sourceMappingURL=get-image-library.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-image-library.d.ts","sourceRoot":"","sources":["../../../../src/get-image-library.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,uBAAuB;qCAQS,MAAM,SAAS,MAAM,QAAQ,MAAM,SAAS,MAAM;;CAgB9F,CAAC"}
1
+ {"version":3,"file":"get-image-library.d.ts","sourceRoot":"","sources":["../../../../src/get-image-library.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AAEzE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAc9D,KAAK,WAAW,GAAG;IACjB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IAC5E,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,WAAW,CAAC;CACxE,CAAC;AAEF,eAAO,MAAM,uBAAuB,QAAO,WAwB1C,CAAC"}
@@ -1,11 +1,10 @@
1
1
  import type { TextStyle } from 'react-native';
2
- import type createIconSourceCache from './create-icon-source-cache';
3
- export declare const getImageSourceSync: (imageSourceCache: ReturnType<typeof createIconSourceCache>, fontReference: string, glyph: string, size?: number, color?: TextStyle["color"]) => {
4
- uri: string;
5
- scale: number;
2
+ import type { createIconSourceCache } from './create-icon-source-cache';
3
+ export type GetImageSourceOptions = {
4
+ size?: number;
5
+ color?: TextStyle['color'];
6
+ lineHeight?: number;
6
7
  };
7
- export declare const getImageSource: (imageSourceCache: ReturnType<typeof createIconSourceCache>, fontReference: string, glyph: string, size?: number, color?: TextStyle["color"]) => Promise<{
8
- uri: string;
9
- scale: number;
10
- }>;
8
+ export declare const getImageSourceSync: (imageSourceCache: ReturnType<typeof createIconSourceCache>, fontReference: string, glyph: string, options?: GetImageSourceOptions) => import("./create-icon-source-cache").ImageResult;
9
+ export declare const getImageSource: (imageSourceCache: ReturnType<typeof createIconSourceCache>, fontReference: string, glyph: string, options?: GetImageSourceOptions) => Promise<import("./create-icon-source-cache").ImageResult>;
11
10
  //# sourceMappingURL=get-image-source.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-image-source.d.ts","sourceRoot":"","sources":["../../../../src/get-image-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAG9C,OAAO,KAAK,qBAAqB,MAAM,4BAA4B,CAAC;AAIpE,eAAO,MAAM,kBAAkB,GAC7B,kBAAkB,UAAU,CAAC,OAAO,qBAAqB,CAAC,EAC1D,eAAe,MAAM,EACrB,OAAO,MAAM,EACb,aAAwB,EACxB,QAAO,SAAS,CAAC,OAAO,CAAsB;;;CA2B/C,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,kBAAkB,UAAU,CAAC,OAAO,qBAAqB,CAAC,EAC1D,eAAe,MAAM,EACrB,OAAO,MAAM,EACb,aAAwB,EACxB,QAAO,SAAS,CAAC,OAAO,CAAsB;;;EA2B/C,CAAC"}
1
+ {"version":3,"file":"get-image-source.d.ts","sourceRoot":"","sources":["../../../../src/get-image-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAIxE,MAAM,MAAM,qBAAqB,GAAG;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAmBF,eAAO,MAAM,kBAAkB,GAC7B,kBAAkB,UAAU,CAAC,OAAO,qBAAqB,CAAC,EAC1D,eAAe,MAAM,EACrB,OAAO,MAAM,EACb,UAAU,qBAAqB,qDAQhC,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,kBAAkB,UAAU,CAAC,OAAO,qBAAqB,CAAC,EAC1D,eAAe,MAAM,EACrB,OAAO,MAAM,EACb,UAAU,qBAAqB,8DAQhC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-vector-icons/common",
3
- "version": "12.4.1",
3
+ "version": "13.0.0",
4
4
  "description": "Customizable Icons for React Native with support for image source and full styling.",
5
5
  "source": "./src/index.ts",
6
6
  "main": "./lib/commonjs/index.js",
@@ -83,11 +83,15 @@
83
83
  "typescript": "^5.7.2"
84
84
  },
85
85
  "peerDependencies": {
86
+ "@react-native/assets-registry": "*",
86
87
  "react": "*",
87
88
  "react-native": "*",
88
- "@react-native-vector-icons/get-image": "^12.3.0"
89
+ "@react-native-vector-icons/get-image": "^13.0.0"
89
90
  },
90
91
  "peerDependenciesMeta": {
92
+ "@react-native/assets-registry": {
93
+ "optional": true
94
+ },
91
95
  "@react-native-vector-icons/get-image": {
92
96
  "optional": true
93
97
  }
@@ -1,22 +1,25 @@
1
- // eslint-disable-next-line import/no-extraneous-dependencies
2
1
  import React, { forwardRef, type Ref, useEffect } from 'react';
3
- // eslint-disable-next-line import/no-extraneous-dependencies
4
2
  import { Platform, Text, type TextProps, type TextStyle } from 'react-native';
5
3
 
6
- import createIconSourceCache from './create-icon-source-cache';
4
+ import { createIconSourceCache, type ImageResult } from './create-icon-source-cache';
7
5
  import { DEFAULT_ICON_COLOR, DEFAULT_ICON_SIZE } from './defaults';
8
6
  import { dynamicLoader } from './dynamicLoading/dynamic-font-loading';
9
7
  import { isDynamicLoadingEnabled } from './dynamicLoading/dynamic-loading-setting';
10
8
  import type { FontSource } from './dynamicLoading/types';
11
- import { getImageSource as getImageSourceImpl, getImageSourceSync as getImageSourceSyncImpl } from './get-image-source';
12
-
13
- type ValueData = { uri: string; scale: number };
14
- type GetImageSourceSyncIconFunc<GM> = (name: GM, size?: number, color?: TextStyle['color']) => ValueData | undefined;
15
- type GetImageSourceIconFunc<GM> = (
16
- name: GM,
17
- size?: number,
18
- color?: TextStyle['color'],
19
- ) => Promise<ValueData | undefined>;
9
+ import {
10
+ type GetImageSourceOptions,
11
+ getImageSource as getImageSourceImpl,
12
+ getImageSourceSync as getImageSourceSyncImpl,
13
+ } from './get-image-source';
14
+
15
+ type GetImageSourceSyncIconFunc<GM> = {
16
+ (name: GM, options: GetImageSourceOptions): ImageResult;
17
+ (name: GM, size?: number, color?: TextStyle['color']): ImageResult;
18
+ };
19
+ type GetImageSourceIconFunc<GM> = {
20
+ (name: GM, options: GetImageSourceOptions): Promise<ImageResult>;
21
+ (name: GM, size?: number, color?: TextStyle['color']): Promise<ImageResult>;
22
+ };
20
23
 
21
24
  export type IconProps<T> = TextProps & {
22
25
  name: T;
@@ -82,6 +85,8 @@ export function createIconSet<GM extends GlyphMap>(
82
85
  fontStyle: 'normal',
83
86
  };
84
87
 
88
+ const fontSource = typeof postScriptNameOrOptions === 'object' && postScriptNameOrOptions?.fontSource;
89
+
85
90
  const resolveGlyph = (name: keyof GM): string => {
86
91
  const glyph = glyphMap[name] || '?';
87
92
 
@@ -102,21 +107,19 @@ export function createIconSet<GM extends GlyphMap>(
102
107
  innerRef,
103
108
  ...props
104
109
  }: IconProps<keyof GM>) => {
110
+ const canUseDynamicLoading = !!fontSource && isDynamicLoadingEnabled();
105
111
  const [isFontLoaded, setIsFontLoaded] = React.useState(
106
- isDynamicLoadingEnabled() ? dynamicLoader.isLoaded(fontReference) : true,
112
+ canUseDynamicLoading ? dynamicLoader.isLoaded(fontReference) : true,
107
113
  );
108
114
  const glyph = isFontLoaded && name ? resolveGlyph(name) : '';
109
115
 
116
+ const shouldLoadFontDynamically = !isFontLoaded && canUseDynamicLoading;
110
117
  // biome-ignore lint/correctness/useExhaustiveDependencies: the dependencies never change
111
118
  useEffect(() => {
112
119
  let isMounted = true;
113
120
 
114
- if (
115
- !isFontLoaded &&
116
- typeof postScriptNameOrOptions === 'object' &&
117
- typeof postScriptNameOrOptions.fontSource !== 'undefined'
118
- ) {
119
- dynamicLoader.loadFontAsync(fontReference, postScriptNameOrOptions.fontSource).finally(() => {
121
+ if (shouldLoadFontDynamically) {
122
+ dynamicLoader.loadFontAsync(fontReference, fontSource).finally(() => {
120
123
  if (isMounted) {
121
124
  setIsFontLoaded(true);
122
125
  }
@@ -134,7 +137,7 @@ export function createIconSet<GM extends GlyphMap>(
134
137
 
135
138
  const newProps: TextProps = {
136
139
  ...props,
137
- style: [styleDefaults, style, styleOverrides, fontStyle || {}],
140
+ style: [styleDefaults, style, styleOverrides, fontStyle],
138
141
  allowFontScaling,
139
142
  };
140
143
 
@@ -153,15 +156,27 @@ export function createIconSet<GM extends GlyphMap>(
153
156
 
154
157
  const imageSourceCache = createIconSourceCache();
155
158
 
156
- const getImageSource: GetImageSourceIconFunc<keyof GM> = async (name, size, color) => {
157
- if (typeof postScriptNameOrOptions === 'object' && typeof postScriptNameOrOptions.fontSource !== 'undefined') {
158
- await dynamicLoader.loadFontAsync(fontReference, postScriptNameOrOptions.fontSource);
159
+ const getImageSource: GetImageSourceIconFunc<keyof GM> = async (
160
+ name: keyof GM,
161
+ sizeOrOptions?: number | GetImageSourceOptions,
162
+ color?: TextStyle['color'],
163
+ ) => {
164
+ const canUseDynamicLoading = !!fontSource && isDynamicLoadingEnabled();
165
+ if (canUseDynamicLoading && !dynamicLoader.isLoaded(fontReference)) {
166
+ await dynamicLoader.loadFontAsync(fontReference, fontSource);
159
167
  }
160
- return getImageSourceImpl(imageSourceCache, fontReference, resolveGlyph(name), size, color);
168
+ const options = typeof sizeOrOptions === 'object' ? sizeOrOptions : { size: sizeOrOptions, color };
169
+ return getImageSourceImpl(imageSourceCache, fontReference, resolveGlyph(name), options);
161
170
  };
162
171
 
163
- const getImageSourceSync: GetImageSourceSyncIconFunc<keyof GM> = (name, size, color) =>
164
- getImageSourceSyncImpl(imageSourceCache, fontReference, resolveGlyph(name), size, color);
172
+ const getImageSourceSync: GetImageSourceSyncIconFunc<keyof GM> = (
173
+ name: keyof GM,
174
+ sizeOrOptions?: number | GetImageSourceOptions,
175
+ color?: TextStyle['color'],
176
+ ) => {
177
+ const options = typeof sizeOrOptions === 'object' ? sizeOrOptions : { size: sizeOrOptions, color };
178
+ return getImageSourceSyncImpl(imageSourceCache, fontReference, resolveGlyph(name), options);
179
+ };
165
180
 
166
181
  const IconNamespace = Object.assign(WrappedIcon, {
167
182
  getImageSource,
@@ -1,29 +1,28 @@
1
- const TYPE_VALUE = 'value';
2
- const TYPE_ERROR = 'error';
3
-
4
- type ValueData = { uri: string; scale: number };
5
-
6
- type Value = { type: typeof TYPE_VALUE; data: ValueData } | { type: typeof TYPE_ERROR; data: Error };
7
-
8
- export default function createIconSourceCache() {
9
- const cache = new Map<string, Value>();
10
-
11
- const setValue = (key: string, value: ValueData) => cache.set(key, { type: TYPE_VALUE, data: value });
12
-
13
- const setError = (key: string, error: Error) => cache.set(key, { type: TYPE_ERROR, data: error });
14
-
15
- const get = (key: string) => {
16
- const value = cache.get(key);
17
- if (!value) {
18
- return undefined;
19
- }
20
-
21
- const { type, data } = value;
22
- if (type === TYPE_ERROR) {
23
- throw data;
24
- }
25
- return data;
26
- };
27
-
28
- return { setValue, setError, get };
1
+ export type ImageResult = {
2
+ /**
3
+ * The file URI to the rendered image.
4
+ */
5
+ uri: string;
6
+ /**
7
+ * Image width in dp.
8
+ */
9
+ width: number;
10
+ /**
11
+ * Image height in dp.
12
+ */
13
+ height: number;
14
+ /**
15
+ * Scale factor of the image. Multiply the dp dimensions by this value to get the dimensions in pixels.
16
+ */
17
+ scale: number;
18
+ };
19
+
20
+ export function createIconSourceCache() {
21
+ const cache = new Map<string, ImageResult>();
22
+
23
+ const setValue = (key: string, value: ImageResult) => cache.set(key, value);
24
+
25
+ const get = (key: string) => cache.get(key);
26
+
27
+ return { setValue, get };
29
28
  }
@@ -5,7 +5,6 @@
5
5
  import { Image, Platform } from 'react-native';
6
6
 
7
7
  // @ts-expect-error missing types
8
- // eslint-disable-next-line import/no-extraneous-dependencies,import/no-unresolved
9
8
  import { getAssetByID } from '@react-native/assets-registry/registry';
10
9
 
11
10
  import { assertExpoModulesPresent, getErrorCallback, type LoadAsyncAsset } from './dynamic-loading-setting';
@@ -18,15 +18,38 @@ type ExpoFontLoaderModule = {
18
18
  loadAsync: (fontFamilyAlias: string, asset: LoadAsyncAsset) => Promise<void>;
19
19
  };
20
20
 
21
+ // RenderToImageResult needs to be usable as the `source` prop for image,
22
+ // so it must stay compatible with ImageURISource type
23
+ type RenderToImageResult = {
24
+ /**
25
+ * The file uri to the image.
26
+ */
27
+ uri: string;
28
+ /**
29
+ * Image width in dp.
30
+ */
31
+ width: number;
32
+ /**
33
+ * Image height in dp.
34
+ */
35
+ height: number;
36
+
37
+ /**
38
+ * Scale factor of the image. Multiply the dp dimensions by this value to get the dimensions in pixels.
39
+ * */
40
+ scale: number;
41
+ };
42
+
21
43
  type ExpoFontUtilsModule = {
22
44
  renderToImageAsync: (
23
45
  glyph: string,
24
46
  options: {
25
- fontFamily?: string;
47
+ fontFamily: string;
26
48
  size?: number;
49
+ lineHeight?: number;
27
50
  color?: number;
28
51
  },
29
- ) => Promise<string>;
52
+ ) => Promise<RenderToImageResult>;
30
53
  };
31
54
 
32
55
  declare global {
@@ -1,8 +1,13 @@
1
+ import { processColor } from 'react-native';
2
+
1
3
  import { getIsRenderToImageSupported } from './dynamicLoading/dynamic-loading-setting';
2
4
 
3
- // eslint-disable-next-line import/no-mutable-exports
4
5
  let NativeIconAPI: typeof import('@react-native-vector-icons/get-image') | null = null;
5
6
 
7
+ import type { ImageOptions } from '@react-native-vector-icons/get-image';
8
+
9
+ import type { ImageResult } from './create-icon-source-cache';
10
+
6
11
  try {
7
12
  // eslint-disable-next-line global-require,import/no-extraneous-dependencies,@typescript-eslint/no-require-imports
8
13
  NativeIconAPI = require('@react-native-vector-icons/get-image');
@@ -15,7 +20,12 @@ try {
15
20
  const globalRef = globalThis;
16
21
  const hasExpoRenderToImage = getIsRenderToImageSupported(globalRef);
17
22
 
18
- export const ensureGetImageAvailable = () => {
23
+ type GetImageAPI = {
24
+ getImageForFont(glyph: string, options: ImageOptions): Promise<ImageResult>;
25
+ getImageForFontSync(glyph: string, options: ImageOptions): ImageResult;
26
+ };
27
+
28
+ export const ensureGetImageAvailable = (): GetImageAPI => {
19
29
  if (NativeIconAPI) {
20
30
  NativeIconAPI.ensureNativeModuleAvailable();
21
31
  return NativeIconAPI;
@@ -23,12 +33,12 @@ export const ensureGetImageAvailable = () => {
23
33
  if (hasExpoRenderToImage) {
24
34
  const { ExpoFontUtils } = globalRef.expo.modules;
25
35
  return {
26
- getImageForFont: async (fontReference: string, glyph: string, size: number, color: number) =>
27
- ExpoFontUtils.renderToImageAsync(glyph, {
28
- fontFamily: fontReference,
29
- size,
30
- color,
31
- }),
36
+ getImageForFont: async (glyph, options) => {
37
+ return await ExpoFontUtils.renderToImageAsync(glyph, {
38
+ ...options,
39
+ color: processColor(options.color ?? 'black') as number,
40
+ });
41
+ },
32
42
  getImageForFontSync: () => {
33
43
  throw new Error(
34
44
  'You attempted to call `getImageForFontSync`. Expo dev client with `@react-native-vector-icons/get-image` installed is required for this. Alternatively, call `getImageForFont` or generate the image yourself and bundle it with the app.',
@@ -37,6 +47,6 @@ export const ensureGetImageAvailable = () => {
37
47
  };
38
48
  }
39
49
  throw new Error(
40
- 'Error in getImageSource / getImageSourceSync: You need to either (1) install `@react-native-vector-icons/get-image` or (2) use Expo SDK 53+ (Expo dev client or Expo Go). Check your setup and rebuild the app.',
50
+ 'Error in getImageSource / getImageSourceSync: You need to either (1) install `@react-native-vector-icons/get-image` or (2) use Expo SDK 54+ (Expo dev client or Expo Go). Check your setup and rebuild the app.',
41
51
  );
42
52
  };
@@ -1,74 +1,56 @@
1
1
  import type { TextStyle } from 'react-native';
2
- import { PixelRatio, processColor } from 'react-native';
3
2
 
4
- import type createIconSourceCache from './create-icon-source-cache';
3
+ import type { createIconSourceCache } from './create-icon-source-cache';
5
4
  import { DEFAULT_ICON_COLOR, DEFAULT_ICON_SIZE } from './defaults';
6
5
  import { ensureGetImageAvailable } from './get-image-library';
7
6
 
8
- export const getImageSourceSync = (
7
+ export type GetImageSourceOptions = {
8
+ size?: number;
9
+ color?: TextStyle['color'];
10
+ lineHeight?: number;
11
+ };
12
+
13
+ const resolveOptions = (
9
14
  imageSourceCache: ReturnType<typeof createIconSourceCache>,
10
15
  fontReference: string,
11
16
  glyph: string,
12
- size = DEFAULT_ICON_SIZE,
13
- color: TextStyle['color'] = DEFAULT_ICON_COLOR,
17
+ { size = DEFAULT_ICON_SIZE, color = DEFAULT_ICON_COLOR, lineHeight }: GetImageSourceOptions = {},
14
18
  ) => {
15
- const NativeIconAPI = ensureGetImageAvailable();
16
-
17
- const processedColor = processColor(color);
18
- const cacheKey = `${glyph}:${size}:${String(processedColor)}`;
19
+ const cacheKey = `${glyph}:${size}:${String(color)}:${lineHeight ?? ''}`;
20
+ const cached = imageSourceCache.get(cacheKey);
21
+ const nativeOptions = {
22
+ fontFamily: fontReference,
23
+ size,
24
+ color: color as string,
25
+ lineHeight,
26
+ };
27
+ return { cacheKey, cached, nativeOptions };
28
+ };
19
29
 
20
- const maybeCachedValue = imageSourceCache.get(cacheKey);
21
- if (maybeCachedValue !== undefined) {
22
- // FIXME: Should this check if it's an error and throw it again?
23
- return maybeCachedValue;
24
- }
30
+ export const getImageSourceSync = (
31
+ imageSourceCache: ReturnType<typeof createIconSourceCache>,
32
+ fontReference: string,
33
+ glyph: string,
34
+ options?: GetImageSourceOptions,
35
+ ) => {
36
+ const { cacheKey, cached, nativeOptions } = resolveOptions(imageSourceCache, fontReference, glyph, options);
37
+ if (cached !== undefined) return cached;
25
38
 
26
- try {
27
- const imagePath = NativeIconAPI.getImageForFontSync(
28
- fontReference,
29
- glyph,
30
- size,
31
- processedColor as number, // FIXME what if a non existent colour was passed in?
32
- );
33
- const value = { uri: imagePath, scale: PixelRatio.get() };
34
- imageSourceCache.setValue(cacheKey, value);
35
- return value;
36
- } catch (error) {
37
- imageSourceCache.setError(cacheKey, error as Error);
38
- throw error;
39
- }
39
+ const value = ensureGetImageAvailable().getImageForFontSync(glyph, nativeOptions);
40
+ imageSourceCache.setValue(cacheKey, value);
41
+ return value;
40
42
  };
41
43
 
42
44
  export const getImageSource = async (
43
45
  imageSourceCache: ReturnType<typeof createIconSourceCache>,
44
46
  fontReference: string,
45
47
  glyph: string,
46
- size = DEFAULT_ICON_SIZE,
47
- color: TextStyle['color'] = DEFAULT_ICON_COLOR,
48
+ options?: GetImageSourceOptions,
48
49
  ) => {
49
- const NativeIconAPI = ensureGetImageAvailable();
50
-
51
- const processedColor = processColor(color);
52
- const cacheKey = `${glyph}:${size}:${String(processedColor)}`;
53
-
54
- const maybeCachedValue = imageSourceCache.get(cacheKey);
55
- if (maybeCachedValue !== undefined) {
56
- // FIXME: Should this check if it's an error and throw it again?
57
- return maybeCachedValue;
58
- }
50
+ const { cacheKey, cached, nativeOptions } = resolveOptions(imageSourceCache, fontReference, glyph, options);
51
+ if (cached !== undefined) return cached;
59
52
 
60
- try {
61
- const imagePath = await NativeIconAPI.getImageForFont(
62
- fontReference,
63
- glyph,
64
- size,
65
- processedColor as number, // FIXME what if a non existent colour was passed in?
66
- );
67
- const value = { uri: imagePath, scale: PixelRatio.get() };
68
- imageSourceCache.setValue(cacheKey, value);
69
- return value;
70
- } catch (error) {
71
- imageSourceCache.setError(cacheKey, error as Error);
72
- throw error;
73
- }
53
+ const value = await ensureGetImageAvailable().getImageForFont(glyph, nativeOptions);
54
+ imageSourceCache.setValue(cacheKey, value);
55
+ return value;
74
56
  };