-
Notifications
You must be signed in to change notification settings - Fork 43
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
de32449
commit a2b1b79
Showing
5 changed files
with
112 additions
and
0 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': patch | ||
--- | ||
|
||
add cubicBezier transformer |
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,22 @@ | ||
import {getMockToken} from '../test-utilities/index.js' | ||
import {isCubicBezier} from './isCubicBezier.js' | ||
|
||
describe('Filter: isCubicBezier', () => { | ||
it('returns true if $type property is `cubicBezier`', () => { | ||
expect(isCubicBezier(getMockToken({$type: 'cubicBezier'}))).toStrictEqual(true) | ||
}) | ||
|
||
it('returns false if $type property is not `cubicBezier`', () => { | ||
expect(isCubicBezier(getMockToken({$type: 'pumpkin'}))).toStrictEqual(false) | ||
}) | ||
|
||
it('returns false if $type property is missing', () => { | ||
expect(isCubicBezier(getMockToken({alpha: 0.4}))).toStrictEqual(false) | ||
}) | ||
|
||
it('returns false if $type property is falsy', () => { | ||
expect(isCubicBezier(getMockToken({$type: false}))).toStrictEqual(false) | ||
expect(isCubicBezier(getMockToken({$type: undefined}))).toStrictEqual(false) | ||
expect(isCubicBezier(getMockToken({$type: null}))).toStrictEqual(false) | ||
}) | ||
}) |
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,11 @@ | ||
import type {TransformedToken} from 'style-dictionary/types' | ||
|
||
/** | ||
* @description Checks if token is of $type `cubicBezier` | ||
* @param token [TransformedToken](https://github.com/amzn/style-dictionary/blob/main/types/TransformedToken.d.ts) | ||
* @returns boolean | ||
*/ | ||
export const isCubicBezier = (token: TransformedToken): boolean => { | ||
const typeValue = token.$type ?? token.type | ||
return typeValue === 'cubicBezier' | ||
} |
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,48 @@ | ||
import {getMockToken} from '../test-utilities/index.js' | ||
import {cubicBezierToCss} from './cubicBezierToCss.js' | ||
|
||
describe('Transformer: cubicBezierToCss', () => { | ||
it('transforms `cubicBezier` token to css cubicBezier function', () => { | ||
const input = getMockToken({ | ||
$value: [0, 0, 1, 1], | ||
}) | ||
|
||
const expectedOutput = 'cubic-bezier(0,0,1,1)' | ||
expect(cubicBezierToCss.transform(input, {}, {})).toStrictEqual(expectedOutput) | ||
}) | ||
|
||
it('throws an error when required values are missing', () => { | ||
// missing blur | ||
expect(() => | ||
cubicBezierToCss.transform( | ||
getMockToken({ | ||
$value: [0, 0, 1], | ||
}), | ||
{}, | ||
{}, | ||
), | ||
).toThrowError() | ||
|
||
// missing spread | ||
expect(() => | ||
cubicBezierToCss.transform( | ||
getMockToken({ | ||
$value: [0, 0, 1, 1, 1, 1], | ||
}), | ||
{}, | ||
{}, | ||
), | ||
).toThrowError() | ||
|
||
// missing offsets | ||
expect(() => | ||
cubicBezierToCss.transform( | ||
getMockToken({ | ||
$value: [0, 0, 1, '1'], | ||
}), | ||
{}, | ||
{}, | ||
), | ||
).toThrowError() | ||
}) | ||
}) |
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 type {PlatformConfig, Transform, TransformedToken} from 'style-dictionary/types' | ||
import {isCubicBezier} from '../filters/isCubicBezier.js' | ||
|
||
/** | ||
* @description converts cubicBezeir tokens array value to a css cubic-bezier | ||
* @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts) | ||
* @matcher matches all tokens of $type `duration` | ||
* @transformer returns a css cubic-bezier function | ||
*/ | ||
export const cubicBezierToCss: Transform = { | ||
name: 'cubicBezeir/css', | ||
type: 'value', | ||
transitive: true, | ||
filter: isCubicBezier, | ||
transform: (token: TransformedToken, _config: PlatformConfig) => { | ||
const value = token.$value ?? token.value | ||
// throw value of more or less than 4 items in array | ||
if (value.length !== 4 || value.some((item: unknown) => typeof item !== 'number')) { | ||
throw new Error( | ||
`Invalid cubicBezier token ${token.path.join('.')}, must be an array with 4 numbers, but got this instead: ${JSON.stringify(value)}`, | ||
) | ||
} | ||
// return value | ||
return `cubic-bezier(${value.join(',')})` | ||
}, | ||
} |