Skip to content

Commit 428f999

Browse files
committed
Add auto-rem plugin
1 parent 6f9e2cd commit 428f999

File tree

7 files changed

+132
-3
lines changed

7 files changed

+132
-3
lines changed

src/auto-rem.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Declaration } from 'postcss';
2+
const unitsConverters = require('./converters');
3+
4+
const rem = unitsConverters.rem;
5+
6+
module.exports = () => {
7+
return {
8+
postcssPlugin: 'postcss-auto-rem',
9+
Declaration: (decl: Declaration) => {
10+
if (!decl.value.includes('px')) {
11+
return;
12+
}
13+
14+
decl.value = rem(decl.value);
15+
},
16+
};
17+
};
18+
19+
module.exports.postcss = true;

src/preset.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const colorMixAlpha = require('./postcss-color-mix');
55
const lightDark = require('./postcss-light-dark');
66
const converters = require('./converters');
77
const theme = require('./postcss-mantine-theme');
8+
const autorem = require('./auto-rem');
89

910
function colorSchemeMixin(colorScheme: 'light' | 'dark', type: 'where' | 'default' = 'default') {
1011
if (type === 'where') {
@@ -127,7 +128,17 @@ const largerThanMixin = (_mixin: string, breakpoint: string) => ({
127128
},
128129
});
129130

130-
module.exports = () => {
131+
interface Options {
132+
autoRem?: boolean;
133+
}
134+
135+
module.exports = (options: Options = {}) => {
136+
const plugins = [];
137+
138+
if (options.autoRem) {
139+
plugins.push(autorem());
140+
}
141+
131142
return {
132143
postcssPlugin: 'postcss-preset-mantine',
133144
plugins: [
@@ -136,6 +147,7 @@ module.exports = () => {
136147
nested(),
137148
colorMixAlpha(),
138149
remEm(),
150+
...plugins,
139151
mixins({
140152
mixins: {
141153
light: colorSchemeMixin('light'),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`auto-rem it transforms input with rem function correctly 1`] = `
4+
"
5+
.demo {
6+
font-size: calc(1rem * var(--mantine-scale));
7+
border: calc(0.0625rem * var(--mantine-scale)) solid red;
8+
padding: calc(1rem * var(--mantine-scale));
9+
}
10+
"
11+
`;
12+
13+
exports[`auto-rem it transforms media query correctly 1`] = `
14+
"
15+
@media (min-width: 320px) {
16+
.demo {
17+
font-size: calc(2rem * var(--mantine-scale))
18+
}
19+
}
20+
"
21+
`;
22+
23+
exports[`auto-rem it transforms px to rem values correctly 1`] = `
24+
"
25+
.demo {
26+
font-size: calc(1rem * var(--mantine-scale));
27+
height: calc(2.5rem * var(--mantine-scale));
28+
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));
29+
background: red;
30+
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);
32+
}
33+
"
34+
`;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ exports[`rem-em works when rem/em is inside the class name 1`] = `
4949
}
5050
"
5151
`;
52+
53+
exports[`rem-em works with space separated values with non-px values 1`] = `
54+
"
55+
.demo {
56+
border: calc(1rem * var(--mantine-scale)) solid red;
57+
}
58+
"
59+
`;

src/tests/auto-rem.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { testTransform } from './utils';
2+
3+
const baseInput = `
4+
.demo {
5+
font-size: 16px;
6+
height: 40px;
7+
padding: 10px 50px 20px 30px;
8+
background: red;
9+
border: 1px solid black;
10+
box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.5);
11+
}
12+
`;
13+
14+
const remFunctionInput = `
15+
.demo {
16+
font-size: rem(16px);
17+
border: rem(1px) solid red;
18+
padding: 16px;
19+
}
20+
`;
21+
22+
const mediaInput = `
23+
.demo {
24+
@media (min-width: 320px) {
25+
font-size: 32px;
26+
}
27+
}
28+
`;
29+
30+
describe('auto-rem', () => {
31+
it('it transforms px to rem values correctly', async () => {
32+
const res = await testTransform(baseInput, { autoRem: true });
33+
expect(res.css).toMatchSnapshot();
34+
});
35+
36+
it('it transforms input with rem function correctly', async () => {
37+
const res = await testTransform(remFunctionInput, { autoRem: true });
38+
expect(res.css).toMatchSnapshot();
39+
});
40+
41+
it('it transforms media query correctly', async () => {
42+
const res = await testTransform(mediaInput, { autoRem: true });
43+
expect(res.css).toMatchSnapshot();
44+
});
45+
});

src/tests/rem-em.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const spaceSeparatedInput = `
1313
}
1414
`;
1515

16+
const spaceSeparatedWithNonPxValuesInput = `
17+
.demo {
18+
border: rem(16px solid red);
19+
}
20+
`;
21+
1622
const mediaInput = `
1723
@media (min-width: em(320px)) {
1824
font-size: rem(32px);
@@ -65,4 +71,9 @@ describe('rem-em', () => {
6571
const res = await testTransform(remEmInsideFunctionInput);
6672
expect(res.css).toMatchSnapshot();
6773
});
74+
75+
it('works with space separated values with non-px values', async () => {
76+
const res = await testTransform(spaceSeparatedWithNonPxValuesInput);
77+
expect(res.css).toMatchSnapshot();
78+
});
6879
});

src/tests/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import postcss from 'postcss';
22
const preset = require('../preset');
33

4-
export function testTransform(input: string) {
5-
return postcss([preset]).process(input, { from: undefined });
4+
export function testTransform(input: string, options?: Record<string, any>) {
5+
return postcss([preset(options)]).process(input, { from: undefined });
66
}

0 commit comments

Comments
 (0)