* docs(button): 更新按钮组件文档对开放能力的描述 - 修改 session-message-title 和 session-message-path 属性名称 - 新增 chooseavatar 和 agreeprivacyauthorization 事件回调 * docs(icon): 更新图标组件文档 - 调整属性描述,明确 name 属性可用于图标名称或图片链接 - 添加事件列表,明确 click 事件的参数 - 移除英文示例代码中不必要的示例和图标列表 - 优化文档结构和格式,使信息更加清晰 * docs(config-provider): 更新全局配置组件文档 - 调整文档中的强调样式,使用加粗替代下划线 - 完善英文文档缺失的内容,保持和中文文档一致 * docs(component): 更新 Popup 组件文档 - 调整文档结构,优化标题层级 - 添加 v-model 使用示例,明确组件绑定值 - 移除冗余的代码示例,精简文档内容 - 更新属性表格,统一属性描述 - 添加防止滚动穿透的解决方案说明
6.3 KiB
ConfigProvider Global Configuration
Used for global configuration of Wot components, providing capabilities such as dark mode and theme customization.
Dark Mode
Set the theme property of the ConfigProvider component to dark to enable dark mode.
Dark mode will take effect globally, making all Wot components on the page appear in dark style.
<wd-config-provider theme="dark">...</wd-config-provider>
:::tip
It's worth noting that enabling Wot's dark mode will only affect the UI of Wot components and will not affect global text color or background color. You can refer to the following CSS to set some global styles:
:::
.wot-theme-dark body {
color: #f5f5f5;
background-color: black;
}
Dynamic Switching
By dynamically setting the theme property, you can switch between light and dark styles.
<wd-config-provider :theme="theme">...</wd-config-provider>
export default {
setup() {
const theme = ref('light')
setTimeout(() => {
theme.value = 'dark'
}, 1000)
return { theme }
}
}
Theme Customization
Wot components organize styles through rich CSS variables. By overriding these CSS variables, you can achieve theme customization, dynamic theme switching, and other effects.
Example
These variables' default values are defined on the page node. If converting to H5, the default values are defined on the :root node.
:root,
page {
--wot-color-success: red;
--wot-color-warning: yellow;
}
Override via CSS
You can directly override these CSS variables in your code, and the style of the Button component will change accordingly:
/* After adding this style, the default Button background color will become green */
:root,
page {
--wot-button-normal-bg: green;
}
Override via ConfigProvider
The ConfigProvider component provides the ability to override CSS variables. You need to wrap a ConfigProvider component at the root node and configure some theme variables through the theme-vars property.
<wd-config-provider :theme-vars="themeVars">
<div style="margin: 16px">
<wd-button round block type="primary">Submit</wd-button>
</div>
</wd-config-provider>
import { ref, reactive } from 'vue'
export default {
setup() {
// Values in themeVars will be converted to corresponding CSS variables
// For example, buttonPrimaryBg will be converted to `--wot-button-primary-bg-color`
const themeVars = reactive({
buttonPrimaryBgColor: '#07c160',
buttonPrimaryColor: '#07c160'
})
return {
themeVars
}
}
}
Using with TypeScript
When defining themeVars in TypeScript, it's recommended to use the ConfigProviderThemeVars type provided by wot-design-uni, which can provide comprehensive type hints:
import type { ConfigProviderThemeVars } from 'wot-design-uni'
const themeVars: ConfigProviderThemeVars = {
colorTheme: 'red'
}
:::tip Note: ConfigProvider only affects the styles of its child components, not the global root node. :::
Global Sharing
Requires the virtual root component (uni-ku-root) for global sharing
Installation
::: code-group
npm i -D @uni-ku/root
yarn add -D @uni-ku/root
pnpm add -D @uni-ku/root
:::
Import
- CLI project: Directly edit vite.config.(js|ts) in the root directory
- HBuilderX project: Need to create vite.config.(js|ts) in the root directory
// 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 If there are plugins that change pages.json, UniKuRoot needs to be placed after them :::
Usage
- Create a root component and handle global configuration components
- CLI project: Create App.ku.vue in the src directory
- HBuilderX project: Create App.ku.vue in the root directory
:::tip
The <KuRootView /> tag in App.ku.vue represents the specified view placement location
:::
<!-- 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>
<!-- Ensure WdConfigProvider component is registered -->
<wd-config-provider :theme="theme" :theme-vars="themeVars">
<KuRootView />
</wd-config-provider>
</template>
- Write a composable function for theme control
// 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
}
}
- Use theme switching in any view file
<!-- src/pages/*.vue -->
<script setup lang="ts">
import { useTheme } from '@/composables/useTheme'
const { theme, toggleTheme } = useTheme()
</script>
<template>
<button @click="toggleTheme">Toggle theme, current mode: {{ theme }}</button>
</template>
Attributes
| Parameter | Description | Type | Options | Default | Version |
|---|---|---|---|---|---|
| theme | Theme style, set to dark to enable dark mode, takes effect globally |
string | dark/light |
- | - |
| theme-vars | Custom theme variables | ConfigProviderThemeVars |
- | - | - |
External Style Classes
| Class Name | Description | Version |
|---|---|---|
| custom-class | Root node style | 1.3.9 |
| custom-style | Root node style | 1.3.9 |