-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.ts
More file actions
318 lines (289 loc) · 8.77 KB
/
Copy paththeme.ts
File metadata and controls
318 lines (289 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* Theme factory.
*
* Wraps a hue/saturation seed, a mutable `ColorMap`, and an optional
* per-theme `GlazeConfigOverride`. Exposes `tokens()` / `tasty()` /
* `json()` / `css()` / `dtcg()` / `dtcgResolver()` / `tailwind()` / `resolve()` /
* `export()` / `extend()`.
*
* The per-theme config override is **merged over the live global config at
* resolve time** so the theme still reacts to later `configure()` calls
* for fields it didn't override. The merged config is memoized by
* `configVersion` to avoid rebuilding it on every export call.
*/
import type { ChannelCtx } from './channels';
import {
freezeConfigForExport,
getConfig,
getConfigVersion,
mergeConfig,
} from './config';
import { assertAllPastel, assertNativeFormat } from './format-guard';
import {
buildCssMap,
buildDtcgMap,
buildDtcgResolver,
buildFlatTokenMap,
buildJsonMap,
buildTailwindMap,
buildTokenMap,
resolveModes,
} from './formatters';
import { resolveAllColors } from './resolver';
import { GLAZE_EXPORT_VERSION } from './serialize';
import type {
ColorDef,
ColorMap,
GlazeConfigOverride,
GlazeConfigResolved,
GlazeCssOptions,
GlazeCssResult,
GlazeDtcgOptions,
GlazeDtcgResolverDocument,
GlazeDtcgResolverOptions,
GlazeDtcgResult,
GlazeExtendOptions,
GlazeJsonOptions,
GlazeTailwindOptions,
GlazeTheme,
GlazeThemeExport,
GlazeThemeSeed,
GlazeTokenOptions,
ResolvedColor,
} from './types';
export function createTheme(
seed: GlazeThemeSeed,
initialColors?: ColorMap,
configOverride?: GlazeConfigOverride,
): GlazeTheme {
const { hue, saturation, darkHue, darkSaturation } = seed;
let colorDefs: ColorMap = initialColors ? { ...initialColors } : {};
let cache: {
map: Map<string, ResolvedColor> | null;
version: number;
effectiveConfig: GlazeConfigResolved;
} | null = null;
function getEffectiveConfig(): GlazeConfigResolved {
const version = getConfigVersion();
if (cache && cache.version === version) return cache.effectiveConfig;
const effectiveConfig = mergeConfig(getConfig(), configOverride);
cache = { map: null, version, effectiveConfig };
return effectiveConfig;
}
function resolveCached(): Map<string, ResolvedColor> {
const version = getConfigVersion();
if (cache && cache.version === version && cache.map) return cache.map;
const effectiveConfig = getEffectiveConfig();
const map = resolveAllColors(seed, colorDefs, effectiveConfig);
cache = { map, version, effectiveConfig };
return map;
}
function invalidate(): void {
cache = null;
}
function channelCtxFor(
options:
| {
splitHue?: boolean;
name?: string;
format?: GlazeCssOptions['format'];
modes?: GlazeJsonOptions['modes'];
}
| undefined,
formatDefault: 'rgb' | 'oklch' | 'okhsl',
prefix: string,
): ChannelCtx | undefined {
const format = options?.format ?? formatDefault;
if (!options?.splitHue || format !== 'oklch') return undefined;
const resolved = resolveCached();
const modes = resolveModes(options?.modes);
assertAllPastel(resolved, modes);
return {
seedHue: hue,
darkSeedHue: darkHue,
baseName: options.name ?? 'theme',
prefix,
defs: colorDefs,
mode: 'theme',
};
}
const theme: GlazeTheme = {
get hue() {
return hue;
},
get saturation() {
return saturation;
},
get darkHue() {
return darkHue;
},
get darkSaturation() {
return darkSaturation;
},
getConfig(): GlazeConfigResolved {
return getEffectiveConfig();
},
colors(defs: ColorMap): void {
colorDefs = { ...colorDefs, ...defs };
invalidate();
},
color(name: string, def?: ColorDef): ColorDef | undefined | void {
if (def === undefined) {
return colorDefs[name];
}
colorDefs[name] = def;
invalidate();
},
remove(names: string | string[]): void {
const list = Array.isArray(names) ? names : [names];
for (const name of list) {
delete colorDefs[name];
}
invalidate();
},
has(name: string): boolean {
return name in colorDefs;
},
list(): string[] {
return Object.keys(colorDefs);
},
reset(): void {
colorDefs = {};
invalidate();
},
export(override?: GlazeConfigOverride): GlazeThemeExport {
return {
kind: 'theme',
version: GLAZE_EXPORT_VERSION,
hue,
saturation,
...(darkHue !== undefined ? { darkHue } : {}),
...(darkSaturation !== undefined ? { darkSaturation } : {}),
colors: structuredClone(colorDefs),
config: freezeConfigForExport(configOverride, override),
};
},
extend(options: GlazeExtendOptions): GlazeTheme {
const childSeed: GlazeThemeSeed = {
hue: options.hue ?? hue,
saturation: options.saturation ?? saturation,
darkHue: options.darkHue ?? darkHue,
darkSaturation: options.darkSaturation ?? darkSaturation,
};
const inheritedColors: ColorMap = {};
for (const [name, def] of Object.entries(colorDefs)) {
if (def.inherit !== false) {
inheritedColors[name] = def;
}
}
const mergedColors = options.colors
? { ...inheritedColors, ...options.colors }
: { ...inheritedColors };
// Child inherits the parent override then merges in the per-extend override.
const mergedConfigOverride: GlazeConfigOverride | undefined =
configOverride || options.config
? { ...(configOverride ?? {}), ...(options.config ?? {}) }
: undefined;
return createTheme(childSeed, mergedColors, mergedConfigOverride);
},
resolve(): Map<string, ResolvedColor> {
// Defensive shallow clone: the cache holds the canonical Map for
// internal exporters; callers that mutate the returned Map must
// not corrupt subsequent cached reads.
return new Map(resolveCached());
},
tokens(options?: GlazeJsonOptions): Record<string, Record<string, string>> {
const format = options?.format ?? 'oklch';
assertNativeFormat(format, 'tokens');
const modes = resolveModes(options?.modes);
return buildFlatTokenMap(
resolveCached(),
'',
modes,
format,
getEffectiveConfig().pastel,
);
},
tasty(options?: GlazeTokenOptions): Record<string, Record<string, string>> {
const cfg = getEffectiveConfig();
const states = {
dark: options?.states?.dark ?? cfg.states.dark,
highContrast: options?.states?.highContrast ?? cfg.states.highContrast,
};
const modes = resolveModes(options?.modes);
const format = options?.format ?? 'oklch';
const channelCtx = channelCtxFor(options, 'oklch', '');
return buildTokenMap(
resolveCached(),
'',
states,
modes,
format,
cfg.pastel,
channelCtx,
);
},
json(options?: GlazeJsonOptions): Record<string, Record<string, string>> {
const format = options?.format ?? 'oklch';
assertNativeFormat(format, 'json');
const modes = resolveModes(options?.modes);
return buildJsonMap(
resolveCached(),
modes,
format,
getEffectiveConfig().pastel,
);
},
css(options?: GlazeCssOptions): GlazeCssResult {
const format = options?.format ?? 'oklch';
assertNativeFormat(format, 'css');
const channelCtx = channelCtxFor(options, 'oklch', '');
return buildCssMap(
resolveCached(),
'',
options?.suffix ?? '-color',
format,
getEffectiveConfig().pastel,
channelCtx,
);
},
dtcg(options?: GlazeDtcgOptions): GlazeDtcgResult {
const modes = resolveModes(options?.modes);
return buildDtcgMap(
resolveCached(),
'',
modes,
options?.colorSpace ?? 'srgb',
getEffectiveConfig().pastel,
);
},
dtcgResolver(
options?: GlazeDtcgResolverOptions,
): GlazeDtcgResolverDocument {
const result = buildDtcgMap(
resolveCached(),
'',
resolveModes(options?.modes),
options?.colorSpace ?? 'srgb',
getEffectiveConfig().pastel,
);
return buildDtcgResolver(result, options);
},
tailwind(options?: GlazeTailwindOptions): string {
const format = options?.format ?? 'oklch';
assertNativeFormat(format, 'tailwind');
const modes = resolveModes(options?.modes);
return buildTailwindMap(
resolveCached(),
'',
options?.namespace ?? 'color-',
modes,
format,
options?.darkSelector ?? '.dark',
options?.highContrastSelector ?? '.high-contrast',
getEffectiveConfig().pastel,
);
},
} as GlazeTheme;
return theme;
}