Skip to content

Commit a87cfcf

Browse files
adding transforms
1 parent 462e488 commit a87cfcf

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

src/platforms/css.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
3737
'typography/css',
3838
'fontFamily/css',
3939
'fontWeight/number',
40+
'gradient/css',
4041
],
4142
options: {
4243
basePxFontSize: 16,

src/primerStyleDictionary.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
fontFamilyToCss,
1313
fontFamilyToFigma,
1414
fontWeightToNumber,
15+
gradientToCss,
1516
jsonDeprecated,
1617
namePathToDotNotation,
1718
namePathToCamelCase,
@@ -157,4 +158,6 @@ PrimerStyleDictionary.registerTransform(fontFamilyToCss)
157158

158159
PrimerStyleDictionary.registerTransform(fontFamilyToFigma)
159160

161+
PrimerStyleDictionary.registerTransform(gradientToCss)
162+
160163
PrimerStyleDictionary.registerPreprocessor(themeOverrides)

src/schemas/gradientToken.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ describe('Schema: gradientToken', () => {
3232
},
3333
],
3434
$type: 'gradient',
35+
$extensions: {
36+
'org.primer.gradient': {
37+
angle: 45,
38+
},
39+
},
3540
},
3641
]
3742

src/schemas/gradientToken.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ export const gradientToken = baseToken
1818
referenceValue,
1919
]),
2020
$type: tokenType('gradient'),
21+
$extensions: z
22+
.object({
23+
'org.primer.gradient': z.object({
24+
angle: z.number().int().min(0).max(360).optional(),
25+
}),
26+
})
27+
.optional(),
2128
})
2229
.strict()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {getMockToken} from '../test-utilities/index.js'
2+
import {gradientToCss} from './gradientToCss.js'
3+
4+
describe('Transformer: gradientToCss', () => {
5+
it('transforms a simple `gradient` token to css gradient string', () => {
6+
const input = getMockToken({
7+
$value: [
8+
{
9+
color: '#000000',
10+
position: 0,
11+
},
12+
{
13+
color: '#ffffff',
14+
position: 1,
15+
},
16+
],
17+
$type: 'gradient',
18+
})
19+
20+
const expectedOutput = 'linear-gradient(180deg, #000000 0%, #ffffff 100%)'
21+
expect(gradientToCss.transform(input, {}, {})).toStrictEqual(expectedOutput)
22+
})
23+
24+
it('transforms complex `gradient` token to css gradient string', () => {
25+
const input = getMockToken({
26+
$value: [
27+
{
28+
color: '#000000',
29+
position: 0,
30+
},
31+
{
32+
color: '#555',
33+
position: 0.3,
34+
},
35+
{
36+
color: '#ffffff',
37+
position: 0.5,
38+
},
39+
],
40+
$type: 'gradient',
41+
$extensions: {
42+
'org.primer.gradient': {
43+
angle: 45,
44+
},
45+
},
46+
})
47+
48+
const expectedOutput = 'linear-gradient(45deg, #000000 0%, #555555 30%, #ffffff 50%)'
49+
expect(gradientToCss.transform(input, {}, {})).toStrictEqual(expectedOutput)
50+
})
51+
})

src/transformers/gradientToCss.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {toHex} from 'color2k'
2+
import {isGradient} from '../filters/isGradient.js'
3+
import {getTokenValue} from './utilities/getTokenValue.js'
4+
import type {Transform, TransformedToken} from 'style-dictionary/types'
5+
6+
/**
7+
* @description converts gradient tokens value to css gradient
8+
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
9+
* @matcher matches all tokens of $type `gradient`
10+
* @transformer returns a `css gradient` string
11+
*/
12+
export const gradientToCss: Transform = {
13+
name: 'gradient/css',
14+
type: 'value',
15+
transitive: true,
16+
filter: isGradient,
17+
transform: (token: TransformedToken) => {
18+
const {angle} = token.$extensions?.['org.primer.gradient'] ?? {}
19+
const stops = getTokenValue(token).map(({color, position}: {color: string; position: number}) => {
20+
return `${toHex(color)} ${position * 100}%`
21+
})
22+
23+
return `linear-gradient(${angle || 180}deg, ${stops.join(', ')})`
24+
},
25+
}

src/transformers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {colorToRgbaFloat} from './colorToRgbaFloat.js'
55
export {cubicBezierToCss} from './cubicBezierToCss.js'
66
export {floatToPixel} from './floatToPixel.js'
77
export {floatToPixelUnitless} from './floatToPixel.js'
8+
export {gradientToCss} from './gradientToCss.js'
89
export {dimensionToRem} from './dimensionToRem.js'
910
export {dimensionToRemPxArray} from './dimensionToRemPxArray.js'
1011
export {dimensionToPixelUnitless} from './dimensionToPixelUnitless.js'

0 commit comments

Comments
 (0)