Repository navigation
Theme: Generate complete border token modes for Figma testing #84035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
153bb4d
Theme: Generate complete border token modes for Figma testing
ciampo 0ca2ab2
Theme: Add changelog entry for Figma import samples
ciampo cc9d33d
Theme: Commit validated Figma mode artifacts and build them by default
ciampo d399f09
Theme: Remove redundant Figma generator tests
ciampo 4f0d654
Theme: Format Figma artifacts in the shared postbuild step
ciampo 206948c
Theme: Document the complete build for Figma artifacts
ciampo 47db752
Merge branch 'trunk' into codex/theme-figma-mode-imports
ciampo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,60 @@ | ||
| import type { Resolver } from '@terrazzo/parser'; | ||
|
|
||
| /** | ||
| * Generate complete DTCG files for independent Figma border collections. | ||
| * | ||
| * @param resolver The parsed design token resolver. | ||
| * @return One import file per collection mode. | ||
| */ | ||
| export function generateFigmaFiles( resolver: Resolver ) { | ||
| const files: { filename: string; contents: string }[] = []; | ||
|
|
||
| for ( const [ group, modifier ] of [ | ||
| [ 'radius', 'corner-radius' ], | ||
| [ 'width', 'pixel-density' ], | ||
| ] ) { | ||
| const contexts = resolver.source.modifiers?.[ modifier ]?.contexts; | ||
| if ( ! contexts ) { | ||
| throw new Error( | ||
| `Missing Figma collection modifier: ${ modifier }` | ||
| ); | ||
| } | ||
|
|
||
| for ( const mode of Object.keys( contexts ) ) { | ||
| const tokens = resolver.apply( | ||
| { [ modifier ]: mode }, | ||
| { modifiers: [ modifier ], resolveAliases: false } | ||
| ); | ||
| const contents: Record< string, unknown > = {}; | ||
|
|
||
| for ( const [ id, token ] of Object.entries( tokens ) ) { | ||
| if ( ! id.startsWith( `wpds-border.${ group }.` ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Keep the original hierarchy so Figma variable names still match. | ||
| const path = id.split( '.' ); | ||
| const name = path.pop()!; | ||
| let parent = contents; | ||
| for ( const segment of path ) { | ||
| parent[ segment ] ??= {}; | ||
| parent = parent[ segment ] as Record< string, unknown >; | ||
| } | ||
| parent[ name ] = { | ||
| $type: token.$type, | ||
| $value: token.$value, | ||
| $description: token.$description, | ||
| $extensions: token.$extensions, | ||
| $deprecated: token.$deprecated, | ||
| }; | ||
| } | ||
|
|
||
| files.push( { | ||
| filename: `${ group }/${ mode }.json`, | ||
| contents: JSON.stringify( contents, null, '\t' ) + '\n', | ||
| } ); | ||
| } | ||
| } | ||
|
|
||
| return files; | ||
| } |
This file contains hidden or 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,23 @@ | ||
| import { mkdir, readFile, writeFile } from 'node:fs/promises'; | ||
| import { dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { parse } from '@terrazzo/parser'; | ||
| import config from '../../terrazzo.config.ts'; | ||
| import { generateFigmaFiles } from './generate.ts'; | ||
|
|
||
| const sources = await Promise.all( | ||
| config.tokens.map( async ( filename: URL ) => ( { | ||
| filename, | ||
| src: await readFile( filename, 'utf8' ), | ||
| } ) ) | ||
| ); | ||
| const { resolver } = await parse( sources, { config } ); | ||
|
|
||
| for ( const file of generateFigmaFiles( resolver ) ) { | ||
| const filename = new URL( | ||
| `../../figma/${ file.filename }`, | ||
| import.meta.url | ||
| ); | ||
| await mkdir( dirname( fileURLToPath( filename ) ), { recursive: true } ); | ||
| await writeFile( filename, file.contents ); | ||
| } |
161 changes: 161 additions & 0 deletions
161
packages/theme/bin/build-figma-tokens/test/generate.test.ts
This file contains hidden or 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,161 @@ | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { beforeAll, describe, expect, it } from 'vitest'; | ||
| import { defineConfig, parse } from '@terrazzo/parser'; | ||
| import config from '../../../terrazzo.config.ts'; | ||
| import { generateFigmaFiles } from '../generate.ts'; | ||
|
|
||
| describe( 'Figma border collection files', () => { | ||
| let files: ReturnType< typeof generateFigmaFiles >; | ||
| let border: Record< | ||
| string, | ||
| Record< | ||
| string, | ||
| { | ||
| $description?: string; | ||
| $extensions?: Record< string, unknown >; | ||
| } | ||
| > | ||
| >; | ||
|
|
||
| beforeAll( async () => { | ||
| const sources = await Promise.all( | ||
| config.tokens.map( async ( filename: URL ) => ( { | ||
| filename, | ||
| src: await readFile( filename, 'utf8' ), | ||
| } ) ) | ||
| ); | ||
| const { resolver } = await parse( sources, { config } ); | ||
| files = generateFigmaFiles( resolver ); | ||
| border = JSON.parse( | ||
| await readFile( | ||
| new URL( '../../../tokens/border.json', import.meta.url ), | ||
| 'utf8' | ||
| ) | ||
| )[ 'wpds-border' ]; | ||
| } ); | ||
|
|
||
| it( 'creates complete mode files with the original names, types, descriptions, and scopes', () => { | ||
| expect( files.map( ( file ) => file.filename ) ).toEqual( [ | ||
| 'radius/none.json', | ||
| 'radius/subtle.json', | ||
| 'radius/moderate.json', | ||
| 'radius/pronounced.json', | ||
| 'width/standard.json', | ||
| 'width/high-dpi.json', | ||
| ] ); | ||
|
|
||
| for ( const file of files ) { | ||
| const group = file.filename.split( '/' )[ 0 ]; | ||
| const document = JSON.parse( file.contents ); | ||
| expect( Object.keys( document ) ).toEqual( [ 'wpds-border' ] ); | ||
| expect( Object.keys( document[ 'wpds-border' ] ) ).toEqual( [ | ||
| group, | ||
| ] ); | ||
| const tokens = document[ 'wpds-border' ][ group ]; | ||
| const names = Object.keys( border[ group ] ).filter( | ||
| ( name ) => ! name.startsWith( '$' ) | ||
| ); | ||
| expect( Object.keys( tokens ) ).toEqual( names ); | ||
| for ( const name of names ) { | ||
| expect( tokens[ name ] ).toEqual( { | ||
| $type: 'dimension', | ||
| $value: { value: expect.any( Number ), unit: 'px' }, | ||
| $description: border[ group ][ name ].$description, | ||
| $extensions: border[ group ][ name ].$extensions, | ||
| } ); | ||
| } | ||
| } | ||
| } ); | ||
|
|
||
| it( 'keeps radius and width modes independent', () => { | ||
| const values = Object.fromEntries( | ||
| files.map( ( file ) => { | ||
| const group = file.filename.split( '/' )[ 0 ]; | ||
| const tokens: Record< string, { $value: { value: number } } > = | ||
| JSON.parse( file.contents )[ 'wpds-border' ][ group ]; | ||
| return [ | ||
| file.filename, | ||
| Object.values( tokens ).map( | ||
| ( token ) => token.$value.value | ||
| ), | ||
| ]; | ||
| } ) | ||
| ); | ||
| expect( values ).toEqual( { | ||
| 'radius/none.json': [ 0, 0, 0, 0, 0 ], | ||
| 'radius/subtle.json': [ 1, 2, 4, 8, 12 ], | ||
| 'radius/moderate.json': [ 6, 8, 12, 16, 20 ], | ||
| 'radius/pronounced.json': [ 18, 20, 22, 24, 26 ], | ||
| 'width/standard.json': [ 1, 2, 4, 8, 2 ], | ||
| 'width/high-dpi.json': [ 1, 2, 4, 8, 1.5 ], | ||
| } ); | ||
| } ); | ||
|
|
||
| it( 'preserves token aliases within a collection', async () => { | ||
| const { resolver } = await parse( | ||
| [ | ||
| { | ||
| filename: new URL( | ||
| 'aliases.resolver.json', | ||
| import.meta.url | ||
| ), | ||
| src: { | ||
| version: '2025.10', | ||
| sets: { | ||
| base: { | ||
| sources: [ | ||
| { | ||
| 'wpds-border': { | ||
| $type: 'dimension', | ||
| radius: { | ||
| sm: { | ||
| $value: { | ||
| value: 2, | ||
| unit: 'px', | ||
| }, | ||
| }, | ||
| control: { | ||
| $value: '{wpds-border.radius.sm}', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| modifiers: { | ||
| 'corner-radius': { | ||
| default: 'subtle', | ||
| contexts: { subtle: [] }, | ||
| }, | ||
| 'pixel-density': { | ||
| default: 'standard', | ||
| contexts: { standard: [] }, | ||
| }, | ||
| }, | ||
| resolutionOrder: [ | ||
| { $ref: '#/sets/base' }, | ||
| { $ref: '#/modifiers/corner-radius' }, | ||
| { $ref: '#/modifiers/pixel-density' }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| { | ||
| config: defineConfig( | ||
| {}, | ||
| { cwd: new URL( './', import.meta.url ) } | ||
| ), | ||
| } | ||
| ); | ||
| const file = generateFigmaFiles( resolver ).find( | ||
| ( candidate ) => candidate.filename === 'radius/subtle.json' | ||
| )!; | ||
| expect( | ||
| JSON.parse( file.contents )[ 'wpds-border' ].radius.control | ||
| ).toEqual( { | ||
| $type: 'dimension', | ||
| $value: '{wpds-border.radius.sm}', | ||
| } ); | ||
| } ); | ||
| } ); |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am I understanding correctly that @mattmiklic is going to test these artifacts from this branch before merging?
In that case, what's the ultimate home for these files? I feel like based on current conventions, it would be committed under
packages/theme/prebuilt/figma/or something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's what I'm thinking too