-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Root } from 'postcss'; | ||
|
||
const colorValueRegexp = new RegExp('\\b' + 'alpha' + '\\(([^()]+)\\)', 'g'); | ||
const varRegexp = new RegExp('\\b' + 'alpha' + '\\(([^()]*\\([^()]*\\)[^()]*)+\\)', 'g'); | ||
|
||
function getMixedColor(input: string, colorSpace = 'srgb') { | ||
if (typeof input !== 'string') { | ||
return input; | ||
} | ||
|
||
const lastCommaIndex = input.lastIndexOf(','); | ||
|
||
if (lastCommaIndex === -1) { | ||
return input; | ||
} | ||
|
||
const color = input.slice(0, lastCommaIndex).trim(); | ||
const alpha = Number(input.slice(lastCommaIndex + 1)); | ||
|
||
if (Number.isNaN(alpha)) { | ||
return input; | ||
} | ||
|
||
const clampedAlpha = Math.max(0, Math.min(1, alpha)); | ||
|
||
if (clampedAlpha === 1) { | ||
return color; | ||
} | ||
|
||
if (clampedAlpha === 0) { | ||
return 'transparent'; | ||
} | ||
|
||
const mixPercentage = (1 - clampedAlpha) * 100; | ||
|
||
return `color-mix(in ${colorSpace}, ${color}, transparent ${mixPercentage}%)`; | ||
} | ||
|
||
module.exports = () => { | ||
return { | ||
postcssPlugin: 'postcss-color-mix-alpha', | ||
|
||
Once(root: Root) { | ||
root.replaceValues(colorValueRegexp, { fast: `alpha(` }, (_, values: string) => { | ||
console.log('value', values); | ||
return getMixedColor(values); | ||
}); | ||
root.replaceValues(varRegexp, { fast: `alpha(` }, (_, values: string) => { | ||
console.log('var', values); | ||
return getMixedColor(values); | ||
}); | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports.postcss = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`color-mix-alpha correctly mixes colors 1`] = ` | ||
" | ||
.demo { | ||
background: color-mix(in srgb, #f00, transparent 50%); | ||
border: calc(0.0625rem * var(--mantine-scale)) solid color-mix(in srgb, var(--mantine-color-gray-4), transparent 90%); | ||
} | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { testTransform } from './utils'; | ||
|
||
const input = ` | ||
.demo { | ||
background: alpha(#f00, 0.5); | ||
border: rem(1px) solid alpha(var(--mantine-color-gray-4), 0.1); | ||
} | ||
`; | ||
|
||
describe('color-mix-alpha', () => { | ||
it('correctly mixes colors', async () => { | ||
const res = await testTransform(input); | ||
expect(res.css).toMatchSnapshot(); | ||
}); | ||
}); |