-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariables.ts
More file actions
427 lines (408 loc) · 11 KB
/
variables.ts
File metadata and controls
427 lines (408 loc) · 11 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
* CSS variable contracts and utilities.
*
* This module provides a type-safe system for defining and using CSS custom
* properties (variables). The contract system allows you to define the shape
* of your CSS variables once and get type-checked references throughout your
* codebase.
*
* @module
* @since 0.0.4
*/
import { type CssValue, hashObject } from "./_internal.ts";
import { type Style, style } from "./style.ts";
// =============================================================================
// Properties and Variables
// =============================================================================
/**
* A CSS custom property key (must start with `--`).
*
* @example
* ```ts
* import type { VariableKey } from "./variables.ts";
*
* const primary: VariableKey = "--primary-color";
* const spacing: VariableKey = "--spacing-unit";
* ```
*
* @since 0.0.4
*/
export type VariableKey = `--${string}`;
/**
* A CSS var() function reference to a custom property.
*
* @example
* ```ts
* import type { VariableValue } from "./variables.ts";
*
* const ref: VariableValue = "var(--primary-color)";
* const withFallback: VariableValue = "var(--primary-color, blue)";
* ```
*
* @since 0.0.4
*/
export type VariableValue = `var(${VariableKey}${string})`;
/**
* A partial record of CSS custom properties and their values.
*
* Used to define CSS variable declarations in style objects.
*
* @example
* ```ts
* import type { Variables } from "./variables.ts";
*
* const vars: Variables = {
* "--primary-color": "blue",
* "--spacing": "8px",
* "--font-size": 16,
* };
* ```
*
* @since 0.0.4
*/
export type Variables = Partial<Readonly<Record<VariableKey, CssValue>>>;
// =============================================================================
// CSS Variable Contracts
// =============================================================================
/**
* Recursive shape type for defining CSS variable structure.
*
* `null` marks a CSS variable leaf, nested objects mark groups.
* Used as input to `contract()` to define the variable structure.
*
* @example
* ```ts
* import type { Shape } from "./variables.ts";
*
* const shape = {
* colors: {
* primary: null, // variable
* brand: {
* light: null, // nested variable
* dark: null,
* },
* },
* spacing: null, // top-level variable
* } satisfies Shape;
* ```
*
* @since 0.0.4
*/
export type Shape<T = null> = {
readonly [key: string]: T | Shape<T>;
};
/**
* Recursively transform a Shape to have a different value type at each leaf.
*
* Maps over the shape structure, replacing leaf values with type `I`.
*
* @example
* ```ts
* import type { Shape, MapShape, VariableValue } from "./variables.ts";
*
* type MyShape = { colors: { primary: null; secondary: null } };
* type Mapped = MapShape<MyShape, VariableValue>;
* // { colors: { primary: VariableValue; secondary: VariableValue } }
* ```
*
* @since 0.0.4
*/
export type MapShape<T, I> = T extends Shape<infer A> ? {
readonly [K in keyof T]: T[K] extends A ? I
: T[K] extends Shape<A> ? MapShape<T[K], I>
: never;
}
: never;
/**
* Internal symbol for branding Contract objects.
*
* @internal
* @since 0.0.4
*/
const ContractHash: unique symbol = Symbol("@baetheus/css/core/contract");
/**
* Contract type - the result of the contract function.
*
* Contains `var()` references for each variable and a non-enumerable hash.
* Use the contract properties in your styles to reference CSS variables.
*
* @example
* ```ts
* import { contract, type Contract, type Shape } from "./variables.ts";
*
* const theme = contract({ colors: { primary: null } });
* // theme.colors.primary === "var(--abc1234-colors-primary)"
* ```
*
* @since 0.0.4
*/
export type Contract<T extends Shape> = MapShape<T, VariableValue> & {
readonly [ContractHash]: string;
};
/**
* Recursively walks a shape, calling onLeaf for each leaf value.
*
* A utility function for transforming shape structures. Traverses the shape
* tree and applies the `onLeaf` callback to each leaf node.
*
* @param shape - The shape structure to walk
* @param isLeaf - Predicate to identify leaf values
* @param onLeaf - Callback invoked for each leaf with value and path
* @param path - Current path in the shape (used internally)
* @returns A new shape with transformed leaf values
*
* @example
* ```ts
* import { walkShape, type Shape } from "./variables.ts";
*
* const shape = { a: { b: null, c: null } };
* const result = walkShape(
* shape,
* (v): v is null => v === null,
* (_, path) => path.join("-"),
* );
* // { a: { b: "a-b", c: "a-c" } }
* ```
*
* @since 0.0.4
*/
export function walkShape<A, T extends Shape<A>, I>(
shape: T,
isLeaf: (value: A | Shape<A>) => value is A,
onLeaf: (value: A, path: string[]) => I,
path: string[] = [],
): MapShape<T, I> {
const out: Record<string, unknown> = {};
for (const key in shape) {
const value = shape[key];
const _path = [...path, key];
if (isLeaf(value)) {
const _value = onLeaf(value, _path);
out[key] = _value;
} else {
out[key] = walkShape(value as Shape<A>, isLeaf, onLeaf, _path);
}
}
return out as MapShape<T, I>;
}
/**
* Recursively builds var() references for a shape.
*
* Transforms a shape structure into one where each leaf is replaced with
* a `var()` reference using the hash and path.
*
* @param shape - The shape structure to transform
* @param hash - The unique hash to use in variable names
* @returns A shape with var() references at each leaf
*
* @example
* ```ts
* import { buildVarShape, type Shape } from "./variables.ts";
*
* const shape = { colors: { primary: null } };
* const result = buildVarShape(shape, "abc1234");
* // { colors: { primary: "var(--abc1234-colors-primary)" } }
* ```
*
* @since 0.0.4
*/
export function buildVarShape<T extends Shape>(
shape: T,
hash: string,
): MapShape<T, VariableValue> {
return walkShape(
shape,
(v) => v === null,
(_, path) => `var(--${hash}-${path.join("-")})` as VariableValue,
);
}
/**
* Creates a CSS variable contract from a shape definition.
*
* The contract defines the structure of CSS variables and provides
* `var()` references that can be used in styles. The actual values
* are set separately using the `vars()` function.
*
* @param shape - An object defining the variable structure (null marks variables)
* @returns A Contract with var() references and a non-enumerable hash
*
* @example
* ```ts
* import { contract, vars, style, render } from "./mod.ts";
*
* // Define the contract with arbitrary nesting
* const theme = contract({
* colors: {
* primary: null,
* secondary: null,
* brand: {
* light: null,
* dark: null,
* },
* },
* spacing: null, // top-level variable
* });
*
* // Use var references in styles
* const button = style({
* color: theme.colors.primary,
* backgroundColor: theme.colors.brand.light,
* padding: theme.spacing,
* });
*
* // Create actual values
* const lightTheme = vars(theme, {
* colors: {
* primary: "blue",
* secondary: "green",
* brand: { light: "#eef", dark: "#335" },
* },
* spacing: "8px",
* });
*
* console.log(render(lightTheme, button));
* ```
*
* @since 0.0.4
*/
export function contract<T extends Shape>(shape: T): Contract<T> {
const hash = hashObject(shape);
const result = buildVarShape(shape, hash);
Object.defineProperty(result, ContractHash, {
value: hash,
enumerable: false,
writable: false,
configurable: false,
});
return result as Contract<T>;
}
/**
* Type guard to check if a value is a Contract object.
*
* @param value - The value to check
* @returns `true` if the value is a Contract, `false` otherwise
*
* @example
* ```ts
* import { contract, isContract } from "./variables.ts";
*
* const theme = contract({ colors: { primary: null } });
* isContract(theme); // true
* isContract({ colors: { primary: null } }); // false
* ```
*
* @since 0.0.4
*/
export function isContract(value: unknown): value is Contract<Shape> {
return typeof value === "object" && value !== null && ContractHash in value;
}
/**
* Recursively builds CSS custom properties from values.
*
* Transforms a shape with CSS values into a flat object of CSS custom
* property declarations.
*
* @param shape - The shape with CSS values at each leaf
* @param hash - The unique hash to use in variable names
* @returns A Variables object with CSS custom property declarations
*
* @example
* ```ts
* import { buildProperties } from "./variables.ts";
*
* const values = { colors: { primary: "blue" } };
* const props = buildProperties(values, "abc1234");
* // { "--abc1234-colors-primary": "blue" }
* ```
*
* @internal
* @since 0.0.4
*/
export function buildProperties<T extends Shape<CssValue>>(
shape: T,
hash: string,
): Variables {
const result: Record<string, CssValue> = {};
walkShape(
shape,
(v: CssValue | Shape<CssValue>): v is CssValue =>
typeof v === "string" || typeof v === "number",
(value, path) => result[`--${hash}-${path.join("-")}`] = value,
);
return result;
}
/**
* Recursively transform a Shape to have CssValue at each null leaf.
*
* Used as the input type for the `vars()` function to ensure values
* match the contract's shape structure.
*
* @example
* ```ts
* import type { Shape, VarsValues } from "./variables.ts";
*
* type MyShape = { colors: { primary: null } };
* type Values = VarsValues<MyShape>;
* // { colors: { primary: string | number } }
* ```
*
* @since 0.0.4
*/
export type VarsValues<T extends Shape> = MapShape<T, CssValue>;
/**
* Creates a Style with CSS custom properties from a contract and values.
*
* This function generates the actual CSS custom property definitions
* that match the contract's structure.
*
* @param contract - The contract defining the variable structure
* @param values - The actual values for each variable
* @returns A Style containing the CSS custom properties
*
* @example
* ```ts
* import { contract, vars, render } from "./mod.ts";
*
* const theme = contract({
* colors: {
* primary: null,
* brand: { light: null, dark: null },
* },
* spacing: null,
* });
*
* const light = vars(theme, {
* colors: {
* primary: "blue",
* brand: { light: "#eef", dark: "#335" },
* },
* spacing: "8px",
* });
*
* const dark = vars(theme, {
* colors: {
* primary: "white",
* brand: { light: "#335", dark: "#eef" },
* },
* spacing: "8px",
* });
*
* console.log(render(light));
* // .abc1234 {
* // --abc1234-colors-primary: blue;
* // --abc1234-colors-brand-light: #eef;
* // --abc1234-colors-brand-dark: #335;
* // --abc1234-spacing: 8px;
* // }
* ```
*
* @since 0.0.4
*/
export function vars<T extends Shape>(
contract: Contract<T>,
values: VarsValues<T>,
): Style {
const hash = contract[ContractHash];
const properties = buildProperties(values, hash);
return style(`.${hash}`, properties);
}