Skip to content

Commit

Permalink
Add auto-rem plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Apr 10, 2024
1 parent 6f9e2cd commit 428f999
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/auto-rem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Declaration } from 'postcss';
const unitsConverters = require('./converters');

const rem = unitsConverters.rem;

module.exports = () => {
return {
postcssPlugin: 'postcss-auto-rem',
Declaration: (decl: Declaration) => {
if (!decl.value.includes('px')) {
return;
}

decl.value = rem(decl.value);
},
};
};

module.exports.postcss = true;
14 changes: 13 additions & 1 deletion src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const colorMixAlpha = require('./postcss-color-mix');
const lightDark = require('./postcss-light-dark');
const converters = require('./converters');
const theme = require('./postcss-mantine-theme');
const autorem = require('./auto-rem');

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

module.exports = () => {
interface Options {
autoRem?: boolean;
}

module.exports = (options: Options = {}) => {
const plugins = [];

if (options.autoRem) {
plugins.push(autorem());
}

return {
postcssPlugin: 'postcss-preset-mantine',
plugins: [
Expand All @@ -136,6 +147,7 @@ module.exports = () => {
nested(),
colorMixAlpha(),
remEm(),
...plugins,
mixins({
mixins: {
light: colorSchemeMixin('light'),
Expand Down
34 changes: 34 additions & 0 deletions src/tests/__snapshots__/auto-rem.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`auto-rem it transforms input with rem function correctly 1`] = `
"
.demo {
font-size: calc(1rem * var(--mantine-scale));
border: calc(0.0625rem * var(--mantine-scale)) solid red;
padding: calc(1rem * var(--mantine-scale));
}
"
`;

exports[`auto-rem it transforms media query correctly 1`] = `
"
@media (min-width: 320px) {
.demo {
font-size: calc(2rem * var(--mantine-scale))
}
}
"
`;

exports[`auto-rem it transforms px to rem values correctly 1`] = `
"
.demo {
font-size: calc(1rem * var(--mantine-scale));
height: calc(2.5rem * var(--mantine-scale));
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));
background: red;
border: calc(0.0625rem * var(--mantine-scale)) solid black;
box-shadow: 0 0 calc(0.625rem * var(--mantine-scale)) calc(0.3125rem * var(--mantine-scale)) rgba(0, 0, 0, 0.5);
}
"
`;
8 changes: 8 additions & 0 deletions src/tests/__snapshots__/rem-em.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ exports[`rem-em works when rem/em is inside the class name 1`] = `
}
"
`;

exports[`rem-em works with space separated values with non-px values 1`] = `
"
.demo {
border: calc(1rem * var(--mantine-scale)) solid red;
}
"
`;
45 changes: 45 additions & 0 deletions src/tests/auto-rem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { testTransform } from './utils';

const baseInput = `
.demo {
font-size: 16px;
height: 40px;
padding: 10px 50px 20px 30px;
background: red;
border: 1px solid black;
box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.5);
}
`;

const remFunctionInput = `
.demo {
font-size: rem(16px);
border: rem(1px) solid red;
padding: 16px;
}
`;

const mediaInput = `
.demo {
@media (min-width: 320px) {
font-size: 32px;
}
}
`;

describe('auto-rem', () => {
it('it transforms px to rem values correctly', async () => {
const res = await testTransform(baseInput, { autoRem: true });
expect(res.css).toMatchSnapshot();
});

it('it transforms input with rem function correctly', async () => {
const res = await testTransform(remFunctionInput, { autoRem: true });
expect(res.css).toMatchSnapshot();
});

it('it transforms media query correctly', async () => {
const res = await testTransform(mediaInput, { autoRem: true });
expect(res.css).toMatchSnapshot();
});
});
11 changes: 11 additions & 0 deletions src/tests/rem-em.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const spaceSeparatedInput = `
}
`;

const spaceSeparatedWithNonPxValuesInput = `
.demo {
border: rem(16px solid red);
}
`;

const mediaInput = `
@media (min-width: em(320px)) {
font-size: rem(32px);
Expand Down Expand Up @@ -65,4 +71,9 @@ describe('rem-em', () => {
const res = await testTransform(remEmInsideFunctionInput);
expect(res.css).toMatchSnapshot();
});

it('works with space separated values with non-px values', async () => {
const res = await testTransform(spaceSeparatedWithNonPxValuesInput);
expect(res.css).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import postcss from 'postcss';
const preset = require('../preset');

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

0 comments on commit 428f999

Please sign in to comment.