@agimon-ai/po-theme 0.0.0-stage → 0.0.2-alpha.1
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 +110 -0
- package/README.md +7 -2
- package/dist/index.cjs +44 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.mjs +9 -0
- package/dist/po-runtime-identity.json +1 -0
- package/dist/src/theme/apply.cjs +33 -0
- package/dist/src/theme/apply.d.mts +24 -0
- package/dist/src/theme/apply.mjs +30 -0
- package/dist/src/theme/builtinThemes.cjs +23 -0
- package/dist/src/theme/builtinThemes.d.mts +10 -0
- package/dist/src/theme/builtinThemes.mjs +20 -0
- package/dist/src/theme/derive.cjs +31 -0
- package/dist/src/theme/derive.d.mts +13 -0
- package/dist/src/theme/derive.mjs +28 -0
- package/dist/src/theme/nativeTheme.cjs +195 -0
- package/dist/src/theme/nativeTheme.d.mts +130 -0
- package/dist/src/theme/nativeTheme.mjs +183 -0
- package/dist/src/theme/parse.cjs +41 -0
- package/dist/src/theme/parse.d.mts +9 -0
- package/dist/src/theme/parse.mjs +41 -0
- package/dist/src/theme/piTheme.cjs +75 -0
- package/dist/src/theme/piTheme.d.mts +18 -0
- package/dist/src/theme/piTheme.mjs +75 -0
- package/dist/src/theme/preference.cjs +23 -0
- package/dist/src/theme/preference.d.mts +14 -0
- package/dist/src/theme/preference.mjs +21 -0
- package/dist/src/types/theme.cjs +74 -0
- package/dist/src/types/theme.d.mts +37 -0
- package/dist/src/types/theme.mjs +70 -0
- package/dist/themes/po-nord-dark.cjs +35 -0
- package/dist/themes/po-nord-dark.mjs +30 -0
- package/dist/themes/po-one-light.cjs +36 -0
- package/dist/themes/po-one-light.mjs +31 -0
- package/package.json +51 -3
- package/themes/po-nord-dark.json +27 -0
- package/themes/po-one-light.json +28 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { ThemeConfig, ThemeScheme, ThemeToken } from "../types/theme.mjs";
|
|
2
|
+
//#region src/theme/nativeTheme.d.ts
|
|
3
|
+
/** The spacing unit, matching `--spacing: 0.25rem` at a 16px root. */
|
|
4
|
+
export declare const NATIVE_SPACING_BASE = 4;
|
|
5
|
+
/** Corner radii in px, matching `--radius-*`. */
|
|
6
|
+
export declare const NATIVE_RADII: {
|
|
7
|
+
readonly xs: 2;
|
|
8
|
+
readonly sm: 3;
|
|
9
|
+
readonly md: 5;
|
|
10
|
+
readonly lg: 10;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Every radius a native style may name: the retunable `--radius-*` steps, plus
|
|
14
|
+
* Tailwind's hardcoded bare `rounded` (4px) and `rounded-full`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const NATIVE_BORDER_RADIUS: {
|
|
17
|
+
readonly xs: 2;
|
|
18
|
+
readonly sm: 3;
|
|
19
|
+
readonly md: 5;
|
|
20
|
+
readonly lg: 10;
|
|
21
|
+
readonly DEFAULT: 4;
|
|
22
|
+
readonly full: 9999;
|
|
23
|
+
};
|
|
24
|
+
/** The type ramp, matching `--text-*` (px) and `--text-*--line-height` (a multiple of the size). */
|
|
25
|
+
export declare const NATIVE_TYPE_RAMP: {
|
|
26
|
+
readonly '2xs': {
|
|
27
|
+
readonly size: 9;
|
|
28
|
+
readonly leading: 1.45;
|
|
29
|
+
};
|
|
30
|
+
readonly xs: {
|
|
31
|
+
readonly size: 10;
|
|
32
|
+
readonly leading: 1.45;
|
|
33
|
+
};
|
|
34
|
+
readonly sm: {
|
|
35
|
+
readonly size: 12;
|
|
36
|
+
readonly leading: 1.45;
|
|
37
|
+
};
|
|
38
|
+
readonly base: {
|
|
39
|
+
readonly size: 13;
|
|
40
|
+
readonly leading: 1.5;
|
|
41
|
+
};
|
|
42
|
+
readonly lg: {
|
|
43
|
+
readonly size: 15;
|
|
44
|
+
readonly leading: 1.4;
|
|
45
|
+
};
|
|
46
|
+
readonly xl: {
|
|
47
|
+
readonly size: 18;
|
|
48
|
+
readonly leading: 1.3;
|
|
49
|
+
};
|
|
50
|
+
readonly '2xl': {
|
|
51
|
+
readonly size: 22;
|
|
52
|
+
readonly leading: 1.25;
|
|
53
|
+
};
|
|
54
|
+
readonly '3xl': {
|
|
55
|
+
readonly size: 28;
|
|
56
|
+
readonly leading: 1.2;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/** Title tracking in em, matching `--text-*--letter-spacing`; steps not listed track at 0. */
|
|
60
|
+
export declare const NATIVE_LETTER_SPACING_EM: {
|
|
61
|
+
readonly xl: -0.01;
|
|
62
|
+
readonly '2xl': -0.01;
|
|
63
|
+
readonly '3xl': -0.01;
|
|
64
|
+
};
|
|
65
|
+
/** Tailwind's `leading-relaxed` multiple, which theme.css does not override. */
|
|
66
|
+
export declare const NATIVE_LEADING_RELAXED = 1.625;
|
|
67
|
+
/** Font weights as React Native takes them, matching `font-normal`, `font-medium` and `font-semibold`. */
|
|
68
|
+
export declare const NATIVE_FONT_WEIGHT: {
|
|
69
|
+
readonly regular: '400';
|
|
70
|
+
readonly medium: '500';
|
|
71
|
+
readonly semibold: '600';
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Durations in ms and the shared easing curve of `--animate-po-*`. `slide`
|
|
75
|
+
* covers both slide directions; `easing` is the `cubic-bezier()` control points
|
|
76
|
+
* for Reanimated's `Easing.bezier`.
|
|
77
|
+
*/
|
|
78
|
+
export declare const NATIVE_MOTION: {
|
|
79
|
+
readonly fade: 140;
|
|
80
|
+
readonly pop: 160;
|
|
81
|
+
readonly rise: 160;
|
|
82
|
+
readonly slide: 180;
|
|
83
|
+
readonly easing: readonly [0.16, 1, 0.3, 1];
|
|
84
|
+
};
|
|
85
|
+
/** A font as native styles name it: one family, plus the platform font to use until it is bundled. */
|
|
86
|
+
export interface NativeFontStack {
|
|
87
|
+
family: string;
|
|
88
|
+
ios: string;
|
|
89
|
+
android: string;
|
|
90
|
+
}
|
|
91
|
+
/** The sans and mono families of `--font-sans` and `--po-font-mono`, with system fallbacks. */
|
|
92
|
+
export declare const NATIVE_FONT_FAMILY: {
|
|
93
|
+
readonly sans: NativeFontStack;
|
|
94
|
+
readonly mono: NativeFontStack;
|
|
95
|
+
};
|
|
96
|
+
export type NativeColorToken = Exclude<ThemeToken, 'font-mono'>;
|
|
97
|
+
export type NativeTypeStep = keyof typeof NATIVE_TYPE_RAMP;
|
|
98
|
+
export type NativeNamedTheme = 'dark' | 'light';
|
|
99
|
+
export interface PoNativeTheme {
|
|
100
|
+
scheme: ThemeScheme;
|
|
101
|
+
colors: Record<NativeColorToken, string>;
|
|
102
|
+
/** `spacing(n)` is n steps of the 4px base, as Tailwind's `p-n`. */
|
|
103
|
+
spacing: (steps: number) => number;
|
|
104
|
+
borderRadius: typeof NATIVE_BORDER_RADIUS;
|
|
105
|
+
fontSize: Record<NativeTypeStep, number>;
|
|
106
|
+
/** Absolute line heights in px, since native styles take no unitless multiple. */
|
|
107
|
+
lineHeight: Record<NativeTypeStep, number>;
|
|
108
|
+
/** `leading-relaxed` line heights in px, for reading text such as markdown bodies. */
|
|
109
|
+
lineHeightRelaxed: Record<NativeTypeStep, number>;
|
|
110
|
+
/** Letter spacing in px per step; titles track slightly tight, the rest at 0. */
|
|
111
|
+
letterSpacing: Record<NativeTypeStep, number>;
|
|
112
|
+
fontWeight: typeof NATIVE_FONT_WEIGHT;
|
|
113
|
+
fontFamily: typeof NATIVE_FONT_FAMILY;
|
|
114
|
+
motion: typeof NATIVE_MOTION;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* `color-mix(in srgb, first percent%, second)` for hex inputs: a straight
|
|
118
|
+
* per-channel blend, rounded to the nearest 8-bit value.
|
|
119
|
+
*/
|
|
120
|
+
export declare function mixHex(first: string, percent: number, second: string): string;
|
|
121
|
+
/**
|
|
122
|
+
* A hex colour at `percent` opacity as `#rrggbbaa`: the native form of
|
|
123
|
+
* Tailwind's `/40`-style opacity modifiers.
|
|
124
|
+
*/
|
|
125
|
+
export declare function withAlpha(hex: string, percent: number): string;
|
|
126
|
+
/** Resolves a web theme config to native values; tokens the theme pins win over the derived mix. */
|
|
127
|
+
export declare function nativeTheme(config: ThemeConfig): PoNativeTheme;
|
|
128
|
+
/** The native themes by the names the native shell registers: `dark` and `light`. */
|
|
129
|
+
export declare const NATIVE_THEMES: Readonly<Record<NativeNamedTheme, PoNativeTheme>>;
|
|
130
|
+
//#endregion
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { ACCENT_TOKENS, PALETTE_TOKENS } from "../types/theme.mjs";
|
|
2
|
+
import "./derive.mjs";
|
|
3
|
+
import { builtinTheme } from "./builtinThemes.mjs";
|
|
4
|
+
//#region src/theme/nativeTheme.ts
|
|
5
|
+
/** The spacing unit, matching `--spacing: 0.25rem` at a 16px root. */
|
|
6
|
+
const NATIVE_SPACING_BASE = 4;
|
|
7
|
+
/** Corner radii in px, matching `--radius-*`. */
|
|
8
|
+
const NATIVE_RADII = {
|
|
9
|
+
xs: 2,
|
|
10
|
+
sm: 3,
|
|
11
|
+
md: 5,
|
|
12
|
+
lg: 10
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Every radius a native style may name: the retunable `--radius-*` steps, plus
|
|
16
|
+
* Tailwind's hardcoded bare `rounded` (4px) and `rounded-full`.
|
|
17
|
+
*/
|
|
18
|
+
const NATIVE_BORDER_RADIUS = {
|
|
19
|
+
...NATIVE_RADII,
|
|
20
|
+
DEFAULT: 4,
|
|
21
|
+
full: 9999
|
|
22
|
+
};
|
|
23
|
+
/** The type ramp, matching `--text-*` (px) and `--text-*--line-height` (a multiple of the size). */
|
|
24
|
+
const NATIVE_TYPE_RAMP = {
|
|
25
|
+
"2xs": {
|
|
26
|
+
size: 9,
|
|
27
|
+
leading: 1.45
|
|
28
|
+
},
|
|
29
|
+
xs: {
|
|
30
|
+
size: 10,
|
|
31
|
+
leading: 1.45
|
|
32
|
+
},
|
|
33
|
+
sm: {
|
|
34
|
+
size: 12,
|
|
35
|
+
leading: 1.45
|
|
36
|
+
},
|
|
37
|
+
base: {
|
|
38
|
+
size: 13,
|
|
39
|
+
leading: 1.5
|
|
40
|
+
},
|
|
41
|
+
lg: {
|
|
42
|
+
size: 15,
|
|
43
|
+
leading: 1.4
|
|
44
|
+
},
|
|
45
|
+
xl: {
|
|
46
|
+
size: 18,
|
|
47
|
+
leading: 1.3
|
|
48
|
+
},
|
|
49
|
+
"2xl": {
|
|
50
|
+
size: 22,
|
|
51
|
+
leading: 1.25
|
|
52
|
+
},
|
|
53
|
+
"3xl": {
|
|
54
|
+
size: 28,
|
|
55
|
+
leading: 1.2
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/** Title tracking in em, matching `--text-*--letter-spacing`; steps not listed track at 0. */
|
|
59
|
+
const NATIVE_LETTER_SPACING_EM = {
|
|
60
|
+
xl: -.01,
|
|
61
|
+
"2xl": -.01,
|
|
62
|
+
"3xl": -.01
|
|
63
|
+
};
|
|
64
|
+
/** Tailwind's `leading-relaxed` multiple, which theme.css does not override. */
|
|
65
|
+
const NATIVE_LEADING_RELAXED = 1.625;
|
|
66
|
+
/** Font weights as React Native takes them, matching `font-normal`, `font-medium` and `font-semibold`. */
|
|
67
|
+
const NATIVE_FONT_WEIGHT = {
|
|
68
|
+
regular: "400",
|
|
69
|
+
medium: "500",
|
|
70
|
+
semibold: "600"
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Durations in ms and the shared easing curve of `--animate-po-*`. `slide`
|
|
74
|
+
* covers both slide directions; `easing` is the `cubic-bezier()` control points
|
|
75
|
+
* for Reanimated's `Easing.bezier`.
|
|
76
|
+
*/
|
|
77
|
+
const NATIVE_MOTION = {
|
|
78
|
+
fade: 140,
|
|
79
|
+
pop: 160,
|
|
80
|
+
rise: 160,
|
|
81
|
+
slide: 180,
|
|
82
|
+
easing: [
|
|
83
|
+
.16,
|
|
84
|
+
1,
|
|
85
|
+
.3,
|
|
86
|
+
1
|
|
87
|
+
]
|
|
88
|
+
};
|
|
89
|
+
/** The sans and mono families of `--font-sans` and `--po-font-mono`, with system fallbacks. */
|
|
90
|
+
const NATIVE_FONT_FAMILY = {
|
|
91
|
+
sans: {
|
|
92
|
+
family: "IBM Plex Sans",
|
|
93
|
+
ios: "System",
|
|
94
|
+
android: "sans-serif"
|
|
95
|
+
},
|
|
96
|
+
mono: {
|
|
97
|
+
family: "JetBrains Mono",
|
|
98
|
+
ios: "Menlo",
|
|
99
|
+
android: "monospace"
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/** The foreground `on-selected` mixes toward, as `white` in derive.ts. */
|
|
103
|
+
const ON_SELECTED_BASE = "#ffffff";
|
|
104
|
+
const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
105
|
+
const PERCENT = 100;
|
|
106
|
+
const CHANNEL_MAX = 255;
|
|
107
|
+
/** Rounds px values to hundredths so `18 * -0.01` reads -0.18, not a float tail. */
|
|
108
|
+
const PX_PRECISION = 100;
|
|
109
|
+
function hexChannels(hex) {
|
|
110
|
+
if (!HEX_COLOR.test(hex)) throw new Error(`Native themes need hex colours; got "${hex}".`);
|
|
111
|
+
const digits = hex.length === 4 ? hex.slice(1).split("").map((digit) => digit + digit).join("") : hex.slice(1);
|
|
112
|
+
return [
|
|
113
|
+
0,
|
|
114
|
+
2,
|
|
115
|
+
4
|
|
116
|
+
].map((offset) => Number.parseInt(digits.slice(offset, offset + 2), 16));
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* `color-mix(in srgb, first percent%, second)` for hex inputs: a straight
|
|
120
|
+
* per-channel blend, rounded to the nearest 8-bit value.
|
|
121
|
+
*/
|
|
122
|
+
function mixHex(first, percent, second) {
|
|
123
|
+
const weight = percent / PERCENT;
|
|
124
|
+
const a = hexChannels(first);
|
|
125
|
+
const b = hexChannels(second);
|
|
126
|
+
return `#${a.map((channel, index) => {
|
|
127
|
+
const mixed = Math.round(channel * weight + b[index] * (1 - weight));
|
|
128
|
+
return Math.min(CHANNEL_MAX, Math.max(0, mixed)).toString(16).padStart(2, "0");
|
|
129
|
+
}).join("")}`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A hex colour at `percent` opacity as `#rrggbbaa`: the native form of
|
|
133
|
+
* Tailwind's `/40`-style opacity modifiers.
|
|
134
|
+
*/
|
|
135
|
+
function withAlpha(hex, percent) {
|
|
136
|
+
const alpha = Math.min(CHANNEL_MAX, Math.max(0, Math.round(percent / PERCENT * CHANNEL_MAX)));
|
|
137
|
+
return `#${[...hexChannels(hex), alpha].map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
|
|
138
|
+
}
|
|
139
|
+
function rampValues(pick) {
|
|
140
|
+
const values = {};
|
|
141
|
+
for (const step of Object.keys(NATIVE_TYPE_RAMP)) values[step] = pick(step);
|
|
142
|
+
return values;
|
|
143
|
+
}
|
|
144
|
+
/** Resolves a web theme config to native values; tokens the theme pins win over the derived mix. */
|
|
145
|
+
function nativeTheme(config) {
|
|
146
|
+
const pinned = config.tokens;
|
|
147
|
+
const colors = {};
|
|
148
|
+
for (const token of PALETTE_TOKENS) colors[token] = config.tokens[token];
|
|
149
|
+
const resolve = (token, fallback) => {
|
|
150
|
+
colors[token] = pinned[token] ?? fallback();
|
|
151
|
+
};
|
|
152
|
+
for (const accent of ACCENT_TOKENS) resolve(`tint-${accent}`, () => mixHex(colors[accent], 16, colors.panel));
|
|
153
|
+
for (const accent of ACCENT_TOKENS) resolve(`edge-${accent}`, () => mixHex(colors[accent], 40, colors.border));
|
|
154
|
+
resolve("on-selected", () => mixHex(colors.selected, 12, ON_SELECTED_BASE));
|
|
155
|
+
return {
|
|
156
|
+
scheme: config.scheme,
|
|
157
|
+
colors,
|
|
158
|
+
spacing: (steps) => steps * 4,
|
|
159
|
+
borderRadius: NATIVE_BORDER_RADIUS,
|
|
160
|
+
fontSize: rampValues((step) => NATIVE_TYPE_RAMP[step].size),
|
|
161
|
+
lineHeight: rampValues((step) => Math.round(NATIVE_TYPE_RAMP[step].size * NATIVE_TYPE_RAMP[step].leading)),
|
|
162
|
+
lineHeightRelaxed: rampValues((step) => Math.round(NATIVE_TYPE_RAMP[step].size * NATIVE_LEADING_RELAXED)),
|
|
163
|
+
letterSpacing: rampValues((step) => {
|
|
164
|
+
const em = NATIVE_LETTER_SPACING_EM[step] ?? 0;
|
|
165
|
+
return Math.round(NATIVE_TYPE_RAMP[step].size * em * PX_PRECISION) / PX_PRECISION;
|
|
166
|
+
}),
|
|
167
|
+
fontWeight: NATIVE_FONT_WEIGHT,
|
|
168
|
+
fontFamily: NATIVE_FONT_FAMILY,
|
|
169
|
+
motion: NATIVE_MOTION
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function shippedNative(name) {
|
|
173
|
+
const theme = builtinTheme(name);
|
|
174
|
+
if (theme === void 0) throw new Error(`The shipped theme ${name} is missing.`);
|
|
175
|
+
return nativeTheme(theme);
|
|
176
|
+
}
|
|
177
|
+
/** The native themes by the names the native shell registers: `dark` and `light`. */
|
|
178
|
+
const NATIVE_THEMES = {
|
|
179
|
+
dark: shippedNative("po-nord-dark"),
|
|
180
|
+
light: shippedNative("po-one-light")
|
|
181
|
+
};
|
|
182
|
+
//#endregion
|
|
183
|
+
export { NATIVE_BORDER_RADIUS, NATIVE_FONT_FAMILY, NATIVE_FONT_WEIGHT, NATIVE_LEADING_RELAXED, NATIVE_LETTER_SPACING_EM, NATIVE_MOTION, NATIVE_RADII, NATIVE_SPACING_BASE, NATIVE_THEMES, NATIVE_TYPE_RAMP, mixHex, nativeTheme, withAlpha };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const require_theme = require("../types/theme.cjs");
|
|
2
|
+
//#region src/theme/parse.ts
|
|
3
|
+
const THEME_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
4
|
+
/** A CSS colour as a theme may spell it: hex, a colour function, or a named colour; never a url or an image. */
|
|
5
|
+
const CSS_COLOR = /^(?:#[0-9a-f]{3,8}|(?:rgb|rgba|hsl|hsla|hwb|lab|lch|oklab|oklch|color|color-mix|light-dark)\((?:[^()]|\([^()]*\))*\)|[a-z]+)$/i;
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Validates a theme config read from JSON. Returns null rather than throwing
|
|
11
|
+
* because a bad theme file must never take the cockpit down; the caller falls
|
|
12
|
+
* back to the shipped default and says so.
|
|
13
|
+
*/
|
|
14
|
+
function parseThemeConfig(input) {
|
|
15
|
+
if (!isRecord(input)) return null;
|
|
16
|
+
const { name, label, scheme, tokens } = input;
|
|
17
|
+
if (typeof name !== "string" || !THEME_NAME.test(name)) return null;
|
|
18
|
+
if (typeof label !== "string" || label.trim() === "") return null;
|
|
19
|
+
if (scheme !== "dark" && scheme !== "light") return null;
|
|
20
|
+
if (!isRecord(tokens)) return null;
|
|
21
|
+
const accepted = {};
|
|
22
|
+
for (const token of require_theme.PALETTE_TOKENS) {
|
|
23
|
+
const value = tokens[token];
|
|
24
|
+
if (typeof value !== "string" || !CSS_COLOR.test(value.trim())) return null;
|
|
25
|
+
accepted[token] = value.trim();
|
|
26
|
+
}
|
|
27
|
+
for (const token of require_theme.DERIVED_TOKENS) {
|
|
28
|
+
const value = tokens[token];
|
|
29
|
+
if (value === void 0) continue;
|
|
30
|
+
if (typeof value !== "string" || value.trim() === "") return null;
|
|
31
|
+
accepted[token] = value.trim();
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
name,
|
|
35
|
+
label,
|
|
36
|
+
scheme,
|
|
37
|
+
tokens: accepted
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
exports.parseThemeConfig = parseThemeConfig;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ThemeConfig } from "../types/theme.mjs";
|
|
2
|
+
//#region src/theme/parse.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Validates a theme config read from JSON. Returns null rather than throwing
|
|
5
|
+
* because a bad theme file must never take the cockpit down; the caller falls
|
|
6
|
+
* back to the shipped default and says so.
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseThemeConfig(input: unknown): ThemeConfig | null;
|
|
9
|
+
//#endregion
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DERIVED_TOKENS, PALETTE_TOKENS } from "../types/theme.mjs";
|
|
2
|
+
//#region src/theme/parse.ts
|
|
3
|
+
const THEME_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
4
|
+
/** A CSS colour as a theme may spell it: hex, a colour function, or a named colour; never a url or an image. */
|
|
5
|
+
const CSS_COLOR = /^(?:#[0-9a-f]{3,8}|(?:rgb|rgba|hsl|hsla|hwb|lab|lch|oklab|oklch|color|color-mix|light-dark)\((?:[^()]|\([^()]*\))*\)|[a-z]+)$/i;
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Validates a theme config read from JSON. Returns null rather than throwing
|
|
11
|
+
* because a bad theme file must never take the cockpit down; the caller falls
|
|
12
|
+
* back to the shipped default and says so.
|
|
13
|
+
*/
|
|
14
|
+
function parseThemeConfig(input) {
|
|
15
|
+
if (!isRecord(input)) return null;
|
|
16
|
+
const { name, label, scheme, tokens } = input;
|
|
17
|
+
if (typeof name !== "string" || !THEME_NAME.test(name)) return null;
|
|
18
|
+
if (typeof label !== "string" || label.trim() === "") return null;
|
|
19
|
+
if (scheme !== "dark" && scheme !== "light") return null;
|
|
20
|
+
if (!isRecord(tokens)) return null;
|
|
21
|
+
const accepted = {};
|
|
22
|
+
for (const token of PALETTE_TOKENS) {
|
|
23
|
+
const value = tokens[token];
|
|
24
|
+
if (typeof value !== "string" || !CSS_COLOR.test(value.trim())) return null;
|
|
25
|
+
accepted[token] = value.trim();
|
|
26
|
+
}
|
|
27
|
+
for (const token of DERIVED_TOKENS) {
|
|
28
|
+
const value = tokens[token];
|
|
29
|
+
if (value === void 0) continue;
|
|
30
|
+
if (typeof value !== "string" || value.trim() === "") return null;
|
|
31
|
+
accepted[token] = value.trim();
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
name,
|
|
35
|
+
label,
|
|
36
|
+
scheme,
|
|
37
|
+
tokens: accepted
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { parseThemeConfig };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const require_parse = require("./parse.cjs");
|
|
2
|
+
//#region src/theme/piTheme.ts
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
function hexLuminance(hex) {
|
|
7
|
+
const match = /^#([0-9a-f]{6})$/i.exec(hex.trim());
|
|
8
|
+
if (!match) return null;
|
|
9
|
+
const value = Number.parseInt(match[1], 16);
|
|
10
|
+
const r = value >> 16 & 255;
|
|
11
|
+
const g = value >> 8 & 255;
|
|
12
|
+
const b = value & 255;
|
|
13
|
+
return (.2126 * r + .7152 * g + .0722 * b) / 255;
|
|
14
|
+
}
|
|
15
|
+
/** Resolves a Pi colour reference: a var name, or a literal colour. */
|
|
16
|
+
function resolve(vars, reference) {
|
|
17
|
+
if (reference === void 0) return void 0;
|
|
18
|
+
return vars[reference] ?? reference;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds a web theme from a Pi TUI theme, so the palette Po ships for
|
|
22
|
+
* the terminal (po-ui/themes/po-dark.json) drives the browser too.
|
|
23
|
+
* The mapping is by role, not by name: Pi has no cockpit "deep" surface or
|
|
24
|
+
* "border-soft" line, so those are mixed from the nearest vars. Returns null
|
|
25
|
+
* when the input is not a Pi theme with a complete palette.
|
|
26
|
+
*/
|
|
27
|
+
function themeFromPiTheme(input, options = {}) {
|
|
28
|
+
if (!isRecord(input)) return null;
|
|
29
|
+
const { name, vars, colors } = input;
|
|
30
|
+
if (!isRecord(vars)) return null;
|
|
31
|
+
const palette = {};
|
|
32
|
+
for (const [key, value] of Object.entries(vars)) if (typeof value === "string") palette[key] = value;
|
|
33
|
+
const semantic = {};
|
|
34
|
+
if (isRecord(colors)) {
|
|
35
|
+
for (const [key, value] of Object.entries(colors)) if (typeof value === "string") semantic[key] = value;
|
|
36
|
+
}
|
|
37
|
+
const bg = palette.bg;
|
|
38
|
+
const bgAlt = palette.bgAlt ?? bg;
|
|
39
|
+
const fg = palette.fg;
|
|
40
|
+
if (bg === void 0 || fg === void 0) return null;
|
|
41
|
+
const luminance = hexLuminance(bg);
|
|
42
|
+
const scheme = options.scheme ?? (luminance !== null && luminance > .5 ? "light" : "dark");
|
|
43
|
+
const deep = `color-mix(in srgb, ${bgAlt} 84%, ${scheme === "dark" ? "black" : "white"})`;
|
|
44
|
+
const border = resolve(palette, semantic.borderMuted) ?? palette.grey ?? `color-mix(in srgb, ${fg} 25%, ${bg})`;
|
|
45
|
+
const themeName = options.name ?? (typeof name === "string" ? name : "pi-theme");
|
|
46
|
+
return require_parse.parseThemeConfig({
|
|
47
|
+
name: themeName,
|
|
48
|
+
label: options.label ?? themeName,
|
|
49
|
+
scheme,
|
|
50
|
+
tokens: {
|
|
51
|
+
bg,
|
|
52
|
+
rail: bgAlt,
|
|
53
|
+
panel: `color-mix(in srgb, ${bgAlt} 70%, ${bg})`,
|
|
54
|
+
deep,
|
|
55
|
+
border,
|
|
56
|
+
"border-soft": `color-mix(in srgb, ${border} 55%, ${bg})`,
|
|
57
|
+
hi: `color-mix(in srgb, ${fg} 65%, ${scheme === "dark" ? "white" : "black"})`,
|
|
58
|
+
text: fg,
|
|
59
|
+
dim: resolve(palette, semantic.muted) ?? palette.fgAlt ?? fg,
|
|
60
|
+
faint: resolve(palette, semantic.dim) ?? palette.comment ?? palette.greyAlt ?? fg,
|
|
61
|
+
blue: palette.blue ?? resolve(palette, semantic.accent) ?? fg,
|
|
62
|
+
green: palette.green ?? resolve(palette, semantic.success) ?? fg,
|
|
63
|
+
yellow: palette.yellow ?? resolve(palette, semantic.warning) ?? fg,
|
|
64
|
+
red: palette.red ?? resolve(palette, semantic.error) ?? fg,
|
|
65
|
+
magenta: palette.magenta ?? palette.violet ?? fg,
|
|
66
|
+
violet: palette.violet ?? palette.magenta ?? fg,
|
|
67
|
+
cyan: palette.cyan ?? palette.blue ?? fg,
|
|
68
|
+
orange: palette.orange ?? palette.yellow ?? fg,
|
|
69
|
+
teal: palette.teal ?? palette.cyan ?? fg,
|
|
70
|
+
selected: palette.darkBlue ?? palette.blue ?? fg
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
exports.themeFromPiTheme = themeFromPiTheme;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ThemeConfig, ThemeScheme } from "../types/theme.mjs";
|
|
2
|
+
//#region src/theme/piTheme.d.ts
|
|
3
|
+
export interface PiThemeBridgeOptions {
|
|
4
|
+
/** The web theme's id; defaults to the Pi theme's name. */
|
|
5
|
+
name?: string;
|
|
6
|
+
label?: string;
|
|
7
|
+
/** Forced scheme; otherwise inferred from the background luminance. */
|
|
8
|
+
scheme?: ThemeScheme;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Builds a web theme from a Pi TUI theme, so the palette Po ships for
|
|
12
|
+
* the terminal (po-ui/themes/po-dark.json) drives the browser too.
|
|
13
|
+
* The mapping is by role, not by name: Pi has no cockpit "deep" surface or
|
|
14
|
+
* "border-soft" line, so those are mixed from the nearest vars. Returns null
|
|
15
|
+
* when the input is not a Pi theme with a complete palette.
|
|
16
|
+
*/
|
|
17
|
+
export declare function themeFromPiTheme(input: unknown, options?: PiThemeBridgeOptions): ThemeConfig | null;
|
|
18
|
+
//#endregion
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { parseThemeConfig } from "./parse.mjs";
|
|
2
|
+
//#region src/theme/piTheme.ts
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
function hexLuminance(hex) {
|
|
7
|
+
const match = /^#([0-9a-f]{6})$/i.exec(hex.trim());
|
|
8
|
+
if (!match) return null;
|
|
9
|
+
const value = Number.parseInt(match[1], 16);
|
|
10
|
+
const r = value >> 16 & 255;
|
|
11
|
+
const g = value >> 8 & 255;
|
|
12
|
+
const b = value & 255;
|
|
13
|
+
return (.2126 * r + .7152 * g + .0722 * b) / 255;
|
|
14
|
+
}
|
|
15
|
+
/** Resolves a Pi colour reference: a var name, or a literal colour. */
|
|
16
|
+
function resolve(vars, reference) {
|
|
17
|
+
if (reference === void 0) return void 0;
|
|
18
|
+
return vars[reference] ?? reference;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds a web theme from a Pi TUI theme, so the palette Po ships for
|
|
22
|
+
* the terminal (po-ui/themes/po-dark.json) drives the browser too.
|
|
23
|
+
* The mapping is by role, not by name: Pi has no cockpit "deep" surface or
|
|
24
|
+
* "border-soft" line, so those are mixed from the nearest vars. Returns null
|
|
25
|
+
* when the input is not a Pi theme with a complete palette.
|
|
26
|
+
*/
|
|
27
|
+
function themeFromPiTheme(input, options = {}) {
|
|
28
|
+
if (!isRecord(input)) return null;
|
|
29
|
+
const { name, vars, colors } = input;
|
|
30
|
+
if (!isRecord(vars)) return null;
|
|
31
|
+
const palette = {};
|
|
32
|
+
for (const [key, value] of Object.entries(vars)) if (typeof value === "string") palette[key] = value;
|
|
33
|
+
const semantic = {};
|
|
34
|
+
if (isRecord(colors)) {
|
|
35
|
+
for (const [key, value] of Object.entries(colors)) if (typeof value === "string") semantic[key] = value;
|
|
36
|
+
}
|
|
37
|
+
const bg = palette.bg;
|
|
38
|
+
const bgAlt = palette.bgAlt ?? bg;
|
|
39
|
+
const fg = palette.fg;
|
|
40
|
+
if (bg === void 0 || fg === void 0) return null;
|
|
41
|
+
const luminance = hexLuminance(bg);
|
|
42
|
+
const scheme = options.scheme ?? (luminance !== null && luminance > .5 ? "light" : "dark");
|
|
43
|
+
const deep = `color-mix(in srgb, ${bgAlt} 84%, ${scheme === "dark" ? "black" : "white"})`;
|
|
44
|
+
const border = resolve(palette, semantic.borderMuted) ?? palette.grey ?? `color-mix(in srgb, ${fg} 25%, ${bg})`;
|
|
45
|
+
const themeName = options.name ?? (typeof name === "string" ? name : "pi-theme");
|
|
46
|
+
return parseThemeConfig({
|
|
47
|
+
name: themeName,
|
|
48
|
+
label: options.label ?? themeName,
|
|
49
|
+
scheme,
|
|
50
|
+
tokens: {
|
|
51
|
+
bg,
|
|
52
|
+
rail: bgAlt,
|
|
53
|
+
panel: `color-mix(in srgb, ${bgAlt} 70%, ${bg})`,
|
|
54
|
+
deep,
|
|
55
|
+
border,
|
|
56
|
+
"border-soft": `color-mix(in srgb, ${border} 55%, ${bg})`,
|
|
57
|
+
hi: `color-mix(in srgb, ${fg} 65%, ${scheme === "dark" ? "white" : "black"})`,
|
|
58
|
+
text: fg,
|
|
59
|
+
dim: resolve(palette, semantic.muted) ?? palette.fgAlt ?? fg,
|
|
60
|
+
faint: resolve(palette, semantic.dim) ?? palette.comment ?? palette.greyAlt ?? fg,
|
|
61
|
+
blue: palette.blue ?? resolve(palette, semantic.accent) ?? fg,
|
|
62
|
+
green: palette.green ?? resolve(palette, semantic.success) ?? fg,
|
|
63
|
+
yellow: palette.yellow ?? resolve(palette, semantic.warning) ?? fg,
|
|
64
|
+
red: palette.red ?? resolve(palette, semantic.error) ?? fg,
|
|
65
|
+
magenta: palette.magenta ?? palette.violet ?? fg,
|
|
66
|
+
violet: palette.violet ?? palette.magenta ?? fg,
|
|
67
|
+
cyan: palette.cyan ?? palette.blue ?? fg,
|
|
68
|
+
orange: palette.orange ?? palette.yellow ?? fg,
|
|
69
|
+
teal: palette.teal ?? palette.cyan ?? fg,
|
|
70
|
+
selected: palette.darkBlue ?? palette.blue ?? fg
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { themeFromPiTheme };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/theme/preference.ts
|
|
2
|
+
/** Where the page remembers the chosen theme; one key, machine-local. */
|
|
3
|
+
const THEME_STORAGE_KEY = "po.web.theme";
|
|
4
|
+
/** The stored theme name, or null when nothing was chosen or storage is unavailable. */
|
|
5
|
+
function readThemePreference(storage) {
|
|
6
|
+
try {
|
|
7
|
+
const value = storage?.getItem("po.web.theme") ?? null;
|
|
8
|
+
return value === null || value === "" ? null : value;
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Remembers a theme name; null forgets it. Storage failures are swallowed: a theme is a convenience. */
|
|
14
|
+
function writeThemePreference(storage, name) {
|
|
15
|
+
try {
|
|
16
|
+
if (name === null) storage?.removeItem(THEME_STORAGE_KEY);
|
|
17
|
+
else storage?.setItem(THEME_STORAGE_KEY, name);
|
|
18
|
+
} catch {}
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
exports.THEME_STORAGE_KEY = THEME_STORAGE_KEY;
|
|
22
|
+
exports.readThemePreference = readThemePreference;
|
|
23
|
+
exports.writeThemePreference = writeThemePreference;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/theme/preference.d.ts
|
|
2
|
+
/** Where the page remembers the chosen theme; one key, machine-local. */
|
|
3
|
+
export declare const THEME_STORAGE_KEY = "po.web.theme";
|
|
4
|
+
/** The two Storage calls the preference needs, so tests pass a plain map. */
|
|
5
|
+
export interface PreferenceStorage {
|
|
6
|
+
getItem(key: string): string | null;
|
|
7
|
+
setItem(key: string, value: string): void;
|
|
8
|
+
removeItem(key: string): void;
|
|
9
|
+
}
|
|
10
|
+
/** The stored theme name, or null when nothing was chosen or storage is unavailable. */
|
|
11
|
+
export declare function readThemePreference(storage: PreferenceStorage | undefined): string | null;
|
|
12
|
+
/** Remembers a theme name; null forgets it. Storage failures are swallowed: a theme is a convenience. */
|
|
13
|
+
export declare function writeThemePreference(storage: PreferenceStorage | undefined, name: string | null): void;
|
|
14
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/theme/preference.ts
|
|
2
|
+
/** Where the page remembers the chosen theme; one key, machine-local. */
|
|
3
|
+
const THEME_STORAGE_KEY = "po.web.theme";
|
|
4
|
+
/** The stored theme name, or null when nothing was chosen or storage is unavailable. */
|
|
5
|
+
function readThemePreference(storage) {
|
|
6
|
+
try {
|
|
7
|
+
const value = storage?.getItem("po.web.theme") ?? null;
|
|
8
|
+
return value === null || value === "" ? null : value;
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** Remembers a theme name; null forgets it. Storage failures are swallowed: a theme is a convenience. */
|
|
14
|
+
function writeThemePreference(storage, name) {
|
|
15
|
+
try {
|
|
16
|
+
if (name === null) storage?.removeItem(THEME_STORAGE_KEY);
|
|
17
|
+
else storage?.setItem(THEME_STORAGE_KEY, name);
|
|
18
|
+
} catch {}
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { THEME_STORAGE_KEY, readThemePreference, writeThemePreference };
|