forked from pagopa/io-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonts.ts
82 lines (74 loc) · 1.94 KB
/
fonts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Utility functions to manage font properties to style mapping for both iOS and Android
* Fonts are managed differently on Android and iOS. Read the Font section of the
* README file included in this repository.
*/
import { Platform, PlatformStatic } from "react-native";
type PlatformSelectType = PlatformStatic["select"];
const fonts = {
TitilliumWeb: Platform.select({
android: "TitilliumWeb",
ios: "Titillium Web"
}),
RobotoMono: Platform.select({
android: "RobotoMono",
ios: "Roboto Mono"
})
};
const fontWeights = {
"300": "Light",
"400": "Regular",
"600": "SemiBold",
"700": "Bold"
};
type FontFamily = keyof typeof fonts;
export type FontWeight = keyof typeof fontWeights;
enum FontStyle {
"normal" = "normal",
"italic" = "italic"
}
type FontStyleObject = {
fontFamily: string;
fontWeight?: FontWeight;
fontStyle?: FontStyle;
};
/**
* Get the correct fontFamily name on both Android and iOS
*/
const makeFontFamilyName = (
osSelect: PlatformSelectType,
font: FontFamily,
weight?: FontWeight,
isItalic: boolean = false
): string =>
osSelect({
default: "undefined",
android: `${fonts[font]}-${fontWeights[weight || "400"]}${
isItalic ? "Italic" : ""
}`,
ios: fonts[font]
});
/**
* This function returns an object containing all the properties needed to use
* a Font correctly on both Android and iOS.
* @deprecated
*/
export const makeFontStyleObject = (
osSelect: PlatformSelectType,
weight: FontWeight | undefined = undefined,
isItalic: boolean | undefined = false,
font: FontFamily | undefined = "TitilliumWeb"
): FontStyleObject =>
osSelect({
default: {
fontFamily: "undefined"
},
android: {
fontFamily: makeFontFamilyName(osSelect, font, weight, isItalic)
},
ios: {
fontFamily: makeFontFamilyName(osSelect, font, weight, isItalic),
fontWeight: weight,
fontStyle: isItalic ? FontStyle.italic : FontStyle.normal
}
});