@wordpress/theme 0.1.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 (168) hide show
  1. package/LICENSE.md +788 -0
  2. package/README.md +67 -0
  3. package/bin/build-tokens.js +83 -0
  4. package/bin/generate-primitive-tokens/index.ts +115 -0
  5. package/bin/terrazzo-plugin-ds-tokens-docs/index.ts +103 -0
  6. package/bin/terrazzo-plugin-figma-ds-token-manager/index.ts +210 -0
  7. package/bin/terrazzo-plugin-figma-ds-token-manager/lib.ts +1 -0
  8. package/bin/terrazzo-plugin-known-wpds-css-variables/index.ts +72 -0
  9. package/build/color-ramps/index.js +132 -0
  10. package/build/color-ramps/index.js.map +7 -0
  11. package/build/color-ramps/lib/cache-utils.js +57 -0
  12. package/build/color-ramps/lib/cache-utils.js.map +7 -0
  13. package/build/color-ramps/lib/constants.js +105 -0
  14. package/build/color-ramps/lib/constants.js.map +7 -0
  15. package/build/color-ramps/lib/find-color-with-constraints.js +141 -0
  16. package/build/color-ramps/lib/find-color-with-constraints.js.map +7 -0
  17. package/build/color-ramps/lib/index.js +264 -0
  18. package/build/color-ramps/lib/index.js.map +7 -0
  19. package/build/color-ramps/lib/ramp-configs.js +315 -0
  20. package/build/color-ramps/lib/ramp-configs.js.map +7 -0
  21. package/build/color-ramps/lib/taper-chroma.js +159 -0
  22. package/build/color-ramps/lib/taper-chroma.js.map +7 -0
  23. package/build/color-ramps/lib/types.js +17 -0
  24. package/build/color-ramps/lib/types.js.map +7 -0
  25. package/build/color-ramps/lib/utils.js +106 -0
  26. package/build/color-ramps/lib/utils.js.map +7 -0
  27. package/build/context.js +34 -0
  28. package/build/context.js.map +7 -0
  29. package/build/index.js +29 -0
  30. package/build/index.js.map +7 -0
  31. package/build/lock-unlock.js +35 -0
  32. package/build/lock-unlock.js.map +7 -0
  33. package/build/prebuilt/js/design-tokens.js +135 -0
  34. package/build/prebuilt/js/design-tokens.js.map +7 -0
  35. package/build/prebuilt/json/figma.json +1317 -0
  36. package/build/prebuilt/ts/design-tokens.js +354 -0
  37. package/build/prebuilt/ts/design-tokens.js.map +7 -0
  38. package/build/private-apis.js +36 -0
  39. package/build/private-apis.js.map +7 -0
  40. package/build/style.module.css.js +2 -0
  41. package/build/theme-provider.js +92 -0
  42. package/build/theme-provider.js.map +7 -0
  43. package/build/types/css-modules.d.js +2 -0
  44. package/build/types/css-modules.d.js.map +7 -0
  45. package/build/types.js +17 -0
  46. package/build/types.js.map +7 -0
  47. package/build/use-theme-provider-styles.js +230 -0
  48. package/build/use-theme-provider-styles.js.map +7 -0
  49. package/build-module/color-ramps/index.js +95 -0
  50. package/build-module/color-ramps/index.js.map +7 -0
  51. package/build-module/color-ramps/lib/cache-utils.js +31 -0
  52. package/build-module/color-ramps/lib/cache-utils.js.map +7 -0
  53. package/build-module/color-ramps/lib/constants.js +63 -0
  54. package/build-module/color-ramps/lib/constants.js.map +7 -0
  55. package/build-module/color-ramps/lib/find-color-with-constraints.js +112 -0
  56. package/build-module/color-ramps/lib/find-color-with-constraints.js.map +7 -0
  57. package/build-module/color-ramps/lib/index.js +235 -0
  58. package/build-module/color-ramps/lib/index.js.map +7 -0
  59. package/build-module/color-ramps/lib/ramp-configs.js +290 -0
  60. package/build-module/color-ramps/lib/ramp-configs.js.map +7 -0
  61. package/build-module/color-ramps/lib/taper-chroma.js +125 -0
  62. package/build-module/color-ramps/lib/taper-chroma.js.map +7 -0
  63. package/build-module/color-ramps/lib/types.js +1 -0
  64. package/build-module/color-ramps/lib/types.js.map +7 -0
  65. package/build-module/color-ramps/lib/utils.js +84 -0
  66. package/build-module/color-ramps/lib/utils.js.map +7 -0
  67. package/build-module/context.js +10 -0
  68. package/build-module/context.js.map +7 -0
  69. package/build-module/index.js +5 -0
  70. package/build-module/index.js.map +7 -0
  71. package/build-module/lock-unlock.js +10 -0
  72. package/build-module/lock-unlock.js.map +7 -0
  73. package/build-module/prebuilt/js/design-tokens.js +115 -0
  74. package/build-module/prebuilt/js/design-tokens.js.map +7 -0
  75. package/build-module/prebuilt/json/figma.json +1317 -0
  76. package/build-module/prebuilt/ts/design-tokens.js +334 -0
  77. package/build-module/prebuilt/ts/design-tokens.js.map +7 -0
  78. package/build-module/private-apis.js +12 -0
  79. package/build-module/private-apis.js.map +7 -0
  80. package/build-module/style.module.css.js +1 -0
  81. package/build-module/theme-provider.js +58 -0
  82. package/build-module/theme-provider.js.map +7 -0
  83. package/build-module/types/css-modules.d.js +1 -0
  84. package/build-module/types/css-modules.d.js.map +7 -0
  85. package/build-module/types.js +1 -0
  86. package/build-module/types.js.map +7 -0
  87. package/build-module/use-theme-provider-styles.js +200 -0
  88. package/build-module/use-theme-provider-styles.js.map +7 -0
  89. package/build-style/style.css +3 -0
  90. package/build-types/color-ramps/index.d.ts +44 -0
  91. package/build-types/color-ramps/index.d.ts.map +1 -0
  92. package/build-types/color-ramps/lib/cache-utils.d.ts +22 -0
  93. package/build-types/color-ramps/lib/cache-utils.d.ts.map +1 -0
  94. package/build-types/color-ramps/lib/constants.d.ts +38 -0
  95. package/build-types/color-ramps/lib/constants.d.ts.map +1 -0
  96. package/build-types/color-ramps/lib/find-color-with-constraints.d.ts +37 -0
  97. package/build-types/color-ramps/lib/find-color-with-constraints.d.ts.map +1 -0
  98. package/build-types/color-ramps/lib/index.d.ts +11 -0
  99. package/build-types/color-ramps/lib/index.d.ts.map +1 -0
  100. package/build-types/color-ramps/lib/ramp-configs.d.ts +7 -0
  101. package/build-types/color-ramps/lib/ramp-configs.d.ts.map +1 -0
  102. package/build-types/color-ramps/lib/taper-chroma.d.ts +32 -0
  103. package/build-types/color-ramps/lib/taper-chroma.d.ts.map +1 -0
  104. package/build-types/color-ramps/lib/types.d.ts +78 -0
  105. package/build-types/color-ramps/lib/types.d.ts.map +1 -0
  106. package/build-types/color-ramps/lib/utils.d.ts +38 -0
  107. package/build-types/color-ramps/lib/utils.d.ts.map +1 -0
  108. package/build-types/color-ramps/stories/index.story.d.ts +14 -0
  109. package/build-types/color-ramps/stories/index.story.d.ts.map +1 -0
  110. package/build-types/color-ramps/stories/ramp-table.d.ts +19 -0
  111. package/build-types/color-ramps/stories/ramp-table.d.ts.map +1 -0
  112. package/build-types/context.d.ts +10 -0
  113. package/build-types/context.d.ts.map +1 -0
  114. package/build-types/index.d.ts +2 -0
  115. package/build-types/index.d.ts.map +1 -0
  116. package/build-types/lock-unlock.d.ts +2 -0
  117. package/build-types/lock-unlock.d.ts.map +1 -0
  118. package/build-types/prebuilt/js/design-tokens.d.ts +3 -0
  119. package/build-types/prebuilt/js/design-tokens.d.ts.map +1 -0
  120. package/build-types/prebuilt/ts/design-tokens.d.ts +7 -0
  121. package/build-types/prebuilt/ts/design-tokens.d.ts.map +1 -0
  122. package/build-types/private-apis.d.ts +2 -0
  123. package/build-types/private-apis.d.ts.map +1 -0
  124. package/build-types/stories/index.story.d.ts +15 -0
  125. package/build-types/stories/index.story.d.ts.map +1 -0
  126. package/build-types/theme-provider.d.ts +3 -0
  127. package/build-types/theme-provider.d.ts.map +1 -0
  128. package/build-types/types.d.ts +42 -0
  129. package/build-types/types.d.ts.map +1 -0
  130. package/build-types/use-theme-provider-styles.d.ts +17 -0
  131. package/build-types/use-theme-provider-styles.d.ts.map +1 -0
  132. package/docs/ds-tokens.md +283 -0
  133. package/package.json +58 -0
  134. package/src/color-ramps/index.ts +155 -0
  135. package/src/color-ramps/lib/cache-utils.ts +56 -0
  136. package/src/color-ramps/lib/constants.ts +85 -0
  137. package/src/color-ramps/lib/find-color-with-constraints.ts +190 -0
  138. package/src/color-ramps/lib/index.ts +369 -0
  139. package/src/color-ramps/lib/ramp-configs.ts +309 -0
  140. package/src/color-ramps/lib/taper-chroma.ts +226 -0
  141. package/src/color-ramps/lib/types.ts +90 -0
  142. package/src/color-ramps/lib/utils.ts +161 -0
  143. package/src/color-ramps/stories/index.story.tsx +264 -0
  144. package/src/color-ramps/stories/ramp-table.tsx +212 -0
  145. package/src/color-ramps/test/__snapshots__/index.test.ts.snap +1280 -0
  146. package/src/color-ramps/test/index.test.ts +94 -0
  147. package/src/context.ts +19 -0
  148. package/src/index.ts +2 -0
  149. package/src/lock-unlock.ts +10 -0
  150. package/src/prebuilt/css/design-tokens.css +401 -0
  151. package/src/prebuilt/js/design-tokens.js +116 -0
  152. package/src/prebuilt/json/figma.json +1317 -0
  153. package/src/prebuilt/ts/design-tokens.ts +335 -0
  154. package/src/private-apis.ts +12 -0
  155. package/src/stories/index.story.tsx +426 -0
  156. package/src/style.module.css +3 -0
  157. package/src/theme-provider.tsx +87 -0
  158. package/src/types/css-modules.d.ts +4 -0
  159. package/src/types.ts +44 -0
  160. package/src/use-theme-provider-styles.ts +247 -0
  161. package/terrazzo.config.ts +102 -0
  162. package/tokens/border.json +34 -0
  163. package/tokens/color.json +877 -0
  164. package/tokens/elevation.json +201 -0
  165. package/tokens/spacing.json +45 -0
  166. package/tokens/typography.json +93 -0
  167. package/tsconfig.json +9 -0
  168. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,161 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import type Color from 'colorjs.io';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import {
10
+ WHITE,
11
+ BLACK,
12
+ UNIVERSAL_CONTRAST_TOPUP,
13
+ WHITE_TEXT_CONTRAST_MARGIN,
14
+ ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS,
15
+ } from './constants';
16
+ import type { Ramp, RampStepConfig, RampDirection } from './types';
17
+ import { getCachedContrast } from './cache-utils';
18
+
19
+ /**
20
+ * Make sure that a color is valid in the p3 gamut, and converts it to oklch.
21
+ * @param c
22
+ */
23
+ export const clampToGamut = ( c: Color ) =>
24
+ c
25
+ .toGamut( { space: 'p3', method: 'css' } ) // map into Display-P3 using CSS OKLCH method
26
+ .to( 'oklch' );
27
+
28
+ /**
29
+ * Build a dependency graph from the steps configuration
30
+ * @param config - The steps configuration object
31
+ */
32
+ function buildDependencyGraph( config: Record< keyof Ramp, RampStepConfig > ): {
33
+ dependencies: Map< keyof Ramp, ( keyof Ramp | 'seed' )[] >;
34
+ dependents: Map< keyof Ramp | 'seed', ( keyof Ramp )[] >;
35
+ } {
36
+ const dependencies = new Map< keyof Ramp, ( keyof Ramp | 'seed' )[] >();
37
+ const dependents = new Map< keyof Ramp | 'seed', ( keyof Ramp )[] >();
38
+
39
+ // Initialize maps
40
+ Object.keys( config ).forEach( ( step ) => {
41
+ dependencies.set( step as keyof Ramp, [] );
42
+ } );
43
+ dependents.set( 'seed', [] );
44
+ Object.keys( config ).forEach( ( step ) => {
45
+ dependents.set( step as keyof Ramp, [] );
46
+ } );
47
+
48
+ // Build the graph
49
+ Object.entries( config ).forEach( ( [ stepName, stepConfig ] ) => {
50
+ const step = stepName as keyof Ramp;
51
+ const reference = stepConfig.contrast.reference;
52
+
53
+ dependencies.get( step )!.push( reference );
54
+ dependents.get( reference )!.push( step );
55
+
56
+ // Add dependency for sameAsIfPossible
57
+ if ( stepConfig.sameAsIfPossible ) {
58
+ dependencies.get( step )!.push( stepConfig.sameAsIfPossible );
59
+ dependents.get( stepConfig.sameAsIfPossible )!.push( step );
60
+ }
61
+ } );
62
+
63
+ return { dependencies, dependents };
64
+ }
65
+
66
+ /**
67
+ * Topologically sort steps based on their dependencies
68
+ * @param config - The steps configuration object
69
+ */
70
+ export function sortByDependency(
71
+ config: Record< keyof Ramp, RampStepConfig >
72
+ ): ( keyof Ramp )[] {
73
+ const { dependents } = buildDependencyGraph( config );
74
+ const result: ( keyof Ramp )[] = [];
75
+ const visited = new Set< keyof Ramp | 'seed' >();
76
+ const visiting = new Set< keyof Ramp | 'seed' >();
77
+
78
+ function visit( node: keyof Ramp | 'seed' ): void {
79
+ if ( visiting.has( node ) ) {
80
+ throw new Error(
81
+ `Circular dependency detected involving step: ${ String(
82
+ node
83
+ ) }`
84
+ );
85
+ }
86
+ if ( visited.has( node ) ) {
87
+ return;
88
+ }
89
+
90
+ visiting.add( node );
91
+
92
+ // Visit all dependents (steps that depend on this node)
93
+ const nodeDependents = dependents.get( node ) || [];
94
+ nodeDependents.forEach( ( dependent ) => {
95
+ visit( dependent );
96
+ } );
97
+
98
+ visiting.delete( node );
99
+ visited.add( node );
100
+
101
+ // Add to result only if it's a step (not 'seed')
102
+ if ( node !== 'seed' ) {
103
+ result.unshift( node ); // Add to front for correct topological order
104
+ }
105
+ }
106
+
107
+ // Start with seed - this will recursively visit all reachable nodes
108
+ visit( 'seed' );
109
+
110
+ return result;
111
+ }
112
+
113
+ /**
114
+ * Finds out whether a lighter or a darker foreground color achieves a better
115
+ * contrast against the seed
116
+ * @param seed
117
+ * @param preferLighter Whether the check should favor white foreground color
118
+ * @return An object with "better" and "worse" properties, each holding a
119
+ * ramp direction value.
120
+ */
121
+ export function computeBetterFgColorDirection(
122
+ seed: Color,
123
+ preferLighter?: boolean
124
+ ): {
125
+ better: RampDirection;
126
+ worse: RampDirection;
127
+ } {
128
+ const contrastAgainstBlack = getCachedContrast( seed, BLACK );
129
+ const contrastAgainstWhite = getCachedContrast( seed, WHITE );
130
+
131
+ return contrastAgainstBlack >
132
+ contrastAgainstWhite +
133
+ ( preferLighter ? WHITE_TEXT_CONTRAST_MARGIN : 0 )
134
+ ? { better: 'darker', worse: 'lighter' }
135
+ : { better: 'lighter', worse: 'darker' };
136
+ }
137
+
138
+ export function adjustContrastTarget( target: number ) {
139
+ if ( target === 1 ) {
140
+ return 1;
141
+ }
142
+
143
+ // Add a little top up to take into account any rounding error and algo imprecisions.
144
+ return target + UNIVERSAL_CONTRAST_TOPUP;
145
+ }
146
+
147
+ /**
148
+ * Prevent the accent scale from referencing a lightness value that
149
+ * would prevent the algorithm from complying with the requirements
150
+ * and cause it to generate unexpected results.
151
+ * @param rawLightness
152
+ * @param direction
153
+ * @return The clamped lightness value
154
+ */
155
+ export function clampAccentScaleReferenceLightness(
156
+ rawLightness: number,
157
+ direction: RampDirection
158
+ ) {
159
+ const thresholds = ACCENT_SCALE_BASE_LIGHTNESS_THRESHOLDS[ direction ];
160
+ return Math.max( thresholds.min, Math.min( thresholds.max, rawLightness ) );
161
+ }
@@ -0,0 +1,264 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import type { Meta, StoryObj } from '@storybook/react';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import { RampTable } from './ramp-table';
10
+ import { buildBgRamp, buildAccentRamp, checkAccessibleCombinations } from '..';
11
+ import { DEFAULT_SEED_COLORS } from '../lib/constants';
12
+
13
+ const ColorGen = ( props: {
14
+ background: string;
15
+ primary: string;
16
+ children: React.ReactNode;
17
+ } ) => {
18
+ return <div>{ props.children }</div>;
19
+ };
20
+
21
+ const meta: Meta< typeof ColorGen > = {
22
+ title: 'Design System/Theme Provider/Color Scales',
23
+ component: ColorGen,
24
+ argTypes: {
25
+ background: {
26
+ control: { type: 'color', presetColors: [ '#1e1e1e', '#f8f8f8' ] },
27
+ },
28
+ primary: {
29
+ control: {
30
+ type: 'color',
31
+ presetColors: [ '#3858e9', '#069e08', '#873eff' ],
32
+ },
33
+ },
34
+ },
35
+ parameters: {
36
+ controls: { expanded: true },
37
+ docs: { canvas: { sourceState: 'shown' } },
38
+ },
39
+ tags: [ 'status-experimental' ],
40
+ };
41
+ export default meta;
42
+
43
+ export const Default: StoryObj< typeof ColorGen > = {
44
+ render: ( args ) => {
45
+ const bgSeed = args.background ?? DEFAULT_SEED_COLORS.bg;
46
+ const primarySeed = args.primary ?? DEFAULT_SEED_COLORS.primary;
47
+ const bgRamp = buildBgRamp( {
48
+ seed: bgSeed,
49
+ } );
50
+
51
+ const bgRampObj = {
52
+ seed: {
53
+ name: 'surface2' as const,
54
+ value: bgSeed,
55
+ },
56
+ ramp: bgRamp.ramp,
57
+ };
58
+
59
+ const primaryRampObj = {
60
+ seed: {
61
+ name: 'bgFill1' as const,
62
+ value: primarySeed,
63
+ },
64
+ ramp: buildAccentRamp( { seed: primarySeed, bgRamp } ).ramp,
65
+ };
66
+ const infoRampObj = {
67
+ seed: {
68
+ name: 'bgFill1' as const,
69
+ value: DEFAULT_SEED_COLORS.info,
70
+ },
71
+ ramp: buildAccentRamp( {
72
+ seed: DEFAULT_SEED_COLORS.info,
73
+ bgRamp,
74
+ } ).ramp,
75
+ };
76
+ const successRampObj = {
77
+ seed: {
78
+ name: 'bgFill1' as const,
79
+ value: DEFAULT_SEED_COLORS.success,
80
+ },
81
+ ramp: buildAccentRamp( {
82
+ seed: DEFAULT_SEED_COLORS.success,
83
+ bgRamp,
84
+ } ).ramp,
85
+ };
86
+ const warningRampObj = {
87
+ seed: {
88
+ name: 'bgFill1' as const,
89
+ value: DEFAULT_SEED_COLORS.warning,
90
+ },
91
+ ramp: buildAccentRamp( {
92
+ seed: DEFAULT_SEED_COLORS.warning,
93
+ bgRamp,
94
+ } ).ramp,
95
+ };
96
+ const errorRampObj = {
97
+ seed: {
98
+ name: 'bgFill1' as const,
99
+ value: DEFAULT_SEED_COLORS.error,
100
+ },
101
+ ramp: buildAccentRamp( {
102
+ seed: DEFAULT_SEED_COLORS.error,
103
+ bgRamp,
104
+ } ).ramp,
105
+ };
106
+
107
+ const unmetTargets = checkAccessibleCombinations( {
108
+ bgRamp,
109
+ } );
110
+
111
+ return (
112
+ <div
113
+ style={ {
114
+ display: 'flex',
115
+ flexDirection: 'column',
116
+ gap: '32px',
117
+ } }
118
+ >
119
+ <RampTable
120
+ ramps={ [
121
+ bgRampObj,
122
+ primaryRampObj,
123
+ infoRampObj,
124
+ successRampObj,
125
+ warningRampObj,
126
+ errorRampObj,
127
+ ] }
128
+ />
129
+
130
+ { unmetTargets.length === 0 ? (
131
+ <p>All accessibility targets met</p>
132
+ ) : (
133
+ <ul>
134
+ { unmetTargets.map(
135
+ (
136
+ {
137
+ bgName,
138
+ bgColor,
139
+ fgName,
140
+ fgColor,
141
+ unmetContrast,
142
+ },
143
+ i
144
+ ) => (
145
+ <li key={ i }>
146
+ <span
147
+ style={ {
148
+ width: 20,
149
+ height: 20,
150
+ backgroundColor: fgColor,
151
+ display: 'inline-block',
152
+ } }
153
+ ></span>
154
+ { fgName } over
155
+ <span
156
+ style={ {
157
+ width: 20,
158
+ height: 20,
159
+ backgroundColor: bgColor,
160
+ display: 'inline-block',
161
+ } }
162
+ ></span>
163
+ { bgName } did not meet { unmetContrast }
164
+ </li>
165
+ )
166
+ ) }
167
+ </ul>
168
+ ) }
169
+ </div>
170
+ );
171
+ },
172
+ args: {},
173
+ };
174
+
175
+ export const SampleCombinations: StoryObj< typeof ColorGen > = {
176
+ render: () => {
177
+ const combinations = [
178
+ // WordPress (light / dark)
179
+ {
180
+ background: '#f8f8f8',
181
+ primary: '#3858e9',
182
+ },
183
+ {
184
+ background: '#1e1e1e',
185
+ primary: '#3858e9',
186
+ },
187
+ // WP Classic
188
+ {
189
+ background: '#1d2327',
190
+ primary: '#2271b1',
191
+ },
192
+ // WP Light
193
+ {
194
+ background: '#e5e5e5',
195
+ primary: '#d64e07',
196
+ },
197
+ // WP Blue
198
+ {
199
+ background: '#096484',
200
+ primary: '#52accc',
201
+ },
202
+ // WP Coffee
203
+ {
204
+ background: '#46403c',
205
+ primary: '#c7a589',
206
+ },
207
+ // WP Ectoplasm
208
+ {
209
+ background: '#413256',
210
+ primary: '#a3b745',
211
+ },
212
+ // WP Ocean
213
+ {
214
+ background: '#627c83',
215
+ primary: '#9ebaa0',
216
+ },
217
+ // Sunrise
218
+ {
219
+ background: '#b43c38',
220
+ primary: '#dd823b',
221
+ },
222
+ ];
223
+
224
+ const ramps = combinations.map( ( { background, primary } ) => {
225
+ const bgRamp = buildBgRamp( { seed: background } );
226
+
227
+ const bgRampObj = {
228
+ seed: {
229
+ name: 'surface2' as const,
230
+ value: background,
231
+ },
232
+ ramp: bgRamp.ramp,
233
+ };
234
+
235
+ const primaryRampObj = {
236
+ seed: {
237
+ name: 'bgFill1' as const,
238
+ value: primary,
239
+ },
240
+ ramp: buildAccentRamp( { seed: primary, bgRamp } ).ramp,
241
+ };
242
+
243
+ return [ bgRampObj, primaryRampObj ];
244
+ } );
245
+
246
+ return (
247
+ <div
248
+ style={ { display: 'flex', flexDirection: 'column', gap: 16 } }
249
+ >
250
+ { ramps.map( ( r, i ) => (
251
+ <RampTable key={ i } ramps={ r } />
252
+ ) ) }
253
+ </div>
254
+ );
255
+ },
256
+ argTypes: {
257
+ background: {
258
+ control: false,
259
+ },
260
+ primary: {
261
+ control: false,
262
+ },
263
+ },
264
+ };
@@ -0,0 +1,212 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { forwardRef } from '@wordpress/element';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type { Ramp } from '../lib/types';
10
+
11
+ // TODO: show token groups better
12
+ const RAMP_TOKENS_ORDER: { tokenName: keyof Ramp; abbr: string }[] = [
13
+ { tokenName: 'surface1', abbr: 'SF1' },
14
+ { tokenName: 'surface2', abbr: 'SF2' },
15
+ { tokenName: 'surface3', abbr: 'SF3' },
16
+ { tokenName: 'surface4', abbr: 'SF4' },
17
+ { tokenName: 'surface5', abbr: 'SF5' },
18
+ { tokenName: 'surface6', abbr: 'SF6' },
19
+ { tokenName: 'bgFill1', abbr: 'BGF1' },
20
+ { tokenName: 'bgFill2', abbr: 'BGF2' },
21
+ { tokenName: 'bgFillInverted1', abbr: 'BGFI1' },
22
+ { tokenName: 'bgFillInverted2', abbr: 'BGFI2' },
23
+ { tokenName: 'bgFillDark', abbr: 'BGFD' },
24
+ { tokenName: 'stroke1', abbr: 'ST1' },
25
+ { tokenName: 'stroke2', abbr: 'ST2' },
26
+ { tokenName: 'stroke3', abbr: 'ST3' },
27
+ { tokenName: 'stroke4', abbr: 'ST4' },
28
+ { tokenName: 'fgSurface1', abbr: 'FGS1' },
29
+ { tokenName: 'fgSurface2', abbr: 'FGS2' },
30
+ { tokenName: 'fgSurface3', abbr: 'FGS3' },
31
+ { tokenName: 'fgSurface4', abbr: 'FGS4' },
32
+ { tokenName: 'fgFill', abbr: 'FGF' },
33
+ { tokenName: 'fgFillInverted', abbr: 'FGFI' },
34
+ { tokenName: 'fgFillDark', abbr: 'FGFD' },
35
+ ];
36
+
37
+ type RampTableProps = {
38
+ ramps: {
39
+ seed: {
40
+ name: keyof Ramp;
41
+ value: string;
42
+ };
43
+ ramp: Record<
44
+ keyof Ramp,
45
+ {
46
+ color: string;
47
+ warning: boolean;
48
+ }
49
+ >;
50
+ }[];
51
+ };
52
+ export const RampTable = forwardRef< HTMLDivElement, RampTableProps >(
53
+ function RampTable( { ramps }, forwardedRef ) {
54
+ return (
55
+ <div
56
+ style={ { width: '100%', overflowX: 'scroll' } }
57
+ ref={ forwardedRef }
58
+ >
59
+ <div
60
+ style={ {
61
+ display: 'grid',
62
+ gridTemplateColumns: `repeat(${ RAMP_TOKENS_ORDER.length }, minmax(max-content, 1fr))`,
63
+ fontFamily: '-apple-system, "system-ui", sans-serif',
64
+ alignItems: 'end',
65
+ } }
66
+ >
67
+ { RAMP_TOKENS_ORDER.map( ( { tokenName, abbr } ) => (
68
+ <div
69
+ key={ tokenName }
70
+ style={ {
71
+ textAlign: 'center',
72
+ padding: '8px 4px',
73
+ fontSize: 11,
74
+ fontWeight: 500,
75
+ color: ramps[ 0 ].ramp.fgSurface4.color,
76
+ } }
77
+ >
78
+ { abbr }
79
+ </div>
80
+ ) ) }
81
+ { ramps.map( ( { seed, ramp }, i ) =>
82
+ RAMP_TOKENS_ORDER.map( ( { tokenName } ) => (
83
+ <div
84
+ key={ `${ seed }-${ i }-${ tokenName }` }
85
+ style={ {
86
+ marginBlockStart: i !== 0 ? 4 : 0,
87
+ backgroundColor: ramp[ tokenName ].color,
88
+ display: 'grid',
89
+ gridTemplateRows: '20px 1fr',
90
+ placeItems: 'center',
91
+ height: tokenName === seed.name ? 60 : 40,
92
+ minWidth: 32,
93
+ fontSize: 14,
94
+ outline: ramp[ tokenName ].warning
95
+ ? '2px solid red'
96
+ : '',
97
+ outlineOffset: '-2px',
98
+ } }
99
+ >
100
+ { tokenName === seed.name ? (
101
+ <div
102
+ style={ {
103
+ backgroundColor: seed.value,
104
+ height: 20,
105
+ gridRowStart: 1,
106
+ gridRowEnd: 2,
107
+ display: 'grid',
108
+ placeItems: 'center',
109
+ width: '100%',
110
+ fontSize: 8,
111
+ fontWeight: 500,
112
+ color:
113
+ tokenName === 'surface2'
114
+ ? ramp.fgSurface4.color
115
+ : ramp.fgFill.color,
116
+ } }
117
+ >
118
+ SEED
119
+ </div>
120
+ ) : null }
121
+ { [
122
+ 'surface3',
123
+ 'bgFill1',
124
+ 'bgFillInverted1',
125
+ 'bgFillDark',
126
+ ].includes( tokenName ) ? (
127
+ <span
128
+ style={ {
129
+ padding: '2px 6px',
130
+ display: 'flex',
131
+ alignItems: 'center',
132
+ gap: 2,
133
+ gridRowStart:
134
+ tokenName === seed.name ? 2 : 1,
135
+ gridRowEnd: 3,
136
+ } }
137
+ >
138
+ { tokenName === 'surface3' ? (
139
+ <>
140
+ <span
141
+ style={ {
142
+ color: ramp.fgSurface1
143
+ .color,
144
+ } }
145
+ >
146
+ Aa
147
+ </span>
148
+ <span
149
+ style={ {
150
+ color: ramp.fgSurface2
151
+ .color,
152
+ } }
153
+ >
154
+ Aa
155
+ </span>
156
+ <span
157
+ style={ {
158
+ color: ramp.fgSurface3
159
+ .color,
160
+ } }
161
+ >
162
+ Aa
163
+ </span>
164
+ <span
165
+ style={ {
166
+ color: ramp.fgSurface4
167
+ .color,
168
+ } }
169
+ >
170
+ Aa
171
+ </span>
172
+ </>
173
+ ) : null }
174
+ { tokenName === 'bgFill1' ? (
175
+ <span
176
+ style={ {
177
+ color: ramp.fgFill.color,
178
+ } }
179
+ >
180
+ Aa
181
+ </span>
182
+ ) : null }
183
+ { tokenName === 'bgFillInverted1' ? (
184
+ <span
185
+ style={ {
186
+ color: ramp.fgFillInverted
187
+ .color,
188
+ } }
189
+ >
190
+ Aa
191
+ </span>
192
+ ) : null }
193
+ { tokenName === 'bgFillDark' ? (
194
+ <span
195
+ style={ {
196
+ color: ramp.fgFillDark
197
+ .color,
198
+ } }
199
+ >
200
+ Aa
201
+ </span>
202
+ ) : null }
203
+ </span>
204
+ ) : null }
205
+ </div>
206
+ ) )
207
+ ) }
208
+ </div>
209
+ </div>
210
+ );
211
+ }
212
+ );