@radheim/theme 0.0.0-stage → 2.4.3
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/dist/tui/color.d.ts +8 -0
- package/dist/tui/color.js +67 -0
- package/dist/tui/expand.d.ts +4 -0
- package/dist/tui/expand.js +95 -0
- package/dist/tui/index.d.ts +9 -0
- package/dist/tui/index.js +8 -0
- package/dist/tui/resolve.d.ts +3362 -0
- package/dist/tui/resolve.js +265 -0
- package/dist/tui/schema.d.ts +4963 -0
- package/dist/tui/schema.js +240 -0
- package/dist/tui/select.d.ts +8 -0
- package/dist/tui/select.js +18 -0
- package/dist/tui/syntax.d.ts +3 -0
- package/dist/tui/syntax.js +78 -0
- package/dist/tui/types.d.ts +96 -0
- package/dist/tui/types.js +1 -0
- package/dist/tui/v1-migrate-new-designs.d.ts +3 -0
- package/dist/tui/v1-migrate-new-designs.js +353 -0
- package/dist/tui/v1-migrate.d.ts +30 -0
- package/dist/tui/v1-migrate.js +476 -0
- package/dist/tui/v1.d.ts +74 -0
- package/dist/tui/v1.js +1 -0
- package/package.json +43 -4
- package/README.md +0 -3
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { RGBA } from "@opentui/core";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
import { expandTheme, mergeTheme } from "./expand.js";
|
|
4
|
+
import { ActionState, ActionVariant, HueStep, SurfaceName, ThemeDefinition, ThemeDocument, } from "./schema.js";
|
|
5
|
+
import { selectThemeMode, themeModes } from "./select.js";
|
|
6
|
+
const decodeThemeDefinitionSchema = Schema.decodeUnknownSync(ThemeDefinition, { reportInput: true });
|
|
7
|
+
const decodeThemeDocumentSchema = Schema.decodeUnknownSync(ThemeDocument, { reportInput: true });
|
|
8
|
+
function decodeThemeDefinition(input) {
|
|
9
|
+
try {
|
|
10
|
+
return decodeThemeDefinitionSchema(input);
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
throw themeDecodeError(error, "theme");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function themeDecodeError(error, name) {
|
|
17
|
+
const message = Schema.isSchemaError(error) ? error.message : String(error);
|
|
18
|
+
const value = /got ("[^"]*"|\S+)/.exec(message)?.[1] ?? "value";
|
|
19
|
+
return new Error(`Invalid theme: ${name} ${value} is an invalid value`, { cause: error });
|
|
20
|
+
}
|
|
21
|
+
export function parseThemeDocument(input, name = "theme") {
|
|
22
|
+
try {
|
|
23
|
+
const document = decodeThemeDocumentSchema(input);
|
|
24
|
+
themeModes(document).forEach((mode) => resolveThemeDocument(document, mode));
|
|
25
|
+
return document;
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
throw themeDecodeError(error, name);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function resolveThemeDocument(document, mode) {
|
|
32
|
+
const selected = selectThemeMode(document, mode);
|
|
33
|
+
const definition = expandTheme(selected.theme);
|
|
34
|
+
return resolveExpandedTheme(definition);
|
|
35
|
+
}
|
|
36
|
+
export function resolveTheme(definition) {
|
|
37
|
+
return resolveExpandedTheme(expandTheme(decodeThemeDefinition(definition)));
|
|
38
|
+
}
|
|
39
|
+
function resolveExpandedTheme(definition) {
|
|
40
|
+
const hue = resolveHue(definition.hue);
|
|
41
|
+
const categorical = definition.categorical.map((name) => {
|
|
42
|
+
const scale = hue[name];
|
|
43
|
+
if (!scale)
|
|
44
|
+
throw new Error(`Categorical hue "${name}" was not found`);
|
|
45
|
+
return scale;
|
|
46
|
+
});
|
|
47
|
+
const hueSteps = compileHueSteps(hue);
|
|
48
|
+
const base = tokens(definition);
|
|
49
|
+
const views = {};
|
|
50
|
+
const view = (tokens) => ({
|
|
51
|
+
...resolveView(tokens, hue, categorical, hueSteps),
|
|
52
|
+
surface: (name) => views[name],
|
|
53
|
+
});
|
|
54
|
+
const dialog = definition["@dialog"];
|
|
55
|
+
const dialogTokens = dialog ? contextualize(base, tokens(dialog)) : base;
|
|
56
|
+
views.dialog = view(dialogTokens);
|
|
57
|
+
// The footer override layers on the dialog surface, so footers keep every dialog token they don't override.
|
|
58
|
+
const footer = dialog?.["@footer"];
|
|
59
|
+
views["dialog.footer"] = footer ? view(contextualize(dialogTokens, footer)) : views.dialog;
|
|
60
|
+
return view(base);
|
|
61
|
+
}
|
|
62
|
+
function tokens(definition) {
|
|
63
|
+
return {
|
|
64
|
+
text: definition.text,
|
|
65
|
+
background: definition.background,
|
|
66
|
+
border: definition.border,
|
|
67
|
+
scrollbar: definition.scrollbar,
|
|
68
|
+
diff: definition.diff,
|
|
69
|
+
// Optional: an unset key must stay absent, since the resolver rejects undefined values.
|
|
70
|
+
...(definition.find ? { find: definition.find } : {}),
|
|
71
|
+
syntax: definition.syntax,
|
|
72
|
+
markdown: definition.markdown,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function contextualize(base, override) {
|
|
76
|
+
const result = mergeTheme(base, override);
|
|
77
|
+
const text = result["text"];
|
|
78
|
+
const background = result["background"];
|
|
79
|
+
return {
|
|
80
|
+
...result,
|
|
81
|
+
text: { ...text, action: contextualActions(base.text?.action, override.text?.action) },
|
|
82
|
+
background: { ...background, action: contextualActions(base.background?.action, override.background?.action) },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function contextualActions(base, surface) {
|
|
86
|
+
return Object.fromEntries(ActionVariant.literals.map((variant) => {
|
|
87
|
+
const baseVariant = base?.[variant];
|
|
88
|
+
const surfaceVariant = surface?.[variant];
|
|
89
|
+
return [
|
|
90
|
+
variant,
|
|
91
|
+
Object.fromEntries(["base", ...ActionState.literals].map((state) => {
|
|
92
|
+
const key = state === "base" ? undefined : `$${state}`;
|
|
93
|
+
return [
|
|
94
|
+
key ?? "base",
|
|
95
|
+
(key ? surfaceVariant?.[key] : undefined) ??
|
|
96
|
+
surfaceVariant?.base ??
|
|
97
|
+
(key ? baseVariant?.[key] : undefined) ??
|
|
98
|
+
baseVariant?.base,
|
|
99
|
+
];
|
|
100
|
+
})),
|
|
101
|
+
];
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
function resolveView(definition, hue, categorical, hueSteps) {
|
|
105
|
+
const source = { hue, ...definition };
|
|
106
|
+
const resolved = createResolver(source)(source, "theme");
|
|
107
|
+
// Themes may leave any find color unset.
|
|
108
|
+
const find = resolved.find;
|
|
109
|
+
return {
|
|
110
|
+
...resolved,
|
|
111
|
+
hue,
|
|
112
|
+
categorical,
|
|
113
|
+
text: {
|
|
114
|
+
...resolved.text,
|
|
115
|
+
action: statefulActions(resolved.text.action),
|
|
116
|
+
formfield: statefulColor(resolved.text.formfield),
|
|
117
|
+
},
|
|
118
|
+
background: {
|
|
119
|
+
...resolved.background,
|
|
120
|
+
action: statefulActions(resolved.background.action),
|
|
121
|
+
formfield: statefulColor(resolved.background.formfield),
|
|
122
|
+
},
|
|
123
|
+
// Other matches sit on the highest raised surface; the current match looks like a focused action.
|
|
124
|
+
find: {
|
|
125
|
+
text: { current: find?.text?.current ?? resolved.text.action.primary.focused },
|
|
126
|
+
background: {
|
|
127
|
+
match: find?.background?.match ?? resolved.background.raised.max,
|
|
128
|
+
current: find?.background?.current ?? resolved.background.action.primary.focused,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
...hueSteps,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function statefulActions(actions) {
|
|
135
|
+
return Object.fromEntries(ActionVariant.literals.map((variant) => [variant, statefulColor(actions[variant])]));
|
|
136
|
+
}
|
|
137
|
+
function statefulColor(color) {
|
|
138
|
+
return {
|
|
139
|
+
...color,
|
|
140
|
+
state: (states) => color[ActionState.literals.find((state) => states[state]) ?? "base"],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function compileHueSteps(hue) {
|
|
144
|
+
const index = new WeakMap();
|
|
145
|
+
for (const [name, scale] of Object.entries(hue)) {
|
|
146
|
+
HueStep.literals.forEach((step, position) => index.set(scale[step], { hue: name, step, position }));
|
|
147
|
+
}
|
|
148
|
+
const shift = (color, amount) => {
|
|
149
|
+
const match = index.get(color);
|
|
150
|
+
if (!match)
|
|
151
|
+
return color;
|
|
152
|
+
const offset = Number.isFinite(amount) ? Math.trunc(amount) : 0;
|
|
153
|
+
const position = Math.max(0, Math.min(HueStep.literals.length - 1, match.position + offset));
|
|
154
|
+
return hue[match.hue][HueStep.literals[position]];
|
|
155
|
+
};
|
|
156
|
+
return {
|
|
157
|
+
source: (color) => {
|
|
158
|
+
const match = index.get(color);
|
|
159
|
+
return match ? { hue: match.hue, step: match.step } : undefined;
|
|
160
|
+
},
|
|
161
|
+
increase: (color, amount = 1) => shift(color, amount),
|
|
162
|
+
decrease: (color, amount = 1) => shift(color, -amount),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function resolveHue(definition) {
|
|
166
|
+
const source = definition;
|
|
167
|
+
const cache = new Map();
|
|
168
|
+
function resolve(name, stack) {
|
|
169
|
+
const hit = cache.get(name);
|
|
170
|
+
if (hit)
|
|
171
|
+
return hit;
|
|
172
|
+
if (stack.includes(name))
|
|
173
|
+
throw new Error(`Circular hue reference: ${[...stack, name].join(" -> ")}`);
|
|
174
|
+
const value = source[name];
|
|
175
|
+
if (typeof value === "string") {
|
|
176
|
+
const match = /^\$hue\.(.+)$/.exec(value);
|
|
177
|
+
if (!match?.[1])
|
|
178
|
+
throw new Error(`Hue alias "${value}" must reference a hue scale`);
|
|
179
|
+
if (source[match[1]] === undefined)
|
|
180
|
+
throw new Error(`Hue alias "${value}" references a missing hue`);
|
|
181
|
+
const target = resolve(match[1], [...stack, name]);
|
|
182
|
+
const result = Object.fromEntries(HueStep.literals.map((step) => [step, RGBA.clone(target[step])]));
|
|
183
|
+
cache.set(name, result);
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
if (!isRecord(value))
|
|
187
|
+
throw new Error(`Hue "${name}" was not found`);
|
|
188
|
+
const result = Object.fromEntries(HueStep.literals.map((step) => {
|
|
189
|
+
const color = value[step];
|
|
190
|
+
if (typeof color !== "string" || !isHex(color))
|
|
191
|
+
throw new Error(`Invalid hue color at "hue.${name}.${step}"`);
|
|
192
|
+
return [step, RGBA.fromHex(color)];
|
|
193
|
+
}));
|
|
194
|
+
for (const step of Object.keys(value)) {
|
|
195
|
+
if (!HueStep.literals.includes(Number(step)))
|
|
196
|
+
throw new Error(`Unknown hue step at "hue.${name}.${step}"`);
|
|
197
|
+
}
|
|
198
|
+
cache.set(name, result);
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
return Object.fromEntries(Object.keys(source).map((name) => [name, resolve(name, [])]));
|
|
202
|
+
}
|
|
203
|
+
function createResolver(source) {
|
|
204
|
+
const cache = new Map();
|
|
205
|
+
function resolve(value, path, stack = []) {
|
|
206
|
+
if (value instanceof RGBA)
|
|
207
|
+
return value;
|
|
208
|
+
if (typeof value === "string")
|
|
209
|
+
return resolveColor(value, path, stack);
|
|
210
|
+
if (typeof value === "number")
|
|
211
|
+
return value;
|
|
212
|
+
if (!isRecord(value))
|
|
213
|
+
throw new Error(`Invalid theme value at "${path}"`);
|
|
214
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [resolvedKey(key), resolve(item, `${path}.${key}`, stack)]));
|
|
215
|
+
}
|
|
216
|
+
function resolveColor(value, path, stack) {
|
|
217
|
+
if (value === "transparent")
|
|
218
|
+
return RGBA.fromInts(0, 0, 0, 0);
|
|
219
|
+
if (isHex(value))
|
|
220
|
+
return RGBA.fromHex(value);
|
|
221
|
+
if (!value.startsWith("$"))
|
|
222
|
+
throw new Error(`Invalid color "${value}" at "${path}"`);
|
|
223
|
+
const target = value.slice(1);
|
|
224
|
+
const hit = cache.get(target);
|
|
225
|
+
if (hit)
|
|
226
|
+
return hit;
|
|
227
|
+
if (stack.includes(target))
|
|
228
|
+
throw new Error(`Circular theme reference: ${[...stack, target].join(" -> ")}`);
|
|
229
|
+
const result = resolve(readColor(source, target), target, [...stack, target]);
|
|
230
|
+
if (!(result instanceof RGBA))
|
|
231
|
+
throw new Error(`Theme reference "${value}" at "${path}" is not a color`);
|
|
232
|
+
cache.set(target, result);
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
return (value, path) => resolve(value, path);
|
|
236
|
+
}
|
|
237
|
+
function readColor(source, path) {
|
|
238
|
+
const match = /^hue\.(.+)\.(100|200|300|400|500|600|700|800|900)$/.exec(path);
|
|
239
|
+
if (!match?.[1] || !match[2])
|
|
240
|
+
return read(source, path);
|
|
241
|
+
const scales = source.hue;
|
|
242
|
+
const scale = isRecord(scales) ? scales[match[1]] : undefined;
|
|
243
|
+
const color = isRecord(scale) ? scale[match[2]] : undefined;
|
|
244
|
+
if (color === undefined)
|
|
245
|
+
throw new Error(`Theme reference "$${path}" was not found`);
|
|
246
|
+
return color;
|
|
247
|
+
}
|
|
248
|
+
function resolvedKey(key) {
|
|
249
|
+
if (!key.startsWith("$"))
|
|
250
|
+
return key;
|
|
251
|
+
const state = key.slice(1);
|
|
252
|
+
return ActionState.literals.includes(state) ? state : key;
|
|
253
|
+
}
|
|
254
|
+
function read(source, path) {
|
|
255
|
+
const result = path.split(".").reduce((value, key) => (isRecord(value) ? value[key] : undefined), source);
|
|
256
|
+
if (result === undefined)
|
|
257
|
+
throw new Error(`Theme reference "$${path}" was not found`);
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
function isHex(value) {
|
|
261
|
+
return /^#(?:[\da-f]{3}|[\da-f]{4}|[\da-f]{6}|[\da-f]{8})$/i.test(value);
|
|
262
|
+
}
|
|
263
|
+
function isRecord(value) {
|
|
264
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof RGBA);
|
|
265
|
+
}
|