|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | | -import { Theme, Context, Mode } from './interfaces'; |
| 3 | +import { |
| 4 | + Theme, |
| 5 | + Context, |
| 6 | + Mode, |
| 7 | + ReferenceTokens, |
| 8 | + ColorReferenceTokens, |
| 9 | + ColorPaletteInput, |
| 10 | + PaletteStep, |
| 11 | + ColorPaletteDefinition, |
| 12 | +} from './interfaces'; |
4 | 13 |
|
5 | 14 | export type TokenCategory<T extends string, V> = Record<T, V>; |
6 | 15 |
|
@@ -46,7 +55,60 @@ export class ThemeBuilder { |
46 | 55 | return this; |
47 | 56 | } |
48 | 57 |
|
| 58 | + addReferenceTokens(referenceTokens: ReferenceTokens): ThemeBuilder { |
| 59 | + this.theme.referenceTokens = referenceTokens; |
| 60 | + |
| 61 | + // Process reference tokens and add generated tokens to theme |
| 62 | + if (referenceTokens.color) { |
| 63 | + const generatedTokens = this.processReferenceTokens(referenceTokens.color); |
| 64 | + this.theme.tokens = { ...this.theme.tokens, ...generatedTokens }; |
| 65 | + } |
| 66 | + |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
49 | 70 | build(): Theme { |
50 | 71 | return this.theme; |
51 | 72 | } |
| 73 | + |
| 74 | + private processReferenceTokens(colorTokens: ColorReferenceTokens): TokenCategory<string, string> { |
| 75 | + const generatedTokens: TokenCategory<string, string> = {}; |
| 76 | + |
| 77 | + Object.entries(colorTokens).forEach(([colorName, paletteInput]) => { |
| 78 | + const palette = this.processColorPaletteInput(paletteInput); |
| 79 | + |
| 80 | + // Add generated palette tokens with naming convention: colorPrimary50, colorPrimary600, etc. |
| 81 | + Object.entries(palette).forEach(([step, value]) => { |
| 82 | + const tokenName = `color${this.capitalize(colorName)}${step}`; |
| 83 | + generatedTokens[tokenName] = value; |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + return generatedTokens; |
| 88 | + } |
| 89 | + |
| 90 | + // Right now just validates steps, but will also handle seed token color generation in a future PR |
| 91 | + private processColorPaletteInput(input: ColorPaletteInput): ColorPaletteDefinition { |
| 92 | + const validSteps: number[] = []; |
| 93 | + // Add steps 50-1000 in increments of 50 |
| 94 | + for (let i = 50; i <= 1000; i += 50) { |
| 95 | + validSteps.push(i); |
| 96 | + } |
| 97 | + |
| 98 | + const result: ColorPaletteDefinition = {}; |
| 99 | + |
| 100 | + // Add explicit step values |
| 101 | + Object.entries(input).forEach(([step, value]) => { |
| 102 | + const numStep = Number(step); |
| 103 | + if (value && validSteps.includes(numStep)) { |
| 104 | + result[numStep as PaletteStep] = value; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + return result; |
| 109 | + } |
| 110 | + |
| 111 | + private capitalize(str: string): string { |
| 112 | + return str.charAt(0).toUpperCase() + str.slice(1); |
| 113 | + } |
52 | 114 | } |
0 commit comments