@chaoswise/portal-theme 0.0.0-stage → 0.2.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 +243 -2
- package/lib/apply.js +122 -0
- package/lib/color.js +363 -0
- package/lib/defaults.js +100 -0
- package/lib/index.js +214 -0
- package/lib/resolve.js +151 -0
- package/lib/runtime.js +385 -0
- package/package.json +44 -4
- package/types/index.d.ts +179 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @chaoswise/portal-theme 类型声明
|
|
3
|
+
*
|
|
4
|
+
* 包产物是 babel 转出的 CJS,没有 TS 源码,因此这里手写声明。
|
|
5
|
+
* 改 src 的导出必须同步改这里(types 目录随 files 一起发布)。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type ThemeTarget = 'old' | 'new';
|
|
9
|
+
export type ThemeMode = 'light' | 'dark';
|
|
10
|
+
|
|
11
|
+
export interface PortalThemeSider {
|
|
12
|
+
background: string;
|
|
13
|
+
text: string;
|
|
14
|
+
hoverBackground: string;
|
|
15
|
+
selectedBackground: string;
|
|
16
|
+
/** 经典菜单为 null 表示跟随品牌主色 */
|
|
17
|
+
selectedText: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface PortalThemeOld {
|
|
21
|
+
headerBackground: string;
|
|
22
|
+
headerText: string;
|
|
23
|
+
sider: PortalThemeSider;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface PortalThemeNewMode {
|
|
27
|
+
shellBackground: string;
|
|
28
|
+
contentSurface: string;
|
|
29
|
+
headerText: string;
|
|
30
|
+
sider: PortalThemeSider;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface PortalThemeRaw {
|
|
34
|
+
version?: number;
|
|
35
|
+
shared?: { primaryColor?: string };
|
|
36
|
+
old?: Partial<Omit<PortalThemeOld, 'sider'>> & { sider?: Partial<PortalThemeSider> };
|
|
37
|
+
new?: {
|
|
38
|
+
light?: Partial<Omit<PortalThemeNewMode, 'sider'>> & { sider?: Partial<PortalThemeSider> };
|
|
39
|
+
dark?: Partial<Omit<PortalThemeNewMode, 'sider'>> & { sider?: Partial<PortalThemeSider> };
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface PortalThemeResolved {
|
|
44
|
+
version: number;
|
|
45
|
+
shared: { primaryColor: string };
|
|
46
|
+
old: PortalThemeOld;
|
|
47
|
+
new: { light: PortalThemeNewMode; dark: PortalThemeNewMode };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface PortalThemeActiveNew {
|
|
51
|
+
target: 'new';
|
|
52
|
+
mode: ThemeMode;
|
|
53
|
+
primaryColor: string;
|
|
54
|
+
shellBackground: string;
|
|
55
|
+
contentSurface: string;
|
|
56
|
+
headerText: string;
|
|
57
|
+
sider: PortalThemeSider;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface PortalThemeActiveOld {
|
|
61
|
+
target: 'old';
|
|
62
|
+
mode: ThemeMode;
|
|
63
|
+
primaryColor: string;
|
|
64
|
+
headerBackground: string;
|
|
65
|
+
headerText: string;
|
|
66
|
+
sider: PortalThemeSider;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type PortalThemeActive = PortalThemeActiveNew | PortalThemeActiveOld;
|
|
70
|
+
|
|
71
|
+
export interface PortalThemeSnapshot {
|
|
72
|
+
raw: PortalThemeRaw | null;
|
|
73
|
+
resolved: PortalThemeResolved;
|
|
74
|
+
active: PortalThemeActive | null;
|
|
75
|
+
fromCache: boolean;
|
|
76
|
+
loading: boolean;
|
|
77
|
+
error: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface InitPortalThemeOptions {
|
|
81
|
+
/** 必填:返回服务端原始覆盖值,未配置时返回 null */
|
|
82
|
+
getTheme: () => Promise<PortalThemeRaw | null | undefined> | PortalThemeRaw | null | undefined;
|
|
83
|
+
menuTheme?: ThemeTarget;
|
|
84
|
+
mode?: ThemeMode;
|
|
85
|
+
/** 缓存开启(默认)时必填:必须包含 accountId,避免多租户串色 */
|
|
86
|
+
cacheKey?: string;
|
|
87
|
+
cache?: boolean;
|
|
88
|
+
applyCssVars?: boolean;
|
|
89
|
+
onError?: (error: unknown) => void;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const PORTAL_THEME_VERSION: number;
|
|
93
|
+
export const DEFAULT_PRIMARY_COLOR: string;
|
|
94
|
+
export const DEFAULT_SHELL_BACKGROUND: Record<ThemeMode, string>;
|
|
95
|
+
export const DEFAULT_OLD_THEME: PortalThemeOld;
|
|
96
|
+
export const DEFAULT_NEW_THEME: Record<ThemeMode, PortalThemeNewMode>;
|
|
97
|
+
export const DEFAULT_PORTAL_THEME: PortalThemeResolved;
|
|
98
|
+
export const SIDER_FIELDS: ReadonlyArray<keyof PortalThemeSider>;
|
|
99
|
+
export const THEME_TARGET_OLD: 'old';
|
|
100
|
+
export const THEME_TARGET_NEW: 'new';
|
|
101
|
+
export const THEME_MODE_LIGHT: 'light';
|
|
102
|
+
export const THEME_MODE_DARK: 'dark';
|
|
103
|
+
export const MIN_TEXT_CONTRAST: number;
|
|
104
|
+
export const MIN_MUTED_TEXT_CONTRAST: number;
|
|
105
|
+
export const DEFAULT_THEME_CACHE_KEY: string;
|
|
106
|
+
|
|
107
|
+
export const PORTAL_THEME_CSS_VARS: {
|
|
108
|
+
primary: string;
|
|
109
|
+
shellBackground: string;
|
|
110
|
+
contentSurface: string;
|
|
111
|
+
headerText: string;
|
|
112
|
+
siderBackground: string;
|
|
113
|
+
siderText: string;
|
|
114
|
+
siderHoverBackground: string;
|
|
115
|
+
siderSelectedBackground: string;
|
|
116
|
+
siderSelectedText: string;
|
|
117
|
+
siderMutedText: string;
|
|
118
|
+
siderSecondaryText: string;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export interface Rgba {
|
|
122
|
+
r: number;
|
|
123
|
+
g: number;
|
|
124
|
+
b: number;
|
|
125
|
+
a: number;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function parseColor(value: unknown): Rgba | null;
|
|
129
|
+
export function isValidColor(value: unknown): boolean;
|
|
130
|
+
export function compositeColor(
|
|
131
|
+
foreground: string | Rgba,
|
|
132
|
+
background: string | Rgba
|
|
133
|
+
): Rgba | null;
|
|
134
|
+
export function contrastRatio(foreground: string, background: string): number | null;
|
|
135
|
+
export function isContrastEnough(
|
|
136
|
+
textColor: string,
|
|
137
|
+
backgroundColor: string,
|
|
138
|
+
options?: { min?: number; fallbackBackground?: string }
|
|
139
|
+
): boolean;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 派生弱化文字色(次级账号 / 菜单分组标题):在正文色与底色之间求一个 alpha,
|
|
143
|
+
* 使合成后的对比度刚好达到 targetRatio。解析失败返回 null。
|
|
144
|
+
*
|
|
145
|
+
* 正文色本身达不到 targetRatio 时返回原文本色(不弱化)——此时继续压 alpha 只会更不可读,
|
|
146
|
+
* 这种配色应在配置页保存时被 isContrastEnough 拦住。
|
|
147
|
+
*/
|
|
148
|
+
export function deriveMutedText(
|
|
149
|
+
text: string,
|
|
150
|
+
background: string,
|
|
151
|
+
options?: { targetRatio?: number; maxAlpha?: number }
|
|
152
|
+
): string | null;
|
|
153
|
+
|
|
154
|
+
export function sanitizeColor(
|
|
155
|
+
value: unknown,
|
|
156
|
+
options?: { allowTransparent?: boolean }
|
|
157
|
+
): string | undefined;
|
|
158
|
+
export function resolvePortalTheme(raw?: PortalThemeRaw | null): PortalThemeResolved;
|
|
159
|
+
export function resolveActiveTheme(
|
|
160
|
+
resolved: PortalThemeResolved,
|
|
161
|
+
options?: { menuTheme?: ThemeTarget; mode?: ThemeMode }
|
|
162
|
+
): PortalThemeActive;
|
|
163
|
+
|
|
164
|
+
export function applyPortalThemeCssVars(activeTheme: PortalThemeActive | null): void;
|
|
165
|
+
export function removePortalThemeCssVars(): void;
|
|
166
|
+
|
|
167
|
+
export function initPortalTheme(options: InitPortalThemeOptions): Promise<PortalThemeSnapshot>;
|
|
168
|
+
export function refreshPortalTheme(): Promise<PortalThemeSnapshot>;
|
|
169
|
+
export function setPortalThemeContext(context?: {
|
|
170
|
+
menuTheme?: ThemeTarget;
|
|
171
|
+
mode?: ThemeMode;
|
|
172
|
+
}): PortalThemeSnapshot;
|
|
173
|
+
export function getPortalThemeSnapshot(): PortalThemeSnapshot;
|
|
174
|
+
export function getResolvedPortalTheme(): PortalThemeResolved;
|
|
175
|
+
export function subscribePortalTheme(
|
|
176
|
+
listener: (snapshot: PortalThemeSnapshot) => void
|
|
177
|
+
): () => void;
|
|
178
|
+
export function clearPortalThemeCache(cacheKey?: string, all?: boolean): void;
|
|
179
|
+
export function destroyPortalTheme(): void;
|