Skip to content

Commit c88dcbf

Browse files
committed
feat(utils): ✨ add helper function getTextFontFamily
1 parent a372b31 commit c88dcbf

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/theme/getTextFontFamily.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Platform, TextStyle } from "react-native";
2+
/**
3+
* This hook is to switch between font family based on Platform
4+
* iOS using System Font there is no need to set is explicitly
5+
* For Android, we use Inter
6+
* font-thin - 100;
7+
* font-extralight - 200;
8+
* font-light - 300;
9+
* font-normal - 400;
10+
* font-medium - 500;
11+
* font-semibold - 600;
12+
* font-bold - 700;
13+
* font-extrabold - 800;
14+
* font-black - 900;
15+
*/
16+
17+
type FONT_FAMILY_KEY =
18+
| "font-thin"
19+
| "font-extralight"
20+
| "font-light"
21+
| "font-black"
22+
| "font-medium"
23+
| "font-semibold"
24+
| "font-bold"
25+
| "font-extrabold";
26+
27+
export type ObjectKeys<T extends object> = keyof T;
28+
29+
export const objectKeys = Object.keys as <Type extends object>(
30+
value: Type,
31+
) => Array<ObjectKeys<Type>>;
32+
33+
export const getTextFontFamily = (tClassNames: string): TextStyle => {
34+
const fontFamilies: Record<FONT_FAMILY_KEY, string> = {
35+
"font-thin": "Inter-Thin",
36+
"font-extralight": "Inter-Extralight",
37+
"font-light": "Inter-light",
38+
"font-black": "Inter-Black",
39+
"font-medium": "Inter-Medium",
40+
"font-semibold": "Inter-SemiBold",
41+
"font-bold": "Inter-Bold",
42+
"font-extrabold": "Inter-ExtraBold",
43+
};
44+
45+
const tailwindClass = tClassNames.split(" ");
46+
const keys = objectKeys(fontFamilies);
47+
const fontFamily = keys.find(className => tailwindClass.includes(className));
48+
49+
const textStyle =
50+
Platform.OS === "android"
51+
? {
52+
fontFamily: fontFamily ? fontFamilies[fontFamily] : "Inter-Regular",
53+
}
54+
: {
55+
fontFamily: "System",
56+
};
57+
return textStyle;
58+
};

src/theme/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./context";
2+
export * from "./getTextFontFamily";
23
export * from "./useTailwind";

0 commit comments

Comments
 (0)