uView Pro

说明
本文适用于 uView Pro 版本 0.4.1 及以上,支持单主题、多主题、暗黑模式等。
本文面向 uView Pro 组件库使用者,帮助你在 Uni-app 项目中完整启用 uView Pro 的多主题与暗黑模式能力,重点围绕 useTheme 的 API 和最佳实践展开。
initTheme 注入自定义主题,支持多套配置并自动生成暗黑色板。useTheme 暴露 currentTheme、themes、darkMode、cssVars 等 ref,可直接用于模板。auto 跟随系统、light、dark 三种模式,且可在运行时主动切换。$u-* SCSS 变量,内联样式使用 $u.color.xxx 或 useTheme().cssVars / useColor() 返回值,确保实时响应。通过【主题配置工具ThemeGenerate】生成主题文件 uview-pro.theme.ts
src/uview-pro.theme.ts,默认导出一个数组,每个元素描述一套主题:export default [
{
name: 'midnight-black',
label: '午夜黑',
color: {
primary: '#ff7a18',
// ... 亮色色板
},
darkColor: {
primaryLight: '#ffb066'
// 留空的暗色会由系统智能生成
}
},
// 其他主题...
];color 也可以,缺失的 darkColor 会自动按规则补齐;若部分暗色需要自定义,只需在 darkColor 中填写对应键,其余仍会自动生成。在 unis.scss中引入uview-pro/theme.scss
@import 'uview-pro/theme.scss';在 main.ts 中注册 uView Pro
import { createSSRApp } from 'vue';
import App from './App.vue';
import theme from '@/uview-pro.theme';
import uViewPro from '@/uni_modules/uview-pro';
export function createApp() {
const app = createSSRApp(App);
app.use(uViewPro, { theme }); // 只需传入一次
return { app };
}install 内部会调用 useTheme().initTheme,无需手动再次初始化,确保主题数据全局唯一。需要配合虚拟根组件(uni-ku-root) 来做全局共享
npm i -D @uni-ku/rootyarn add -D @uni-ku/rootpnpm add -D @uni-ku/root// 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 放置其后
TIP
在 App.ku.vue 中标签 <KuRootView /> 代表指定视图存放位置
在全局根组件包裹 u-config-provider
<script setup lang="ts">
import { useTheme } from 'uview-pro';
import { computed } from 'vue';
const { darkMode, themes, currentTheme } = useTheme();
const themeName = computed(() => currentTheme.value?.name);
</script>
<template>
<u-config-provider :dark-mode="darkMode" :current-theme="themeName" :themes="themes">
<KuRootView />
</u-config-provider>
</template>u-config-provider 负责把 CSS 变量注入到页面,同时监听主题/模式切换事件,具体 API 见 u-config-provider 文档。以上配置完成后需重新编译项目即可生效使用
在 scss 中使用:
<style lang="scss" scoped>
.title{
color: $u-type-primary;
}
</style>在 vue/ts 中使用:
import { ref, onMounted } from 'vue';
import {$u} from 'uview-pro'
const color = ref('');
onMounted(() => {
color.value = uni.$u.color['primary'];
// 或
color.value = $u.color['primary'];
});useTheme API const {
currentTheme, // Ref<Theme | null>
themes, // Ref<Theme[]>
darkMode, // Ref<'light' | 'dark' | 'auto'>
cssVars, // Ref<Record<string, string>>
setTheme,
getCurrentTheme,
getAvailableThemes,
initTheme,
getDarkMode,
setDarkMode,
isInDarkMode,
toggleDarkMode
} = useTheme();currentTheme?.name、themes.map(...)。--u-type-primary, --u-bg-color 等变量,可配合 v-bind 绑定到行内样式。Storage,下次启动自动恢复。setDarkMode('auto') 会立即跟随系统主题。light 与 dark 间快速切换,常用于快捷开关。auto 时会考虑系统设置)。$u-xxx 变量,例如.card {
background-color: $u-bg-white;
border-color: $u-border-color;
}$u.color.xxx 或 useTheme().cssVars<view :style="{ color: $u.color.primary }">按钮</view>
<view :style="{ backgroundColor: `rgba(var(--u-type-primary-rgb), 0.15)` }" />
<view :style="{ backgroundColor: `var(--u-type-primary)` }" />useColor()(可选)提供 getColor(name),在脚本逻辑中获取实时颜色值。<script setup lang="ts">
import { useTheme } from 'uview-pro';
const { themes, currentTheme, setTheme, darkMode, setDarkMode, toggleDarkMode } = useTheme();
</script>
<template>
<picker :range="themes" range-key="label" @change="e => setTheme(themes[e.detail.value].name)">
<view>当前主题:{{ currentTheme?.label }}</view>
</picker>
<u-switch :checked="darkMode === 'dark'" @change="toggleDarkMode" />
<u-button @click="setDarkMode('auto')">跟随系统</u-button>
</template>darkMode 设置为 auto,库会读取 uni.getSystemInfoSync().theme 并立即同步。uni.onThemeChange/window.matchMedia 监听系统变化,保持与系统一致。$u.color.xxx 或 useTheme().cssVars,而不是硬编码十六进制。options.theme 时会与默认主题合并,仍具备暗黑模式。darkColor 中写入需要覆盖的键(例如 primaryLight),其余键仍由系统生成,保证一致性。完成以上步骤即可获得统一、响应式、可扩展的主题与暗黑体验。若需更复杂的用例,可结合 useTheme 返回的 cssVars 做自定义动画或渐变效果。