Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

util.ts

62 lines1.6 KB

1import { type StyleProp, StyleSheet, type TextStyle } from "react-native";
2import type { NativeFontWeight } from "./T3MarkdownTextRunNativeComponent";
3​
4export function flattenStyles(rootStyle: TextStyle, style: StyleProp<TextStyle>) {
5 const flattenedStyle = StyleSheet.flatten([rootStyle, style]) as TextStyle;
6 return {
7 ...flattenedStyle,
8 fontWeight: fontWeightToNativeProp(flattenedStyle.fontWeight ?? "normal"),
9 backgroundColor: flattenedStyle.backgroundColor
10 ? flattenedStyle.backgroundColor
11 : "transparent",
12 shadowOffset: flattenedStyle.shadowOffset
13 ? flattenedStyle.shadowOffset
14 : { width: 0, height: 0 },
15 };
17​
18// Codegen doesn't like using integer values for enums (c++ L) so we'll conver them to the proper native prop
19// value before returning flattened styles.
20function fontWeightToNativeProp(fontWeight: TextStyle["fontWeight"]): NativeFontWeight {
21 switch (fontWeight) {
22 case "normal":
23 return "normal";
24 case "bold":
25 return "bold";
26 case 100:
27 case "100":
28 case "ultralight":
29 return "ultraLight";
30 case 200:
31 case "200":
32 return "ultraLight";
33 case 300:
34 case "300":
35 case "light":
36 return "light";
37 case 400:
38 case "400":
39 case "regular":
40 return "normal";
41 case 500:
42 case "500":
43 case "medium":
44 return "medium";
45 case 600:
46 case "600":
47 case "semibold":
48 return "semibold";
49 case 700:
50 case "700":
51 return "semibold";
52 case 800:
53 case "800":
54 return "bold";
55 case 900:
56 case "900":
57 case "heavy":
58 return "heavy";
59 default:
60 return "normal";
61 }