Skip to content

Commit 3fd1434

Browse files
authored
refactor: icomoon and fontello with expo (#1829)
1 parent 8badc7b commit 3fd1434

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

packages/common/src/create-icon-set.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type IconProps<T> = TextProps & {
2525
innerRef?: Ref<Text>;
2626
};
2727

28-
type IconComponent<GM extends Record<string, number>> = React.FC<
28+
export type IconComponent<GM extends Record<string, number>> = React.FC<
2929
TextProps & {
3030
name: keyof GM;
3131
size?: number;
@@ -150,7 +150,7 @@ export function createIconSet<GM extends Record<string, number>>(
150150
const WrappedIcon = forwardRef<Text, IconProps<keyof typeof glyphMap>>((props, ref) => (
151151
<Icon innerRef={ref} {...props} />
152152
));
153-
WrappedIcon.displayName = 'Icon';
153+
WrappedIcon.displayName = `Icon(${postScriptName})`;
154154

155155
const imageSourceCache = createIconSourceCache();
156156

packages/common/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { CreateIconSetOptions, IconProps } from './create-icon-set';
1+
export type { CreateIconSetOptions, IconComponent, IconProps } from './create-icon-set';
22
export { createIconSet } from './create-icon-set';
33
export { DEFAULT_ICON_COLOR, DEFAULT_ICON_SIZE } from './defaults';
44
export {

packages/fontello/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import createIconSet from '@react-native-vector-icons/fontello';
2727
import fontelloConfig from './config.json';
2828
const Icon = createIconSet(fontelloConfig);
2929

30-
cont icon = <Icon name="comments" />;
30+
const icon = <Icon name="comments" />;
3131
```
3232

3333
If you want to customise the font postscript name and filename you can pass extra arguments.

packages/fontello/src/index.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Usage: <Fontello name="icon-name" size={20} color="#4F8EF7" />
44
*/
55

6-
import { createIconSet } from '@react-native-vector-icons/common';
6+
import { type CreateIconSetOptions, createIconSet, type IconComponent } from '@react-native-vector-icons/common';
77

88
type FontelloConfig = {
99
name: string;
@@ -20,13 +20,41 @@ type FontelloConfig = {
2020
}>;
2121
};
2222

23-
export default function createIconSetFromFontello(config: FontelloConfig, fontFamilyArg?: string, fontFile?: string) {
23+
type FontelloComponent = IconComponent<Record<string, number>>;
24+
25+
// entries are optional because they can be derived from the config
26+
type Options = Partial<CreateIconSetOptions>;
27+
28+
export default function createIconSetFromFontello(
29+
config: FontelloConfig,
30+
postScriptName?: string,
31+
fontFileName?: string,
32+
): FontelloComponent;
33+
export default function createIconSetFromFontello(config: FontelloConfig, options: Options): FontelloComponent;
34+
export default function createIconSetFromFontello(
35+
config: FontelloConfig,
36+
postScriptNameOrOptions?: string | Options,
37+
fontFileNameParam?: string,
38+
): FontelloComponent {
39+
const { postScriptName, fontFileName, fontSource, fontStyle } =
40+
typeof postScriptNameOrOptions === 'object'
41+
? postScriptNameOrOptions
42+
: {
43+
postScriptName: postScriptNameOrOptions,
44+
fontFileName: fontFileNameParam,
45+
};
46+
2447
const glyphMap: Record<string, number> = {};
2548
config.glyphs.forEach((glyph) => {
2649
glyphMap[glyph.css] = glyph.code;
2750
});
2851

29-
const fontFamily = fontFamilyArg || config.name || 'fontello';
52+
const fontFamily = postScriptName || config.name || 'fontello';
3053

31-
return createIconSet(glyphMap, fontFamily, fontFile || `${fontFamily}.ttf`);
54+
return createIconSet(glyphMap, {
55+
postScriptName: fontFamily,
56+
fontFileName: fontFileName || `${fontFamily}.ttf`,
57+
fontSource,
58+
fontStyle,
59+
});
3260
}

packages/icomoon/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import createIconSet from '@react-native-vector-icons/icomoon';
2727
import icoMoonConfig from './IcoMoon-Free.json';
2828
const Icon = createIconSet(icoMoonConfig);
2929

30-
cont icon = <Icon name="comments" />;
30+
const icon = <Icon name="comments" />;
3131
```
3232

3333
If you want to customise the font postscript name and filename you can pass extra arguments.

packages/icomoon/src/index.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Usage: <Fontello name="icon-name" size={20} color="#4F8EF7" />
44
*/
55

6-
import { createIconSet } from '@react-native-vector-icons/common';
6+
import { type CreateIconSetOptions, createIconSet, type IconComponent } from '@react-native-vector-icons/common';
77

88
type IcoMoonIcon = {
99
properties: {
@@ -22,15 +22,43 @@ type IcoMoonConfig = {
2222
};
2323
};
2424

25-
export default function createIconSetFromIcoMoon(config: IcoMoonConfig, fontFamilyArg?: string, fontFile?: string) {
25+
type IcoMoonComponent = IconComponent<Record<string, number>>;
26+
27+
// entries are optional because they can be derived from the config
28+
type Options = Partial<CreateIconSetOptions>;
29+
30+
export default function createIconSetFromIcoMoon(
31+
config: IcoMoonConfig,
32+
postScriptName?: string,
33+
fontFileName?: string,
34+
): IcoMoonComponent;
35+
export default function createIconSetFromIcoMoon(config: IcoMoonConfig, options: Options): IcoMoonComponent;
36+
export default function createIconSetFromIcoMoon(
37+
config: IcoMoonConfig,
38+
postScriptNameOrOptions?: string | Options,
39+
fontFileNameParam?: string,
40+
): IcoMoonComponent {
41+
const { postScriptName, fontFileName, fontSource, fontStyle } =
42+
typeof postScriptNameOrOptions === 'object'
43+
? postScriptNameOrOptions
44+
: {
45+
postScriptName: postScriptNameOrOptions,
46+
fontFileName: fontFileNameParam,
47+
};
48+
2649
const glyphMap: Record<string, number> = {};
2750
config.icons.forEach((icon) => {
2851
icon.properties.name.split(/\s*,\s*/g).forEach((name) => {
2952
glyphMap[name] = icon.properties.code;
3053
});
3154
});
3255

33-
const fontFamily = fontFamilyArg || config.preferences.fontPref.metadata.fontFamily;
56+
const fontFamily = postScriptName || config.preferences.fontPref.metadata.fontFamily;
3457

35-
return createIconSet(glyphMap, fontFamily, fontFile || `${fontFamily}.ttf`);
58+
return createIconSet(glyphMap, {
59+
postScriptName: fontFamily,
60+
fontFileName: fontFileName || `${fontFamily}.ttf`,
61+
fontSource,
62+
fontStyle,
63+
});
3664
}

0 commit comments

Comments
 (0)