Skip to content

Commit 0a7b555

Browse files
authored
fix(text): Numerical margin values being overwritten with 0 (#2220)
1 parent aa518a3 commit 0a7b555

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

.changeset/open-pears-sink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-email/text": patch
3+
---
4+
5+
fix numerical margin values being overwritten by 0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`<Text> component > gives priority to the user's style 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><p style="font-size:14px;line-height:24px;margin-top:0px;margin-bottom:16px"></p><!--/$-->"`;
4+
5+
exports[`<Text> component > passes style and other props correctly 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><p style="font-size:16px;line-height:24px;margin-top:16px;margin-bottom:16px">Test</p><!--/$-->"`;
6+
7+
exports[`<Text> component > renders children correctly 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><p style="font-size:14px;line-height:24px;margin-top:16px;margin-bottom:16px">Test message</p><!--/$-->"`;
8+
39
exports[`<Text> component > renders correctly 1`] = `"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--$--><p style="font-size:14px;line-height:24px;margin-top:16px;margin-bottom:16px">Lorem ipsum</p><!--/$-->"`;

packages/text/src/text.spec.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ describe('<Text> component', () => {
55
it('renders children correctly', async () => {
66
const testMessage = 'Test message';
77
const html = await render(<Text>{testMessage}</Text>);
8-
expect(html).toContain(testMessage);
8+
expect(html).toMatchSnapshot();
99
});
1010

1111
it("gives priority to the user's style", async () => {
1212
const style = { marginTop: '0px' };
1313
const html = await render(<Text style={style} />);
14-
expect(html).toContain('margin-top:0px');
14+
expect(html).toMatchSnapshot();
1515
});
1616

1717
it('passes style and other props correctly', async () => {
1818
const style = { fontSize: '16px' };
19-
const html = await render(
20-
<Text data-testid="text-test" style={style}>
21-
Test
22-
</Text>,
23-
);
24-
expect(html).toContain('font-size:16px');
25-
expect(html).toContain('data-testid="text-test"');
19+
const html = await render(<Text style={style}>Test</Text>);
20+
expect(html).toMatchSnapshot();
2621
});
2722

2823
it('renders correctly', async () => {

packages/text/src/utils/compute-margins.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import { computeMargins } from './compute-margins';
22

33
describe('parseMargins()', () => {
4+
it('should work with numeric and text margins', () => {
5+
expect(computeMargins({ margin: 24 })).toEqual({
6+
marginTop: 24,
7+
marginRight: 24,
8+
marginBottom: 24,
9+
marginLeft: 24,
10+
});
11+
12+
expect(computeMargins({ margin: '24px' })).toEqual({
13+
marginTop: '24px',
14+
marginRight: '24px',
15+
marginBottom: '24px',
16+
marginLeft: '24px',
17+
});
18+
19+
expect(computeMargins({ margin: '24px', marginTop: 10 })).toEqual({
20+
marginTop: 10,
21+
marginRight: '24px',
22+
marginBottom: '24px',
23+
marginLeft: '24px',
24+
});
25+
});
26+
427
it('should compute the margins according to the order of styles', () => {
528
expect(
629
computeMargins({

packages/text/src/utils/compute-margins.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ interface MarginResult {
1717

1818
function parseMarginValue(value: MarginType): MarginResult {
1919
if (typeof value === 'number')
20-
return { marginTop: 0, marginBottom: 0, marginLeft: 0, marginRight: 0 };
20+
return {
21+
marginTop: value,
22+
marginBottom: value,
23+
marginLeft: value,
24+
marginRight: value,
25+
};
2126

2227
if (typeof value === 'string') {
2328
const values = value.toString().trim().split(/\s+/);

0 commit comments

Comments
 (0)