@haklex/rich-style-token 0.0.11 → 0.0.13

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/README.md CHANGED
@@ -21,12 +21,60 @@ export const container = style({
21
21
  })
22
22
  ```
23
23
 
24
+ ## 运行时颜色覆写
25
+
26
+ 主题契约生成标准 CSS 自定义属性(`--rc-*`),使用方可在运行时覆写。
27
+
28
+ ### 方式一:`createThemeStyle`(类型安全)
29
+
30
+ ```tsx
31
+ import { createThemeStyle } from '@haklex/rich-style-token'
32
+
33
+ const style = createThemeStyle({
34
+ color: {
35
+ accent: '#0ea5e9',
36
+ text: '#0c4a6e',
37
+ bg: '#f0f9ff',
38
+ },
39
+ })
40
+
41
+ // CSS 变量向下级联,所有子组件自动生效
42
+ <div style={style}>
43
+ <RichEditor />
44
+ </div>
45
+ ```
46
+
47
+ `ThemeTokens` 类型自动从 `vars` 契约推导,支持 `color`、`spacing`、`typography`、`borderRadius` 四组的任意子集覆写。
48
+
49
+ ### 方式二:纯 CSS
50
+
51
+ ```css
52
+ .my-theme {
53
+ --rc-accent: #0ea5e9;
54
+ --rc-text: #0c4a6e;
55
+ --rc-bg: #f0f9ff;
56
+ }
57
+ ```
58
+
59
+ ### 可覆写的 CSS 变量
60
+
61
+ | 分组 | 变量 |
62
+ |------|------|
63
+ | **Color** | `--rc-text`, `--rc-text-secondary`, `--rc-bg`, `--rc-bg-secondary`, `--rc-border`, `--rc-accent`, `--rc-accent-light`, `--rc-link`, `--rc-code-text`, `--rc-code-bg`, `--rc-quote-border`, `--rc-quote-bg`, `--rc-alert-info`, `--rc-alert-warning`, `--rc-alert-tip`, `--rc-alert-caution`, `--rc-alert-important` |
64
+ | **Spacing** | `--rc-space-xs`, `--rc-space-sm`, `--rc-space-md`, `--rc-space-lg`, `--rc-space-xl` |
65
+ | **Typography** | `--rc-font-family`, `--rc-font-mono`, `--rc-font-size-base`, `--rc-font-size-small`, `--rc-font-size-large`, `--rc-line-height`, `--rc-line-height-tight` |
66
+ | **Border Radius** | `--rc-radius-sm`, `--rc-radius-md`, `--rc-radius-lg` |
67
+
24
68
  ## 导出
25
69
 
26
70
  ```ts
27
71
  // CSS 变量契约
28
72
  export { vars } from './vars.css'
29
73
 
74
+ // 运行时覆写工具
75
+ export { createThemeStyle } from './create-theme-style'
76
+ export type { ThemeTokens } from './create-theme-style'
77
+
30
78
  // 主题配置
31
79
  export { articleLayout, commentLayout, noteLayout } from './themes'
32
80
  export { serifFonts, sharedFonts } from './themes'
@@ -93,10 +141,10 @@ vars.borderRadius.lg // article: 12px, comment: 8px
93
141
  ## 预设主题
94
142
 
95
143
  ```ts
96
- import {
97
- lightArticleColors,
98
- darkColors,
99
- articleLayout
144
+ import {
145
+ lightArticleColors,
146
+ darkColors,
147
+ articleLayout
100
148
  } from '@haklex/rich-style-token'
101
149
 
102
150
  // 浅色文章主题
@@ -0,0 +1,9 @@
1
+ import { CSSProperties } from 'react';
2
+ import { vars } from './vars.css';
3
+ type VarsContract = typeof vars;
4
+ export type ThemeTokens = {
5
+ [K in keyof VarsContract]?: Partial<Record<keyof VarsContract[K], string>>;
6
+ };
7
+ export declare function createThemeStyle(tokens: ThemeTokens): CSSProperties;
8
+ export {};
9
+ //# sourceMappingURL=create-theme-style.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-theme-style.d.ts","sourceRoot":"","sources":["../src/create-theme-style.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAEjC,KAAK,YAAY,GAAG,OAAO,IAAI,CAAA;AAE/B,MAAM,MAAM,WAAW,GAAG;KACvB,CAAC,IAAI,MAAM,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,MAAM,YAAY,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;CAC3E,CAAA;AAOD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,GAAG,aAAa,CAoBnE"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export type { ThemeTokens } from './create-theme-style';
2
+ export { createThemeStyle } from './create-theme-style';
1
3
  export { PortalThemeProvider, PortalThemeWrapper, usePortalTheme, } from './portal-theme';
2
4
  export { articleLayout, commentLayout, darkColors, lightArticleColors, lightCommentColors, noteLayout, serifFonts, sharedFonts, } from './themes';
3
5
  export { vars } from './vars.css';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,cAAc,GACf,MAAM,gBAAgB,CAAA;AACvB,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,kBAAkB,EAClB,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,WAAW,GACZ,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,cAAc,GACf,MAAM,gBAAgB,CAAA;AACvB,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,kBAAkB,EAClB,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,WAAW,GACZ,MAAM,UAAU,CAAA;AACjB,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA"}
package/dist/index.mjs CHANGED
@@ -1,5 +1,26 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { createContext, useMemo, use } from "react";
3
+ var vars = { color: { text: "var(--rc-text)", textSecondary: "var(--rc-text-secondary)", bg: "var(--rc-bg)", bgSecondary: "var(--rc-bg-secondary)", border: "var(--rc-border)", accent: "var(--rc-accent)", accentLight: "var(--rc-accent-light)", link: "var(--rc-link)", codeText: "var(--rc-code-text)", codeBg: "var(--rc-code-bg)", quoteBorder: "var(--rc-quote-border)", quoteBg: "var(--rc-quote-bg)", alertInfo: "var(--rc-alert-info)", alertWarning: "var(--rc-alert-warning)", alertTip: "var(--rc-alert-tip)", alertCaution: "var(--rc-alert-caution)", alertImportant: "var(--rc-alert-important)" }, spacing: { xs: "var(--rc-space-xs)", sm: "var(--rc-space-sm)", md: "var(--rc-space-md)", lg: "var(--rc-space-lg)", xl: "var(--rc-space-xl)" }, typography: { fontFamily: "var(--rc-font-family)", fontMono: "var(--rc-font-mono)", fontSizeBase: "var(--rc-font-size-base)", fontSizeSmall: "var(--rc-font-size-small)", fontSizeLarge: "var(--rc-font-size-large)", lineHeight: "var(--rc-line-height)", lineHeightTight: "var(--rc-line-height-tight)" }, borderRadius: { sm: "var(--rc-radius-sm)", md: "var(--rc-radius-md)", lg: "var(--rc-radius-lg)" } };
4
+ function extractVarName(cssVarRef) {
5
+ const match = cssVarRef.match(/^var\((.+)\)$/);
6
+ return match ? match[1] : cssVarRef;
7
+ }
8
+ function createThemeStyle(tokens) {
9
+ const style = {};
10
+ for (const [group, values] of Object.entries(tokens)) {
11
+ if (!values) continue;
12
+ const contractGroup = vars[group];
13
+ if (!contractGroup) continue;
14
+ for (const [key, value] of Object.entries(values)) {
15
+ if (value == null) continue;
16
+ const cssVarRef = contractGroup[key];
17
+ if (cssVarRef) {
18
+ style[extractVarName(cssVarRef)] = value;
19
+ }
20
+ }
21
+ }
22
+ return style;
23
+ }
3
24
  const PortalThemeContext = createContext({
4
25
  className: ""
5
26
  });
@@ -110,12 +131,12 @@ const commentLayout = {
110
131
  },
111
132
  borderRadius: { sm: "3px", md: "6px", lg: "8px" }
112
133
  };
113
- var vars = { color: { text: "var(--rc-text)", textSecondary: "var(--rc-text-secondary)", bg: "var(--rc-bg)", bgSecondary: "var(--rc-bg-secondary)", border: "var(--rc-border)", accent: "var(--rc-accent)", accentLight: "var(--rc-accent-light)", link: "var(--rc-link)", codeText: "var(--rc-code-text)", codeBg: "var(--rc-code-bg)", quoteBorder: "var(--rc-quote-border)", quoteBg: "var(--rc-quote-bg)", alertInfo: "var(--rc-alert-info)", alertWarning: "var(--rc-alert-warning)", alertTip: "var(--rc-alert-tip)", alertCaution: "var(--rc-alert-caution)", alertImportant: "var(--rc-alert-important)" }, spacing: { xs: "var(--rc-space-xs)", sm: "var(--rc-space-sm)", md: "var(--rc-space-md)", lg: "var(--rc-space-lg)", xl: "var(--rc-space-xl)" }, typography: { fontFamily: "var(--rc-font-family)", fontMono: "var(--rc-font-mono)", fontSizeBase: "var(--rc-font-size-base)", fontSizeSmall: "var(--rc-font-size-small)", fontSizeLarge: "var(--rc-font-size-large)", lineHeight: "var(--rc-line-height)", lineHeightTight: "var(--rc-line-height-tight)" }, borderRadius: { sm: "var(--rc-radius-sm)", md: "var(--rc-radius-md)", lg: "var(--rc-radius-lg)" } };
114
134
  export {
115
135
  PortalThemeProvider,
116
136
  PortalThemeWrapper,
117
137
  articleLayout,
118
138
  commentLayout,
139
+ createThemeStyle,
119
140
  darkColors,
120
141
  lightArticleColors,
121
142
  lightCommentColors,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-style-token",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "Style tokens and CSS variables for haklex rich editor",
5
5
  "license": "MIT",
6
6
  "type": "module",