-
Notifications
You must be signed in to change notification settings - Fork 44
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
1 parent
691c521
commit 33a2d9a
Showing
8 changed files
with
68 additions
and
5 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
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,37 @@ | ||
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(#123, transparent 25%)', | ||
'color-mix(#343434, transparent 60%)', | ||
'color-mix(#34343466, transparent 10%)', | ||
] | ||
expect(input.map(item => colorAlphaToCss.transform(item, {}, {}))).toStrictEqual(expectedOutput) | ||
Check failure on line 26 in src/transformers/colorAlphaToCss.test.ts GitHub Actions / Test & Lintsrc/transformers/colorAlphaToCss.test.ts > Transformer: colorAlphaToCss > transforms hex3, hex6, hex8 `color` tokens with alpha value
|
||
}) | ||
|
||
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({base.color.red.5}, transparent 25%)'] | ||
expect(input.map(item => colorAlphaToCss.transform(item, {}, {}))).toStrictEqual(expectedOutput) | ||
Check failure on line 35 in src/transformers/colorAlphaToCss.test.ts GitHub Actions / Test & Lintsrc/transformers/colorAlphaToCss.test.ts > Transformer: colorAlphaToCss > transforms references with and without alpha value
|
||
}) | ||
}) |
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,24 @@ | ||
import {Transform, TransformedToken} from 'style-dictionary/types' | ||
import {isColorWithAlpha} from '../filters/isColorWithAlpha.js' | ||
import {getTokenValue} from './utilities/getTokenValue.js' | ||
|
||
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.`, | ||
) | ||
} | ||
|
||
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', 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
Empty file.
Empty file.