Skip to content

Commit

Permalink
Add % units support to alpha, lighten and darken functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Dec 21, 2023
1 parent 6ab81bd commit ff5c392
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ tsconfig.json
yarn.lock
src
tests
scripts
node_modules
test.ts
.DS_Store
5 changes: 4 additions & 1 deletion src/postcss-color-mix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function getParsedColor(input: unknown) {
return null;
}

const payload = Number(color.slice(lastCommaIndex + 1));
const rawPayload = color.slice(lastCommaIndex + 1).trim();
const payload = rawPayload.endsWith('%')
? Number(rawPayload.slice(0, -1)) / 100
: Number(color.slice(lastCommaIndex + 1));

if (Number.isNaN(payload)) {
return null;
Expand Down
9 changes: 9 additions & 0 deletions src/tests/__snapshots__/color-mix.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ exports[`color-mix correctly replaces lighten function 1`] = `
}
"
`;

exports[`color-mix correctly replaces percentage values 1`] = `
"
.demo {
background: color-mix(in srgb, #f00, white 50%);
border: calc(0.0625rem * var(--mantine-scale)) solid color-mix(in srgb, var(--mantine-color-gray-4), transparent 90%);
}
"
`;
12 changes: 12 additions & 0 deletions src/tests/color-mix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const darkenInput = `
}
`;

const percentageInput = `
.demo {
background: lighten(#f00, 50%);
border: rem(1px) solid alpha(var(--mantine-color-gray-4), 10%);
}
`;

describe('color-mix', () => {
it('correctly replaces alpha function', async () => {
const res = await testTransform(alphaInput);
Expand All @@ -36,4 +43,9 @@ describe('color-mix', () => {
const res = await testTransform(darkenInput);
expect(res.css).toMatchSnapshot();
});

it('correctly replaces percentage values', async () => {
const res = await testTransform(percentageInput);
expect(res.css).toMatchSnapshot();
});
});

0 comments on commit ff5c392

Please sign in to comment.