Skip to content

Commit ed14813

Browse files
committed
Fix several issues with rem transforms
1 parent 646d00c commit ed14813

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/converters.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
function scaleRem(remValue: string) {
2+
if (remValue === '0rem') {
3+
return '0rem';
4+
}
5+
26
return `calc(${remValue} * var(--mantine-scale))`;
37
}
48

59
function createConverter(units: string, { shouldScale = false } = {}) {
610
function converter(value: unknown): string {
711
if (value === 0 || value === '0') {
8-
return '0';
12+
return `0${units}`;
913
}
1014

1115
if (typeof value === 'number') {
@@ -14,10 +18,17 @@ function createConverter(units: string, { shouldScale = false } = {}) {
1418
}
1519

1620
if (typeof value === 'string') {
17-
if (value.startsWith('calc(') || value.startsWith('var(') || value.startsWith('clamp(')) {
21+
if (value.startsWith('calc(') || value.startsWith('clamp(') || value.includes('rgba(')) {
1822
return value;
1923
}
2024

25+
if (value.includes(',')) {
26+
return value
27+
.split(',')
28+
.map((val) => converter(val))
29+
.join(',');
30+
}
31+
2132
if (value.includes(' ')) {
2233
return value
2334
.split(' ')

src/tests/__snapshots__/auto-rem.test.ts.snap

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`auto-rem does not transform strings with rgba 1`] = `
4+
"
5+
.demo {
6+
box-shadow:
7+
rgba(0, 0, 0, 0.1) 0 0 0 5px inset,
8+
rgb(0, 0, 0, 0.15) 0 0 3px inset;
9+
}
10+
"
11+
`;
12+
313
exports[`auto-rem it transforms input with rem function correctly 1`] = `
414
"
515
.demo {
@@ -28,7 +38,15 @@ exports[`auto-rem it transforms px to rem values correctly 1`] = `
2838
padding: calc(0.625rem * var(--mantine-scale)) calc(3.125rem * var(--mantine-scale)) calc(1.25rem * var(--mantine-scale)) calc(1.875rem * var(--mantine-scale));
2939
background: red;
3040
border: calc(0.0625rem * var(--mantine-scale)) solid black;
31-
box-shadow: 0 0 calc(0.625rem * var(--mantine-scale)) calc(0.3125rem * var(--mantine-scale)) rgba(0, 0, 0, 0.5);
41+
box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.5);
42+
}
43+
"
44+
`;
45+
46+
exports[`auto-rem transforms coma separated values correctly 1`] = `
47+
"
48+
.demo {
49+
background-position: 0rem 0rem,0rem 0rem calc(0.25rem * var(--mantine-scale)),0rem calc(0.25rem * var(--mantine-scale)) calc(-0.25rem * var(--mantine-scale)),0rem calc(-0.25rem * var(--mantine-scale)) 0rem;
3250
}
3351
"
3452
`;

src/tests/auto-rem.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ const mediaInput = `
2727
}
2828
`;
2929

30+
const rgbaInput = `
31+
.demo {
32+
box-shadow:
33+
rgba(0, 0, 0, 0.1) 0 0 0 5px inset,
34+
rgb(0, 0, 0, 0.15) 0 0 3px inset;
35+
}
36+
`;
37+
38+
const comaSeparatedInput = `
39+
.demo {
40+
background-position: 0 0, 0 4px, 4px -4px, -4px 0;
41+
}
42+
`;
43+
3044
describe('auto-rem', () => {
3145
it('it transforms px to rem values correctly', async () => {
3246
const res = await testTransform(baseInput, { autoRem: true });
@@ -42,4 +56,14 @@ describe('auto-rem', () => {
4256
const res = await testTransform(mediaInput, { autoRem: true });
4357
expect(res.css).toMatchSnapshot();
4458
});
59+
60+
it('does not transform strings with rgba', async () => {
61+
const res = await testTransform(rgbaInput, { autoRem: true });
62+
expect(res.css).toMatchSnapshot();
63+
});
64+
65+
it('transforms coma separated values correctly', async () => {
66+
const res = await testTransform(comaSeparatedInput, { autoRem: true });
67+
expect(res.css).toMatchSnapshot();
68+
});
4569
});

0 commit comments

Comments
 (0)