@react-native-vector-icons/common 0.0.1-alpha.2 → 0.0.1-alpha.22

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 (172) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +268 -0
  3. package/android/build.gradle +40 -6
  4. package/android/generated/java/com/reactnativevectoricons/common/NativeVectorIconsSpec.java +42 -0
  5. package/android/generated/jni/CMakeLists.txt +49 -0
  6. package/android/generated/jni/RNVectorIconsSpec-generated.cpp +38 -0
  7. package/android/generated/jni/RNVectorIconsSpec.h +31 -0
  8. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/ComponentDescriptors.cpp +22 -0
  9. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/ComponentDescriptors.h +24 -0
  10. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/EventEmitters.cpp +16 -0
  11. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/EventEmitters.h +17 -0
  12. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/Props.cpp +19 -0
  13. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/Props.h +18 -0
  14. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/RNVectorIconsSpecJSI-generated.cpp +40 -0
  15. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/RNVectorIconsSpecJSI.h +76 -0
  16. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/ShadowNodes.cpp +17 -0
  17. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/ShadowNodes.h +23 -0
  18. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/States.cpp +16 -0
  19. package/android/generated/jni/react/renderer/components/RNVectorIconsSpec/States.h +19 -0
  20. package/android/src/main/AndroidManifestNew.xml +1 -1
  21. package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsModule.kt +90 -0
  22. package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsPackage.kt +35 -0
  23. package/android/src/newarch/VectorIconsSpec.kt +7 -0
  24. package/android/src/oldarch/VectorIconsSpec.kt +38 -0
  25. package/ios/VectorIcons.h +13 -17
  26. package/ios/VectorIcons.mm +76 -92
  27. package/ios/generated/RNVectorIconsSpec/ComponentDescriptors.cpp +22 -0
  28. package/ios/generated/RNVectorIconsSpec/ComponentDescriptors.h +24 -0
  29. package/ios/generated/RNVectorIconsSpec/EventEmitters.cpp +16 -0
  30. package/ios/generated/RNVectorIconsSpec/EventEmitters.h +17 -0
  31. package/ios/generated/RNVectorIconsSpec/Props.cpp +19 -0
  32. package/ios/generated/RNVectorIconsSpec/Props.h +18 -0
  33. package/ios/generated/RNVectorIconsSpec/RCTComponentViewHelpers.h +18 -0
  34. package/ios/generated/RNVectorIconsSpec/RNVectorIconsSpec-generated.mm +36 -0
  35. package/ios/generated/RNVectorIconsSpec/RNVectorIconsSpec.h +58 -0
  36. package/ios/generated/RNVectorIconsSpec/ShadowNodes.cpp +17 -0
  37. package/ios/generated/RNVectorIconsSpec/ShadowNodes.h +23 -0
  38. package/ios/generated/RNVectorIconsSpec/States.cpp +16 -0
  39. package/ios/generated/RNVectorIconsSpec/States.h +19 -0
  40. package/ios/generated/RNVectorIconsSpecJSI-generated.cpp +40 -0
  41. package/ios/generated/RNVectorIconsSpecJSI.h +76 -0
  42. package/lib/commonjs/NativeVectorIcons.js.map +1 -1
  43. package/lib/commonjs/NativeVectorIcons.web.js.map +1 -1
  44. package/lib/commonjs/create-icon-set.js +52 -35
  45. package/lib/commonjs/create-icon-set.js.map +1 -1
  46. package/lib/commonjs/create-icon-source-cache.js.map +1 -1
  47. package/lib/commonjs/dynamicLoading/dynamic-font-loading.js +92 -0
  48. package/lib/commonjs/dynamicLoading/dynamic-font-loading.js.map +1 -0
  49. package/lib/commonjs/dynamicLoading/dynamic-font-loading.web.js +20 -0
  50. package/lib/commonjs/dynamicLoading/dynamic-font-loading.web.js.map +1 -0
  51. package/lib/commonjs/dynamicLoading/dynamic-loading-setting.js +50 -0
  52. package/lib/commonjs/dynamicLoading/dynamic-loading-setting.js.map +1 -0
  53. package/lib/commonjs/dynamicLoading/types.js +2 -0
  54. package/lib/commonjs/dynamicLoading/types.js.map +1 -0
  55. package/lib/commonjs/ensure-native-module-available.js +1 -1
  56. package/lib/commonjs/ensure-native-module-available.js.map +1 -1
  57. package/lib/commonjs/index.js +57 -1
  58. package/lib/commonjs/index.js.map +1 -1
  59. package/lib/commonjs/scripts/common.js +49 -0
  60. package/lib/commonjs/scripts/common.js.map +1 -0
  61. package/lib/commonjs/scripts/getFonts.js +6 -40
  62. package/lib/commonjs/scripts/getFonts.js.map +1 -1
  63. package/lib/commonjs/scripts/updatePlist.js +54 -0
  64. package/lib/commonjs/scripts/updatePlist.js.map +1 -0
  65. package/lib/module/NativeVectorIcons.js +2 -0
  66. package/lib/module/NativeVectorIcons.js.map +1 -1
  67. package/lib/module/NativeVectorIcons.web.js +2 -0
  68. package/lib/module/NativeVectorIcons.web.js.map +1 -1
  69. package/lib/module/create-icon-set.js +52 -33
  70. package/lib/module/create-icon-set.js.map +1 -1
  71. package/lib/module/create-icon-source-cache.js +2 -0
  72. package/lib/module/create-icon-source-cache.js.map +1 -1
  73. package/lib/module/dynamicLoading/dynamic-font-loading.js +85 -0
  74. package/lib/module/dynamicLoading/dynamic-font-loading.js.map +1 -0
  75. package/lib/module/dynamicLoading/dynamic-font-loading.web.js +16 -0
  76. package/lib/module/dynamicLoading/dynamic-font-loading.web.js.map +1 -0
  77. package/lib/module/dynamicLoading/dynamic-loading-setting.js +41 -0
  78. package/lib/module/dynamicLoading/dynamic-loading-setting.js.map +1 -0
  79. package/lib/module/dynamicLoading/types.js +2 -0
  80. package/lib/module/dynamicLoading/types.js.map +1 -0
  81. package/lib/module/ensure-native-module-available.js +2 -0
  82. package/lib/module/ensure-native-module-available.js.map +1 -1
  83. package/lib/module/index.js +33 -1
  84. package/lib/module/index.js.map +1 -1
  85. package/lib/module/scripts/common.js +43 -0
  86. package/lib/module/scripts/common.js.map +1 -0
  87. package/lib/module/scripts/getFonts.js +8 -39
  88. package/lib/module/scripts/getFonts.js.map +1 -1
  89. package/lib/module/scripts/updatePlist.js +51 -0
  90. package/lib/module/scripts/updatePlist.js.map +1 -0
  91. package/lib/typescript/commonjs/package.json +1 -0
  92. package/lib/typescript/commonjs/src/NativeVectorIcons.d.ts +8 -0
  93. package/lib/typescript/commonjs/src/NativeVectorIcons.d.ts.map +1 -0
  94. package/lib/typescript/commonjs/src/NativeVectorIcons.web.d.ts.map +1 -0
  95. package/lib/typescript/commonjs/src/create-icon-set.d.ts +36 -0
  96. package/lib/typescript/commonjs/src/create-icon-set.d.ts.map +1 -0
  97. package/lib/typescript/commonjs/src/create-icon-source-cache.d.ts.map +1 -0
  98. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.d.ts +3 -0
  99. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.d.ts.map +1 -0
  100. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.web.d.ts +6 -0
  101. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.web.d.ts.map +1 -0
  102. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-loading-setting.d.ts +41 -0
  103. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-loading-setting.d.ts.map +1 -0
  104. package/lib/typescript/commonjs/src/dynamicLoading/types.d.ts +9 -0
  105. package/lib/typescript/commonjs/src/dynamicLoading/types.d.ts.map +1 -0
  106. package/lib/typescript/commonjs/src/ensure-native-module-available.d.ts.map +1 -0
  107. package/lib/typescript/commonjs/src/index.d.ts +6 -0
  108. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  109. package/lib/typescript/commonjs/src/scripts/common.d.ts +2 -0
  110. package/lib/typescript/commonjs/src/scripts/common.d.ts.map +1 -0
  111. package/lib/typescript/commonjs/src/scripts/getFonts.d.ts.map +1 -0
  112. package/lib/typescript/commonjs/src/scripts/updatePlist.d.ts +3 -0
  113. package/lib/typescript/commonjs/src/scripts/updatePlist.d.ts.map +1 -0
  114. package/lib/typescript/module/package.json +1 -0
  115. package/lib/typescript/module/src/NativeVectorIcons.d.ts +8 -0
  116. package/lib/typescript/module/src/NativeVectorIcons.d.ts.map +1 -0
  117. package/lib/typescript/module/src/NativeVectorIcons.web.d.ts +3 -0
  118. package/lib/typescript/module/src/NativeVectorIcons.web.d.ts.map +1 -0
  119. package/lib/typescript/module/src/create-icon-set.d.ts +36 -0
  120. package/lib/typescript/module/src/create-icon-set.d.ts.map +1 -0
  121. package/lib/typescript/module/src/create-icon-source-cache.d.ts +21 -0
  122. package/lib/typescript/module/src/create-icon-source-cache.d.ts.map +1 -0
  123. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.d.ts +3 -0
  124. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.d.ts.map +1 -0
  125. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.web.d.ts +6 -0
  126. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.web.d.ts.map +1 -0
  127. package/lib/typescript/module/src/dynamicLoading/dynamic-loading-setting.d.ts +41 -0
  128. package/lib/typescript/module/src/dynamicLoading/dynamic-loading-setting.d.ts.map +1 -0
  129. package/lib/typescript/module/src/dynamicLoading/types.d.ts +9 -0
  130. package/lib/typescript/module/src/dynamicLoading/types.d.ts.map +1 -0
  131. package/lib/typescript/module/src/ensure-native-module-available.d.ts +2 -0
  132. package/lib/typescript/module/src/ensure-native-module-available.d.ts.map +1 -0
  133. package/lib/typescript/module/src/index.d.ts +6 -0
  134. package/lib/typescript/module/src/index.d.ts.map +1 -0
  135. package/lib/typescript/module/src/scripts/common.d.ts +2 -0
  136. package/lib/typescript/module/src/scripts/common.d.ts.map +1 -0
  137. package/lib/typescript/module/src/scripts/getFonts.d.ts +3 -0
  138. package/lib/typescript/module/src/scripts/getFonts.d.ts.map +1 -0
  139. package/lib/typescript/module/src/scripts/updatePlist.d.ts +3 -0
  140. package/lib/typescript/module/src/scripts/updatePlist.d.ts.map +1 -0
  141. package/package.json +72 -81
  142. package/react-native-vector-icons.podspec +79 -35
  143. package/src/NativeVectorIcons.ts +2 -14
  144. package/src/create-icon-set.tsx +97 -54
  145. package/src/create-icon-source-cache.ts +3 -7
  146. package/src/dynamicLoading/dynamic-font-loading.ts +97 -0
  147. package/src/dynamicLoading/dynamic-font-loading.web.ts +15 -0
  148. package/src/dynamicLoading/dynamic-loading-setting.ts +77 -0
  149. package/src/dynamicLoading/types.ts +10 -0
  150. package/src/ensure-native-module-available.ts +1 -1
  151. package/src/index.ts +48 -1
  152. package/src/scripts/common.ts +52 -0
  153. package/src/scripts/getFonts.ts +6 -47
  154. package/src/scripts/updatePlist.ts +58 -0
  155. package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsModule.java +0 -121
  156. package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsPackage.java +0 -45
  157. package/android/src/newarch/VectorIconsSpec.java +0 -9
  158. package/android/src/oldarch/VectorIconsSpec.java +0 -18
  159. package/lib/typescript/src/NativeVectorIcons.d.ts +0 -9
  160. package/lib/typescript/src/NativeVectorIcons.d.ts.map +0 -1
  161. package/lib/typescript/src/NativeVectorIcons.web.d.ts.map +0 -1
  162. package/lib/typescript/src/create-icon-set.d.ts +0 -26
  163. package/lib/typescript/src/create-icon-set.d.ts.map +0 -1
  164. package/lib/typescript/src/create-icon-source-cache.d.ts.map +0 -1
  165. package/lib/typescript/src/ensure-native-module-available.d.ts.map +0 -1
  166. package/lib/typescript/src/index.d.ts +0 -3
  167. package/lib/typescript/src/index.d.ts.map +0 -1
  168. package/lib/typescript/src/scripts/getFonts.d.ts.map +0 -1
  169. /package/lib/typescript/{src → commonjs/src}/NativeVectorIcons.web.d.ts +0 -0
  170. /package/lib/typescript/{src → commonjs/src}/create-icon-source-cache.d.ts +0 -0
  171. /package/lib/typescript/{src → commonjs/src}/ensure-native-module-available.d.ts +0 -0
  172. /package/lib/typescript/{src → commonjs/src}/scripts/getFonts.d.ts +0 -0
@@ -1,21 +1,25 @@
1
- import React, { forwardRef, type Ref } from 'react';
1
+ import React, { forwardRef, type Ref, useEffect } from 'react';
2
2
 
3
- import {
4
- Platform,
5
- PixelRatio,
6
- processColor,
7
- Text,
8
- type TextProps,
9
- type TextStyle,
10
- } from 'react-native';
3
+ import { PixelRatio, Platform, Text, type TextProps, type TextStyle, processColor } from 'react-native';
11
4
 
12
- import ensureNativeModuleAvailable from './ensure-native-module-available';
13
- import createIconSourceCache from './create-icon-source-cache';
14
5
  import NativeIconAPI from './NativeVectorIcons';
6
+ import createIconSourceCache from './create-icon-source-cache';
7
+ import { dynamicLoader } from './dynamicLoading/dynamic-font-loading';
8
+ import { isDynamicLoadingEnabled } from './dynamicLoading/dynamic-loading-setting';
9
+ import type { FontSource } from './dynamicLoading/types';
10
+ import ensureNativeModuleAvailable from './ensure-native-module-available';
15
11
 
16
12
  export const DEFAULT_ICON_SIZE = 12;
17
13
  export const DEFAULT_ICON_COLOR = 'black';
18
14
 
15
+ type ValueData = { uri: string; scale: number };
16
+ type GetImageSourceSyncIconFunc<GM> = (name: GM, size?: number, color?: TextStyle['color']) => ValueData | undefined;
17
+ type GetImageSourceIconFunc<GM> = (
18
+ name: GM,
19
+ size?: number,
20
+ color?: TextStyle['color'],
21
+ ) => Promise<ValueData | undefined>;
22
+
19
23
  export type IconProps<T> = TextProps & {
20
24
  name: T;
21
25
  size?: number;
@@ -23,22 +27,53 @@ export type IconProps<T> = TextProps & {
23
27
  innerRef?: Ref<Text>;
24
28
  };
25
29
 
26
- export const createIconSet = <GM extends Record<string, number>>(
30
+ type IconComponent<GM extends Record<string, number>> = React.FC<
31
+ TextProps & {
32
+ name: keyof GM;
33
+ size?: number;
34
+ color?: TextStyle['color'];
35
+ innerRef?: Ref<Text>;
36
+ } & React.RefAttributes<Text>
37
+ > & {
38
+ getImageSource: GetImageSourceIconFunc<keyof GM>;
39
+ getImageSourceSync: GetImageSourceSyncIconFunc<keyof GM>;
40
+ };
41
+
42
+ export type CreateIconSetOptions = {
43
+ postScriptName: string;
44
+ fontFileName: string;
45
+ fontSource?: FontSource;
46
+ fontStyle?: TextProps['style'];
47
+ };
48
+
49
+ export function createIconSet<GM extends Record<string, number>>(
50
+ glyphMap: GM,
51
+ postScriptName: string,
52
+ fontFileName: string,
53
+ fontStyle?: TextProps['style'],
54
+ ): IconComponent<GM>;
55
+ export function createIconSet<GM extends Record<string, number>>(
27
56
  glyphMap: GM,
28
- fontFamily: string,
29
- fontFile: string,
30
- fontStyle?: TextProps['style']
31
- ) => {
32
- // Android doesn't care about actual fontFamily name, it will only look in fonts folder.
33
- const fontBasename = fontFile
34
- ? fontFile.replace(/\.(otf|ttf)$/, '')
35
- : fontFamily;
57
+ options: CreateIconSetOptions,
58
+ ): IconComponent<GM>;
59
+ export function createIconSet<GM extends Record<string, number>>(
60
+ glyphMap: GM,
61
+ postScriptNameOrOptions: string | CreateIconSetOptions,
62
+ fontFileNameParam?: string,
63
+ fontStyleParam?: TextProps['style'],
64
+ ): IconComponent<GM> {
65
+ const { postScriptName, fontFileName, fontStyle } =
66
+ typeof postScriptNameOrOptions === 'string'
67
+ ? { postScriptName: postScriptNameOrOptions, fontFileName: fontFileNameParam, fontStyle: fontStyleParam }
68
+ : postScriptNameOrOptions;
69
+
70
+ const fontBasename = fontFileName ? fontFileName.replace(/\.(otf|ttf)$/, '') : postScriptName;
36
71
 
37
72
  const fontReference = Platform.select({
38
- windows: `/Assets/${fontFile}#${fontFamily}`,
73
+ windows: `/Assets/${fontFileName}#${postScriptName}`,
39
74
  android: fontBasename,
40
75
  web: fontBasename,
41
- default: fontFamily,
76
+ default: postScriptName,
42
77
  });
43
78
 
44
79
  const resolveGlyph = (name: keyof GM) => {
@@ -54,14 +89,37 @@ export const createIconSet = <GM extends Record<string, number>>(
54
89
  const Icon = ({
55
90
  name,
56
91
  size = DEFAULT_ICON_SIZE,
57
- color,
92
+ color = DEFAULT_ICON_COLOR,
58
93
  style,
59
94
  children,
60
95
  allowFontScaling = false,
61
96
  innerRef,
62
97
  ...props
63
98
  }: IconProps<keyof GM>) => {
64
- const glyph = name ? resolveGlyph(name as string) : '';
99
+ const [isFontLoaded, setIsFontLoaded] = React.useState(
100
+ isDynamicLoadingEnabled() ? dynamicLoader.isLoaded(fontReference) : true,
101
+ );
102
+ const glyph = isFontLoaded && name ? resolveGlyph(name) : '';
103
+
104
+ // biome-ignore lint/correctness/useExhaustiveDependencies: the dependencies never change
105
+ useEffect(() => {
106
+ let isMounted = true;
107
+
108
+ if (
109
+ !isFontLoaded &&
110
+ typeof postScriptNameOrOptions === 'object' &&
111
+ typeof postScriptNameOrOptions.fontSource !== 'undefined'
112
+ ) {
113
+ dynamicLoader.loadFontAsync(fontReference, postScriptNameOrOptions.fontSource).finally(() => {
114
+ if (isMounted) {
115
+ setIsFontLoaded(true);
116
+ }
117
+ });
118
+ }
119
+ return () => {
120
+ isMounted = false;
121
+ };
122
+ }, []);
65
123
 
66
124
  const styleDefaults = {
67
125
  fontSize: size,
@@ -88,7 +146,9 @@ export const createIconSet = <GM extends Record<string, number>>(
88
146
  );
89
147
  };
90
148
 
91
- const WrappedIcon = forwardRef<Text, IconProps<keyof typeof glyphMap>>((props, ref) => <Icon innerRef={ref} {...props} />);
149
+ const WrappedIcon = forwardRef<Text, IconProps<keyof typeof glyphMap>>((props, ref) => (
150
+ <Icon innerRef={ref} {...props} />
151
+ ));
92
152
  WrappedIcon.displayName = 'Icon';
93
153
 
94
154
  const imageSourceCache = createIconSourceCache();
@@ -96,7 +156,7 @@ export const createIconSet = <GM extends Record<string, number>>(
96
156
  const getImageSourceSync = (
97
157
  name: keyof GM,
98
158
  size = DEFAULT_ICON_SIZE,
99
- color: TextStyle['color'] = DEFAULT_ICON_COLOR
159
+ color: TextStyle['color'] = DEFAULT_ICON_COLOR,
100
160
  ) => {
101
161
  ensureNativeModuleAvailable();
102
162
 
@@ -110,13 +170,12 @@ export const createIconSet = <GM extends Record<string, number>>(
110
170
  }
111
171
 
112
172
  try {
113
- const imagePath =
114
- NativeIconAPI.getImageForFontSync(
115
- fontReference,
116
- glyph,
117
- size,
118
- processedColor as number // FIXME what if a non existant colour was passed in?
119
- );
173
+ const imagePath = NativeIconAPI.getImageForFontSync(
174
+ fontReference,
175
+ glyph,
176
+ size,
177
+ processedColor as number, // FIXME what if a non existant colour was passed in?
178
+ );
120
179
  const value = { uri: imagePath, scale: PixelRatio.get() };
121
180
  imageSourceCache.setValue(cacheKey, value);
122
181
  return value;
@@ -129,7 +188,7 @@ export const createIconSet = <GM extends Record<string, number>>(
129
188
  const getImageSource = async (
130
189
  name: keyof GM,
131
190
  size = DEFAULT_ICON_SIZE,
132
- color: TextStyle['color'] = DEFAULT_ICON_COLOR
191
+ color: TextStyle['color'] = DEFAULT_ICON_COLOR,
133
192
  ) => {
134
193
  ensureNativeModuleAvailable();
135
194
 
@@ -147,7 +206,7 @@ export const createIconSet = <GM extends Record<string, number>>(
147
206
  fontReference,
148
207
  glyph,
149
208
  size,
150
- processedColor as number // FIXME what if a non existant colour was passed in?
209
+ processedColor as number, // FIXME what if a non existant colour was passed in?
151
210
  );
152
211
  const value = { uri: imagePath, scale: PixelRatio.get() };
153
212
  imageSourceCache.setValue(cacheKey, value);
@@ -158,26 +217,10 @@ export const createIconSet = <GM extends Record<string, number>>(
158
217
  }
159
218
  };
160
219
 
161
- const loadFont = async () => {
162
- if (Platform.OS !== 'ios') {
163
- return;
164
- }
165
- ensureNativeModuleAvailable();
166
-
167
- const [filename, extension] = fontFile.split('.'); // FIXME: what if filename has two dots?
168
- if (!extension) {
169
- throw new Error('Font needs a filename extensison.');
170
- }
171
-
172
- await NativeIconAPI.loadFontWithFileName(filename!, extension, 'react-native-vector-icons');
173
- };
174
-
175
- loadFont();
176
-
177
220
  const IconNamespace = Object.assign(WrappedIcon, {
178
- getImageSource: getImageSource,
179
- getImageSourceSync: getImageSourceSync,
221
+ getImageSource,
222
+ getImageSourceSync,
180
223
  });
181
224
 
182
225
  return IconNamespace;
183
- };
226
+ }
@@ -3,18 +3,14 @@ const TYPE_ERROR = 'error';
3
3
 
4
4
  type ValueData = { uri: string; scale: number };
5
5
 
6
- type Value =
7
- | { type: typeof TYPE_VALUE; data: ValueData }
8
- | { type: typeof TYPE_ERROR; data: Error };
6
+ type Value = { type: typeof TYPE_VALUE; data: ValueData } | { type: typeof TYPE_ERROR; data: Error };
9
7
 
10
8
  export default function createIconSourceCache() {
11
9
  const cache = new Map<string, Value>();
12
10
 
13
- const setValue = (key: string, value: ValueData) =>
14
- cache.set(key, { type: TYPE_VALUE, data: value });
11
+ const setValue = (key: string, value: ValueData) => cache.set(key, { type: TYPE_VALUE, data: value });
15
12
 
16
- const setError = (key: string, error: Error) =>
17
- cache.set(key, { type: TYPE_ERROR, data: error });
13
+ const setError = (key: string, error: Error) => cache.set(key, { type: TYPE_ERROR, data: error });
18
14
 
19
15
  const has = (key: string) => cache.has(key);
20
16
 
@@ -0,0 +1,97 @@
1
+ /*
2
+ * The following imports are always present when react native is installed
3
+ * in the future, more explicit apis will be exposed by the core, including typings
4
+ * */
5
+ // @ts-expect-error missing types
6
+ // eslint-disable-next-line import/no-extraneous-dependencies
7
+ import { getAssetByID } from '@react-native/assets-registry/registry';
8
+ import type { Image } from 'react-native';
9
+ // @ts-expect-error missing types
10
+ import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
11
+ import { getErrorCallback } from './dynamic-loading-setting';
12
+ import type { DynamicLoader, FontSource } from './types';
13
+
14
+ const loadPromises: { [fontSource: string]: Promise<void> } = {};
15
+
16
+ const loadFontAsync = async (fontFamily: string, fontSource: FontSource): Promise<void> => {
17
+ const expoModules = globalThis?.expo?.modules;
18
+ if (!expoModules) {
19
+ throw new Error('Expo is not available. Dynamic font loading is not available.');
20
+ }
21
+
22
+ if (loadPromises[fontFamily]) {
23
+ return loadPromises[fontFamily];
24
+ }
25
+
26
+ loadPromises[fontFamily] = (async function LoadFont() {
27
+ try {
28
+ const localUri = await (() => {
29
+ if (typeof fontSource === 'string') {
30
+ // a local filesystem uri
31
+ return fontSource;
32
+ }
33
+ // a module id
34
+ const { uri, type, hash } = getLocalFontUrl(fontSource, fontFamily);
35
+ return expoModules.ExpoAsset.downloadAsync(uri, hash, type);
36
+ })();
37
+
38
+ await expoModules.ExpoFontLoader.loadAsync(fontFamily, localUri);
39
+ } catch (error) {
40
+ console.error(`Failed to load font ${fontFamily}`, error); // eslint-disable-line no-console
41
+
42
+ getErrorCallback()?.({
43
+ error: error as Error,
44
+ fontFamily,
45
+ fontSource,
46
+ });
47
+ } finally {
48
+ delete loadPromises[fontFamily];
49
+ }
50
+ })();
51
+
52
+ return loadPromises[fontFamily];
53
+ };
54
+
55
+ type AssetRegistryEntry = {
56
+ name: string;
57
+ httpServerLocation: string;
58
+ hash: string;
59
+ type: string; // file extension
60
+ };
61
+
62
+ const getLocalFontUrl = (fontModuleId: number, fontFamily: string) => {
63
+ const assetMeta: AssetRegistryEntry = getAssetByID(fontModuleId);
64
+ if (!assetMeta) {
65
+ throw new Error(`no asset found for font family "${fontFamily}", moduleId: ${String(fontModuleId)}`);
66
+ }
67
+
68
+ const resolver: typeof Image.resolveAssetSource = resolveAssetSource;
69
+ const assetSource = resolver(fontModuleId);
70
+
71
+ return { ...assetMeta, ...assetSource };
72
+ };
73
+
74
+ const loadedFontsCache: { [name: string]: boolean } = {};
75
+
76
+ const isLoadedNative = (fontFamily: string) => {
77
+ if (fontFamily in loadedFontsCache) {
78
+ return true;
79
+ }
80
+
81
+ const { expo } = globalThis;
82
+ if (!expo) {
83
+ throw new Error('Expo is not available. Dynamic font loading is not available.');
84
+ }
85
+
86
+ const loadedNativeFonts = expo.modules.ExpoFontLoader.getLoadedFonts();
87
+ loadedNativeFonts.forEach((font) => {
88
+ loadedFontsCache[font] = true;
89
+ });
90
+
91
+ return fontFamily in loadedFontsCache;
92
+ };
93
+
94
+ export const dynamicLoader: DynamicLoader = {
95
+ isLoaded: isLoadedNative,
96
+ loadFontAsync,
97
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * dynamic font loading isn't supported on web
3
+ * */
4
+ import type { DynamicLoader, FontSource } from './types';
5
+
6
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
7
+ const loadFontAsync = async (_fontFamily: string, _fontSource: FontSource): Promise<void> => undefined;
8
+
9
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
+ const isLoaded = (_fontFamily: string) => true;
11
+
12
+ export const dynamicLoader: DynamicLoader = {
13
+ isLoaded,
14
+ loadFontAsync,
15
+ };
@@ -0,0 +1,77 @@
1
+ import type { FontSource } from './types';
2
+
3
+ declare global {
4
+ interface ExpoGlobal {
5
+ modules: {
6
+ ExpoAsset: {
7
+ // definition from
8
+ // https://github.com/expo/expo/blob/1f5a5991d14aad09282d1ce1612b44d30e7e7d3d/packages/expo-asset/ios/AssetModule.swift#L23
9
+ downloadAsync: (uri: string, hash: string | undefined, type: string) => Promise<string>;
10
+ };
11
+ ExpoFontLoader: {
12
+ // definition from
13
+ // https://github.com/expo/expo/blob/1f5a5991d14aad09282d1ce1612b44d30e7e7d3d/packages/expo-font/ios/FontLoaderModule.swift#L18
14
+ getLoadedFonts: () => string[];
15
+ loadAsync: (fontFamilyAlias: string, fileUri: string) => Promise<void>;
16
+ };
17
+ };
18
+ }
19
+
20
+ // eslint-disable-next-line vars-on-top
21
+ var expo: ExpoGlobal | undefined;
22
+ }
23
+
24
+ const hasNecessaryExpoModules = !!globalThis.expo?.modules?.ExpoAsset && !!globalThis.expo?.modules?.ExpoFontLoader;
25
+
26
+ const hasNecessaryExpoFeatures =
27
+ hasNecessaryExpoModules && typeof globalThis.expo?.modules.ExpoFontLoader.getLoadedFonts === 'function';
28
+
29
+ let dynamicFontLoadingEnabled = hasNecessaryExpoFeatures;
30
+
31
+ export const isDynamicLoadingSupported = () => hasNecessaryExpoFeatures;
32
+
33
+ /**
34
+ * Set whether dynamic loading of fonts is enabled.
35
+ * Currently, the presence of Expo Asset and Font Loader modules is a prerequisite for enabling.
36
+ * In the future, React Native core apis will be used for dynamic font loading.
37
+ *
38
+ * @param value - whether dynamic loading of fonts is enabled
39
+ * @returns `true` if dynamic loading of fonts was successfully set. `false` otherwise.
40
+ * */
41
+ export const setDynamicLoadingEnabled = (value: boolean): boolean => {
42
+ if (!hasNecessaryExpoFeatures) {
43
+ if (process.env.NODE_ENV !== 'production' && !!value) {
44
+ const message = hasNecessaryExpoModules
45
+ ? 'Expo is installed, but does not support dynamic font loading. Make sure to use Expo SDK 52 or newer.'
46
+ : 'Necessary Expo modules not found. Dynamic font loading is not available on Web or when necessary Expo modules are not present.';
47
+ console.error(message); // eslint-disable-line no-console
48
+ }
49
+ return false;
50
+ }
51
+
52
+ dynamicFontLoadingEnabled = !!value;
53
+
54
+ return true;
55
+ };
56
+
57
+ /**
58
+ * Whether dynamic loading of fonts is enabled.
59
+ * */
60
+ export const isDynamicLoadingEnabled = () => dynamicFontLoadingEnabled;
61
+
62
+ type ErrorCallback = (args: {
63
+ error: Error;
64
+ fontFamily: string;
65
+ fontSource: FontSource;
66
+ }) => void;
67
+
68
+ let dynamicLoadingErrorCallback: undefined | ErrorCallback;
69
+
70
+ /**
71
+ * Set a callback to be called when an error occurs during dynamic font loading.
72
+ * */
73
+ export const setDynamicLoadingErrorCallback = (callback: ErrorCallback) => {
74
+ dynamicLoadingErrorCallback = callback;
75
+ };
76
+
77
+ export const getErrorCallback = () => dynamicLoadingErrorCallback;
@@ -0,0 +1,10 @@
1
+ // the result of calling require('path')
2
+ type ModuleId = number;
3
+ type LocalFileSystemUri = string;
4
+
5
+ export type FontSource = ModuleId | LocalFileSystemUri;
6
+
7
+ export type DynamicLoader = {
8
+ loadFontAsync: (fontFamily: string, fontSource: FontSource) => Promise<void>;
9
+ isLoaded: (fontFamily: string) => boolean;
10
+ };
@@ -3,7 +3,7 @@ import NativeIconAPI from './NativeVectorIcons';
3
3
  export default function ensureNativeModuleAvailable() {
4
4
  if (!NativeIconAPI) {
5
5
  throw new Error(
6
- 'The native RNVectorIcons API is not available, did you properly integrate the module? Please verify your autolinking setup and recompile.'
6
+ 'The native RNVectorIcons API is not available, did you properly integrate the module? Please verify your autolinking setup and recompile.',
7
7
  );
8
8
  }
9
9
  }
package/src/index.ts CHANGED
@@ -1,2 +1,49 @@
1
+ import { NativeModules, Platform } from 'react-native';
2
+
1
3
  export { createIconSet, DEFAULT_ICON_SIZE, DEFAULT_ICON_COLOR } from './create-icon-set';
2
- export type { IconProps } from './create-icon-set';
4
+ export type { IconProps, CreateIconSetOptions } from './create-icon-set';
5
+ export {
6
+ setDynamicLoadingEnabled,
7
+ isDynamicLoadingEnabled,
8
+ isDynamicLoadingSupported,
9
+ setDynamicLoadingErrorCallback,
10
+ } from './dynamicLoading/dynamic-loading-setting';
11
+
12
+ const LINKING_ERROR = `
13
+ The package '@react-native-vector-icons/common' doesn't seem to be linked. Make sure:
14
+ ${Platform.select({ ios: "- You have run 'pod install'\n", default: '' })}
15
+ - You rebuilt the app after installing the package\n'
16
+ - You are not using Expo Go
17
+ `;
18
+
19
+ // @ts-expect-error : NativeModules type is incomplete
20
+ const isTurboModuleEnabled = global.__turboModuleProxy != null; // eslint-disable-line no-underscore-dangle
21
+
22
+ const VectorIconsModule = isTurboModuleEnabled
23
+ ? require('./NativeVectorIcons').default // eslint-disable-line @typescript-eslint/no-require-imports
24
+ : NativeModules.VectorIcons;
25
+
26
+ const VectorIcons = VectorIconsModule
27
+ ? VectorIconsModule
28
+ : new Proxy(
29
+ {},
30
+ {
31
+ get() {
32
+ throw new Error(LINKING_ERROR);
33
+ },
34
+ },
35
+ );
36
+
37
+ // TODO: Do we want to expose this as a public API?
38
+ export function getImageForFont(
39
+ fontFamilyName: string,
40
+ glyph: string,
41
+ fontSize: number,
42
+ color: number,
43
+ ): Promise<string> {
44
+ return VectorIcons.getImageForFont(fontFamilyName, glyph, fontSize, color);
45
+ }
46
+
47
+ export function getImageForFontSync(fontFamilyName: string, glyph: string, fontSize: number, color: number): string {
48
+ return VectorIcons.getImageForFontSync(fontFamilyName, glyph, fontSize, color);
49
+ }
@@ -0,0 +1,52 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { resolveNodeModuleDir } from '@react-native-community/cli-tools';
4
+
5
+ const getPackageJson = (filename: string) => {
6
+ const packageData = fs.readFileSync(filename, 'utf-8');
7
+ const packageJson = JSON.parse(packageData);
8
+
9
+ return packageJson;
10
+ };
11
+
12
+ const getPackageFontDirectories = (packageJsonFilename: string) => {
13
+ const rootPackageJson = getPackageJson(packageJsonFilename);
14
+ const dependencies = Object.keys(rootPackageJson.dependencies || {});
15
+
16
+ const packageDirs: string[] = [];
17
+ dependencies.forEach((dependency) => {
18
+ const dir = resolveNodeModuleDir(packageJsonFilename, dependency);
19
+ const packageJson = getPackageJson(`${dir}/package.json`);
20
+ if (packageJson.keywords?.includes?.('react-native-vector-icons-icon')) {
21
+ packageDirs.push(`${dir}/fonts`);
22
+ }
23
+ });
24
+
25
+ return packageDirs;
26
+ };
27
+
28
+ const getLocalFontsDir = (packageJsonFilename: string) => {
29
+ const rootPackageJson = getPackageJson(packageJsonFilename);
30
+ const config = rootPackageJson.reactNativeVectorIcons || {};
31
+
32
+ return `${path.dirname(packageJsonFilename)}/${config.fontDir || 'rnvi-fonts'}`;
33
+ };
34
+
35
+ const getFonts = (fontDir: string) => {
36
+ if (!fs.existsSync(fontDir)) {
37
+ return [];
38
+ }
39
+
40
+ const fonts = fs.readdirSync(fontDir);
41
+ const fontPaths = fonts.map((font) => `${fontDir}/${font}`);
42
+
43
+ return fontPaths;
44
+ };
45
+
46
+ export const getFontPaths = (packageJsonFilename: string) => {
47
+ const packageDirs = getPackageFontDirectories(packageJsonFilename);
48
+ packageDirs.push(getLocalFontsDir(packageJsonFilename));
49
+ const fonts = packageDirs.map(getFonts);
50
+
51
+ return fonts.flat();
52
+ };
@@ -1,52 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import fs from 'node:fs';
4
- import path from 'node:path';
5
- import { resolveNodeModuleDir } from '@react-native-community/cli-tools';
3
+ import { getFontPaths } from './common';
6
4
 
7
- const rootDir = process.argv[2];
8
- if (!rootDir) {
9
- throw new Error('Need rootDir as first argument');
5
+ const packageJsonFilename = process.argv[2];
6
+ if (!packageJsonFilename) {
7
+ throw new Error('Need the path to the roo package.json as the first argument');
10
8
  }
11
9
 
12
- const getPackageJson = (dir: string) => {
13
- const packageData = fs.readFileSync(path.join(dir, 'package.json'), 'utf-8');
14
- const packageJson = JSON.parse(packageData);
15
-
16
- return packageJson;
17
- };
18
-
19
- const getPackages = () => {
20
- const rootPackageJson = getPackageJson(rootDir);
21
- const dependencies = Object.keys(rootPackageJson.dependencies || {});
22
-
23
- const packageDirs: string[] = [];
24
- dependencies.forEach((dependency) => {
25
- const dir = resolveNodeModuleDir(rootDir, dependency);
26
- const packageJson = getPackageJson(dir);
27
- if (packageJson.keywords?.includes?.('react-native-vector-icons-icon')) {
28
- packageDirs.push(dir);
29
- }
30
- });
31
- return packageDirs;
32
- };
33
-
34
- const getFonts = (dir: string) => {
35
- const fontDirs = [`${dir}/fonts`];
36
-
37
- const rootPackageJson = getPackageJson(rootDir);
38
- const config = rootPackageJson.reactNativeVectorIcons || {};
39
- fontDirs.push(`${rootDir}/${config.fontDir || 'rnvi-fonts'}`);
40
-
41
- fontDirs.forEach((fontDir) => {
42
- if (!fs.existsSync(fontDir)) {
43
- return;
44
- }
45
-
46
- const fonts = fs.readdirSync(fontDir);
47
- fonts.forEach((font) => console.log(`${fontDir}/${font}`));
48
- });
49
- };
50
-
51
- const packageDirs = getPackages();
52
- packageDirs.forEach((dir) => getFonts(dir));
10
+ const fonts = getFontPaths(packageJsonFilename);
11
+ fonts.map((font) => console.log(font)); // eslint-disable-line no-console
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+
4
+ import fs from 'node:fs';
5
+ import * as path from 'node:path';
6
+ import pc from 'picocolors';
7
+ import * as plist from 'plist';
8
+
9
+ import { getFontPaths } from './common';
10
+
11
+ const getFontName = (fontPath: string) => path.basename(fontPath);
12
+
13
+ const packageJsonFilename = process.argv[2];
14
+ if (!packageJsonFilename) {
15
+ throw new Error('Need the path to the root package.json as the first argument');
16
+ }
17
+
18
+ const infoPlistFilename = process.argv[3];
19
+ if (!infoPlistFilename) {
20
+ throw new Error('Need the path to the Info.plist as the second argument');
21
+ }
22
+
23
+ const fonts = getFontPaths(packageJsonFilename);
24
+ console.log(`Found ${fonts.length} fonts`);
25
+
26
+ const infoPlistContent = fs.readFileSync(infoPlistFilename, 'utf8');
27
+ const infoPlist = plist.parse(infoPlistContent) as Record<string, string[]>;
28
+
29
+ const plistFonts = new Set(infoPlist.UIAppFonts || []);
30
+ const providedFonts = new Set(fonts.map(getFontName));
31
+
32
+ let hasChanges = false;
33
+
34
+ // Check for missing fonts and add them
35
+ providedFonts.forEach((font) => {
36
+ if (!plistFonts.has(font)) {
37
+ plistFonts.add(font);
38
+ console.log(pc.green(`Added ${font}`));
39
+ hasChanges = true;
40
+ } else {
41
+ console.log(`Existing ${font}`);
42
+ }
43
+ });
44
+
45
+ // Check for extra fonts in Info.plist
46
+ plistFonts.forEach((font) => {
47
+ if (!providedFonts.has(font)) {
48
+ console.log(pc.red(`Extra ${font} (Please remove manually if not needed)`));
49
+ }
50
+ });
51
+
52
+ // Update Info.plist if there were changes
53
+ if (hasChanges) {
54
+ infoPlist.UIAppFonts = Array.from(plistFonts);
55
+ const updatedInfoPlistContent = plist.build(infoPlist).replace(/^ {2}/gm, '').replace(/ {2}/gm, '\t');
56
+
57
+ fs.writeFileSync(infoPlistFilename, updatedInfoPlistContent, 'utf8');
58
+ }