wot-design-uni/docs/component/config-provider.md
TAYUN 04d0d11849
docs: 修复 Button/Icon/ConfigProvider/Popup 组件文档不一致问题 (#1190)
* docs(button): 更新按钮组件文档对开放能力的描述

- 修改 session-message-title 和 session-message-path 属性名称
- 新增 chooseavatar 和 agreeprivacyauthorization 事件回调

* docs(icon): 更新图标组件文档

- 调整属性描述,明确 name 属性可用于图标名称或图片链接
- 添加事件列表,明确 click 事件的参数
- 移除英文示例代码中不必要的示例和图标列表
- 优化文档结构和格式,使信息更加清晰

* docs(config-provider): 更新全局配置组件文档

- 调整文档中的强调样式,使用加粗替代下划线
- 完善英文文档缺失的内容,保持和中文文档一致

* docs(component): 更新 Popup 组件文档

- 调整文档结构,优化标题层级
- 添加 v-model 使用示例,明确组件绑定值
- 移除冗余的代码示例,精简文档内容
- 更新属性表格,统一属性描述
- 添加防止滚动穿透的解决方案说明
2025-08-09 15:12:23 +08:00

255 lines
5.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ConfigProvider 全局配置
用于全局配置 `Wot` 组件,提供深色模式、主题定制等能力。
## 深色模式
将 ConfigProvider 组件的 `theme` 属性设置为 `dark`,可以开启深色模式。
深色模式会全局生效,使页面上的所有 `Wot` 组件变为深色风格。
```vue
<wd-config-provider theme="dark">...</wd-config-provider>
```
:::tip
值得注意的是,开启 `Wot` 的深色模式只会影响 `Wot` 组件的 `UI`,并不会影响全局的文字颜色或背景颜色,你可以参考以下 `CSS` 来设置一些全局样式:
:::
```css
.wot-theme-dark body {
color: #f5f5f5;
background-color: black;
}
```
## 动态切换
通过动态设置 `theme` 属性,可以在浅色风格和深色风格之间进行切换。
```vue
<wd-config-provider :theme="theme">...</wd-config-provider>
```
```ts
export default {
setup() {
const theme = ref('light')
setTimeout(() => {
theme.value = 'dark'
}, 1000)
return { theme }
}
}
```
## 定制主题
`Wot` 组件通过丰富的 [CSS 变量](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Using_CSS_custom_properties) 来组织样式,通过覆盖这些 `CSS` 变量,可以实现定制主题、动态切换主题等效果。
### 示例
这些变量的默认值被定义在 `page` 节点上,如果要转 `H5`,默认值被定义在 `:root` 节点上
```css
:root,
page {
--wot-color-success: red;
--wot-color-warning: yellow;
}
```
### 通过 CSS 覆盖
你可以直接在代码中覆盖这些 `CSS` 变量,`Button` 组件的样式会随之发生改变:
```css
/* 添加这段样式后,默认 Button 底色会变成绿色 */
:root,
page {
--wot-button-normal-bg: green;
}
```
### 通过 ConfigProvider 覆盖
`ConfigProvider` 组件提供了覆盖 `CSS` 变量的能力,你需要在根节点包裹一个 `ConfigProvider` 组件,并通过 `theme-vars` 属性来配置一些主题变量
```html
<wd-config-provider :theme-vars="themeVars">
<div style="margin: 16px">
<wd-button round block type="primary">提交</wd-button>
</div>
</wd-config-provider>
```
```ts
import { ref, reactive } from 'vue'
export default {
setup() {
// themeVars 内的值会被转换成对应 CSS 变量
// 比如 buttonPrimaryBg 会转换成 `--wot-button-primary-bg-color`
const themeVars = reactive({
buttonPrimaryBgColor: '#07c160',
buttonPrimaryColor: '#07c160'
})
return {
themeVars
}
}
}
```
### 在 TypeScript 中使用
在 TypeScript 中定义 `themeVars` 时,建议使用 **wot-design-uni** 提供的 **ConfigProviderThemeVars** 类型,可以提供完善的类型提示:
```ts
import type { ConfigProviderThemeVars } from 'wot-design-uni';
const themeVars: ConfigProviderThemeVars = {
colorTheme: 'red'
}
```
:::tip
注意ConfigProvider 仅影响它的子组件的样式,不影响全局 root 节点。
:::
## 全局共享
> 需要配合虚拟根组件([uni-ku-root](https://github.com/uni-ku/root)) 来做全局共享
### 安装
::: code-group
```bash [npm]
npm i -D @uni-ku/root
```
```bash [yarn]
yarn add -D @uni-ku/root
```
```bash [pnpm]
pnpm add -D @uni-ku/root
```
:::
### 引入
- CLI项目: 直接编辑 根目录下的 vite.config.(js|ts)
- HBuilderX项目: 需要在根目录下 创建 vite.config.(js|ts)
```ts
// vite.config.(js|ts)
import { defineConfig } from 'vite'
import UniKuRoot from '@uni-ku/root'
import Uni from '@dcloudio/vite-plugin-uni'
export default defineConfig({
plugins: [
// ...plugins
UniKuRoot(),
Uni()
]
})
```
:::tip
若存在改变 pages.json 的插件,需要将 UniKuRoot 放置其后
:::
### 使用
1. 创建根组件并处理全局配置组件
- CLI项目: 在 **src** 目录下创建下 App.ku.vue
- HBuilderX项目: 在 **根** 目录下创建 App.ku.vue
:::tip
在 App.ku.vue 中标签 `<KuRootView />` 代表指定视图存放位置
:::
```vue
<!-- src/App.ku.vue | App.ku.vue -->
<script setup lang="ts">
import { useTheme } from './composables/useTheme'
const { theme, themeVars } = useTheme({
buttonPrimaryBgColor: '#07c160',
buttonPrimaryColor: '#07c160'
})
</script>
<template>
<div>Hello AppKuVue</div>
<!-- 需要确保已注册 WdConfigProvider 组件 -->
<wd-config-provider :theme="theme" :theme-vars="themeVars">
<KuRootView />
</wd-config-provider>
</template>
```
2. 编写控制主题组合式函数
```ts
// src/composables/useTheme.ts
import type { ConfigProviderThemeVars } from 'wot-design-uni'
import { ref } from 'vue'
const theme = ref<'light' | 'dark'>()
const themeVars = ref<ConfigProviderThemeVars>()
export function useTheme(vars?: ConfigProviderThemeVars) {
vars && (themeVars.value = vars)
function toggleTheme(mode?: 'light' | 'dark') {
theme.value = mode || (theme.value === 'light' ? 'dark' : 'light')
}
return {
theme,
themeVars,
toggleTheme,
}
}
```
3. 在任意视图文件中使用切换主题模式
```vue
<!-- src/pages/*.vue -->
<script setup lang="ts">
import { useTheme } from '@/composables/useTheme'
const { theme, toggleTheme } = useTheme()
</script>
<template>
<button @click="toggleTheme">
切换主题,当前模式:{{ theme }}
</button>
</template>
```
## Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| ---------- | ------------------------------------------------ | ------ | -------------- | ------ | -------- |
| theme | 主题风格,设置为 `dark` 来开启深色模式,全局生效 | string | `dark`/`light` | - | - |
| theme-vars | 自定义主题变量 | `ConfigProviderThemeVars` | - | - | - |
## 外部样式类
| 类名 | 说明 | 最低版本 |
| ------------ | ---------- | ---------------- |
| custom-class | 根节点样式 | 1.3.9 |
| custom-style | 根节点样式 | 1.3.9 |