Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Dec 6, 2024
1 parent 691c521 commit 33a2d9a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/platforms/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
preprocessors: ['themeOverrides'],
transforms: [
'name/pathToKebabCase',
'color/hex',
'color/hexMix',
// 'color/hexMix',
'colorAlpha/css',
'cubicBezier/css',
'dimension/rem',
'duration/css',
'shadow/css',
// 'shadow/css',
'border/css',
'typography/css',
'fontFamily/css',
Expand Down
3 changes: 3 additions & 0 deletions src/primerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
jsonFigma,
} from './formats/index.js'
import {themeOverrides} from './preprocessors/themeOverrides.js'
import {colorAlphaToCss} from './transformers/colorAlphaToCss.js'

/**
* @name {@link PrimerStyleDictionary}
Expand Down Expand Up @@ -100,6 +101,8 @@ PrimerStyleDictionary.registerFormat({
* Transformers
*
*/
PrimerStyleDictionary.registerTransform(colorAlphaToCss)

PrimerStyleDictionary.registerTransform(colorToRgbAlpha)

PrimerStyleDictionary.registerTransform(colorToRgbaFloat)
Expand Down
1 change: 0 additions & 1 deletion src/schemas/colorToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const colorToken = baseToken
.optional(),
$extensions: z
.object({
alpha: z.number().min(0).max(1).optional().nullable(),
'org.primer.figma': z
.object({
collection: collection([
Expand Down
37 changes: 37 additions & 0 deletions src/transformers/colorAlphaToCss.test.ts
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

View workflow job for this annotation

GitHub Actions / Test & Lint

src/transformers/colorAlphaToCss.test.ts > Transformer: colorAlphaToCss > transforms hex3, hex6, hex8 `color` tokens with alpha value

AssertionError: expected [ …(3) ] to strictly equal [ …(3) ] - Expected + Received Array [ - "color-mix(#123, transparent 25%)", - "color-mix(#343434, transparent 60%)", - "color-mix(#34343466, transparent 10%)", + "color-mix(in srgb, #123, transparent 25%)", + "color-mix(in srgb, #343434, transparent 60%)", + "color-mix(in srgb, #34343466, transparent 10%)", ] ❯ src/transformers/colorAlphaToCss.test.ts:26:72
})

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

View workflow job for this annotation

GitHub Actions / Test & Lint

src/transformers/colorAlphaToCss.test.ts > Transformer: colorAlphaToCss > transforms references with and without alpha value

AssertionError: expected [ '{base.color.green.5}', …(1) ] to strictly equal [ '{base.color.green.5}', …(1) ] - Expected + Received Array [ "{base.color.green.5}", - "color-mix({base.color.red.5}, transparent 25%)", + "color-mix(in srgb, {base.color.red.5}, transparent 25%)", ] ❯ src/transformers/colorAlphaToCss.test.ts:35:72
})
})
24 changes: 24 additions & 0 deletions src/transformers/colorAlphaToCss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Transform, TransformedToken} from 'style-dictionary/types'

Check warning on line 1 in src/transformers/colorAlphaToCss.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

All imports in the declaration are only used as types. Use `import type`
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)
},
}
2 changes: 1 addition & 1 deletion src/transformers/colorToHex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const colorToHex: Transform = {
transitive: true,
filter: isColor,
transform: (token: TransformedToken) => {
const alphaValue = token.alpha ?? token.$extensions?.alpha
const alphaValue = token.alpha
if (alphaValue === null || alphaValue === undefined) {
return toHex(getTokenValue(token))
}
Expand Down
Empty file.
Empty file.

0 comments on commit 33a2d9a

Please sign in to comment.