-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add color/Alpha transformer * use cssColorMix in shadows * add changeset * dont apply new transformer
- Loading branch information
1 parent
02776ce
commit da8a1d9
Showing
6 changed files
with
85 additions
and
2 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,5 @@ | ||
--- | ||
'@primer/primitives': minor | ||
--- | ||
|
||
use cssColorMix instead of converting variables to hex when we use transparency |
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
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,49 @@ | ||
import {getMockToken} from '../test-utilities/index.js' | ||
import {colorAlphaToCss} from './colorAlphaToCss.js' | ||
|
||
describe('Transformer: colorAlphaToCss', () => { | ||
it('transforms hex3, hex6, hex8 `color` tokens without alpha value', () => { | ||
const input = [ | ||
getMockToken({$value: '#123'}), | ||
getMockToken({$value: '#343434'}), | ||
getMockToken({$value: '#34343455'}), | ||
] | ||
const expectedOutput = ['#123', '#343434', '#34343455'] | ||
expect(input.map(item => colorAlphaToCss.transform(item, {}, {}))).toStrictEqual(expectedOutput) | ||
}) | ||
|
||
it('transforms hex3, hex6, hex8 `color` tokens with alpha value', () => { | ||
const input = [ | ||
getMockToken({$value: '#123', alpha: 0.25}), | ||
getMockToken({$value: '#343434', alpha: 0.6}), | ||
getMockToken({$value: '#34343466', alpha: 0.1}), | ||
] | ||
const expectedOutput = [ | ||
'color-mix(in srgb, #123, transparent 75%)', | ||
'color-mix(in srgb, #343434, transparent 40%)', | ||
'color-mix(in srgb, #34343466, transparent 90%)', | ||
] | ||
expect(input.map(item => colorAlphaToCss.transform(item, {}, {}))).toStrictEqual(expectedOutput) | ||
}) | ||
|
||
it('transforms references with and without alpha value', () => { | ||
const input = [ | ||
getMockToken({$value: '{base.color.green.5}'}), | ||
getMockToken({$value: '{base.color.red.5}', alpha: 0.25}), | ||
] | ||
const expectedOutput = ['{base.color.green.5}', 'color-mix(in srgb, {base.color.red.5}, transparent 75%)'] | ||
expect(input.map(item => colorAlphaToCss.transform(item as TransformedToken, {}, {}))).toStrictEqual(expectedOutput) | ||
}) | ||
|
||
it('transforms color-mix with and without alpha value', () => { | ||
const input = [ | ||
getMockToken({$value: 'color-mix(in srgb, {base.color.red.5}, transparent 25%)'}), | ||
getMockToken({$value: 'color-mix(in srgb, {base.color.red.5}, transparent 25%)', alpha: 0.35}), | ||
] | ||
const expectedOutput = [ | ||
'color-mix(in srgb, {base.color.red.5}, transparent 25%)', | ||
'color-mix(in srgb, color-mix(in srgb, {base.color.red.5}, transparent 25%), transparent 65%)', | ||
] | ||
expect(input.map(item => colorAlphaToCss.transform(item as TransformedToken, {}, {}))).toStrictEqual(expectedOutput) | ||
}) | ||
}) |
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,26 @@ | ||
import {Transform, TransformedToken} from 'style-dictionary/types' | ||
import {isColorWithAlpha} from '../filters/isColorWithAlpha.js' | ||
import {getTokenValue} from './utilities/getTokenValue.js' | ||
|
||
export const cssColorMix = (colorA: string, colorB: string, colorBPercent: number) => { | ||
if (colorBPercent < 0 || colorBPercent > 1) { | ||
throw new Error( | ||
`Invalid argument for "cssColorMix", colorBPercent must be between 0 and 1, ${colorBPercent} provided.`, | ||
) | ||
} | ||
if (colorBPercent === 0) return colorA | ||
if (colorBPercent === 1) return colorB | ||
|
||
return `color-mix(in srgb, ${colorA}, ${colorB} ${colorBPercent * 100}%)` | ||
} | ||
|
||
export const colorAlphaToCss: Transform = { | ||
name: 'colorAlpha/css', | ||
type: 'value', | ||
transitive: true, | ||
filter: isColorWithAlpha, | ||
transform: (token: TransformedToken) => { | ||
if (!token.alpha || token.alpha === null) return getTokenValue(token) | ||
return cssColorMix(getTokenValue(token), 'transparent', 1 - token.alpha) | ||
}, | ||
} |
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