Skip to content

Commit 6b7ee3e

Browse files
authored
feat: optimize the dark mode switching method, reduce the size of non-dark theme user packages, and add dark theme switching documents (#3153)
* feat: optimize the dark mode switching method, reduce the size of non-dark theme user packages, and add dark theme switching documents * chore: 优化文字描述 * chore: 更新暗黑模式文档,调整样式文件引入方式,优化主题切换方法 * chore: 更新文档中的“暗黑模式”为“深色模式”,统一术语并优化相关描述
1 parent 4517ede commit 6b7ee3e

File tree

7 files changed

+148
-1
lines changed

7 files changed

+148
-1
lines changed

examples/sites/demos/pc/menus.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const docMenusChildren = [
3737
theme: ['default']
3838
}
3939
},
40+
{
41+
'title': '暗黑模式',
42+
'titleEn': 'theme-dark',
43+
'key': 'theme-dark'
44+
},
4045
{ 'title': '表单校验配置', 'titleEn': 'formValid', 'key': 'form-valid' },
4146
{ 'title': '常见问题', 'titleEn': 'faq', 'key': 'faq' },
4247
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Dark Mode
2+
3+
The `TinyVue` component library has supported dark mode since version `v3.22.0`.
4+
5+
## Usage
6+
7+
Import the dark theme style file in the entry file `main.js` (if you're using Method 3 `theme-tool`, you don't need to import `dark-theme.css`)
8+
9+
```js
10+
// Import dark mode style file
11+
import '@opentiny/vue-theme/base/dark-theme.css'
12+
```
13+
14+
## How to Enable Dark Mode
15+
16+
### Method 1: HTML Class Toggle
17+
18+
Add the `dark` class directly to the HTML root element
19+
20+
```html
21+
<html class="dark">
22+
<head></head>
23+
<body></body>
24+
</html>
25+
```
26+
27+
If you want to switch dynamically, we recommend using [useDark | VueUse](https://vueuse.org/core/useDark/)
28+
29+
### Method 2: JavaScript Dynamic Switching
30+
31+
```js
32+
// Switch to dark mode
33+
document.documentElement.classList.add('dark')
34+
35+
// Switch to light mode
36+
document.documentElement.classList.remove('dark')
37+
```
38+
39+
### Method 3: Use TinyThemeTool for Switching
40+
41+
Similar to the theme switching feature, you can use the `TinyThemeTool` class to manage dark mode:
42+
43+
```js
44+
import TinyThemeTool, { tinyDarkTheme } from '@opentiny/vue-theme/theme-tool'
45+
46+
const themeTool = new TinyThemeTool(tinyDarkTheme)
47+
```
48+
49+
## Customize Dark Mode Variables
50+
51+
You can override the default dark mode variables by creating a new CSS file:
52+
53+
```css
54+
html.dark {
55+
/* Customize dark mode background color */
56+
--tv-base-color: #1d1e1f;
57+
}
58+
```
59+
60+
Then import it in your entry file:
61+
62+
```js
63+
import '@opentiny/vue-theme/base/dark-theme.css'
64+
65+
// Import custom dark mode variables
66+
import './styles/dark-theme.css'
67+
```
68+
69+
This way, you can implement personalized dark theme customization while preserving the basic dark mode of the TinyVue component library.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 深色模式
2+
3+
`TinyVue` 组件库从 `v3.22.0` 版本开始支持深色模式啦!!!
4+
5+
## 如何启用深色模式
6+
7+
### 方法一:HTML 类名切换
8+
9+
在入口文件 `main.js` 引入深色主题样式文件
10+
11+
```js
12+
// 引入深色模式样式文件
13+
import '@opentiny/vue-theme/dark-theme-index.css'
14+
```
15+
16+
然后直接在 HTML 根元素添加 `dark` 类名
17+
18+
```html
19+
<html class="dark">
20+
<head></head>
21+
<body></body>
22+
</html>
23+
```
24+
25+
如果您想动态切换,可以自己写 js 切换样式,示例如下:
26+
27+
```js
28+
// 切换为深色模式
29+
document.documentElement.classList.add('dark')
30+
31+
// 切换为亮色模式
32+
document.documentElement.classList.remove('dark')
33+
```
34+
35+
也可以使用业界现成的切换方案,建议使用 [useDark | VueUse](https://vueuse.org/core/useDark/)
36+
37+
### 方法二:使用 TinyThemeTool 切换
38+
39+
类似于主题切换功能,您可以使用 `TinyThemeTool` 类来管理深色模式:
40+
41+
```js
42+
import TinyThemeTool, { tinyDarkTheme } from '@opentiny/vue-theme/theme-tool'
43+
44+
const themeTool = new TinyThemeTool(tinyDarkTheme)
45+
```
46+
47+
## 自定义深色模式变量
48+
49+
您可以通过创建新的 CSS 文件来覆盖默认的深色模式变量:
50+
51+
```css
52+
html.dark {
53+
/* 自定义深色模式背景色 */
54+
--tv-base-color: #1d1e1f;
55+
}
56+
```
57+
58+
然后在入口文件中导入:
59+
60+
```js
61+
import '@opentiny/vue-theme/dark-theme-index.css'
62+
63+
// 导入自定义深色模式变量
64+
import './styles/dark-theme.css'
65+
```
66+
67+
这样,就可以在保留 TinyVue 组件库深色模式基础上,实现个性化的深色主题定制。

examples/sites/src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import html from 'highlight.js/lib/languages/xml'
3030
import docsearch from '@docsearch/js'
3131
import '@docsearch/css'
3232
import { doSearchEverySite } from './tools/docsearch'
33+
import '@opentiny/vue-theme/dark-theme-index.css'
3334

3435
const envTarget = import.meta.env.VITE_BUILD_TARGET || 'open'
3536

internals/cli/src/config/vite.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const getAlias = (vueVersion: string | number, theme = '', design?: string) => {
2323
'@opentiny/vue-icon$': pathFromWorkspaceRoot(`packages/vue-icon${ns(design || theme)}/index.ts`),
2424
'@opentiny/vue-icon-multicolor$': pathFromWorkspaceRoot(`packages/vue-icon-multicolor${ns(theme)}/index.ts`),
2525
'@opentiny/vue-renderless': pathFromWorkspaceRoot('packages/renderless/src'),
26+
'@opentiny/vue-theme/dark-theme-index.css': pathFromWorkspaceRoot(
27+
`packages/theme${ns(design || theme)}/src/base/dark-theme.less`
28+
),
2629
'@opentiny/vue-theme': pathFromWorkspaceRoot(`packages/theme${ns(design || theme)}/src`),
2730
'@opentiny/vue-theme-saas': pathFromWorkspaceRoot('packages/theme-saas/src'),
2831
'@opentiny/vue-common': pathFromWorkspaceRoot('packages/vue-common/src/index-dev'),

packages/theme/build/release.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export default {
4949
`
5050
cssStr = fs.readFileSync(path.resolve(root, 'dist/dark-theme-index.css'), 'utf8')
5151

52+
// 2.3、把 :root.dark 替换为 :root
53+
cssStr = cssStr.replace(':root.dark', ':root')
54+
5255
jsStr = jsStr.replace('#CSS#', cssStr)
5356
fs.writeFileSync(path.resolve(root, 'src/dark-theme-index.js'), jsStr) // 供开发时(pnpm site), 可以访问到最新的定制主题变量
5457
fs.writeFileSync(path.resolve(root, 'dist/dark-theme-index.js'), jsStr) // 打包发布用

packages/theme/src/base/index.less

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@
1414
@import './vars.less';
1515
// @import './old-theme.less';
1616
// @import './aurora-theme.less';
17-
@import './dark-theme.less';
1817
@import './transition.less';

0 commit comments

Comments
 (0)