Skip to content

Commit

Permalink
Figma token name transformer (#698)
Browse files Browse the repository at this point in the history
* added transformer that changes specific token names to be better represented in figma

* Update tidy-readers-type.md
  • Loading branch information
lukasoppermann authored Jul 21, 2023
1 parent a911592 commit 870cc60
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-readers-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

Update figma token names & rename `fgColor-link-rest` to `fgColor-link`
11 changes: 11 additions & 0 deletions src/PrimerStyleDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
colorToRgbaFloat,
namePathToSlashNotation,
figmaAttributes,
namePathToFigma,
} from './transformers'
import {
scssMixinCssVariables,
Expand Down Expand Up @@ -154,11 +155,21 @@ StyleDictionary.registerTransform({
...jsonDeprecated,
})

StyleDictionary.registerTransform({
name: 'name/pathToCamelCase',
...namePathToCamelCase,
})

StyleDictionary.registerTransform({
name: 'name/pathToDotNotation',
...namePathToDotNotation,
})

StyleDictionary.registerTransform({
name: 'name/pathToFigma',
...namePathToFigma,
})

StyleDictionary.registerTransform({
name: 'name/pathToCamelCase',
...namePathToCamelCase,
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/figma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const figma: PlatformInitializer = (outputFile, prefix, buildPath, option
buildPath,
transforms: [
'color/rgbaFloat',
'name/pathToSlashNotation',
'name/pathToFigma',
// 'name/pathToSlashNotation',
'figma/attributes',
'dimension/pixelUnitless',
// 'border/figma',
Expand Down
16 changes: 7 additions & 9 deletions src/tokens/functional/color/dark/primitives-dark.json5
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@
},
},
link: {
rest: {
$value: '{fgColor.accent}',
$type: 'color',
$extensions: {
'org.primer.figma': {
collection: 'mode',
mode: 'dark',
scopes: ['fgColor'],
},
$value: '{fgColor.accent}',
$type: 'color',
$extensions: {
'org.primer.figma': {
collection: 'mode',
mode: 'dark',
scopes: ['fgColor'],
},
},
},
Expand Down
16 changes: 7 additions & 9 deletions src/tokens/functional/color/light/primitives-light.json5
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,13 @@
},
},
link: {
rest: {
$value: '{fgColor.accent}',
$type: 'color',
$extensions: {
'org.primer.figma': {
collection: 'mode',
mode: 'light',
scopes: ['fgColor'],
},
$value: '{fgColor.accent}',
$type: 'color',
$extensions: {
'org.primer.figma': {
collection: 'mode',
mode: 'light',
scopes: ['fgColor'],
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export {figmaAttributes} from './figmaAttributes'
export {fontFamilyToCss} from './fontFamilyToCss'
export {fontWeightToNumber} from './fontWeightToNumber'
export {jsonDeprecated} from './jsonDeprecated'
export {namePathToDotNotation} from './namePathToDotNotation'
export {namePathToCamelCase} from './namePathToCamelCase'
export {namePathToDotNotation} from './namePathToDotNotation'
export {namePathToFigma} from './namePathToFigma'
export {namePathToKebabCase} from './namePathToKebabCase'
export {namePathToSlashNotation} from './namePathToSlashNotation'
export {shadowToCss} from './shadowToCss'
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/namePathToDotNotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const camelCase = (string: string): string => {
* @description converts the [TransformedToken's](https://github.com/amzn/style-dictionary/blob/main/types/TransformedToken.d.ts) `.path` array to a dot.notation string
* @type name transformer — [StyleDictionary.NameTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher omitted to match all tokens
* @transformer returns `string` on dot.notation
* @transformer returns `string` in dot.notation
*/
export const namePathToDotNotation: StyleDictionary.Transform = {
type: `name`,
Expand Down
23 changes: 23 additions & 0 deletions src/transformers/namePathToFigma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type StyleDictionary from 'style-dictionary'

/**
* @description converts the [TransformedToken's](https://github.com/amzn/style-dictionary/blob/main/types/TransformedToken.d.ts) `.path` array to a specific figma name
* @type name transformer — [StyleDictionary.NameTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher omitted to match all tokens
* @transformer returns `string`
*/
export const namePathToFigma: StyleDictionary.Transform = {
type: `name`,
transformer: (token: StyleDictionary.TransformedToken, options?: StyleDictionary.Platform): string => {
let pathArray = token.path
if (['fgColor', 'borderColor', 'bgColor'].includes(pathArray[0]) && pathArray.length === 3) {
pathArray = [pathArray[0], `${pathArray[1]}-${pathArray[2]}`]
}
return (
[options?.prefix, ...pathArray]
// remove undefined if exists
.filter((part: unknown): part is string => typeof part === 'string')
.join('/')
)
},
}

0 comments on commit 870cc60

Please sign in to comment.