@qlover/create-app 0.6.2 → 0.7.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.
Files changed (81) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/templates/react-app/README.en.md +257 -0
  5. package/dist/templates/react-app/README.md +29 -231
  6. package/dist/templates/react-app/__tests__/__mocks__/I18nService.ts +13 -0
  7. package/dist/templates/react-app/__tests__/__mocks__/MockAppConfit.ts +48 -0
  8. package/dist/templates/react-app/__tests__/__mocks__/MockDialogHandler.ts +16 -0
  9. package/dist/templates/react-app/__tests__/__mocks__/MockLogger.ts +14 -0
  10. package/dist/templates/react-app/__tests__/__mocks__/createMockGlobals.ts +92 -0
  11. package/dist/templates/react-app/__tests__/setup/index.ts +51 -0
  12. package/dist/templates/react-app/__tests__/src/App.test.tsx +139 -0
  13. package/dist/templates/react-app/__tests__/src/base/cases/AppConfig.test.ts +288 -0
  14. package/dist/templates/react-app/__tests__/src/base/cases/AppError.test.ts +102 -0
  15. package/dist/templates/react-app/__tests__/src/base/cases/DialogHandler.test.ts +228 -0
  16. package/dist/templates/react-app/__tests__/src/base/cases/I18nKeyErrorPlugin.test.ts +207 -0
  17. package/dist/templates/react-app/__tests__/src/base/cases/InversifyContainer.test.ts +181 -0
  18. package/dist/templates/react-app/__tests__/src/base/cases/PublicAssetsPath.test.ts +61 -0
  19. package/dist/templates/react-app/__tests__/src/base/cases/RequestLogger.test.ts +199 -0
  20. package/dist/templates/react-app/__tests__/src/base/cases/RequestStatusCatcher.test.ts +192 -0
  21. package/dist/templates/react-app/__tests__/src/base/cases/RouterLoader.test.ts +235 -0
  22. package/dist/templates/react-app/__tests__/src/base/services/I18nService.test.ts +224 -0
  23. package/dist/templates/react-app/__tests__/src/core/IOC.test.ts +257 -0
  24. package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapsApp.test.ts +72 -0
  25. package/dist/templates/react-app/__tests__/src/main.integration.test.tsx +62 -0
  26. package/dist/templates/react-app/__tests__/src/main.test.tsx +46 -0
  27. package/dist/templates/react-app/__tests__/src/uikit/components/BaseHeader.test.tsx +88 -0
  28. package/dist/templates/react-app/config/app.router.ts +155 -0
  29. package/dist/templates/react-app/config/common.ts +9 -1
  30. package/dist/templates/react-app/docs/en/bootstrap.md +562 -0
  31. package/dist/templates/react-app/docs/en/development-guide.md +523 -0
  32. package/dist/templates/react-app/docs/en/env.md +482 -0
  33. package/dist/templates/react-app/docs/en/global.md +509 -0
  34. package/dist/templates/react-app/docs/en/i18n.md +268 -0
  35. package/dist/templates/react-app/docs/en/index.md +173 -0
  36. package/dist/templates/react-app/docs/en/ioc.md +424 -0
  37. package/dist/templates/react-app/docs/en/project-structure.md +434 -0
  38. package/dist/templates/react-app/docs/en/request.md +425 -0
  39. package/dist/templates/react-app/docs/en/router.md +404 -0
  40. package/dist/templates/react-app/docs/en/store.md +321 -0
  41. package/dist/templates/react-app/docs/en/test-guide.md +782 -0
  42. package/dist/templates/react-app/docs/en/theme.md +424 -0
  43. package/dist/templates/react-app/docs/en/typescript-guide.md +473 -0
  44. package/dist/templates/react-app/docs/zh/bootstrap.md +7 -0
  45. package/dist/templates/react-app/docs/zh/development-guide.md +523 -0
  46. package/dist/templates/react-app/docs/zh/env.md +24 -25
  47. package/dist/templates/react-app/docs/zh/global.md +28 -27
  48. package/dist/templates/react-app/docs/zh/i18n.md +268 -0
  49. package/dist/templates/react-app/docs/zh/index.md +173 -0
  50. package/dist/templates/react-app/docs/zh/ioc.md +44 -32
  51. package/dist/templates/react-app/docs/zh/project-structure.md +434 -0
  52. package/dist/templates/react-app/docs/zh/request.md +429 -0
  53. package/dist/templates/react-app/docs/zh/router.md +408 -0
  54. package/dist/templates/react-app/docs/zh/store.md +321 -0
  55. package/dist/templates/react-app/docs/zh/test-guide.md +782 -0
  56. package/dist/templates/react-app/docs/zh/theme.md +424 -0
  57. package/dist/templates/react-app/docs/zh/typescript-guide.md +473 -0
  58. package/dist/templates/react-app/package.json +9 -20
  59. package/dist/templates/react-app/src/base/cases/AppConfig.ts +16 -9
  60. package/dist/templates/react-app/src/base/cases/PublicAssetsPath.ts +7 -1
  61. package/dist/templates/react-app/src/base/services/I18nService.ts +15 -4
  62. package/dist/templates/react-app/src/base/services/RouteService.ts +43 -7
  63. package/dist/templates/react-app/src/core/bootstraps/BootstrapApp.ts +31 -10
  64. package/dist/templates/react-app/src/core/bootstraps/BootstrapsRegistry.ts +1 -1
  65. package/dist/templates/react-app/src/core/globals.ts +1 -3
  66. package/dist/templates/react-app/src/core/registers/RegisterCommon.ts +5 -3
  67. package/dist/templates/react-app/src/main.tsx +6 -1
  68. package/dist/templates/react-app/src/pages/404.tsx +0 -1
  69. package/dist/templates/react-app/src/pages/500.tsx +1 -1
  70. package/dist/templates/react-app/src/pages/base/RedirectPathname.tsx +3 -1
  71. package/dist/templates/react-app/src/styles/css/antd-themes/dark.css +3 -1
  72. package/dist/templates/react-app/src/styles/css/antd-themes/index.css +1 -1
  73. package/dist/templates/react-app/src/styles/css/antd-themes/pink.css +6 -1
  74. package/dist/templates/react-app/src/styles/css/page.css +1 -1
  75. package/dist/templates/react-app/src/uikit/components/BaseHeader.tsx +9 -2
  76. package/dist/templates/react-app/src/uikit/components/LocaleLink.tsx +5 -3
  77. package/dist/templates/react-app/src/uikit/hooks/useI18nGuard.ts +4 -6
  78. package/dist/templates/react-app/tsconfig.json +2 -1
  79. package/dist/templates/react-app/tsconfig.test.json +13 -0
  80. package/dist/templates/react-app/vite.config.ts +3 -2
  81. package/package.json +1 -1
@@ -0,0 +1,424 @@
1
+ # 主题系统
2
+
3
+ ## 概述
4
+
5
+ 主题系统采用 CSS 变量实现,结合 Tailwind CSS 和 Ant Design 组件库,提供了灵活且强大的主题定制能力。主要特点:
6
+
7
+ - **CSS 变量驱动**:使用 CSS 变量实现主题切换
8
+ - **Tailwind 集成**:与 Tailwind CSS 完美配合
9
+ - **多主题支持**:内置 light、dark、pink 三种主题
10
+ - **Ant Design 集成**:自定义 Ant Design 组件样式
11
+ - **响应式设计**:支持系统主题跟随
12
+ - **状态管理**:与 Store 系统集成
13
+ - **类型安全**:完整的 TypeScript 类型支持
14
+
15
+ ## 技术栈集成
16
+
17
+ ### 1. Tailwind CSS 配置
18
+
19
+ 项目使用 Tailwind CSS 进行样式管理,通过 PostCSS 插件集成:
20
+
21
+ ```javascript
22
+ // postcss.config.js
23
+ export default {
24
+ plugins: {
25
+ '@tailwindcss/postcss': {}
26
+ }
27
+ };
28
+
29
+ // tailwind.config.js
30
+ export default {
31
+ darkMode: 'class',
32
+ content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}']
33
+ };
34
+ ```
35
+
36
+ ### 2. 样式层级管理
37
+
38
+ ```css
39
+ /* styles/css/tailwind.css */
40
+ @layer theme, base, antd, components, utilities;
41
+
42
+ @import 'tailwindcss';
43
+ @import './themes/index.css';
44
+ ```
45
+
46
+ ### 3. CSS 变量与 Tailwind 结合
47
+
48
+ 主题变量设计时考虑了与 Tailwind 的协同:
49
+
50
+ ```css
51
+ /* 定义主题变量 */
52
+ :root {
53
+ --color-bg-base: 255 255 255; /* 支持 Tailwind 的 rgb 值格式 */
54
+ --text-primary: 15 23 42; /* slate-900 */
55
+ --color-brand: 37 99 235; /* blue-600 */
56
+ }
57
+
58
+ /* 在 Tailwind 中使用主题变量 */
59
+ .custom-element {
60
+ @apply bg-[rgb(var(--color-bg-base))] /* 背景色 */
61
+ text-[rgb(var(--text-primary))] /* 文字颜色 */
62
+ border-[rgb(var(--color-border))] /* 边框颜色 */;
63
+ }
64
+ ```
65
+
66
+ ## 主题配置
67
+
68
+ ### 1. 基础配置
69
+
70
+ 在 `config/theme.ts` 中定义主题配置:
71
+
72
+ ```typescript
73
+ export const themeConfig: ThemeConfig = {
74
+ domAttribute: 'data-theme', // 主题属性名
75
+ defaultTheme: 'system', // 默认主题
76
+ target: 'html', // 主题属性目标元素
77
+ supportedThemes: [
78
+ // 支持的主题列表
79
+ 'light',
80
+ 'dark',
81
+ 'pink'
82
+ ],
83
+ storageKey: 'fe_theme', // 主题存储键名
84
+ init: true, // 自动初始化
85
+ prioritizeStore: true // 优先使用存储的主题
86
+ };
87
+ ```
88
+
89
+ ### 2. 主题变量定义
90
+
91
+ 每个主题都定义了一套完整的 CSS 变量:
92
+
93
+ ```css
94
+ /* 默认主题 (_default.css) */
95
+ :root {
96
+ /* 基础背景色 */
97
+ --color-bg-base: 255 255 255;
98
+ --color-bg-secondary: 241 245 249;
99
+ --color-bg-elevated: 248 250 252;
100
+
101
+ /* 文字颜色 */
102
+ --text-primary: 15 23 42;
103
+ --text-secondary: 71 85 105;
104
+ --text-tertiary: 100 116 139;
105
+
106
+ /* 边框颜色 */
107
+ --color-border: 226 232 240;
108
+
109
+ /* 品牌色 */
110
+ --color-brand: 37 99 235;
111
+ --color-brand-hover: 59 130 246;
112
+ }
113
+
114
+ /* 暗色主题 (dark.css) */
115
+ [data-theme='dark'] {
116
+ /* 基础背景色 */
117
+ --color-bg-base: 15 23 42;
118
+ --color-bg-secondary: 30 41 59;
119
+ --color-bg-elevated: 51 65 85;
120
+
121
+ /* 文字颜色 */
122
+ --text-primary: 255 255 255;
123
+ --text-secondary: 148 163 184;
124
+ --text-tertiary: 100 116 139;
125
+ }
126
+
127
+ /* 粉色主题 (pink.css) */
128
+ [data-theme='pink'] {
129
+ /* 基础背景色 */
130
+ --color-bg-base: 255 241 242;
131
+ --color-bg-secondary: 255 228 230;
132
+ --color-bg-elevated: 254 205 211;
133
+
134
+ /* 文字颜色 */
135
+ --text-primary: 190 18 60;
136
+ --text-secondary: 225 29 72;
137
+ --text-tertiary: 244 63 94;
138
+ }
139
+ ```
140
+
141
+ ## Ant Design 主题定制
142
+
143
+ ### 1. 组件变量
144
+
145
+ 为 Ant Design 组件定制的 CSS 变量:
146
+
147
+ ```css
148
+ .fe-theme {
149
+ /* 主色调 */
150
+ --fe-color-primary: #60a5fa;
151
+ --fe-color-primary-hover: #3b82f6;
152
+ --fe-color-primary-active: #2563eb;
153
+
154
+ /* 状态色 */
155
+ --fe-color-success: #52c41a;
156
+ --fe-color-warning: #faad14;
157
+ --fe-color-error: #ff4d4f;
158
+ --fe-color-info: var(--fe-color-primary);
159
+
160
+ /* 文本颜色 */
161
+ --fe-color-text: rgba(15 23 42 / 0.85);
162
+ --fe-color-text-secondary: rgba(15 23 42 / 0.45);
163
+ --fe-color-text-tertiary: rgba(15 23 42 / 0.35);
164
+
165
+ /* 组件样式 */
166
+ --fe-border-radius: 6px;
167
+ --fe-line-width: 1px;
168
+ --fe-motion-duration-mid: 0.2s;
169
+ }
170
+ ```
171
+
172
+ ### 2. 组件级别定制
173
+
174
+ 针对特定组件的样式定制:
175
+
176
+ ```css
177
+ /* Button 组件 */
178
+ .ant-btn {
179
+ --fe-button-primary-color: #fff;
180
+ --fe-button-primary-bg: #60a5fa;
181
+ --fe-button-primary-hover-bg: #3b82f6;
182
+ --fe-button-padding-inline: 15px;
183
+ --fe-button-font-size: 14px;
184
+ }
185
+
186
+ /* Input 组件 */
187
+ .ant-input {
188
+ --fe-input-padding-block: 4px;
189
+ --fe-input-hover-border-color: #4096ff;
190
+ --fe-input-active-shadow: 0 0 0 2px rgba(5, 145, 255, 0.1);
191
+ }
192
+
193
+ /* Select 组件 */
194
+ .ant-select {
195
+ --fe-select-option-selected-bg: var(--fe-color-primary-bg);
196
+ --fe-select-hover-border-color: var(--fe-color-primary);
197
+ }
198
+ ```
199
+
200
+ ## 主题切换实现
201
+
202
+ ### 1. 主题切换组件
203
+
204
+ ```tsx
205
+ export default function ThemeSwitcher() {
206
+ const themeService = IOC(ThemeService);
207
+ const { theme } = useStore(themeService);
208
+ const themes = themeService.getSupportedThemes();
209
+
210
+ const themeOptions = useMemo(() => {
211
+ return themes.map((themeName) => ({
212
+ value: themeName,
213
+ label: (
214
+ <div className={clsx('flex items-center gap-2')}>
215
+ <ThemeIcon theme={themeName} />
216
+ <span>{t(`HEADER_THEME_${themeName.toUpperCase()}`)}</span>
217
+ </div>
218
+ )
219
+ }));
220
+ }, [themes, t]);
221
+
222
+ return (
223
+ <Select
224
+ value={theme}
225
+ onChange={(value) => themeService.changeTheme(value)}
226
+ options={themeOptions}
227
+ />
228
+ );
229
+ }
230
+ ```
231
+
232
+ ### 2. 主题服务集成
233
+
234
+ ```tsx
235
+ function App() {
236
+ return (
237
+ <AntdThemeProvider
238
+ staticApi={IOC('DialogHandler')}
239
+ theme={{
240
+ cssVar: {
241
+ key: 'fe-theme',
242
+ prefix: 'fe'
243
+ }
244
+ }}
245
+ >
246
+ <RouterProvider router={routerBase} />
247
+ </AntdThemeProvider>
248
+ );
249
+ }
250
+ ```
251
+
252
+ ## 使用方式
253
+
254
+ ### 1. Tailwind 工具类与主题变量
255
+
256
+ ```tsx
257
+ function CustomCard() {
258
+ return (
259
+ <div
260
+ className="
261
+ /* 背景和边框 */
262
+ bg-[rgb(var(--color-bg-base))]
263
+ border-[1px]
264
+ border-[rgb(var(--color-border))]
265
+ rounded-lg
266
+
267
+ /* 间距和阴影 */
268
+ p-4
269
+ shadow-md
270
+
271
+ /* 文字样式 */
272
+ text-[rgb(var(--text-primary))]
273
+
274
+ /* 交互状态 */
275
+ hover:bg-[rgb(var(--color-bg-secondary))]
276
+ transition-colors
277
+ "
278
+ >
279
+ <h3 className="text-lg font-medium mb-2">卡片标题</h3>
280
+ <p className="text-[rgb(var(--text-secondary))]">卡片内容</p>
281
+ </div>
282
+ );
283
+ }
284
+ ```
285
+
286
+ ### 2. 响应式设计
287
+
288
+ ```tsx
289
+ function ResponsiveLayout() {
290
+ return (
291
+ <div
292
+ className="
293
+ /* 响应式布局 */
294
+ grid
295
+ grid-cols-1
296
+ sm:grid-cols-2
297
+ lg:grid-cols-3
298
+ gap-4
299
+
300
+ /* 主题相关样式 */
301
+ bg-[rgb(var(--color-bg-secondary))]
302
+ p-4
303
+ sm:p-6
304
+ lg:p-8
305
+ "
306
+ >
307
+ {/* 内容 */}
308
+ </div>
309
+ );
310
+ }
311
+ ```
312
+
313
+ ### 3. 组件样式复用
314
+
315
+ ```tsx
316
+ // 定义通用按钮样式
317
+ const buttonStyles = {
318
+ base: `
319
+ px-4
320
+ py-2
321
+ rounded-md
322
+ transition-colors
323
+ disabled:opacity-50
324
+ `,
325
+ primary: `
326
+ bg-[rgb(var(--color-brand))]
327
+ text-white
328
+ hover:bg-[rgb(var(--color-brand-hover))]
329
+ `,
330
+ secondary: `
331
+ bg-[rgb(var(--color-bg-secondary))]
332
+ text-[rgb(var(--text-primary))]
333
+ hover:bg-[rgb(var(--color-bg-elevated))]
334
+ `
335
+ };
336
+
337
+ function Button({ variant = 'primary', className = '', ...props }) {
338
+ return (
339
+ <button
340
+ className={`
341
+ ${buttonStyles.base}
342
+ ${buttonStyles[variant]}
343
+ ${className}
344
+ `}
345
+ {...props}
346
+ />
347
+ );
348
+ }
349
+ ```
350
+
351
+ ### 2. 切换主题
352
+
353
+ ```tsx
354
+ function ThemeToggle() {
355
+ const themeService = IOC(ThemeService);
356
+
357
+ const toggleTheme = () => {
358
+ const currentTheme = themeService.getTheme();
359
+ const newTheme = currentTheme === 'light' ? 'dark' : 'light';
360
+ themeService.changeTheme(newTheme);
361
+ };
362
+
363
+ return <button onClick={toggleTheme}>切换主题</button>;
364
+ }
365
+ ```
366
+
367
+ ## 最佳实践
368
+
369
+ 1. **变量命名**
370
+ - 使用语义化的变量名
371
+ - 遵循命名空间规范
372
+ - 保持变量名一致性
373
+
374
+ 2. **主题组织**
375
+ - 按功能分类变量
376
+ - 使用 CSS 层级管理
377
+ - 避免重复定义
378
+
379
+ 3. **性能优化**
380
+ - 合理使用 CSS 变量
381
+ - 避免运行时计算
382
+ - 使用 CSS 预处理
383
+
384
+ 4. **可维护性**
385
+ - 统一的变量管理
386
+ - 清晰的主题结构
387
+ - 完善的类型定义
388
+
389
+ 5. **Tailwind 使用建议**
390
+ - 使用主题变量而不是硬编码颜色
391
+ - 合理组织工具类顺序
392
+ - 抽取常用样式组合
393
+ - 使用 @apply 处理复杂样式
394
+
395
+ 6. **性能优化**
396
+ - 使用 PurgeCSS 清理未使用的样式
397
+ - 合理使用动态类名
398
+ - 避免过度使用自定义属性
399
+
400
+ ## 常见问题
401
+
402
+ ### 1. 主题切换不生效
403
+
404
+ 检查以下几点:
405
+
406
+ - 确保主题配置正确
407
+ - 检查 CSS 变量是否正确加载
408
+ - 验证主题切换服务是否正常
409
+
410
+ ### 2. 组件样式异常
411
+
412
+ 可能的解决方案:
413
+
414
+ - 检查组件变量覆盖
415
+ - 确认 CSS 优先级
416
+ - 查看主题变量继承关系
417
+
418
+ ### 3. 自定义主题
419
+
420
+ 开发建议:
421
+
422
+ - 遵循现有主题结构
423
+ - 完整定义所有必要变量
424
+ - 注意与组件库的兼容性