Skip to content

Commit 7a8c9e5

Browse files
committed
fix: support decimal values in arbitrary font-size and line-height
Update parseArbitraryFontSize and parseArbitraryLineHeight to accept decimal values like text-[13.5px] and leading-[21.5px], matching the pattern already used in parseArbitraryLetterSpacing. Changes: - Update regex to match decimals: /^\[(-?\d+(?:\.\d+)?|-?\.\d+)(?:px)?\]$/ - Change from parseInt to parseFloat - Support Tailwind shorthand decimals like text-[.5] - Add comprehensive test coverage for decimal values
1 parent 7a0a171 commit 7a8c9e5

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/parser/typography.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ describe("parseTypography - font size", () => {
3939
expect(parseTypography("text-[22]")).toEqual({ fontSize: 22 });
4040
expect(parseTypography("text-[100px]")).toEqual({ fontSize: 100 });
4141
});
42+
43+
it("should parse font size with decimal arbitrary values", () => {
44+
expect(parseTypography("text-[13.5px]")).toEqual({ fontSize: 13.5 });
45+
expect(parseTypography("text-[13.5]")).toEqual({ fontSize: 13.5 });
46+
expect(parseTypography("text-[18.75px]")).toEqual({ fontSize: 18.75 });
47+
expect(parseTypography("text-[18.75]")).toEqual({ fontSize: 18.75 });
48+
expect(parseTypography("text-[22.5]")).toEqual({ fontSize: 22.5 });
49+
});
50+
51+
it("should parse font size with Tailwind shorthand decimals", () => {
52+
expect(parseTypography("text-[.5]")).toEqual({ fontSize: 0.5 });
53+
expect(parseTypography("text-[.75px]")).toEqual({ fontSize: 0.75 });
54+
expect(parseTypography("text-[.5px]")).toEqual({ fontSize: 0.5 });
55+
});
4256
});
4357

4458
describe("parseTypography - font family", () => {
@@ -136,6 +150,20 @@ describe("parseTypography - line height", () => {
136150
expect(parseTypography("leading-[30]")).toEqual({ lineHeight: 30 });
137151
expect(parseTypography("leading-[50px]")).toEqual({ lineHeight: 50 });
138152
});
153+
154+
it("should parse line height with decimal arbitrary values", () => {
155+
expect(parseTypography("leading-[21.5px]")).toEqual({ lineHeight: 21.5 });
156+
expect(parseTypography("leading-[21.5]")).toEqual({ lineHeight: 21.5 });
157+
expect(parseTypography("leading-[28.75px]")).toEqual({ lineHeight: 28.75 });
158+
expect(parseTypography("leading-[28.75]")).toEqual({ lineHeight: 28.75 });
159+
expect(parseTypography("leading-[32.5]")).toEqual({ lineHeight: 32.5 });
160+
});
161+
162+
it("should parse line height with Tailwind shorthand decimals", () => {
163+
expect(parseTypography("leading-[.5]")).toEqual({ lineHeight: 0.5 });
164+
expect(parseTypography("leading-[.75px]")).toEqual({ lineHeight: 0.75 });
165+
expect(parseTypography("leading-[.5px]")).toEqual({ lineHeight: 0.5 });
166+
});
139167
});
140168

141169
describe("parseTypography - letter spacing", () => {

src/parser/typography.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,22 @@ const TRACKING_MAP: Record<string, StyleObject> = {
113113
};
114114

115115
/**
116-
* Parse arbitrary font size value: [18px], [20]
117-
* Returns number for px values, null for unsupported formats
116+
* Parse arbitrary font size value: [18px], [20], [13.5px], [.5]
117+
* Returns number for px values (including decimals), null for unsupported formats
118118
*/
119119
function parseArbitraryFontSize(value: string): number | null {
120-
// Match: [18px] or [18] (pixels only)
121-
const pxMatch = value.match(/^\[(\d+)(?:px)?\]$/);
120+
// Match: [18px], [18], [13.5px], [13.5], [.5] (pixels, including decimals)
121+
const pxMatch = value.match(/^\[(-?\d+(?:\.\d+)?|-?\.\d+)(?:px)?\]$/);
122122
if (pxMatch) {
123-
return parseInt(pxMatch[1], 10);
123+
return parseFloat(pxMatch[1]);
124124
}
125125

126126
// Warn about unsupported formats
127127
if (value.startsWith("[") && value.endsWith("]")) {
128128
/* v8 ignore next 5 */
129129
if (process.env.NODE_ENV !== "production") {
130130
console.warn(
131-
`[react-native-tailwind] Unsupported arbitrary font size value: ${value}. Only px values are supported (e.g., [18px] or [18]).`,
131+
`[react-native-tailwind] Unsupported arbitrary font size value: ${value}. Only px values are supported (e.g., [18px], [13.5px], [.5]).`,
132132
);
133133
}
134134
return null;
@@ -138,22 +138,22 @@ function parseArbitraryFontSize(value: string): number | null {
138138
}
139139

140140
/**
141-
* Parse arbitrary line height value: [24px], [28]
142-
* Returns number for px values, null for unsupported formats
141+
* Parse arbitrary line height value: [24px], [28], [21.5px], [.5]
142+
* Returns number for px values (including decimals), null for unsupported formats
143143
*/
144144
function parseArbitraryLineHeight(value: string): number | null {
145-
// Match: [24px] or [24] (pixels only)
146-
const pxMatch = value.match(/^\[(\d+)(?:px)?\]$/);
145+
// Match: [24px], [24], [21.5px], [21.5], [.5] (pixels, including decimals)
146+
const pxMatch = value.match(/^\[(-?\d+(?:\.\d+)?|-?\.\d+)(?:px)?\]$/);
147147
if (pxMatch) {
148-
return parseInt(pxMatch[1], 10);
148+
return parseFloat(pxMatch[1]);
149149
}
150150

151151
// Warn about unsupported formats
152152
if (value.startsWith("[") && value.endsWith("]")) {
153153
/* v8 ignore next 5 */
154154
if (process.env.NODE_ENV !== "production") {
155155
console.warn(
156-
`[react-native-tailwind] Unsupported arbitrary line height value: ${value}. Only px values are supported (e.g., [24px] or [24]).`,
156+
`[react-native-tailwind] Unsupported arbitrary line height value: ${value}. Only px values are supported (e.g., [24px], [21.5px], [.5]).`,
157157
);
158158
}
159159
return null;

0 commit comments

Comments
 (0)