@react-native-vector-icons/common 0.0.1-alpha.8 → 11.0.0-alpha.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 (169) hide show
  1. package/LICENSE +1 -2
  2. package/README.md +49 -91
  3. package/android/build.gradle +37 -3
  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/create-icon-set.js +49 -36
  44. package/lib/commonjs/create-icon-set.js.map +1 -1
  45. package/lib/commonjs/dynamicLoading/dynamic-font-loading.js +92 -0
  46. package/lib/commonjs/dynamicLoading/dynamic-font-loading.js.map +1 -0
  47. package/lib/commonjs/dynamicLoading/dynamic-font-loading.web.js +20 -0
  48. package/lib/commonjs/dynamicLoading/dynamic-font-loading.web.js.map +1 -0
  49. package/lib/commonjs/dynamicLoading/dynamic-loading-setting.js +50 -0
  50. package/lib/commonjs/dynamicLoading/dynamic-loading-setting.js.map +1 -0
  51. package/lib/commonjs/dynamicLoading/types.js +2 -0
  52. package/lib/commonjs/dynamicLoading/types.js.map +1 -0
  53. package/lib/commonjs/ensure-native-module-available.js +1 -1
  54. package/lib/commonjs/ensure-native-module-available.js.map +1 -1
  55. package/lib/commonjs/index.js +57 -1
  56. package/lib/commonjs/index.js.map +1 -1
  57. package/lib/commonjs/scripts/common.js +49 -0
  58. package/lib/commonjs/scripts/common.js.map +1 -0
  59. package/lib/commonjs/scripts/getFonts.js +6 -40
  60. package/lib/commonjs/scripts/getFonts.js.map +1 -1
  61. package/lib/commonjs/scripts/updatePlist.js +54 -0
  62. package/lib/commonjs/scripts/updatePlist.js.map +1 -0
  63. package/lib/module/NativeVectorIcons.js +2 -0
  64. package/lib/module/NativeVectorIcons.js.map +1 -1
  65. package/lib/module/NativeVectorIcons.web.js +2 -0
  66. package/lib/module/NativeVectorIcons.web.js.map +1 -1
  67. package/lib/module/create-icon-set.js +49 -34
  68. package/lib/module/create-icon-set.js.map +1 -1
  69. package/lib/module/create-icon-source-cache.js +2 -0
  70. package/lib/module/create-icon-source-cache.js.map +1 -1
  71. package/lib/module/dynamicLoading/dynamic-font-loading.js +85 -0
  72. package/lib/module/dynamicLoading/dynamic-font-loading.js.map +1 -0
  73. package/lib/module/dynamicLoading/dynamic-font-loading.web.js +16 -0
  74. package/lib/module/dynamicLoading/dynamic-font-loading.web.js.map +1 -0
  75. package/lib/module/dynamicLoading/dynamic-loading-setting.js +41 -0
  76. package/lib/module/dynamicLoading/dynamic-loading-setting.js.map +1 -0
  77. package/lib/module/dynamicLoading/types.js +2 -0
  78. package/lib/module/dynamicLoading/types.js.map +1 -0
  79. package/lib/module/ensure-native-module-available.js +2 -0
  80. package/lib/module/ensure-native-module-available.js.map +1 -1
  81. package/lib/module/index.js +33 -1
  82. package/lib/module/index.js.map +1 -1
  83. package/lib/module/scripts/common.js +43 -0
  84. package/lib/module/scripts/common.js.map +1 -0
  85. package/lib/module/scripts/getFonts.js +8 -39
  86. package/lib/module/scripts/getFonts.js.map +1 -1
  87. package/lib/module/scripts/updatePlist.js +51 -0
  88. package/lib/module/scripts/updatePlist.js.map +1 -0
  89. package/lib/typescript/commonjs/package.json +1 -0
  90. package/lib/typescript/commonjs/src/NativeVectorIcons.d.ts +8 -0
  91. package/lib/typescript/commonjs/src/NativeVectorIcons.d.ts.map +1 -0
  92. package/lib/typescript/commonjs/src/NativeVectorIcons.web.d.ts.map +1 -0
  93. package/lib/typescript/commonjs/src/create-icon-set.d.ts +36 -0
  94. package/lib/typescript/commonjs/src/create-icon-set.d.ts.map +1 -0
  95. package/lib/typescript/commonjs/src/create-icon-source-cache.d.ts.map +1 -0
  96. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.d.ts +3 -0
  97. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.d.ts.map +1 -0
  98. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.web.d.ts +6 -0
  99. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-font-loading.web.d.ts.map +1 -0
  100. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-loading-setting.d.ts +41 -0
  101. package/lib/typescript/commonjs/src/dynamicLoading/dynamic-loading-setting.d.ts.map +1 -0
  102. package/lib/typescript/commonjs/src/dynamicLoading/types.d.ts +9 -0
  103. package/lib/typescript/commonjs/src/dynamicLoading/types.d.ts.map +1 -0
  104. package/lib/typescript/commonjs/src/ensure-native-module-available.d.ts.map +1 -0
  105. package/lib/typescript/commonjs/src/index.d.ts +6 -0
  106. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  107. package/lib/typescript/commonjs/src/scripts/common.d.ts +2 -0
  108. package/lib/typescript/commonjs/src/scripts/common.d.ts.map +1 -0
  109. package/lib/typescript/commonjs/src/scripts/getFonts.d.ts.map +1 -0
  110. package/lib/typescript/commonjs/src/scripts/updatePlist.d.ts +3 -0
  111. package/lib/typescript/commonjs/src/scripts/updatePlist.d.ts.map +1 -0
  112. package/lib/typescript/module/package.json +1 -0
  113. package/lib/typescript/module/src/NativeVectorIcons.d.ts +8 -0
  114. package/lib/typescript/module/src/NativeVectorIcons.d.ts.map +1 -0
  115. package/lib/typescript/module/src/NativeVectorIcons.web.d.ts +3 -0
  116. package/lib/typescript/module/src/NativeVectorIcons.web.d.ts.map +1 -0
  117. package/lib/typescript/module/src/create-icon-set.d.ts +36 -0
  118. package/lib/typescript/module/src/create-icon-set.d.ts.map +1 -0
  119. package/lib/typescript/module/src/create-icon-source-cache.d.ts +21 -0
  120. package/lib/typescript/module/src/create-icon-source-cache.d.ts.map +1 -0
  121. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.d.ts +3 -0
  122. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.d.ts.map +1 -0
  123. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.web.d.ts +6 -0
  124. package/lib/typescript/module/src/dynamicLoading/dynamic-font-loading.web.d.ts.map +1 -0
  125. package/lib/typescript/module/src/dynamicLoading/dynamic-loading-setting.d.ts +41 -0
  126. package/lib/typescript/module/src/dynamicLoading/dynamic-loading-setting.d.ts.map +1 -0
  127. package/lib/typescript/module/src/dynamicLoading/types.d.ts +9 -0
  128. package/lib/typescript/module/src/dynamicLoading/types.d.ts.map +1 -0
  129. package/lib/typescript/module/src/ensure-native-module-available.d.ts +2 -0
  130. package/lib/typescript/module/src/ensure-native-module-available.d.ts.map +1 -0
  131. package/lib/typescript/module/src/index.d.ts +6 -0
  132. package/lib/typescript/module/src/index.d.ts.map +1 -0
  133. package/lib/typescript/module/src/scripts/common.d.ts +2 -0
  134. package/lib/typescript/module/src/scripts/common.d.ts.map +1 -0
  135. package/lib/typescript/module/src/scripts/getFonts.d.ts +3 -0
  136. package/lib/typescript/module/src/scripts/getFonts.d.ts.map +1 -0
  137. package/lib/typescript/module/src/scripts/updatePlist.d.ts +3 -0
  138. package/lib/typescript/module/src/scripts/updatePlist.d.ts.map +1 -0
  139. package/package.json +72 -44
  140. package/react-native-vector-icons.podspec +67 -42
  141. package/react-native.config.js +12 -0
  142. package/src/NativeVectorIcons.ts +2 -4
  143. package/src/create-icon-set.tsx +79 -32
  144. package/src/dynamicLoading/dynamic-font-loading.ts +97 -0
  145. package/src/dynamicLoading/dynamic-font-loading.web.ts +15 -0
  146. package/src/dynamicLoading/dynamic-loading-setting.ts +77 -0
  147. package/src/dynamicLoading/types.ts +10 -0
  148. package/src/index.ts +48 -1
  149. package/src/scripts/common.ts +52 -0
  150. package/src/scripts/getFonts.ts +6 -47
  151. package/src/scripts/updatePlist.ts +58 -0
  152. package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsModule.java +0 -121
  153. package/android/src/main/java/com/reactnativevectoricons/common/VectorIconsPackage.java +0 -45
  154. package/android/src/newarch/VectorIconsSpec.java +0 -9
  155. package/android/src/oldarch/VectorIconsSpec.java +0 -18
  156. package/lib/typescript/src/NativeVectorIcons.d.ts +0 -9
  157. package/lib/typescript/src/NativeVectorIcons.d.ts.map +0 -1
  158. package/lib/typescript/src/NativeVectorIcons.web.d.ts.map +0 -1
  159. package/lib/typescript/src/create-icon-set.d.ts +0 -26
  160. package/lib/typescript/src/create-icon-set.d.ts.map +0 -1
  161. package/lib/typescript/src/create-icon-source-cache.d.ts.map +0 -1
  162. package/lib/typescript/src/ensure-native-module-available.d.ts.map +0 -1
  163. package/lib/typescript/src/index.d.ts +0 -3
  164. package/lib/typescript/src/index.d.ts.map +0 -1
  165. package/lib/typescript/src/scripts/getFonts.d.ts.map +0 -1
  166. /package/lib/typescript/{src → commonjs/src}/NativeVectorIcons.web.d.ts +0 -0
  167. /package/lib/typescript/{src → commonjs/src}/create-icon-source-cache.d.ts +0 -0
  168. /package/lib/typescript/{src → commonjs/src}/ensure-native-module-available.d.ts +0 -0
  169. /package/lib/typescript/{src → commonjs/src}/scripts/getFonts.d.ts +0 -0
@@ -1,14 +1,25 @@
1
- import React, { forwardRef, type Ref } from 'react';
1
+ import React, { forwardRef, type Ref, useEffect } from 'react';
2
2
 
3
3
  import { PixelRatio, Platform, Text, type TextProps, type TextStyle, processColor } from 'react-native';
4
4
 
5
5
  import NativeIconAPI from './NativeVectorIcons';
6
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';
7
10
  import ensureNativeModuleAvailable from './ensure-native-module-available';
8
11
 
9
12
  export const DEFAULT_ICON_SIZE = 12;
10
13
  export const DEFAULT_ICON_COLOR = 'black';
11
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
+
12
23
  export type IconProps<T> = TextProps & {
13
24
  name: T;
14
25
  size?: number;
@@ -16,20 +27,53 @@ export type IconProps<T> = TextProps & {
16
27
  innerRef?: Ref<Text>;
17
28
  };
18
29
 
19
- 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>>(
20
50
  glyphMap: GM,
21
- fontFamily: string,
22
- fontFile: string,
51
+ postScriptName: string,
52
+ fontFileName: string,
23
53
  fontStyle?: TextProps['style'],
24
- ) => {
25
- // Android doesn't care about actual fontFamily name, it will only look in fonts folder.
26
- const fontBasename = fontFile ? fontFile.replace(/\.(otf|ttf)$/, '') : fontFamily;
54
+ ): IconComponent<GM>;
55
+ export function createIconSet<GM extends Record<string, number>>(
56
+ glyphMap: GM,
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;
27
71
 
28
72
  const fontReference = Platform.select({
29
- windows: `/Assets/${fontFile}#${fontFamily}`,
73
+ windows: `/Assets/${fontFileName}#${postScriptName}`,
30
74
  android: fontBasename,
31
75
  web: fontBasename,
32
- default: fontFamily,
76
+ default: postScriptName,
33
77
  });
34
78
 
35
79
  const resolveGlyph = (name: keyof GM) => {
@@ -45,14 +89,37 @@ export const createIconSet = <GM extends Record<string, number>>(
45
89
  const Icon = ({
46
90
  name,
47
91
  size = DEFAULT_ICON_SIZE,
48
- color,
92
+ color = DEFAULT_ICON_COLOR,
49
93
  style,
50
94
  children,
51
95
  allowFontScaling = false,
52
96
  innerRef,
53
97
  ...props
54
98
  }: IconProps<keyof GM>) => {
55
- 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
+ }, []);
56
123
 
57
124
  const styleDefaults = {
58
125
  fontSize: size,
@@ -150,30 +217,10 @@ export const createIconSet = <GM extends Record<string, number>>(
150
217
  }
151
218
  };
152
219
 
153
- const loadFont = async () => {
154
- if (Platform.OS !== 'ios') {
155
- return;
156
- }
157
- ensureNativeModuleAvailable();
158
-
159
- const [filename, extension] = fontFile.split('.'); // FIXME: what if filename has two dots?
160
- if (!filename) {
161
- // NOTE: Thie is impossible but TypeScript doesn't know that
162
- throw new Error('Font needs a filename.');
163
- }
164
- if (!extension) {
165
- throw new Error('Font needs a filename extensison.');
166
- }
167
-
168
- await NativeIconAPI.loadFontWithFileName(filename, extension, 'react-native-vector-icons');
169
- };
170
-
171
- loadFont();
172
-
173
220
  const IconNamespace = Object.assign(WrappedIcon, {
174
221
  getImageSource,
175
222
  getImageSourceSync,
176
223
  });
177
224
 
178
225
  return IconNamespace;
179
- };
226
+ }
@@ -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
+ };
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}`)); // eslint-disable-line no-console
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
+ }