Skip to content

Commit c8c9989

Browse files
committed
Fix incorrect auto-rem transform of uniless values
1 parent cbabc38 commit c8c9989

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/auto-rem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Declaration } from 'postcss';
22
const unitsConverters = require('./converters');
33

4-
const rem = unitsConverters.rem;
4+
const rem = unitsConverters.remStrict;
55

66
module.exports = () => {
77
return {

src/converters.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function scaleRem(remValue: string) {
66
return `calc(${remValue} * var(--mantine-scale))`;
77
}
88

9-
function createConverter(units: string, { shouldScale = false } = {}) {
9+
function createConverter(units: string, { shouldScale = false, transformUnitLess = true } = {}) {
1010
function converter(value: unknown): string {
1111
if (value === 0 || value === '0') {
1212
return `0${units}`;
@@ -41,6 +41,11 @@ function createConverter(units: string, { shouldScale = false } = {}) {
4141
}
4242

4343
const replaced = value.replace('px', '');
44+
45+
if (replaced === value && !transformUnitLess) {
46+
return value;
47+
}
48+
4449
if (!Number.isNaN(Number(replaced))) {
4550
const val = `${Number(replaced) / 16}${units}`;
4651
return shouldScale ? scaleRem(val) : val;
@@ -54,6 +59,7 @@ function createConverter(units: string, { shouldScale = false } = {}) {
5459
}
5560

5661
const rem = createConverter('rem', { shouldScale: true });
62+
const remStrict = createConverter('rem', { shouldScale: true, transformUnitLess: false });
5763
const remNoScale = createConverter('rem');
5864
const em = createConverter('em');
5965

@@ -102,5 +108,6 @@ module.exports = {
102108
px,
103109
em,
104110
rem,
111+
remStrict,
105112
remNoScale,
106113
};

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ exports[`auto-rem does not transform strings with rgba 1`] = `
1010
"
1111
`;
1212

13+
exports[`auto-rem does not transform values without units 1`] = `
14+
"
15+
.demo {
16+
flex: 1 1 calc(7.5rem * var(--mantine-scale))
17+
}
18+
"
19+
`;
20+
1321
exports[`auto-rem it transforms input with rem function correctly 1`] = `
1422
"
1523
.demo {
@@ -46,7 +54,7 @@ exports[`auto-rem it transforms px to rem values correctly 1`] = `
4654
exports[`auto-rem transforms coma separated values correctly 1`] = `
4755
"
4856
.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;
57+
background-position: 0rem 0rem, 0rem calc(0.25rem * var(--mantine-scale)), calc(0.25rem * var(--mantine-scale)) calc(-0.25rem * var(--mantine-scale)), calc(-0.25rem * var(--mantine-scale)) 0rem;
5058
}
5159
"
5260
`;

src/tests/auto-rem.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ const comaSeparatedInput = `
4141
}
4242
`;
4343

44+
const unitLessInput = `
45+
.demo {
46+
flex: 1 1 120px
47+
}
48+
`;
49+
4450
describe('auto-rem', () => {
4551
it('it transforms px to rem values correctly', async () => {
4652
const res = await testTransform(baseInput, { autoRem: true });
@@ -66,4 +72,9 @@ describe('auto-rem', () => {
6672
const res = await testTransform(comaSeparatedInput, { autoRem: true });
6773
expect(res.css).toMatchSnapshot();
6874
});
75+
76+
it('does not transform values without units', async () => {
77+
const res = await testTransform(unitLessInput, { autoRem: true });
78+
expect(res.css).toMatchSnapshot();
79+
});
6980
});

0 commit comments

Comments
 (0)