Skip to content

Commit c31b73e

Browse files
authoredApr 1, 2025··
fix(text): properly parse margin on the text component (#1985)
1 parent 0c2fd11 commit c31b73e

File tree

3 files changed

+97
-13
lines changed

3 files changed

+97
-13
lines changed
 

‎.changeset/grumpy-steaks-brake.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/text": patch
3+
---
4+
5+
Merge all coming margin style properties into a single `margin`

‎packages/text/src/text.tsx

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import * as React from 'react';
2+
import { parseMargin } from './utils/parse-margins';
23

34
export type TextProps = Readonly<React.ComponentPropsWithoutRef<'p'>>;
45

56
export const Text = React.forwardRef<HTMLParagraphElement, TextProps>(
6-
({ style, ...props }, ref) => (
7-
<p
8-
{...props}
9-
ref={ref}
10-
style={{
11-
fontSize: '14px',
12-
lineHeight: '24px',
13-
marginBottom: '16px',
14-
marginTop: '16px',
15-
...style,
16-
}}
17-
/>
18-
),
7+
({ style, ...props }, ref) => {
8+
const margins = parseMargin({
9+
margin: style?.margin,
10+
marginBottom: style?.marginBottom ?? '16px',
11+
marginTop: style?.marginTop ?? '16px',
12+
marginLeft: style?.marginLeft,
13+
marginRight: style?.marginRight,
14+
});
15+
16+
return (
17+
<p
18+
{...props}
19+
ref={ref}
20+
style={{
21+
fontSize: '14px',
22+
lineHeight: '24px',
23+
...style,
24+
marginBottom: margins.mb,
25+
marginTop: margins.mt,
26+
marginLeft: margins.ml,
27+
marginRight: margins.mr,
28+
}}
29+
/>
30+
);
31+
},
1932
);
2033

2134
Text.displayName = 'Text';
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
type MarginType = string | number | undefined;
2+
3+
interface MarginProperties {
4+
margin: MarginType;
5+
marginTop?: MarginType;
6+
marginRight?: MarginType;
7+
marginBottom?: MarginType;
8+
marginLeft?: MarginType;
9+
}
10+
11+
/**
12+
* Parses all the values out of a margin string to get the value for all margin props in `px`
13+
* @example e.g. "10px" =\> mt: 10, mr: 10, mb: 10, ml: 10
14+
*/
15+
export function parseMargin({
16+
margin,
17+
marginTop,
18+
marginRight,
19+
marginBottom,
20+
marginLeft,
21+
}: MarginProperties) {
22+
let mt = marginTop;
23+
let mr = marginRight;
24+
let mb = marginBottom;
25+
let ml = marginLeft;
26+
27+
if (typeof margin === 'number') {
28+
mt = margin;
29+
mr = margin;
30+
mb = margin;
31+
ml = margin;
32+
} else if (typeof margin === 'string') {
33+
const values = margin.split(/\s+/);
34+
35+
switch (values.length) {
36+
case 1:
37+
mt = values[0];
38+
mr = values[0];
39+
mb = values[0];
40+
ml = values[0];
41+
break;
42+
case 2:
43+
mt = values[0];
44+
mb = values[0];
45+
mr = values[1];
46+
ml = values[1];
47+
break;
48+
case 3:
49+
mt = values[0];
50+
mr = values[1];
51+
mb = values[2];
52+
ml = values[1];
53+
break;
54+
case 4:
55+
mt = values[0];
56+
mr = values[1];
57+
mb = values[2];
58+
ml = values[3];
59+
break;
60+
default:
61+
break;
62+
}
63+
}
64+
65+
return { mt, mr, mb, ml };
66+
}

0 commit comments

Comments
 (0)