@wordpress/theme 0.4.0 → 0.4.1-next.76cff8c98.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE.md +1 -1
  3. package/README.md +97 -33
  4. package/build/color-ramps/{index.js → index.cjs} +9 -9
  5. package/build/color-ramps/index.cjs.map +7 -0
  6. package/build/color-ramps/lib/{color-utils.js → color-utils.cjs} +3 -3
  7. package/build/color-ramps/lib/color-utils.cjs.map +7 -0
  8. package/build/color-ramps/lib/{constants.js → constants.cjs} +2 -2
  9. package/build/color-ramps/lib/{default-ramps.js → default-ramps.cjs} +1 -1
  10. package/build/color-ramps/lib/{find-color-with-constraints.js → find-color-with-constraints.cjs} +6 -6
  11. package/build/color-ramps/lib/{index.js → index.cjs} +7 -7
  12. package/build/color-ramps/lib/index.cjs.map +7 -0
  13. package/build/color-ramps/lib/{ramp-configs.js → ramp-configs.cjs} +1 -1
  14. package/build/color-ramps/lib/{register-color-spaces.js → register-color-spaces.cjs} +1 -1
  15. package/build/color-ramps/lib/{taper-chroma.js → taper-chroma.cjs} +4 -4
  16. package/build/color-ramps/lib/{taper-chroma.js.map → taper-chroma.cjs.map} +2 -2
  17. package/build/color-ramps/lib/{types.js → types.cjs} +1 -1
  18. package/build/color-ramps/lib/{utils.js → utils.cjs} +4 -4
  19. package/build/{context.js → context.cjs} +1 -1
  20. package/build/{index.js → index.cjs} +2 -2
  21. package/build/index.cjs.map +7 -0
  22. package/build/{lock-unlock.js → lock-unlock.cjs} +1 -1
  23. package/build/prebuilt/js/{design-tokens.js → design-tokens.cjs} +16 -14
  24. package/build/prebuilt/js/design-tokens.cjs.map +7 -0
  25. package/build/prebuilt/ts/{color-tokens.js → color-tokens.cjs} +1 -1
  26. package/build/prebuilt/ts/color-tokens.cjs.map +7 -0
  27. package/build/prebuilt/ts/token-types.cjs +18 -0
  28. package/build/prebuilt/ts/token-types.cjs.map +7 -0
  29. package/build/{private-apis.js → private-apis.cjs} +4 -4
  30. package/build/stylelint-plugins/no-setting-wpds-custom-properties.cjs +69 -0
  31. package/build/stylelint-plugins/no-setting-wpds-custom-properties.cjs.map +7 -0
  32. package/build/stylelint-plugins/no-unknown-ds-tokens.cjs +98 -0
  33. package/build/stylelint-plugins/no-unknown-ds-tokens.cjs.map +7 -0
  34. package/build/{theme-provider.js → theme-provider.cjs} +3 -3
  35. package/build/{types.js → types.cjs} +1 -1
  36. package/build/{use-theme-provider-styles.js → use-theme-provider-styles.cjs} +19 -35
  37. package/build/use-theme-provider-styles.cjs.map +7 -0
  38. package/build-module/color-ramps/{index.js → index.mjs} +10 -10
  39. package/build-module/color-ramps/index.mjs.map +7 -0
  40. package/build-module/color-ramps/lib/{color-utils.js → color-utils.mjs} +3 -3
  41. package/build-module/color-ramps/lib/color-utils.mjs.map +7 -0
  42. package/build-module/color-ramps/lib/{constants.js → constants.mjs} +2 -2
  43. package/build-module/color-ramps/lib/{default-ramps.js → default-ramps.mjs} +1 -1
  44. package/build-module/color-ramps/lib/{find-color-with-constraints.js → find-color-with-constraints.mjs} +6 -6
  45. package/build-module/color-ramps/lib/{index.js → index.mjs} +7 -8
  46. package/build-module/color-ramps/lib/index.mjs.map +7 -0
  47. package/build-module/color-ramps/lib/{ramp-configs.js → ramp-configs.mjs} +1 -1
  48. package/build-module/color-ramps/lib/{register-color-spaces.js → register-color-spaces.mjs} +1 -1
  49. package/build-module/color-ramps/lib/{taper-chroma.js → taper-chroma.mjs} +4 -4
  50. package/build-module/color-ramps/lib/{taper-chroma.js.map → taper-chroma.mjs.map} +2 -2
  51. package/build-module/color-ramps/lib/types.mjs +1 -0
  52. package/build-module/color-ramps/lib/{utils.js → utils.mjs} +4 -4
  53. package/build-module/{context.js → context.mjs} +1 -1
  54. package/build-module/index.mjs +6 -0
  55. package/build-module/index.mjs.map +7 -0
  56. package/build-module/{lock-unlock.js → lock-unlock.mjs} +1 -1
  57. package/build-module/prebuilt/js/{design-tokens.js → design-tokens.mjs} +16 -14
  58. package/build-module/prebuilt/js/design-tokens.mjs.map +7 -0
  59. package/build-module/prebuilt/ts/{color-tokens.js → color-tokens.mjs} +1 -1
  60. package/build-module/prebuilt/ts/color-tokens.mjs.map +7 -0
  61. package/build-module/prebuilt/ts/token-types.mjs +1 -0
  62. package/build-module/{private-apis.js → private-apis.mjs} +4 -4
  63. package/build-module/stylelint-plugins/no-setting-wpds-custom-properties.mjs +39 -0
  64. package/build-module/stylelint-plugins/no-setting-wpds-custom-properties.mjs.map +7 -0
  65. package/build-module/stylelint-plugins/no-unknown-ds-tokens.mjs +68 -0
  66. package/build-module/stylelint-plugins/no-unknown-ds-tokens.mjs.map +7 -0
  67. package/build-module/{theme-provider.js → theme-provider.mjs} +3 -3
  68. package/build-module/types.mjs +1 -0
  69. package/build-module/types.mjs.map +7 -0
  70. package/build-module/{use-theme-provider-styles.js → use-theme-provider-styles.mjs} +21 -37
  71. package/build-module/use-theme-provider-styles.mjs.map +7 -0
  72. package/build-types/color-ramps/lib/color-utils.d.ts +1 -1
  73. package/build-types/color-ramps/lib/color-utils.d.ts.map +1 -1
  74. package/build-types/color-ramps/lib/constants.d.ts +2 -2
  75. package/build-types/color-ramps/lib/constants.d.ts.map +1 -1
  76. package/build-types/color-ramps/lib/index.d.ts.map +1 -1
  77. package/build-types/color-ramps/stories/index.story.d.ts +1 -1
  78. package/build-types/color-ramps/stories/index.story.d.ts.map +1 -1
  79. package/build-types/index.d.ts +2 -0
  80. package/build-types/index.d.ts.map +1 -1
  81. package/build-types/prebuilt/js/{design-tokens.d.ts → design-tokens.d.mts} +1 -1
  82. package/build-types/prebuilt/js/design-tokens.d.mts.map +1 -0
  83. package/build-types/prebuilt/ts/color-tokens.d.ts +0 -4
  84. package/build-types/prebuilt/ts/color-tokens.d.ts.map +1 -1
  85. package/build-types/prebuilt/ts/token-types.d.ts +65 -0
  86. package/build-types/prebuilt/ts/token-types.d.ts.map +1 -0
  87. package/build-types/stories/index.story.d.ts +1 -1
  88. package/build-types/stories/index.story.d.ts.map +1 -1
  89. package/build-types/stylelint-plugins/no-setting-wpds-custom-properties.d.mts +4 -0
  90. package/build-types/stylelint-plugins/no-setting-wpds-custom-properties.d.mts.map +1 -0
  91. package/build-types/stylelint-plugins/no-unknown-ds-tokens.d.mts +4 -0
  92. package/build-types/stylelint-plugins/no-unknown-ds-tokens.d.mts.map +1 -0
  93. package/build-types/use-theme-provider-styles.d.ts.map +1 -1
  94. package/package.json +42 -12
  95. package/src/color-ramps/index.ts +2 -2
  96. package/src/color-ramps/lib/color-utils.ts +1 -1
  97. package/src/color-ramps/lib/index.ts +1 -2
  98. package/src/color-ramps/lib/taper-chroma.ts +2 -2
  99. package/src/color-ramps/stories/index.story.tsx +1 -1
  100. package/src/color-ramps/test/__snapshots__/index.test.ts.snap +7060 -7041
  101. package/src/color-ramps/test/index.test.ts +14 -23
  102. package/src/index.ts +2 -1
  103. package/src/prebuilt/css/design-tokens.css +14 -12
  104. package/src/prebuilt/js/{design-tokens.js → design-tokens.mjs} +16 -14
  105. package/src/prebuilt/ts/color-tokens.ts +1 -1
  106. package/src/prebuilt/ts/token-types.ts +179 -0
  107. package/src/stories/index.story.tsx +1 -1
  108. package/src/stylelint-plugins/no-setting-wpds-custom-properties.mjs +45 -0
  109. package/src/stylelint-plugins/no-unknown-ds-tokens.mjs +101 -0
  110. package/src/stylelint-plugins/test/.stylelintignore +1 -0
  111. package/src/stylelint-plugins/test/.stylelintrc.json +10 -0
  112. package/src/stylelint-plugins/test/__snapshots__/no-setting-wpds-custom-properties.test.ts.snap +24 -0
  113. package/src/stylelint-plugins/test/__snapshots__/no-unknown-ds-tokens.test.ts.snap +24 -0
  114. package/src/stylelint-plugins/test/fixtures/no-setting-wpds-custom-properties-invalid.css +9 -0
  115. package/src/stylelint-plugins/test/fixtures/no-setting-wpds-custom-properties-valid.css +12 -0
  116. package/src/stylelint-plugins/test/fixtures/no-unknown-ds-tokens-invalid.css +9 -0
  117. package/src/stylelint-plugins/test/fixtures/no-unknown-ds-tokens-valid.css +19 -0
  118. package/src/stylelint-plugins/test/no-setting-wpds-custom-properties.test.ts +47 -0
  119. package/src/stylelint-plugins/test/no-unknown-ds-tokens.test.ts +47 -0
  120. package/src/stylelint-plugins/test/utils/index.ts +29 -0
  121. package/src/use-theme-provider-styles.ts +19 -30
  122. package/bin/generate-default-ramps/index.ts +0 -49
  123. package/bin/generate-primitive-tokens/index.ts +0 -113
  124. package/bin/terrazzo-plugin-ds-tokens-docs/index.ts +0 -84
  125. package/bin/terrazzo-plugin-inline-alias-values/index.ts +0 -113
  126. package/bin/terrazzo-plugin-known-wpds-css-variables/index.ts +0 -52
  127. package/bin/terrazzo-plugin-mode-overrides/index.ts +0 -208
  128. package/build/color-ramps/index.js.map +0 -7
  129. package/build/color-ramps/lib/color-utils.js.map +0 -7
  130. package/build/color-ramps/lib/index.js.map +0 -7
  131. package/build/index.js.map +0 -7
  132. package/build/prebuilt/js/design-tokens.js.map +0 -7
  133. package/build/prebuilt/ts/color-tokens.js.map +0 -7
  134. package/build/use-theme-provider-styles.js.map +0 -7
  135. package/build-module/color-ramps/index.js.map +0 -7
  136. package/build-module/color-ramps/lib/color-utils.js.map +0 -7
  137. package/build-module/color-ramps/lib/index.js.map +0 -7
  138. package/build-module/color-ramps/lib/types.js +0 -1
  139. package/build-module/index.js +0 -6
  140. package/build-module/index.js.map +0 -7
  141. package/build-module/prebuilt/js/design-tokens.js.map +0 -7
  142. package/build-module/prebuilt/ts/color-tokens.js.map +0 -7
  143. package/build-module/types.js +0 -1
  144. package/build-module/use-theme-provider-styles.js.map +0 -7
  145. package/build-types/prebuilt/js/design-tokens.d.ts.map +0 -1
  146. package/docs/ds-tokens.md +0 -176
  147. package/terrazzo.config.ts +0 -123
  148. package/tokens/border.json +0 -56
  149. package/tokens/color.json +0 -1559
  150. package/tokens/dimension.json +0 -169
  151. package/tokens/elevation.json +0 -201
  152. package/tokens/modes/border.high-dpi.json +0 -15
  153. package/tokens/modes/dimension.comfortable.json +0 -54
  154. package/tokens/modes/dimension.compact.json +0 -54
  155. package/tokens/typography.json +0 -93
  156. package/tsconfig.bin.json +0 -13
  157. package/tsconfig.bin.tsbuildinfo +0 -1
  158. package/tsconfig.json +0 -11
  159. package/tsconfig.src.json +0 -9
  160. package/tsconfig.src.tsbuildinfo +0 -1
  161. /package/build/color-ramps/lib/{constants.js.map → constants.cjs.map} +0 -0
  162. /package/build/color-ramps/lib/{default-ramps.js.map → default-ramps.cjs.map} +0 -0
  163. /package/build/color-ramps/lib/{find-color-with-constraints.js.map → find-color-with-constraints.cjs.map} +0 -0
  164. /package/build/color-ramps/lib/{ramp-configs.js.map → ramp-configs.cjs.map} +0 -0
  165. /package/build/color-ramps/lib/{register-color-spaces.js.map → register-color-spaces.cjs.map} +0 -0
  166. /package/build/color-ramps/lib/{types.js.map → types.cjs.map} +0 -0
  167. /package/build/color-ramps/lib/{utils.js.map → utils.cjs.map} +0 -0
  168. /package/build/{context.js.map → context.cjs.map} +0 -0
  169. /package/build/{lock-unlock.js.map → lock-unlock.cjs.map} +0 -0
  170. /package/build/{private-apis.js.map → private-apis.cjs.map} +0 -0
  171. /package/build/{theme-provider.js.map → theme-provider.cjs.map} +0 -0
  172. /package/build/{types.js.map → types.cjs.map} +0 -0
  173. /package/build-module/color-ramps/lib/{constants.js.map → constants.mjs.map} +0 -0
  174. /package/build-module/color-ramps/lib/{default-ramps.js.map → default-ramps.mjs.map} +0 -0
  175. /package/build-module/color-ramps/lib/{find-color-with-constraints.js.map → find-color-with-constraints.mjs.map} +0 -0
  176. /package/build-module/color-ramps/lib/{ramp-configs.js.map → ramp-configs.mjs.map} +0 -0
  177. /package/build-module/color-ramps/lib/{register-color-spaces.js.map → register-color-spaces.mjs.map} +0 -0
  178. /package/build-module/color-ramps/lib/{types.js.map → types.mjs.map} +0 -0
  179. /package/build-module/color-ramps/lib/{utils.js.map → utils.mjs.map} +0 -0
  180. /package/build-module/{context.js.map → context.mjs.map} +0 -0
  181. /package/build-module/{lock-unlock.js.map → lock-unlock.mjs.map} +0 -0
  182. /package/build-module/{types.js.map → prebuilt/ts/token-types.mjs.map} +0 -0
  183. /package/build-module/{private-apis.js.map → private-apis.mjs.map} +0 -0
  184. /package/build-module/{theme-provider.js.map → theme-provider.mjs.map} +0 -0
@@ -0,0 +1,47 @@
1
+ import { getStylelintResult } from './utils';
2
+
3
+ describe( 'flags no warnings with valid wpds tokens css', () => {
4
+ let result: ReturnType< typeof getStylelintResult >;
5
+
6
+ beforeEach( () => {
7
+ result = getStylelintResult(
8
+ './fixtures/no-unknown-ds-tokens-valid.css'
9
+ );
10
+ } );
11
+
12
+ it( 'did not error', () => {
13
+ return result.then( ( data ) => expect( data.errored ).toBeFalsy() );
14
+ } );
15
+
16
+ it( 'flags no warnings', () => {
17
+ return result.then( ( data ) =>
18
+ expect( data.results[ 0 ].warnings ).toHaveLength( 0 )
19
+ );
20
+ } );
21
+ } );
22
+
23
+ describe( 'flags warnings with invalid wpds tokens css', () => {
24
+ let result: ReturnType< typeof getStylelintResult >;
25
+
26
+ beforeEach( () => {
27
+ result = getStylelintResult(
28
+ './fixtures/no-unknown-ds-tokens-invalid.css'
29
+ );
30
+ } );
31
+
32
+ it( 'did error', () => {
33
+ return result.then( ( data ) => expect( data.errored ).toBeTruthy() );
34
+ } );
35
+
36
+ it( 'flags correct number of warnings', () => {
37
+ return result.then( ( data ) =>
38
+ expect( data.results[ 0 ].warnings ).toHaveLength( 2 )
39
+ );
40
+ } );
41
+
42
+ it( 'snapshot matches warnings', () => {
43
+ return result.then( ( data ) =>
44
+ expect( data.results[ 0 ].warnings ).toMatchSnapshot()
45
+ );
46
+ } );
47
+ } );
@@ -0,0 +1,29 @@
1
+ import util from 'node:util';
2
+ import path from 'node:path';
3
+ import childProcess from 'node:child_process';
4
+
5
+ const execute = util.promisify( childProcess.exec );
6
+
7
+ const generateStylelintCommand = ( filename: string ): string =>
8
+ 'npx stylelint ' +
9
+ path.resolve( __dirname, '../', filename ) +
10
+ ' -c ' +
11
+ path.resolve( __dirname, '../', './.stylelintrc.json' ) +
12
+ ' --formatter json' +
13
+ ' --ignore-path ' +
14
+ path.resolve( __dirname, '../', './.stylelintignore' );
15
+
16
+ export const getStylelintResult = ( filename: string ) =>
17
+ execute( generateStylelintCommand( filename ) )
18
+ .then( ( { stderr } ) => {
19
+ return {
20
+ errored: false,
21
+ results: JSON.parse( stderr as string ),
22
+ };
23
+ } )
24
+ .catch( ( { stderr } ) => {
25
+ return {
26
+ errored: true,
27
+ results: JSON.parse( stderr ),
28
+ };
29
+ } );
@@ -3,13 +3,13 @@
3
3
  */
4
4
  import type { CSSProperties } from 'react';
5
5
  import {
6
- parse,
6
+ clone,
7
+ set,
7
8
  to,
8
- get,
9
9
  serialize,
10
10
  sRGB,
11
11
  HSL,
12
- type ColorTypes,
12
+ type PlainColorObject,
13
13
  } from 'colorjs.io/fn';
14
14
  import memoize from 'memize';
15
15
 
@@ -93,44 +93,33 @@ const legacyWpComponentsOverridesCSS: Entry[] = [
93
93
  ],
94
94
  ];
95
95
 
96
- function customRgbFormat( color: ColorTypes ) {
96
+ function customRgbFormat( color: PlainColorObject ): string {
97
97
  const rgb = to( color, sRGB );
98
- return [ get( rgb, 'srgb.r' ), get( rgb, 'srgb.g' ), get( rgb, 'srgb.b' ) ]
99
- .map( ( n ) => Math.round( n * 255 ) )
98
+ return rgb.coords
99
+ .map( ( n ) => Math.round( ( n ?? 0 ) * 255 ) )
100
100
  .join( ', ' );
101
101
  }
102
102
 
103
103
  function legacyWpAdminThemeOverridesCSS( accent: string ): Entry[] {
104
- const parsedAccent = to( parse( accent ), HSL );
105
-
106
- const coords = parsedAccent.coords;
107
- const darker10 = to(
108
- {
109
- space: HSL,
110
- coords: [
111
- coords[ 0 ], // h
112
- coords[ 1 ], // s
113
- Math.max( 0, Math.min( 100, coords[ 2 ] - 5 ) ), // l (reduced by 5%)
114
- ],
115
- },
116
- sRGB
104
+ const parsedAccent = to( accent, HSL );
105
+ const parsedL = parsedAccent.coords[ 2 ] ?? 0;
106
+
107
+ // Create darker version of accent —
108
+ const darker10 = set(
109
+ clone( parsedAccent ),
110
+ [ HSL, 'l' ],
111
+ Math.max( 0, parsedL - 5 ) // L reduced by 5%
117
112
  );
118
- const darker20 = to(
119
- {
120
- space: HSL,
121
- coords: [
122
- coords[ 0 ], // h
123
- coords[ 1 ], // s
124
- Math.max( 0, Math.min( 100, coords[ 2 ] - 10 ) ), // l (reduced by 10%)
125
- ],
126
- },
127
- sRGB
113
+ const darker20 = set(
114
+ clone( parsedAccent ),
115
+ [ HSL, 'l' ],
116
+ Math.max( 0, parsedL - 10 ) // L reduced by 10%
128
117
  );
129
118
 
130
119
  return [
131
120
  [
132
121
  '--wp-admin-theme-color',
133
- serialize( to( parsedAccent, sRGB ), { format: 'hex' } ),
122
+ serialize( parsedAccent, { format: 'hex' } ),
134
123
  ],
135
124
  [ '--wp-admin-theme-color--rgb', customRgbFormat( parsedAccent ) ],
136
125
  [
@@ -1,49 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { writeFile } from 'node:fs/promises';
5
- import { dirname, join } from 'node:path';
6
- import { fileURLToPath } from 'node:url';
7
-
8
- /**
9
- * Internal dependencies
10
- */
11
- import {
12
- DEFAULT_SEED_COLORS,
13
- buildBgRamp,
14
- buildAccentRamp,
15
- } from '../../src/color-ramps/index.ts';
16
-
17
- const __dirname = dirname( fileURLToPath( import.meta.url ) );
18
-
19
- const bgRamp = buildBgRamp( DEFAULT_SEED_COLORS.bg );
20
- const accentRamps = Object.fromEntries(
21
- [ ...Object.entries( DEFAULT_SEED_COLORS ) ]
22
- .filter( ( [ scaleName ] ) => scaleName !== 'bg' )
23
- .map( ( [ scaleName, seed ] ) => [
24
- scaleName,
25
- buildAccentRamp( seed, bgRamp ),
26
- ] )
27
- );
28
-
29
- const ramps = { bg: bgRamp, ...accentRamps };
30
-
31
- const outputPath = join(
32
- __dirname,
33
- '../../src/color-ramps/lib/default-ramps.ts'
34
- );
35
-
36
- const content = `
37
- /**
38
- * Internal dependencies
39
- */
40
- import type { RampResult } from './types';
41
- import type { DEFAULT_SEED_COLORS } from './constants';
42
-
43
- export const DEFAULT_RAMPS: Record<
44
- keyof typeof DEFAULT_SEED_COLORS,
45
- RampResult
46
- > = ${ JSON.stringify( ramps ) };
47
- `;
48
-
49
- await writeFile( outputPath, content );
@@ -1,113 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import fs from 'fs';
5
- import path from 'path';
6
- import { fileURLToPath } from 'url';
7
- import { parse, to, serialize, sRGB } from 'colorjs.io/fn';
8
-
9
- /**
10
- * Internal dependencies
11
- */
12
- import '../../src/color-ramps/lib/register-color-spaces';
13
- import {
14
- DEFAULT_SEED_COLORS,
15
- buildBgRamp,
16
- buildAccentRamp,
17
- } from '../../src/color-ramps/index';
18
-
19
- const __filename = fileURLToPath( import.meta.url );
20
- const __dirname = path.dirname( __filename );
21
-
22
- // Path to the color.json file
23
- const colorJsonPath = path.join( __dirname, '../../tokens/color.json' );
24
-
25
- /**
26
- * Rounds a given hex value (0-255) to 3 decimal places.
27
- *
28
- * 3 decimal places is the minimum precision for lossless hex serialization.
29
- * With 3 decimal places rounding to the nearest 0.001, the maximum rounding
30
- * error is 0.0005. With 256 possible hex values, 0.0005 × 256 = 0.128,
31
- * guaranteeing the rounded value stays within 0.5 of the original value.
32
- *
33
- * @param n - The hex value to round.
34
- * @return The rounded hex value.
35
- */
36
- const roundHexComponent = ( n: number ) => Math.round( n * 1000 ) / 1000;
37
-
38
- const transformColorStringToDTCGValue = ( color: string ) => {
39
- const parsed = to( parse( color ), sRGB );
40
-
41
- return {
42
- colorSpace: 'srgb',
43
- components: parsed.coords.map( roundHexComponent ),
44
- ...( parsed.alpha < 1 ? { alpha: parsed.alpha } : undefined ),
45
- hex: serialize( parsed, { format: 'hex' } ),
46
- };
47
- };
48
-
49
- // Main function
50
- function generatePrimitiveColorTokens() {
51
- const startTime = performance.now();
52
- console.log( '🎨 Starting primitive color tokens generation...' );
53
-
54
- try {
55
- // Read the color.json file
56
- const colorJson = JSON.parse(
57
- fs.readFileSync( colorJsonPath, 'utf8' )
58
- );
59
-
60
- // Build the ramps
61
- const bgRamp = buildBgRamp( DEFAULT_SEED_COLORS.bg );
62
- const accentRamps = [ ...Object.entries( DEFAULT_SEED_COLORS ) ]
63
- .filter( ( [ scaleName ] ) => scaleName !== 'bg' )
64
- .map( ( [ scaleName, seed ] ) => ( {
65
- scaleName,
66
- ramp: buildAccentRamp( seed, bgRamp ),
67
- } ) );
68
-
69
- // Convert the ramp values in a DTCG compatible format
70
- [
71
- {
72
- scaleName: 'bg',
73
- ramp: bgRamp,
74
- },
75
- ...accentRamps,
76
- ].forEach( ( { scaleName, ramp } ) => {
77
- colorJson[ 'wpds-color' ].primitive[ scaleName ] = {};
78
- for ( const [ tokenName, tokenValue ] of Object.entries(
79
- ramp.ramp
80
- ) ) {
81
- colorJson[ 'wpds-color' ].primitive[ scaleName ][ tokenName ] =
82
- {
83
- $value: transformColorStringToDTCGValue( tokenValue ),
84
- };
85
- }
86
- } );
87
-
88
- // Write the updated JSON back to the file with proper formatting
89
- fs.writeFileSync(
90
- colorJsonPath,
91
- JSON.stringify( colorJson, null, '\t' )
92
- );
93
-
94
- const endTime = performance.now();
95
- const duration = endTime - startTime;
96
- console.log(
97
- `✅ Successfully updated color.json (${ duration.toFixed( 2 ) }ms)`
98
- );
99
- } catch ( error ) {
100
- const endTime = performance.now();
101
- const duration = endTime - startTime;
102
- console.error(
103
- `❌ Error updating color tokens after ${ duration.toFixed(
104
- 2
105
- ) }ms:`,
106
- error
107
- );
108
- process.exit( 1 );
109
- }
110
- }
111
-
112
- // Run the script
113
- generatePrimitiveColorTokens();
@@ -1,84 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { FORMAT_ID } from '@terrazzo/plugin-css';
5
- import type { Plugin } from '@terrazzo/parser';
6
-
7
- function titleCase( str: string ) {
8
- return str[ 0 ].toUpperCase() + str.slice( 1 );
9
- }
10
-
11
- type TokensMap = Record< string, Record< string, string > >;
12
-
13
- export default function pluginDsTokenDocs( {
14
- filename = 'design-tokens.md',
15
- } = {} ): Plugin {
16
- return {
17
- name: '@terrazzo/terrazzo-plugin-ds-tokens-docs',
18
- async build( { getTransforms, outputFile } ) {
19
- if ( ! filename ) {
20
- return;
21
- }
22
-
23
- const semanticTokens: TokensMap = {};
24
- // Re-use transformed tokens from the CSS plugin
25
- for ( const token of getTransforms( {
26
- format: FORMAT_ID,
27
- id: '*',
28
- mode: '.',
29
- } ) ) {
30
- if ( token.localID === undefined ) {
31
- console.warn(
32
- 'Unexpected — Missing local ID when building token list for eslint plugin'
33
- );
34
- continue;
35
- }
36
-
37
- // Use the tokens filename (without .json) as the group name
38
- const group =
39
- token.token.source.loc
40
- ?.split( '/' )
41
- .at( -1 )
42
- ?.split( '.json' )[ 0 ] ?? 'Miscellaneous';
43
-
44
- // Group by category
45
- semanticTokens[ group ] ??= {};
46
- semanticTokens[ group ][ token.localID ] =
47
- token.token.$description ?? 'N/A';
48
- }
49
-
50
- function tokensToMdTable( tokens: TokensMap ) {
51
- return Object.entries( tokens )
52
- .map( ( [ group, tokensInGroup ] ) => [
53
- `### ${ titleCase( group ) }`,
54
- '',
55
- '| Variable name | Description |',
56
- '|---|---|',
57
- ...Object.entries( tokensInGroup ).map(
58
- ( [ name, description ] ) =>
59
- `| \`${ name }\` | ${ description } |`
60
- ),
61
- '',
62
- ] )
63
- .flat( 2 );
64
- }
65
-
66
- outputFile(
67
- filename,
68
- [
69
- '<!--',
70
- 'This file is generated by @terrazzo/terrazzo-plugin-ds-tokens-docs.',
71
- 'Do not edit directly.',
72
- '-->',
73
- '',
74
- '# DS Tokens reference',
75
- '',
76
- '## Semantic tokens',
77
- '',
78
- ...tokensToMdTable( semanticTokens ),
79
- '', // final empty line
80
- ].join( '\n' )
81
- );
82
- },
83
- };
84
- }
@@ -1,113 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { type Plugin, type TokenNormalized } from '@terrazzo/parser';
5
-
6
- interface InlineAliasValuesOptions {
7
- /**
8
- * Output filename for the generated aliased tokens mapping.
9
- */
10
- filename?: string;
11
-
12
- /**
13
- * Pattern matching IDs of tokens whose values should be inlined into their alias references.
14
- */
15
- pattern: RegExp;
16
-
17
- /**
18
- * Optional function to transform token IDs in the output.
19
- */
20
- tokenId?: ( tokenId: string ) => string;
21
- }
22
-
23
- /**
24
- * Inlines the values of tokens matching the given pattern into their alias
25
- * references. Matching tokens are removed from the output, and a TypeScript
26
- * file is emitted with the mapping of aliased tokens.
27
- *
28
- * @param options The options for the plugin.
29
- * @param options.filename
30
- * @param options.pattern
31
- * @param options.tokenId
32
- */
33
- export default function inlineAliasValues( {
34
- filename,
35
- pattern,
36
- tokenId = ( id ) => id,
37
- }: InlineAliasValuesOptions ): Plugin {
38
- const aliasedBy: Record< string, string[] > = {};
39
-
40
- return {
41
- name: '@wordpress/terrazzo-plugin-inline-alias-values',
42
- async transform( { tokens } ) {
43
- // Map of primitive token ID -> array of references to inline
44
- const inlineMap: Record< string, TokenNormalized[] > = {};
45
-
46
- // Single pass: identify primitives and collect references
47
- for ( const [ id, token ] of Object.entries( tokens ) ) {
48
- const shouldInline = pattern.test( id );
49
-
50
- if ( shouldInline ) {
51
- // Track this token for inlining
52
- inlineMap[ id ] = [];
53
-
54
- // Track aliased tokens for output file
55
- if ( token.aliasedBy ) {
56
- aliasedBy[ tokenId( id ) ] =
57
- token.aliasedBy.map( tokenId );
58
- }
59
- }
60
-
61
- // Check if this token's main value references a primitive
62
- if ( token.aliasOf && pattern.test( token.aliasOf ) ) {
63
- inlineMap[ token.aliasOf ] ??= [];
64
- inlineMap[ token.aliasOf ].push( token );
65
- }
66
-
67
- // Check if any mode values reference a primitive
68
- for ( const modeValue of Object.values( token.mode ) ) {
69
- const { aliasOf } = modeValue;
70
- if ( aliasOf && pattern.test( aliasOf ) ) {
71
- const primitiveId = aliasOf;
72
- inlineMap[ primitiveId ] ??= [];
73
- inlineMap[ primitiveId ].push( modeValue );
74
- }
75
- }
76
- }
77
-
78
- // Inline values and delete primitives
79
- for ( const [ id, references ] of Object.entries( inlineMap ) ) {
80
- const token = tokens[ id ];
81
-
82
- for ( const target of references ) {
83
- target.$value = token.$value;
84
- target.originalValue = token.originalValue;
85
- delete target.aliasOf;
86
- delete target.aliasChain;
87
- }
88
-
89
- delete tokens[ id ];
90
- }
91
- },
92
- async build( { outputFile } ) {
93
- if ( ! filename ) {
94
- return;
95
- }
96
-
97
- const json = JSON.stringify( aliasedBy, null, 2 );
98
-
99
- outputFile(
100
- filename,
101
- [
102
- '/**',
103
- ' * This file is generated by the @wordpress/terrazzo-plugin-inline-alias-values plugin.',
104
- ' * Do not edit this file directly.',
105
- ' */',
106
- '',
107
- `export default ${ json } as Record< string, string[] >;`,
108
- '',
109
- ].join( '\n' )
110
- );
111
- },
112
- };
113
- }
@@ -1,52 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { FORMAT_ID } from '@terrazzo/plugin-css';
5
- import type { Plugin } from '@terrazzo/parser';
6
-
7
- export default function pluginKnownWpdsCssVariables( {
8
- filename = 'design-tokens.js',
9
- } = {} ): Plugin {
10
- return {
11
- name: '@terrazzo/plugin-known-wpds-css-variables',
12
- async build( { getTransforms, outputFile } ) {
13
- // Either a string (modes=false) or an object (modes=true)
14
- const tokensToExport: Record<
15
- string,
16
- Record< string, string | Record< string, string > >
17
- > = {};
18
-
19
- for ( const token of getTransforms( {
20
- format: FORMAT_ID,
21
- id: '*',
22
- } ) ) {
23
- if ( ! token.localID ) {
24
- console.warn(
25
- 'Unexpected — Missing local ID when building token list for eslint plugin'
26
- );
27
- continue;
28
- }
29
-
30
- tokensToExport[ token.localID ] ??= {};
31
- tokensToExport[ token.localID ][ token.mode ] = token.value;
32
- }
33
-
34
- outputFile(
35
- filename,
36
- [
37
- '/**',
38
- ' * This file is generated by the @terrazzo/plugin-known-wpds-css-variables plugin.',
39
- ' * Do not edit this file directly.',
40
- ' */',
41
- '',
42
- `export default ${ JSON.stringify(
43
- Array.from( new Set( Object.keys( tokensToExport ) ) ),
44
- null,
45
- 2
46
- ) }`,
47
- '', // final empty line
48
- ].join( '\n' )
49
- );
50
- },
51
- };
52
- }