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