Skip to content

Commit 165c58a

Browse files
committed
docs: optimize themes system related
1 parent b86a796 commit 165c58a

File tree

6 files changed

+77
-117
lines changed

6 files changed

+77
-117
lines changed

packages/varlet-ui/src/style-provider/docs/en-US.md

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,7 @@ function toggleTheme() {
5050

5151
### Function Call
5252

53-
A functional call is to update variables directly on `:root`, which is suitable for situations where a global update style is required.
54-
55-
```html
56-
<script setup>
57-
import { StyleProvider } from '@varlet/ui'
58-
59-
let rootStyleVars = null
60-
61-
const darkTheme = {
62-
'--color-primary': 'var(--color-info)'
63-
}
64-
65-
function toggleRootTheme() {
66-
rootStyleVars = rootStyleVars ? null : darkTheme
67-
StyleProvider(rootStyleVars)
68-
}
69-
</script>
70-
71-
<template>
72-
<var-button type="primary" block @click="toggleRootTheme">Toggle Root Theme</var-button>
73-
</template>
74-
```
53+
A functional call is to update variables directly on `:root`, which is suitable for situations where a global update style is required. Details see [Theme Customization](#/en-US/themes).
7554

7655
## API
7756

packages/varlet-ui/src/style-provider/docs/zh-CN.md

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
### 组件式调用
88

9-
组件式调用范围性的定制组件样式,需要注意是有些使用 `teleport` 传送到 `StyleProvider` 外的元素将不会生效。
9+
局部的定制组件样式,需要注意的是使用 `teleport` 传送到 `StyleProvider` 外的元素将不会生效。
1010

1111
```html
1212
<script setup>
@@ -49,28 +49,7 @@ function toggleTheme() {
4949

5050
### 函数式调用
5151

52-
函数式的调用直接更新 `:root` 上的变量,适合需要全局更新样式的情况。
53-
54-
```html
55-
<script setup>
56-
import { StyleProvider } from '@varlet/ui'
57-
58-
let rootStyleVars = null
59-
60-
const darkTheme = {
61-
'--color-primary': 'var(--color-info)'
62-
}
63-
64-
function toggleRootTheme() {
65-
rootStyleVars = rootStyleVars ? null : darkTheme
66-
StyleProvider(rootStyleVars)
67-
}
68-
</script>
69-
70-
<template>
71-
<var-button type="primary" block @click="toggleRootTheme">切换根节点样式变量</var-button>
72-
</template>
73-
```
52+
直接更新 `:root` 上的样式变量,适合需要全局更新样式的情况,详情见[主题定制](#/zh-CN/themes)
7453

7554
## API
7655

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,27 @@
11
<script setup>
2-
import { onUnmounted, reactive, ref } from 'vue'
3-
import { AppType, getBrowserTheme, getSiteStyleVars, onThemeChange, watchLang } from '@varlet/cli/client'
4-
import { StyleProvider, Themes } from '@varlet/ui'
2+
import { reactive, ref } from 'vue'
3+
import { AppType, watchLang } from '@varlet/cli/client'
54
import { t, use } from './locale'
65
7-
const VarStyleProvider = StyleProvider.Component
8-
96
const state = reactive({
107
score: 5,
118
license: true,
129
})
1310
1411
const styleVars = ref(null)
1512
16-
let isRootStyleChanged = false
17-
1813
const successTheme = {
1914
'--rate-primary-color': 'var(--color-success)',
2015
'--button-primary-color': 'var(--color-success)',
2116
'--switch-handle-active-background': 'var(--color-success)',
2217
'--switch-track-active-background': 'var(--color-success)',
2318
}
2419
25-
const themeMap = {
26-
lightTheme: null,
27-
darkTheme: Themes.dark,
28-
md3LightTheme: Themes.md3Light,
29-
md3DarkTheme: Themes.md3Dark,
30-
}
31-
3220
watchLang(use)
3321
34-
onThemeChange(() => {
35-
isRootStyleChanged = false
36-
})
37-
38-
onUnmounted(() => {
39-
const siteTheme = getBrowserTheme()
40-
StyleProvider({ ...getSiteStyleVars(siteTheme), ...themeMap[siteTheme] })
41-
})
42-
4322
function toggleTheme() {
4423
styleVars.value = styleVars.value ? null : successTheme
4524
}
46-
47-
function toggleRootTheme() {
48-
const siteTheme = getBrowserTheme()
49-
const styleVars = isRootStyleChanged
50-
? themeMap[siteTheme]
51-
: {
52-
...themeMap[siteTheme],
53-
...{
54-
'--color-primary': 'var(--color-info)',
55-
},
56-
}
57-
StyleProvider({ ...getSiteStyleVars(siteTheme), ...styleVars })
58-
isRootStyleChanged = !isRootStyleChanged
59-
}
6025
</script>
6126

6227
<template>
@@ -66,7 +31,4 @@ function toggleRootTheme() {
6631
<var-switch v-model="state.license" />
6732
<var-button style="margin-top: 10px" type="primary" block @click="toggleTheme">{{ t('toggleTheme') }}</var-button>
6833
</var-style-provider>
69-
70-
<app-type>{{ t('functionCall') }}</app-type>
71-
<var-button type="primary" block @click="toggleRootTheme">{{ t('toggleRootTheme') }}</var-button>
7234
</template>

packages/varlet-ui/src/themes/docs/en-US.md

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
1-
# Theme System
1+
# Theme Customization
22

33
### Intro
44

5-
The component library organizes styles through [css variables](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties). Variables are divided into general style variables and component style variables. It is recommended to use the [StyleProvider](#/en-US/style-provider) component to load theme files and customize the theme. The themes supported by the component library are as follows.
5+
The component library organizes styles through [css variables](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties). It is recommended to use [StyleProvider](#/zh-CN/style-provider) to load and customize themes. The built-in themes of the component library are as follows.
66

7-
### Available Themes
7+
### Built-in themes
88

9-
| Available Themes | Theme Pack |
9+
| Theme name | Theme package |
1010
| --- | --- |
11-
| `Material Design 2 Light` | _CSS Default_ |
11+
| `Material Design 2 Light` | _Default theme, based on css file_ |
1212
| `Material Design 2 Dark` | _Themes.dark_ |
1313
| `Material Design 3 Light` | _Themes.md3Light_ |
1414
| `Material Design 3 Dark` | _Themes.md3Dark_ |
1515

16-
### Toggle Theme
16+
### Set body variables
17+
18+
Set some basic style variables recommended by the component library to control the basic style of the application.
19+
20+
```css
21+
/* playground-ignore */
22+
body {
23+
transition: background-color .25s, color .25s;
24+
color: var(--color-text);
25+
background-color: var(--color-body);
26+
color-scheme: var(--color-scheme);
27+
}
28+
```
29+
30+
### Switch Theme
1731

1832
```html
1933
<!-- playground-ignore -->
2034
<script setup>
2135
import { StyleProvider, Themes } from '@varlet/ui'
2236
</script>
2337

24-
<template>
25-
<var-space direction="column" size="large">
26-
<var-button type="primary" block @click="StyleProvider(null)">Material Design 2 Light</var-button>
27-
<var-button type="primary" block @click="StyleProvider(Themes.dark)">Material Design 2 Dark</var-button>
38+
<template>
39+
<var-space direction="column" size="large">
40+
<var-button type="primary" block @click="StyleProvider(null)">Material Design 2 Light</var-button>
41+
<var-button type="primary" block @click="StyleProvider(Themes.dark)">Material Design 2 dark</var-button>
2842
<var-button type="primary" block @click="StyleProvider(Themes.md3Light)">Material Design 3 Light</var-button>
2943
<var-button type="primary" block @click="StyleProvider(Themes.md3Dark)">Material Design 3 Dark</var-button>
3044
</var-space>
3145
</template>
3246
```
3347

34-
Inject the `text color` and `background color` variables recommended by the component library to control the overall color.
48+
### Theme Customization
3549

36-
```css
37-
/* playground-ignore */
38-
body {
39-
transition: background-color .25s, color .25s;
40-
color: var(--color-text);
41-
background-color: var(--color-body);
42-
color-scheme: var(--color-scheme);
50+
Due to the huge workload of defining a theme, it is recommended to extend it based on the built-in theme.
51+
52+
```html
53+
<!-- playground-ignore -->
54+
<script setup>
55+
import { StyleProvider, Themes } from '@varlet/ui'
56+
57+
const customTheme = {
58+
...Themes.md3Light,
59+
'--color-primary': 'red'
4360
}
61+
62+
StyleProvider(customTheme)
63+
</script>
4464
```
4565

4666
### Theme Builder

packages/varlet-ui/src/themes/docs/zh-CN.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
# 主题系统
1+
# 主题定制
22

33
### 介绍
44

5-
组件库通过 [css 变量](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties) 来组织样式,变量分为通用样式变量和组件样式变量。推荐使用 [StyleProvider](#/zh-CN/style-provider) 组件加载主题文件、对主题进行定制。组件库支持的主题如下
5+
组件库通过 [css 变量](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties) 组织样式。推荐使用 [StyleProvider](#/zh-CN/style-provider) 加载主题、对主题进行定制。组件库内置的主题如下
66

7-
### 可用主题
7+
### 内置主题
88

99
| 主题名称 | 主题包 |
1010
| --- | --- |
11-
| `Material Design 2 亮色` | _css 样式_ |
11+
| `Material Design 2 亮色` | _默认主题,基于 css 文件_ |
1212
| `Material Design 2 暗色` | _Themes.dark_ |
1313
| `Material Design 3 亮色` | _Themes.md3Light_ |
1414
| `Material Design 3 暗色` | _Themes.md3Dark_ |
1515

16+
### 设置 body 变量
17+
18+
给 body 设置组件库推荐的一些基础样式变量,以控制应用的基本样式。
19+
20+
```css
21+
/* playground-ignore */
22+
body {
23+
transition: background-color .25s, color .25s;
24+
color: var(--color-text);
25+
background-color: var(--color-body);
26+
color-scheme: var(--color-scheme);
27+
}
28+
```
29+
1630
### 切换主题
1731

1832
```html
@@ -31,16 +45,22 @@ import { StyleProvider, Themes } from '@varlet/ui'
3145
</template>
3246
```
3347

34-
注入组件库推荐的 `文字颜色``背景颜色` 变量来控制整体颜色。
48+
### 主题定制
3549

36-
```css
37-
/* playground-ignore */
38-
body {
39-
transition: background-color .25s, color .25s;
40-
color: var(--color-text);
41-
background-color: var(--color-body);
42-
color-scheme: var(--color-scheme);
50+
由于定义一套主题的工作量巨大,推荐基于内置主题进行扩展。
51+
52+
```html
53+
<!-- playground-ignore -->
54+
<script setup>
55+
import { StyleProvider, Themes } from '@varlet/ui'
56+
57+
const customTheme = {
58+
...Themes.md3Light,
59+
'--color-primary': 'red'
4360
}
61+
62+
StyleProvider(customTheme)
63+
</script>
4464
```
4565

4666
### 主题生成器

packages/varlet-ui/varlet.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export default defineConfig({
5151
},
5252
{
5353
text: {
54-
'zh-CN': '主题系统',
55-
'en-US': 'Theme System',
54+
'zh-CN': '主题定制',
55+
'en-US': 'Theme Customization',
5656
},
5757
doc: 'themes',
5858
type: 2,

0 commit comments

Comments
 (0)