Skip to content

Commit bf4f95a

Browse files
fix transformer
1 parent 021eeb2 commit bf4f95a

File tree

3 files changed

+36
-81
lines changed

3 files changed

+36
-81
lines changed

src/transformers/cubicBezierToCss.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ export const cubicBezierToCss: Transform = {
1414
filter: isCubicBezier,
1515
transform: (token: TransformedToken, _config: PlatformConfig) => {
1616
const value = token.$value ?? token.value
17-
// throw value of more or less than 4 items in array
18-
if (value.length !== 4 || value.some((item: unknown) => typeof item !== 'number')) {
19-
throw new Error(
20-
`Invalid cubicBezier token ${token.path.join('.')}, must be an array with 4 numbers, but got this instead: ${JSON.stringify(value)}`,
21-
)
22-
}
23-
// return value
24-
return `cubic-bezier(${value.join(',')})`
17+
return cubicBezierArrayToCss(value, token.path)
2518
},
2619
}
20+
21+
export const cubicBezierArrayToCss = (value: number[], path: string[]) => {
22+
// throw value of more or less than 4 items in array
23+
if (value.length !== 4 || value.some((item: unknown) => typeof item !== 'number')) {
24+
throw new Error(
25+
`Invalid cubicBezier token ${path.join('.')}, must be an array with 4 numbers, but got this instead: ${JSON.stringify(value)}`,
26+
)
27+
}
28+
// return value
29+
return `cubic-bezier(${value.join(',')})`
30+
}

src/transformers/transitionToCss.test.ts

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,130 +6,80 @@ describe('transitionToCss', () => {
66
const token = getMockToken({
77
$value: {
88
duration: '300ms',
9-
timing: [0.4, 0, 0.2, 1],
9+
timingFunction: [0.4, 0, 0.2, 1],
1010
},
1111
$type: 'transition',
1212
})
1313

14-
expect(transitionToCss.transform(token, {}, {})).toBe('300ms cubic-bezier(0.4, 0, 0.2, 1)')
14+
expect(transitionToCss.transform(token, {}, {})).toBe('300ms cubic-bezier(0.4,0,0.2,1)')
1515
})
1616

1717
it('should transform transition token with delay to CSS', () => {
1818
const token = getMockToken({
1919
$value: {
2020
duration: '300ms',
21-
timing: [0.4, 0, 0.2, 1],
21+
timingFunction: [0.4, 0, 0.2, 1],
2222
delay: '100ms',
2323
},
2424
$type: 'transition',
2525
})
2626

27-
expect(transitionToCss.transform(token, {}, {})).toBe('300ms cubic-bezier(0.4, 0, 0.2, 1) 100ms')
27+
expect(transitionToCss.transform(token, {}, {})).toBe('300ms cubic-bezier(0.4,0,0.2,1) 100ms')
2828
})
2929

3030
it('should transform transition token with resolved references', () => {
3131
const token = {
3232
$value: {
3333
duration: '300ms',
34-
timing: [0.4, 0, 0.2, 1],
34+
timingFunction: [0.4, 0, 0.2, 1],
3535
delay: '0ms',
3636
},
3737
$type: 'transition',
3838
}
3939
const resolvedRefs = {
4040
duration: '300ms',
41-
timing: [0.4, 0, 0.2, 1],
41+
timingFunction: [0.4, 0, 0.2, 1],
4242
delay: '0ms',
4343
}
4444

45-
expect(transitionToCss.transform(token, resolvedRefs)).toBe('300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms')
46-
})
47-
48-
it('should transform transition token with mixed values and references', () => {
49-
const token = getMockToken({
50-
$value: {
51-
duration: '{motion.duration.base.value}',
52-
timing: [0.4, 0, 0.2, 1],
53-
delay: '0ms',
54-
},
55-
$type: 'transition',
56-
})
57-
58-
expect(transitionToCss.transform(token, resolvedRefs)).toBe('300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms')
59-
})
60-
61-
it('should handle common timing function aliases', () => {
62-
const token = {
63-
$value: {
64-
duration: '300ms',
65-
timing: [0.4, 0, 1, 1],
66-
},
67-
$type: 'transition',
68-
}
69-
70-
expect(transitionToCss.transform(token, {}, {})).toBe('300ms ease-out')
71-
})
72-
73-
it('should handle direct reference token', () => {
74-
const token = {
75-
$value: '{motion.transition.standard}',
76-
$type: 'transition',
77-
}
78-
const resolvedRefs = {
79-
'motion.transition.standard': {
80-
duration: '300ms',
81-
timing: [0.4, 0, 0.2, 1],
82-
delay: '0ms',
83-
},
84-
}
85-
86-
expect(transitionToCss.transform(token, resolvedRefs)).toBe('300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms')
45+
expect(transitionToCss.transform(token, resolvedRefs)).toBe('300ms cubic-bezier(0.4,0,0.2,1) 0ms')
8746
})
8847

8948
it('should handle missing optional delay', () => {
90-
const token = {
49+
const token = getMockToken({
9150
$value: {
9251
duration: '300ms',
93-
timing: [0.4, 0, 0.2, 1],
52+
timingFunction: [0.4, 0, 0.2, 1],
9453
},
9554
$type: 'transition',
96-
}
97-
98-
expect(transitionToCss.transform(token, {}, {})).toBe('300ms cubic-bezier(0.4, 0, 0.2, 1)')
99-
})
100-
101-
it('should throw error for invalid token type', () => {
102-
const token = {
103-
$value: {
104-
duration: '300ms',
105-
timing: [0.4, 0, 0.2, 1],
106-
},
107-
$type: 'invalid',
108-
}
55+
})
10956

110-
expect(() => transitionToCss.transform(token, {}, {})).toThrow('Invalid token type: invalid')
57+
expect(transitionToCss.transform(token, {}, {})).toBe('300ms cubic-bezier(0.4,0,0.2,1)')
11158
})
11259

11360
it('should throw error for missing required properties', () => {
114-
const token = {
61+
const token = getMockToken({
11562
$value: {
11663
duration: '300ms',
11764
},
11865
$type: 'transition',
119-
}
120-
// @ts-expect-error: testing invalid token
121-
expect(() => transitionToCss.transform(token, {}, {})).toThrow('Missing required property: timing')
66+
})
67+
expect(() => transitionToCss.transform(token, {}, {})).toThrow(
68+
'Missing property: timingFunction on token with value {"duration":"300ms"}',
69+
)
12270
})
12371

12472
it('should throw error for invalid timing value', () => {
125-
const token = {
73+
const token = getMockToken({
12674
$value: {
12775
duration: '300ms',
128-
timing: [0.4, 0, 0.2], // Missing one value
76+
timingFunction: [0.4, 0, 0.2], // Missing one value
12977
},
13078
$type: 'transition',
131-
}
79+
})
13280

133-
expect(() => transitionToCss.transform(token, {}, {})).toThrow('Invalid timing value')
81+
expect(() => transitionToCss.transform(token, {}, {})).toThrow(
82+
'Invalid cubicBezier token path, must be an array with 4 numbers, but got this instead: [0.4,0,0.2]',
83+
)
13484
})
13585
})

src/transformers/transitionToCss.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {isTransition} from '../filters/index.js'
2+
import {cubicBezierArrayToCss} from './cubicBezierToCss.js'
23
import {checkRequiredTokenProperties} from './utilities/checkRequiredTokenProperties.js'
34
import {getTokenValue} from './utilities/getTokenValue.js'
45
import type {Transform, TransformedToken} from 'style-dictionary/types'
@@ -26,6 +27,6 @@ export const transitionToCss: Transform = {
2627
// check required properties
2728
checkRequiredTokenProperties(value, ['duration', 'timingFunction'])
2829

29-
return `${value.duration} ${value.timingFunction} ${value.delay ? value.delay : ''}`.trim()
30+
return `${value.duration} ${cubicBezierArrayToCss(value.timingFunction, token.path)} ${value.delay ? value.delay : ''}`.trim()
3031
},
3132
}

0 commit comments

Comments
 (0)