Skip to content

Commit

Permalink
Adding a group prop for figma to the tokens (#723)
Browse files Browse the repository at this point in the history
* adding a group prop for figma to the tokens

* added changeset

* working on groups

* making groups work

* github-actions[bot] Regenerated snapshots

---------

Co-authored-by: lukasoppermann <lukasoppermann@users.noreply.github.com>
  • Loading branch information
lukasoppermann and lukasoppermann authored Sep 12, 2023
1 parent c85c3d1 commit a3b0d21
Show file tree
Hide file tree
Showing 14 changed files with 1,114 additions and 476 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-rockets-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

Adding group prop to figma output
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 23 additions & 11 deletions scripts/buildPlatforms/buildFigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export const buildFigma = (buildOptions: ConfigGeneratorOptions): void => {
name: 'dark',
source: [`src/tokens/base/color/dark/dark.json5`],
},
// {
// name: 'dark-dimmed',
// source: [`src/tokens/base/color/dark/dark.json5`, `src/tokens/base/color/dark/dark.dimmed.json5`],
// },
]

for (const {name, source} of baseScales) {
Expand All @@ -34,7 +30,7 @@ export const buildFigma = (buildOptions: ConfigGeneratorOptions): void => {
}
//
for (const {filename, source, include} of themes) {
if (['light', 'dark' /*, 'dark-dimmed'*/].includes(filename)) {
if (['light', 'dark'].includes(filename)) {
// build functional scales
PrimerStyleDictionary.extend({
source,
Expand Down Expand Up @@ -83,15 +79,31 @@ export const buildFigma = (buildOptions: ConfigGeneratorOptions): void => {
const tokens: {
collection: string
mode: string
group: string
name: string
}[] = files.flatMap(filePath => JSON.parse(fs.readFileSync(filePath, 'utf8')))
const collections: Record<string, string[]> = {}
// create a list of groups with collections and modes
const collections: Record<
string,
{
modes: string[]
groups: string[]
}
> = {}

for (const {collection, mode} of tokens) {
for (const {collection, mode, group} of tokens) {
if (!(collection in collections)) {
collections[collection] = []
collections[collection] = {
modes: [],
groups: [],
}
}
if (!collections[collection].includes(mode)) {
collections[collection].push(mode)
if (!collections[collection].modes.includes(mode)) {
collections[collection].modes.push(mode)
}

if (!collections[collection].groups.includes(group)) {
collections[collection].groups.push(group)
}
}

Expand All @@ -100,7 +112,7 @@ export const buildFigma = (buildOptions: ConfigGeneratorOptions): void => {
const modeOrder = ['light', 'dark'].reverse()
// sort modes in the order defined above
for (const collection in collections) {
collections[collection].sort((a, b) => modeOrder.indexOf(b) - modeOrder.indexOf(a))
collections[collection].modes.sort((a, b) => modeOrder.indexOf(b) - modeOrder.indexOf(a))
}
// write to file
fs.writeFileSync(`${buildOptions.buildPath}figma/figma.json`, JSON.stringify({collections, files}, null, 2))
Expand Down
3 changes: 2 additions & 1 deletion src/formats/jsonFigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const jsonFigma: StyleDictionary.Formatter = ({dictionary, file: _file, p
// sort tokens by reference
const tokens = dictionary.allTokens.sort(sortByReference(dictionary)).map(token => {
const {attributes, value, $type, comment: description, original, alpha, mix} = token
const {mode, collection, scopes} = attributes || {}
const {mode, collection, scopes, group} = attributes || {}

return {
name: token.name,
Expand All @@ -50,6 +50,7 @@ export const jsonFigma: StyleDictionary.Formatter = ({dictionary, file: _file, p
reference: getReference(dictionary, original.value, platform),
collection,
mode,
group,
scopes,
}
})
Expand Down
Loading

0 comments on commit a3b0d21

Please sign in to comment.