@und-ui/tokens 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +345 -2
- package/dist/android/values/und_colors.xml +91 -0
- package/dist/android/values/und_dimens.xml +93 -0
- package/dist/android/values-night/und_colors.xml +91 -0
- package/dist/css/base.css +2335 -0
- package/dist/css/motion.css +223 -0
- package/dist/css/themes/kesar.css +68 -0
- package/dist/css/themes/mitti.css +84 -0
- package/dist/css/themes/neem.css +84 -0
- package/dist/css/themes/raat.css +507 -0
- package/dist/css/themes/sutra.css +109 -0
- package/dist/css/themes/ujala.css +67 -0
- package/dist/css/tokens.css +3167 -0
- package/dist/dart/und_tokens.dart +3223 -0
- package/dist/js/contexts.d.ts +2 -0
- package/dist/js/contexts.js +3 -0
- package/dist/js/index.d.ts +1883 -0
- package/dist/js/index.js +2230 -0
- package/dist/json/flat/kesar.dark.json +737 -0
- package/dist/json/flat/kesar.darkMore.json +737 -0
- package/dist/json/flat/kesar.light.json +737 -0
- package/dist/json/flat/kesar.lightMore.json +737 -0
- package/dist/json/flat/mitti.dark.json +737 -0
- package/dist/json/flat/mitti.darkMore.json +737 -0
- package/dist/json/flat/mitti.light.json +737 -0
- package/dist/json/flat/mitti.lightMore.json +737 -0
- package/dist/json/flat/neem.dark.json +737 -0
- package/dist/json/flat/neem.darkMore.json +737 -0
- package/dist/json/flat/neem.light.json +737 -0
- package/dist/json/flat/neem.lightMore.json +737 -0
- package/dist/json/flat/raat.dark.json +737 -0
- package/dist/json/flat/raat.darkMore.json +737 -0
- package/dist/json/flat/sutra.dark.json +738 -0
- package/dist/json/flat/sutra.darkMore.json +738 -0
- package/dist/json/flat/sutra.light.json +738 -0
- package/dist/json/flat/sutra.lightMore.json +738 -0
- package/dist/json/flat/ujala.dark.json +737 -0
- package/dist/json/flat/ujala.darkMore.json +737 -0
- package/dist/json/flat/ujala.light.json +737 -0
- package/dist/json/flat/ujala.lightMore.json +737 -0
- package/dist/json/nested/kesar.dark.json +885 -0
- package/dist/json/nested/kesar.darkMore.json +885 -0
- package/dist/json/nested/kesar.light.json +885 -0
- package/dist/json/nested/kesar.lightMore.json +885 -0
- package/dist/json/nested/mitti.dark.json +885 -0
- package/dist/json/nested/mitti.darkMore.json +885 -0
- package/dist/json/nested/mitti.light.json +885 -0
- package/dist/json/nested/mitti.lightMore.json +885 -0
- package/dist/json/nested/neem.dark.json +885 -0
- package/dist/json/nested/neem.darkMore.json +885 -0
- package/dist/json/nested/neem.light.json +885 -0
- package/dist/json/nested/neem.lightMore.json +885 -0
- package/dist/json/nested/raat.dark.json +885 -0
- package/dist/json/nested/raat.darkMore.json +885 -0
- package/dist/json/nested/sutra.dark.json +886 -0
- package/dist/json/nested/sutra.darkMore.json +886 -0
- package/dist/json/nested/sutra.light.json +886 -0
- package/dist/json/nested/sutra.lightMore.json +886 -0
- package/dist/json/nested/ujala.dark.json +885 -0
- package/dist/json/nested/ujala.darkMore.json +885 -0
- package/dist/json/nested/ujala.light.json +885 -0
- package/dist/json/nested/ujala.lightMore.json +885 -0
- package/dist/kotlin/UndTokens.kt +2870 -0
- package/dist/lib/build/contexts.d.ts +13 -0
- package/dist/lib/build/contexts.js +20 -0
- package/dist/lib/build/css-values.d.ts +31 -0
- package/dist/lib/build/css-values.js +191 -0
- package/dist/lib/build/formats/css.d.ts +43 -0
- package/dist/lib/build/formats/css.js +380 -0
- package/dist/lib/build/formats/dart.d.ts +9 -0
- package/dist/lib/build/formats/dart.js +342 -0
- package/dist/lib/build/formats/js.d.ts +29 -0
- package/dist/lib/build/formats/js.js +297 -0
- package/dist/lib/build/formats/kotlin.d.ts +8 -0
- package/dist/lib/build/formats/kotlin.js +232 -0
- package/dist/lib/build/formats/react-native.d.ts +2 -0
- package/dist/lib/build/formats/react-native.js +208 -0
- package/dist/lib/build/formats/style-dictionary.d.ts +8 -0
- package/dist/lib/build/formats/style-dictionary.js +163 -0
- package/dist/lib/build/formats/swift.d.ts +8 -0
- package/dist/lib/build/formats/swift.js +395 -0
- package/dist/lib/build/formats/tailwind.d.ts +9 -0
- package/dist/lib/build/formats/tailwind.js +105 -0
- package/dist/lib/build/index.d.ts +12 -0
- package/dist/lib/build/index.js +133 -0
- package/dist/lib/build/materialize.d.ts +102 -0
- package/dist/lib/build/materialize.js +170 -0
- package/dist/lib/build/native-model.d.ts +46 -0
- package/dist/lib/build/native-model.js +177 -0
- package/dist/lib/color/contrast.d.ts +24 -0
- package/dist/lib/color/contrast.js +95 -0
- package/dist/lib/color/convert.d.ts +39 -0
- package/dist/lib/color/convert.js +91 -0
- package/dist/lib/color/pairs.d.ts +29 -0
- package/dist/lib/color/pairs.js +90 -0
- package/dist/lib/color/palette.d.ts +118 -0
- package/dist/lib/color/palette.js +199 -0
- package/dist/lib/color/scales.d.ts +18 -0
- package/dist/lib/color/scales.js +115 -0
- package/dist/lib/dtcg/engine.d.ts +75 -0
- package/dist/lib/dtcg/engine.js +395 -0
- package/dist/lib/dtcg/format-json.d.ts +1 -0
- package/dist/lib/dtcg/format-json.js +59 -0
- package/dist/lib/dtcg/types.d.ts +124 -0
- package/dist/lib/dtcg/types.js +54 -0
- package/dist/lib/index.d.ts +20 -0
- package/dist/lib/index.js +14 -0
- package/dist/lib/motion/spring.d.ts +40 -0
- package/dist/lib/motion/spring.js +129 -0
- package/dist/lib/source/generate.d.ts +28 -0
- package/dist/lib/source/generate.js +468 -0
- package/dist/lib/themes/definitions.d.ts +43 -0
- package/dist/lib/themes/definitions.js +140 -0
- package/dist/react-native/index.d.ts +130 -0
- package/dist/react-native/index.js +15 -0
- package/dist/react-native/tokens.d.ts +2 -0
- package/dist/react-native/tokens.js +4413 -0
- package/dist/scss/_variables.scss +738 -0
- package/dist/scss/_vars.scss +857 -0
- package/dist/scss/kesar.dark.scss +738 -0
- package/dist/scss/kesar.darkMore.scss +738 -0
- package/dist/scss/kesar.light.scss +738 -0
- package/dist/scss/kesar.lightMore.scss +738 -0
- package/dist/scss/mitti.dark.scss +738 -0
- package/dist/scss/mitti.darkMore.scss +738 -0
- package/dist/scss/mitti.light.scss +738 -0
- package/dist/scss/mitti.lightMore.scss +738 -0
- package/dist/scss/neem.dark.scss +738 -0
- package/dist/scss/neem.darkMore.scss +738 -0
- package/dist/scss/neem.light.scss +738 -0
- package/dist/scss/neem.lightMore.scss +738 -0
- package/dist/scss/raat.dark.scss +738 -0
- package/dist/scss/raat.darkMore.scss +738 -0
- package/dist/scss/sutra.dark.scss +739 -0
- package/dist/scss/sutra.darkMore.scss +739 -0
- package/dist/scss/sutra.light.scss +739 -0
- package/dist/scss/sutra.lightMore.scss +739 -0
- package/dist/scss/ujala.dark.scss +738 -0
- package/dist/scss/ujala.darkMore.scss +738 -0
- package/dist/scss/ujala.light.scss +738 -0
- package/dist/scss/ujala.lightMore.scss +738 -0
- package/dist/swift/UndTokens.swift +4117 -0
- package/dist/tailwind/und.css +586 -0
- package/package.json +84 -3
- package/tokens/contrast/more.tokens.json +46 -0
- package/tokens/density/comfortable.tokens.json +64 -0
- package/tokens/density/compact.tokens.json +68 -0
- package/tokens/density/touch.tokens.json +64 -0
- package/tokens/modes/dark.tokens.json +197 -0
- package/tokens/modes/light.tokens.json +166 -0
- package/tokens/motion/reduced.tokens.json +119 -0
- package/tokens/primitives/color.dark.tokens.json +2001 -0
- package/tokens/primitives/color.light.tokens.json +2077 -0
- package/tokens/primitives/dimension.tokens.json +181 -0
- package/tokens/primitives/motion.tokens.json +41 -0
- package/tokens/primitives/spring.tokens.json +138 -0
- package/tokens/primitives/typography.tokens.json +107 -0
- package/tokens/semantic/bridge.tokens.json +52 -0
- package/tokens/semantic/color.tokens.json +129 -0
- package/tokens/semantic/layout.tokens.json +84 -0
- package/tokens/semantic/motion.tokens.json +111 -0
- package/tokens/semantic/scale.tokens.json +66 -0
- package/tokens/semantic/typography.tokens.json +281 -0
- package/tokens/themes/kesar.tokens.json +105 -0
- package/tokens/themes/mitti.tokens.json +123 -0
- package/tokens/themes/neem.tokens.json +123 -0
- package/tokens/themes/raat.tokens.json +104 -0
- package/tokens/themes/sutra.tokens.json +126 -0
- package/tokens/themes/ujala.tokens.json +96 -0
- package/tokens/transparency/reduced.tokens.json +30 -0
- package/tokens/und.resolver.json +83 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { DtcgType } from './types.js';
|
|
2
|
+
type Obj = Record<string, unknown>;
|
|
3
|
+
export interface FlatToken {
|
|
4
|
+
/** Dot path, for example `bg.surface`. */
|
|
5
|
+
path: string;
|
|
6
|
+
/** Effective type (own, inherited from a group, or from the referenced token). */
|
|
7
|
+
type: DtcgType | undefined;
|
|
8
|
+
/** Raw `$value` as written. */
|
|
9
|
+
value: unknown;
|
|
10
|
+
description?: string;
|
|
11
|
+
extensions?: Obj;
|
|
12
|
+
deprecated?: boolean | string;
|
|
13
|
+
/** Source file (relative to tokens/). */
|
|
14
|
+
source: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ResolvedToken extends FlatToken {
|
|
17
|
+
type: DtcgType;
|
|
18
|
+
/** Fully resolved value (no references left). */
|
|
19
|
+
resolved: unknown;
|
|
20
|
+
/** If `$value` is a pure alias, the target path. */
|
|
21
|
+
alias?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ResolverDoc {
|
|
24
|
+
version: string;
|
|
25
|
+
sets: Record<string, {
|
|
26
|
+
sources: {
|
|
27
|
+
$ref: string;
|
|
28
|
+
}[];
|
|
29
|
+
description?: string;
|
|
30
|
+
}>;
|
|
31
|
+
modifiers: Record<string, {
|
|
32
|
+
contexts: Record<string, {
|
|
33
|
+
$ref: string;
|
|
34
|
+
}[]>;
|
|
35
|
+
default?: string;
|
|
36
|
+
description?: string;
|
|
37
|
+
}>;
|
|
38
|
+
resolutionOrder: {
|
|
39
|
+
$ref: string;
|
|
40
|
+
}[];
|
|
41
|
+
}
|
|
42
|
+
export type ResolverInput = Partial<Record<string, string>>;
|
|
43
|
+
export declare class TokenError extends Error {
|
|
44
|
+
}
|
|
45
|
+
export declare class TokenFiles {
|
|
46
|
+
readonly dir: string;
|
|
47
|
+
private cache;
|
|
48
|
+
constructor(dir: string);
|
|
49
|
+
read(rel: string): Obj;
|
|
50
|
+
private flatCache;
|
|
51
|
+
/** Flattened tokens of a file (cached; parse errors are re-reported on every call). */
|
|
52
|
+
flat(rel: string, errors?: string[]): FlatToken[];
|
|
53
|
+
resolver(path?: string): ResolverDoc;
|
|
54
|
+
}
|
|
55
|
+
/** Flattens one file into tokens, applying group `$type` inheritance. */
|
|
56
|
+
export declare function flattenFile(doc: Obj, source: string, errors?: string[]): FlatToken[];
|
|
57
|
+
export declare function contextSources(resolver: ResolverDoc, input?: ResolverInput): string[];
|
|
58
|
+
/** Merge files in order; a later token replaces an earlier one with the same path. */
|
|
59
|
+
export declare function mergeFiles(files: TokenFiles, sources: string[], errors?: string[]): Map<string, FlatToken>;
|
|
60
|
+
export declare function checkValue(type: DtcgType, v: unknown): string | null;
|
|
61
|
+
export interface ResolveResult {
|
|
62
|
+
tokens: Map<string, ResolvedToken>;
|
|
63
|
+
errors: string[];
|
|
64
|
+
}
|
|
65
|
+
export declare function resolveTokens(merged: Map<string, FlatToken>): ResolveResult;
|
|
66
|
+
export interface Context {
|
|
67
|
+
input: Required<Record<'mode' | 'theme' | 'contrast' | 'density' | 'transparency' | 'motion', string>>;
|
|
68
|
+
sources: string[];
|
|
69
|
+
tokens: Map<string, ResolvedToken>;
|
|
70
|
+
}
|
|
71
|
+
export declare const DEFAULT_INPUT: Context['input'];
|
|
72
|
+
export declare function resolveContext(files: TokenFiles, input?: Partial<Context['input']>): Context;
|
|
73
|
+
/** Every source file referenced by the resolver. */
|
|
74
|
+
export declare function allSources(resolver: ResolverDoc): string[];
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal, strict DTCG 2025.10 engine: load files through the resolver
|
|
3
|
+
* document, merge sources in resolution order, validate, and resolve
|
|
4
|
+
* references (including references nested inside composite values).
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { COLOR_SPACES, DTCG_TYPES, isReference, refPath } from './types.js';
|
|
9
|
+
const NAME_FORBIDDEN = /[{}.]/;
|
|
10
|
+
export class TokenError extends Error {
|
|
11
|
+
}
|
|
12
|
+
// ---------------------------------------------------------------- files
|
|
13
|
+
export class TokenFiles {
|
|
14
|
+
dir;
|
|
15
|
+
cache = new Map();
|
|
16
|
+
constructor(dir) {
|
|
17
|
+
this.dir = dir;
|
|
18
|
+
}
|
|
19
|
+
read(rel) {
|
|
20
|
+
let doc = this.cache.get(rel);
|
|
21
|
+
if (!doc) {
|
|
22
|
+
doc = JSON.parse(readFileSync(join(this.dir, rel), 'utf8'));
|
|
23
|
+
this.cache.set(rel, doc);
|
|
24
|
+
}
|
|
25
|
+
return doc;
|
|
26
|
+
}
|
|
27
|
+
flatCache = new Map();
|
|
28
|
+
/** Flattened tokens of a file (cached; parse errors are re-reported on every call). */
|
|
29
|
+
flat(rel, errors = []) {
|
|
30
|
+
let entry = this.flatCache.get(rel);
|
|
31
|
+
if (!entry) {
|
|
32
|
+
const errs = [];
|
|
33
|
+
entry = { tokens: flattenFile(this.read(rel), rel, errs), errors: errs };
|
|
34
|
+
this.flatCache.set(rel, entry);
|
|
35
|
+
}
|
|
36
|
+
errors.push(...entry.errors);
|
|
37
|
+
return entry.tokens;
|
|
38
|
+
}
|
|
39
|
+
resolver(path = 'und.resolver.json') {
|
|
40
|
+
return this.read(path);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// ---------------------------------------------------------------- flatten
|
|
44
|
+
/** Flattens one file into tokens, applying group `$type` inheritance. */
|
|
45
|
+
export function flattenFile(doc, source, errors = []) {
|
|
46
|
+
const out = [];
|
|
47
|
+
const walk = (node, path, inheritedType) => {
|
|
48
|
+
for (const [key, child] of Object.entries(node)) {
|
|
49
|
+
// `$root` is the DTCG reserved name for a group's base token.
|
|
50
|
+
if (key.startsWith('$') && key !== '$root')
|
|
51
|
+
continue;
|
|
52
|
+
if (NAME_FORBIDDEN.test(key))
|
|
53
|
+
errors.push(`${source}: invalid name "${[...path, key].join('.')}" (must not contain { } .)`);
|
|
54
|
+
if (typeof child !== 'object' || child === null || Array.isArray(child)) {
|
|
55
|
+
errors.push(`${source}: "${[...path, key].join('.')}" is not a token or group`);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const c = child;
|
|
59
|
+
const ownType = c.$type;
|
|
60
|
+
if (ownType !== undefined && !DTCG_TYPES.includes(ownType)) {
|
|
61
|
+
errors.push(`${source}: "${[...path, key].join('.')}" has unknown $type "${ownType}"`);
|
|
62
|
+
}
|
|
63
|
+
const type = ownType ?? inheritedType;
|
|
64
|
+
if ('$value' in c) {
|
|
65
|
+
for (const k of Object.keys(c)) {
|
|
66
|
+
if (!k.startsWith('$'))
|
|
67
|
+
errors.push(`${source}: token "${[...path, key].join('.')}" has child "${k}"`);
|
|
68
|
+
else if (!['$value', '$type', '$description', '$extensions', '$deprecated'].includes(k)) {
|
|
69
|
+
errors.push(`${source}: token "${[...path, key].join('.')}" has unknown property "${k}"`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
out.push({
|
|
73
|
+
path: [...path, key].join('.'),
|
|
74
|
+
type,
|
|
75
|
+
value: c.$value,
|
|
76
|
+
...(typeof c.$description === 'string' ? { description: c.$description } : {}),
|
|
77
|
+
...(c.$extensions ? { extensions: c.$extensions } : {}),
|
|
78
|
+
...(c.$deprecated !== undefined ? { deprecated: c.$deprecated } : {}),
|
|
79
|
+
source,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
walk(c, [...path, key], type);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
walk(doc, [], doc.$type);
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
// ---------------------------------------------------------------- resolver
|
|
91
|
+
export function contextSources(resolver, input = {}) {
|
|
92
|
+
if (resolver.version !== '2025.10')
|
|
93
|
+
throw new TokenError(`Unsupported resolver version ${resolver.version}`);
|
|
94
|
+
const files = [];
|
|
95
|
+
for (const entry of resolver.resolutionOrder) {
|
|
96
|
+
const ref = entry.$ref;
|
|
97
|
+
const setMatch = /^#\/sets\/(.+)$/.exec(ref);
|
|
98
|
+
const modMatch = /^#\/modifiers\/(.+)$/.exec(ref);
|
|
99
|
+
if (setMatch) {
|
|
100
|
+
const set = resolver.sets[setMatch[1]];
|
|
101
|
+
if (!set)
|
|
102
|
+
throw new TokenError(`Unknown set ${ref}`);
|
|
103
|
+
files.push(...set.sources.map((s) => s.$ref));
|
|
104
|
+
}
|
|
105
|
+
else if (modMatch) {
|
|
106
|
+
const name = modMatch[1];
|
|
107
|
+
const mod = resolver.modifiers[name];
|
|
108
|
+
if (!mod)
|
|
109
|
+
throw new TokenError(`Unknown modifier ${ref}`);
|
|
110
|
+
const ctx = input[name] ?? mod.default;
|
|
111
|
+
if (ctx === undefined)
|
|
112
|
+
throw new TokenError(`Modifier ${name} needs an input`);
|
|
113
|
+
const sources = mod.contexts[ctx];
|
|
114
|
+
if (!sources)
|
|
115
|
+
throw new TokenError(`Modifier ${name} has no context "${ctx}"`);
|
|
116
|
+
files.push(...sources.map((s) => s.$ref));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
throw new TokenError(`Unsupported resolutionOrder entry ${ref}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return files;
|
|
123
|
+
}
|
|
124
|
+
/** Merge files in order; a later token replaces an earlier one with the same path. */
|
|
125
|
+
export function mergeFiles(files, sources, errors = []) {
|
|
126
|
+
const merged = new Map();
|
|
127
|
+
for (const src of sources) {
|
|
128
|
+
for (const t of files.flat(src, errors)) {
|
|
129
|
+
const prev = merged.get(t.path);
|
|
130
|
+
// A token that omits $type in an override inherits the earlier token's type.
|
|
131
|
+
merged.set(t.path, t.type === undefined && prev?.type ? { ...t, type: prev.type } : t);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return merged;
|
|
135
|
+
}
|
|
136
|
+
// ---------------------------------------------------------------- value validation
|
|
137
|
+
const isNum = (v) => typeof v === 'number' && Number.isFinite(v);
|
|
138
|
+
function checkDimension(v) {
|
|
139
|
+
if (isReference(v))
|
|
140
|
+
return null;
|
|
141
|
+
const d = v;
|
|
142
|
+
if (typeof d !== 'object' ||
|
|
143
|
+
d === null ||
|
|
144
|
+
!isNum(d.value) ||
|
|
145
|
+
!['px', 'rem'].includes(d.unit)) {
|
|
146
|
+
return 'dimension must be { value: number, unit: "px" | "rem" }';
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
function checkDuration(v) {
|
|
151
|
+
if (isReference(v))
|
|
152
|
+
return null;
|
|
153
|
+
const d = v;
|
|
154
|
+
if (typeof d !== 'object' ||
|
|
155
|
+
d === null ||
|
|
156
|
+
!isNum(d.value) ||
|
|
157
|
+
!['ms', 's'].includes(d.unit)) {
|
|
158
|
+
return 'duration must be { value: number, unit: "ms" | "s" }';
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
function checkColor(v) {
|
|
163
|
+
if (isReference(v))
|
|
164
|
+
return null;
|
|
165
|
+
const c = v;
|
|
166
|
+
if (typeof c !== 'object' || c === null)
|
|
167
|
+
return 'color must be an object';
|
|
168
|
+
if (!COLOR_SPACES.includes(c.colorSpace))
|
|
169
|
+
return `unknown colorSpace ${String(c.colorSpace)}`;
|
|
170
|
+
if (!Array.isArray(c.components) || c.components.length !== 3)
|
|
171
|
+
return 'color needs 3 components';
|
|
172
|
+
if (!c.components.every((x) => isNum(x) || x === 'none'))
|
|
173
|
+
return 'components must be numbers or "none"';
|
|
174
|
+
if (c.alpha !== undefined && (!isNum(c.alpha) || c.alpha < 0 || c.alpha > 1))
|
|
175
|
+
return 'alpha must be 0..1';
|
|
176
|
+
if (c.hex !== undefined && !/^#[0-9a-f]{6}$/i.test(c.hex))
|
|
177
|
+
return 'hex must be 6-digit CSS hex';
|
|
178
|
+
if (c.colorSpace === 'oklch') {
|
|
179
|
+
const [l, ch, h] = c.components;
|
|
180
|
+
if (l < 0 || l > 1)
|
|
181
|
+
return 'oklch L must be 0..1';
|
|
182
|
+
if (ch < 0)
|
|
183
|
+
return 'oklch C must be >= 0';
|
|
184
|
+
if (h < 0 || h >= 360)
|
|
185
|
+
return 'oklch h must be 0..<360';
|
|
186
|
+
}
|
|
187
|
+
if (c.colorSpace === 'srgb' && !c.components.every((x) => x >= 0 && x <= 1))
|
|
188
|
+
return 'srgb components must be 0..1';
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
function checkCubic(v) {
|
|
192
|
+
if (isReference(v))
|
|
193
|
+
return null;
|
|
194
|
+
if (!Array.isArray(v) || v.length !== 4 || !v.every(isNum))
|
|
195
|
+
return 'cubicBezier must be 4 numbers';
|
|
196
|
+
const [x1, , x2] = v;
|
|
197
|
+
if (x1 < 0 || x1 > 1 || x2 < 0 || x2 > 1)
|
|
198
|
+
return 'cubicBezier x values must be 0..1';
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
const FONT_WEIGHT_NAMES = [
|
|
202
|
+
'thin',
|
|
203
|
+
'hairline',
|
|
204
|
+
'extra-light',
|
|
205
|
+
'ultra-light',
|
|
206
|
+
'light',
|
|
207
|
+
'normal',
|
|
208
|
+
'regular',
|
|
209
|
+
'book',
|
|
210
|
+
'medium',
|
|
211
|
+
'semi-bold',
|
|
212
|
+
'demi-bold',
|
|
213
|
+
'bold',
|
|
214
|
+
'extra-bold',
|
|
215
|
+
'ultra-bold',
|
|
216
|
+
'black',
|
|
217
|
+
'heavy',
|
|
218
|
+
'extra-black',
|
|
219
|
+
'ultra-black',
|
|
220
|
+
];
|
|
221
|
+
export function checkValue(type, v) {
|
|
222
|
+
if (isReference(v))
|
|
223
|
+
return null;
|
|
224
|
+
switch (type) {
|
|
225
|
+
case 'color':
|
|
226
|
+
return checkColor(v);
|
|
227
|
+
case 'dimension':
|
|
228
|
+
return checkDimension(v);
|
|
229
|
+
case 'duration':
|
|
230
|
+
return checkDuration(v);
|
|
231
|
+
case 'cubicBezier':
|
|
232
|
+
return checkCubic(v);
|
|
233
|
+
case 'number':
|
|
234
|
+
return isNum(v) ? null : 'number must be a JSON number';
|
|
235
|
+
case 'fontFamily':
|
|
236
|
+
return typeof v === 'string' ||
|
|
237
|
+
(Array.isArray(v) && v.length > 0 && v.every((x) => typeof x === 'string'))
|
|
238
|
+
? null
|
|
239
|
+
: 'fontFamily must be a string or string[]';
|
|
240
|
+
case 'fontWeight':
|
|
241
|
+
return (isNum(v) && v >= 1 && v <= 1000) || FONT_WEIGHT_NAMES.includes(v)
|
|
242
|
+
? null
|
|
243
|
+
: 'invalid fontWeight';
|
|
244
|
+
case 'shadow': {
|
|
245
|
+
const layers = Array.isArray(v) ? v : [v];
|
|
246
|
+
for (const l of layers) {
|
|
247
|
+
if (isReference(l))
|
|
248
|
+
continue;
|
|
249
|
+
const s = l;
|
|
250
|
+
const e = checkColor(s.color) ??
|
|
251
|
+
checkDimension(s.offsetX) ??
|
|
252
|
+
checkDimension(s.offsetY) ??
|
|
253
|
+
checkDimension(s.blur) ??
|
|
254
|
+
checkDimension(s.spread);
|
|
255
|
+
if (e)
|
|
256
|
+
return `shadow: ${e}`;
|
|
257
|
+
if (s.inset !== undefined && typeof s.inset !== 'boolean')
|
|
258
|
+
return 'shadow inset must be boolean';
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
case 'transition': {
|
|
263
|
+
const t = v;
|
|
264
|
+
return checkDuration(t.duration) ?? checkDuration(t.delay) ?? checkCubic(t.timingFunction);
|
|
265
|
+
}
|
|
266
|
+
case 'typography': {
|
|
267
|
+
const t = v;
|
|
268
|
+
const fam = t.fontFamily;
|
|
269
|
+
if (!isReference(fam) && checkValue('fontFamily', fam))
|
|
270
|
+
return 'typography.fontFamily invalid';
|
|
271
|
+
return (checkDimension(t.fontSize) ??
|
|
272
|
+
(isReference(t.fontWeight) ? null : checkValue('fontWeight', t.fontWeight)) ??
|
|
273
|
+
checkDimension(t.letterSpacing) ??
|
|
274
|
+
(isReference(t.lineHeight) || isNum(t.lineHeight)
|
|
275
|
+
? null
|
|
276
|
+
: 'typography.lineHeight must be a number'));
|
|
277
|
+
}
|
|
278
|
+
case 'border': {
|
|
279
|
+
const b = v;
|
|
280
|
+
return checkColor(b.color) ?? checkDimension(b.width);
|
|
281
|
+
}
|
|
282
|
+
case 'strokeStyle':
|
|
283
|
+
case 'gradient':
|
|
284
|
+
return null;
|
|
285
|
+
default:
|
|
286
|
+
return `unsupported type ${String(type)}`;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
export function resolveTokens(merged) {
|
|
290
|
+
const errors = [];
|
|
291
|
+
const out = new Map();
|
|
292
|
+
const visiting = new Set();
|
|
293
|
+
const typeOf = (path, chain = []) => {
|
|
294
|
+
const t = merged.get(path);
|
|
295
|
+
if (!t)
|
|
296
|
+
return undefined;
|
|
297
|
+
if (t.type)
|
|
298
|
+
return t.type;
|
|
299
|
+
if (isReference(t.value) && !chain.includes(path))
|
|
300
|
+
return typeOf(refPath(t.value), [...chain, path]);
|
|
301
|
+
return undefined;
|
|
302
|
+
};
|
|
303
|
+
const resolveDeep = (value, from) => {
|
|
304
|
+
if (isReference(value))
|
|
305
|
+
return resolvePath(refPath(value), from);
|
|
306
|
+
if (Array.isArray(value))
|
|
307
|
+
return value.map((v) => resolveDeep(v, from));
|
|
308
|
+
if (typeof value === 'object' && value !== null) {
|
|
309
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, resolveDeep(v, from)]));
|
|
310
|
+
}
|
|
311
|
+
return value;
|
|
312
|
+
};
|
|
313
|
+
const resolvePath = (path, from) => {
|
|
314
|
+
const target = merged.get(path);
|
|
315
|
+
if (!target) {
|
|
316
|
+
errors.push(`${from}: reference {${path}} does not resolve`);
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
const done = out.get(path);
|
|
320
|
+
if (done)
|
|
321
|
+
return done.resolved;
|
|
322
|
+
if (visiting.has(path)) {
|
|
323
|
+
errors.push(`${from}: reference cycle through {${path}}`);
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
visiting.add(path);
|
|
327
|
+
const resolved = resolveDeep(target.value, path);
|
|
328
|
+
visiting.delete(path);
|
|
329
|
+
const type = typeOf(path);
|
|
330
|
+
if (!type)
|
|
331
|
+
errors.push(`${target.source}: token "${path}" has no determinable $type`);
|
|
332
|
+
out.set(path, {
|
|
333
|
+
...target,
|
|
334
|
+
type: type ?? 'number',
|
|
335
|
+
resolved,
|
|
336
|
+
...(isReference(target.value) ? { alias: refPath(target.value) } : {}),
|
|
337
|
+
});
|
|
338
|
+
return resolved;
|
|
339
|
+
};
|
|
340
|
+
// A node is either a token or a group (DTCG); `$root` is how a group carries its own token.
|
|
341
|
+
const groups = new Set();
|
|
342
|
+
for (const path of merged.keys()) {
|
|
343
|
+
const parts = path.split('.');
|
|
344
|
+
for (let i = 1; i < parts.length; i++)
|
|
345
|
+
groups.add(parts.slice(0, i).join('.'));
|
|
346
|
+
}
|
|
347
|
+
for (const path of merged.keys()) {
|
|
348
|
+
if (groups.has(path))
|
|
349
|
+
errors.push(`${merged.get(path)?.source}: "${path}" is both a token and a group (use $root)`);
|
|
350
|
+
}
|
|
351
|
+
for (const path of merged.keys())
|
|
352
|
+
resolvePath(path, path);
|
|
353
|
+
for (const t of out.values()) {
|
|
354
|
+
const err = checkValue(t.type, t.value);
|
|
355
|
+
if (err)
|
|
356
|
+
errors.push(`${t.source}: "${t.path}": ${err}`);
|
|
357
|
+
if (t.alias) {
|
|
358
|
+
const targetType = out.get(t.alias)?.type;
|
|
359
|
+
if (targetType && targetType !== t.type)
|
|
360
|
+
errors.push(`${t.source}: "${t.path}" (${t.type}) aliases {${t.alias}} (${targetType})`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return { tokens: out, errors };
|
|
364
|
+
}
|
|
365
|
+
export const DEFAULT_INPUT = {
|
|
366
|
+
mode: 'light',
|
|
367
|
+
theme: 'ujala',
|
|
368
|
+
contrast: 'standard',
|
|
369
|
+
density: 'auto',
|
|
370
|
+
transparency: 'full',
|
|
371
|
+
motion: 'full',
|
|
372
|
+
};
|
|
373
|
+
export function resolveContext(files, input = {}) {
|
|
374
|
+
const full = { ...DEFAULT_INPUT, ...input };
|
|
375
|
+
const sources = contextSources(files.resolver(), full);
|
|
376
|
+
const errors = [];
|
|
377
|
+
const merged = mergeFiles(files, sources, errors);
|
|
378
|
+
const res = resolveTokens(merged);
|
|
379
|
+
errors.push(...res.errors);
|
|
380
|
+
if (errors.length)
|
|
381
|
+
throw new TokenError(`Token errors in context ${JSON.stringify(full)}:\n ${errors.join('\n ')}`);
|
|
382
|
+
return { input: full, sources, tokens: res.tokens };
|
|
383
|
+
}
|
|
384
|
+
/** Every source file referenced by the resolver. */
|
|
385
|
+
export function allSources(resolver) {
|
|
386
|
+
const set = new Set();
|
|
387
|
+
for (const s of Object.values(resolver.sets))
|
|
388
|
+
for (const r of s.sources)
|
|
389
|
+
set.add(r.$ref);
|
|
390
|
+
for (const m of Object.values(resolver.modifiers))
|
|
391
|
+
for (const c of Object.values(m.contexts))
|
|
392
|
+
for (const r of c)
|
|
393
|
+
set.add(r.$ref);
|
|
394
|
+
return [...set];
|
|
395
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatJson(value: unknown): string;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable JSON formatter for token files: objects and arrays that fit within
|
|
3
|
+
* the line budget and contain no nested objects-with-children stay on one line.
|
|
4
|
+
*/
|
|
5
|
+
const MAX = 100;
|
|
6
|
+
export function formatJson(value) {
|
|
7
|
+
return `${format(value, 0)}\n`;
|
|
8
|
+
}
|
|
9
|
+
function inline(value) {
|
|
10
|
+
return JSON.stringify(value, null, 1)
|
|
11
|
+
.replace(/\n\s*/g, ' ')
|
|
12
|
+
.replace(/\[ /g, '[')
|
|
13
|
+
.replace(/ \]/g, ']')
|
|
14
|
+
.replace(/\{ /g, '{ ')
|
|
15
|
+
.replace(/ \}/g, ' }');
|
|
16
|
+
}
|
|
17
|
+
function isLeafish(value) {
|
|
18
|
+
if (Array.isArray(value))
|
|
19
|
+
return value.every((v) => typeof v !== 'object' || v === null || isLeafish(v));
|
|
20
|
+
if (typeof value === 'object' && value !== null) {
|
|
21
|
+
return Object.values(value).every((v) => typeof v !== 'object' ||
|
|
22
|
+
v === null ||
|
|
23
|
+
(Array.isArray(v) && isLeafish(v)) ||
|
|
24
|
+
(!Array.isArray(v) &&
|
|
25
|
+
Object.values(v).every((x) => typeof x !== 'object' || x === null || Array.isArray(x))));
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
const NUMERIC = /^\d+(_\d+)?$/;
|
|
30
|
+
/**
|
|
31
|
+
* Key order: `$`-properties first, then numeric-like names (`0`, `0_5`, `12`)
|
|
32
|
+
* in numeric order, then the rest in insertion order. JS objects always put
|
|
33
|
+
* integer keys first, so this restores a human order for scales.
|
|
34
|
+
*/
|
|
35
|
+
function orderedEntries(obj) {
|
|
36
|
+
const all = Object.entries(obj);
|
|
37
|
+
const dollar = all.filter(([k]) => k.startsWith('$'));
|
|
38
|
+
const numeric = all
|
|
39
|
+
.filter(([k]) => NUMERIC.test(k))
|
|
40
|
+
.sort(([a], [b]) => Number(a.replace('_', '.')) - Number(b.replace('_', '.')));
|
|
41
|
+
const rest = all.filter(([k]) => !k.startsWith('$') && !NUMERIC.test(k));
|
|
42
|
+
return [...dollar, ...numeric, ...rest];
|
|
43
|
+
}
|
|
44
|
+
function format(value, indent) {
|
|
45
|
+
if (typeof value !== 'object' || value === null)
|
|
46
|
+
return JSON.stringify(value);
|
|
47
|
+
const pad = ' '.repeat(indent);
|
|
48
|
+
const one = inline(value);
|
|
49
|
+
if (isLeafish(value) && one.length + pad.length <= MAX)
|
|
50
|
+
return one;
|
|
51
|
+
if (Array.isArray(value)) {
|
|
52
|
+
const items = value.map((v) => `${pad} ${format(v, indent + 1)}`);
|
|
53
|
+
return `[\n${items.join(',\n')}\n${pad}]`;
|
|
54
|
+
}
|
|
55
|
+
const entries = orderedEntries(value).map(([k, v]) => `${pad} ${JSON.stringify(k)}: ${format(v, indent + 1)}`);
|
|
56
|
+
if (entries.length === 0)
|
|
57
|
+
return '{}';
|
|
58
|
+
return `{\n${entries.join(',\n')}\n${pad}}`;
|
|
59
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DTCG 2025.10 types used by UND UI (Format module + Color module).
|
|
3
|
+
* https://www.designtokens.org/tr/2025.10/format/
|
|
4
|
+
* https://www.designtokens.org/tr/2025.10/color/
|
|
5
|
+
*/
|
|
6
|
+
export declare const EXT: "com.urnextdoor.und";
|
|
7
|
+
export declare const DTCG_TYPES: readonly ["color", "dimension", "fontFamily", "fontWeight", "duration", "cubicBezier", "number", "strokeStyle", "border", "transition", "shadow", "gradient", "typography"];
|
|
8
|
+
export type DtcgType = (typeof DTCG_TYPES)[number];
|
|
9
|
+
export declare const COLOR_SPACES: readonly ["srgb", "srgb-linear", "hsl", "hwb", "lab", "lch", "oklab", "oklch", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "xyz-d65", "xyz-d50"];
|
|
10
|
+
export interface DtcgColor {
|
|
11
|
+
colorSpace: (typeof COLOR_SPACES)[number];
|
|
12
|
+
components: (number | 'none')[];
|
|
13
|
+
alpha?: number;
|
|
14
|
+
hex?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface DtcgDimension {
|
|
17
|
+
value: number;
|
|
18
|
+
unit: 'px' | 'rem';
|
|
19
|
+
}
|
|
20
|
+
export interface DtcgDuration {
|
|
21
|
+
value: number;
|
|
22
|
+
unit: 'ms' | 's';
|
|
23
|
+
}
|
|
24
|
+
export type Reference = `{${string}}`;
|
|
25
|
+
export interface DtcgShadowLayer {
|
|
26
|
+
color: DtcgColor | Reference;
|
|
27
|
+
offsetX: DtcgDimension | Reference;
|
|
28
|
+
offsetY: DtcgDimension | Reference;
|
|
29
|
+
blur: DtcgDimension | Reference;
|
|
30
|
+
spread: DtcgDimension | Reference;
|
|
31
|
+
inset?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface DtcgTransition {
|
|
34
|
+
duration: DtcgDuration | Reference;
|
|
35
|
+
delay: DtcgDuration | Reference;
|
|
36
|
+
timingFunction: [number, number, number, number] | Reference;
|
|
37
|
+
}
|
|
38
|
+
export interface DtcgTypography {
|
|
39
|
+
fontFamily: string | string[] | Reference;
|
|
40
|
+
fontSize: DtcgDimension | Reference;
|
|
41
|
+
fontWeight: number | string | Reference;
|
|
42
|
+
letterSpacing: DtcgDimension | Reference;
|
|
43
|
+
lineHeight: number | Reference;
|
|
44
|
+
}
|
|
45
|
+
export interface DtcgToken {
|
|
46
|
+
$value: unknown;
|
|
47
|
+
$type?: DtcgType;
|
|
48
|
+
$description?: string;
|
|
49
|
+
$extensions?: Record<string, unknown>;
|
|
50
|
+
$deprecated?: boolean | string;
|
|
51
|
+
}
|
|
52
|
+
export interface DtcgGroup {
|
|
53
|
+
$type?: DtcgType;
|
|
54
|
+
$description?: string;
|
|
55
|
+
$extensions?: Record<string, unknown>;
|
|
56
|
+
$deprecated?: boolean | string;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
/** UND-specific extension payload (`$extensions["com.urnextdoor.und"]`). */
|
|
60
|
+
export interface UndExtension {
|
|
61
|
+
/** P3 components of an oklch colour (display-p3, 0..1). */
|
|
62
|
+
p3?: [number, number, number];
|
|
63
|
+
/** sRGB-safe chroma of an oklch colour. */
|
|
64
|
+
srgbChroma?: number;
|
|
65
|
+
/** Spring parameters (spring tokens). */
|
|
66
|
+
kind?: 'spring';
|
|
67
|
+
response?: number;
|
|
68
|
+
dampingRatio?: number;
|
|
69
|
+
mass?: number;
|
|
70
|
+
stiffness?: number;
|
|
71
|
+
damping?: number;
|
|
72
|
+
bounce?: number;
|
|
73
|
+
overshoot?: number;
|
|
74
|
+
settleMs?: number;
|
|
75
|
+
linear?: string;
|
|
76
|
+
/** Use this spring as the easing (semantic easing tokens). */
|
|
77
|
+
spring?: string;
|
|
78
|
+
/** Duration tokens: false = not scaled by tempo. */
|
|
79
|
+
tempo?: boolean;
|
|
80
|
+
/** Radius tokens: scaled by `radius.factor`. */
|
|
81
|
+
radiusScale?: boolean;
|
|
82
|
+
/** Fluid font size. */
|
|
83
|
+
fluid?: {
|
|
84
|
+
minPx: number;
|
|
85
|
+
maxPx: number;
|
|
86
|
+
minViewportPx: number;
|
|
87
|
+
maxViewportPx: number;
|
|
88
|
+
};
|
|
89
|
+
/** Letter spacing in em (Latin only). */
|
|
90
|
+
letterSpacingEm?: number;
|
|
91
|
+
/** Font size in em (code). */
|
|
92
|
+
fontSizeEm?: number;
|
|
93
|
+
/** Indic (Devanagari etc.) line height. */
|
|
94
|
+
lineHeightIndic?: number;
|
|
95
|
+
ios?: {
|
|
96
|
+
textStyle: string;
|
|
97
|
+
size: number;
|
|
98
|
+
weight: string;
|
|
99
|
+
};
|
|
100
|
+
android?: {
|
|
101
|
+
textStyle: string;
|
|
102
|
+
size: number;
|
|
103
|
+
lineHeight: number;
|
|
104
|
+
};
|
|
105
|
+
/** Number tokens with a CSS unit that DTCG dimension cannot express (for example ch). */
|
|
106
|
+
unit?: string;
|
|
107
|
+
/** v0 component bridge name (CSS and JS only). */
|
|
108
|
+
bridge?: boolean;
|
|
109
|
+
/** Hairline at 2dppx. */
|
|
110
|
+
hiDpiPx?: number;
|
|
111
|
+
[key: string]: unknown;
|
|
112
|
+
}
|
|
113
|
+
export declare function isBridge(token: {
|
|
114
|
+
$extensions?: Record<string, unknown>;
|
|
115
|
+
extensions?: Record<string, unknown>;
|
|
116
|
+
}): boolean;
|
|
117
|
+
export declare function isReference(v: unknown): v is Reference;
|
|
118
|
+
export declare function refPath(ref: string): string;
|
|
119
|
+
export declare function isToken(node: unknown): node is DtcgToken;
|
|
120
|
+
/** UND extension payload of a raw DTCG token (`$extensions`) or a flattened token (`extensions`). */
|
|
121
|
+
export declare function undExt(token: {
|
|
122
|
+
$extensions?: Record<string, unknown>;
|
|
123
|
+
extensions?: Record<string, unknown>;
|
|
124
|
+
}): UndExtension;
|