Skip to content

Commit ff5c392

Browse files
committed
Add % units support to alpha, lighten and darken functions
1 parent 6ab81bd commit ff5c392

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tsconfig.json
55
yarn.lock
66
src
77
tests
8+
scripts
89
node_modules
910
test.ts
1011
.DS_Store

src/postcss-color-mix.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ function getParsedColor(input: unknown) {
2323
return null;
2424
}
2525

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

2831
if (Number.isNaN(payload)) {
2932
return null;

src/tests/__snapshots__/color-mix.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,12 @@ exports[`color-mix correctly replaces lighten function 1`] = `
2626
}
2727
"
2828
`;
29+
30+
exports[`color-mix correctly replaces percentage values 1`] = `
31+
"
32+
.demo {
33+
background: color-mix(in srgb, #f00, white 50%);
34+
border: calc(0.0625rem * var(--mantine-scale)) solid color-mix(in srgb, var(--mantine-color-gray-4), transparent 90%);
35+
}
36+
"
37+
`;

src/tests/color-mix.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ const darkenInput = `
2121
}
2222
`;
2323

24+
const percentageInput = `
25+
.demo {
26+
background: lighten(#f00, 50%);
27+
border: rem(1px) solid alpha(var(--mantine-color-gray-4), 10%);
28+
}
29+
`;
30+
2431
describe('color-mix', () => {
2532
it('correctly replaces alpha function', async () => {
2633
const res = await testTransform(alphaInput);
@@ -36,4 +43,9 @@ describe('color-mix', () => {
3643
const res = await testTransform(darkenInput);
3744
expect(res.css).toMatchSnapshot();
3845
});
46+
47+
it('correctly replaces percentage values', async () => {
48+
const res = await testTransform(percentageInput);
49+
expect(res.css).toMatchSnapshot();
50+
});
3951
});

0 commit comments

Comments
 (0)