Skip to content

Commit 8126eb6

Browse files
Enhance Typography component with Android-specific text rendering fix (#678)
1 parent 96a94ca commit 8126eb6

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

Diff for: src/components/Typography.jsx

+62-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from "react";
2+
import { Platform } from "react-native";
23

34
import propTypes from "@styled-system/prop-types";
45
import PropTypes from "prop-types";
@@ -14,6 +15,8 @@ import {
1415
system,
1516
} from "styled-system";
1617

18+
import { theme } from "../theme";
19+
1720
export const Text = styled.Text`
1821
${textStyle}
1922
${space}
@@ -29,6 +32,13 @@ export const Text = styled.Text`
2932
},
3033
textTransform: { property: "textTransform", cssProperty: "textTransform" },
3134
})}
35+
${Platform.select({
36+
android: `
37+
include-font-padding: false;
38+
text-align-vertical: center;
39+
`,
40+
ios: "",
41+
})}
3242
`;
3343

3444
/**
@@ -74,9 +84,54 @@ export const Text = styled.Text`
7484
* @extends StyledSystems props /styled-system
7585
*/
7686

77-
export const Typography = ({ children, ...rest }) => (
78-
<Text {...rest}>{children}</Text>
79-
);
87+
export const Typography = ({
88+
children,
89+
fontSize,
90+
lineHeightMultiplier = 1.3,
91+
style,
92+
...rest
93+
}) => {
94+
const parseSize = size => {
95+
if (typeof size === "number") return size;
96+
97+
if (typeof size === "string") {
98+
// Check if it's a theme token
99+
const themeSize = theme.fontSizes[size];
100+
if (themeSize !== undefined) {
101+
return themeSize;
102+
}
103+
104+
const parsed = parseFloat(size);
105+
106+
return isNaN(parsed) ? theme.fontSizes.s : parsed;
107+
}
108+
109+
return theme.fontSizes.s;
110+
};
111+
112+
const calculatedLineHeight = React.useMemo(() => {
113+
const size = parseSize(fontSize || rest.fontSize || theme.fontSizes.s);
114+
115+
return Math.floor(size * lineHeightMultiplier);
116+
}, [fontSize, rest.fontSize, lineHeightMultiplier]);
117+
118+
return (
119+
<Text
120+
android_hyphenationFrequency="full"
121+
fontSize={fontSize}
122+
textBreakStrategy="simple"
123+
{...rest}
124+
style={[
125+
Platform.OS === "android" && {
126+
lineHeight: calculatedLineHeight,
127+
},
128+
style,
129+
]}
130+
>
131+
{children}
132+
</Text>
133+
);
134+
};
80135

81136
Typography.propTypes = {
82137
...propTypes.space,
@@ -86,8 +141,12 @@ Typography.propTypes = {
86141
...propTypes.color,
87142
...propTypes.textStyle,
88143
children: PropTypes.node,
144+
fontSize: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
145+
lineHeightMultiplier: PropTypes.number,
146+
style: PropTypes.object,
89147
};
90148

91149
Typography.defaultProps = {
92150
textStyle: "defaultTextStyle",
151+
lineHeightMultiplier: 1.3,
93152
};

0 commit comments

Comments
 (0)