theme 主题
11 个导出:外观三档(浅色 / 深色 / 跟随系统)与六套品牌色,页面根节点挂一个 class 即可,不重编译、不换包。
01 / Signature
导出
| 导出 | 类型 | 说明 |
|---|---|---|
| cuThemeModeList | string[] | 可选主题模式:light / dark / auto |
| cuThemeBrandList | string[] | 可选品牌色:blue / purple / cyan / green / orange / pink |
| cuThemeMode | Ref<string> | 当前主题模式,默认 auto |
| cuThemeBrand | Ref<string> | 当前品牌色,默认 blue |
| cuSystemDark | Ref<boolean> | 系统(或宿主)当前是否深色 |
| cuThemeIsDark() | boolean | 显式模式优先;auto 时看 cuSystemDark |
| cuThemeRootClass() | string | 页面根节点要挂的 class 串 |
| applyThemeNavigationBar() | void | 原生导航栏 / 状态栏配色,仅 APP 生效 |
| setThemeMode(mode) | void | 切主题模式并持久化 |
| setThemeBrand(brand) | void | 切品牌色并持久化 |
| initTheme() | void | 启动时调一次:读持久化、取系统主题、订阅变化 |
02 / Setup
接入
// App.uvue:启动时初始化一次 import { initTheme } from '@/uni_modules/colorui-x/store/theme.uts' initTheme() // 页面:根节点挂 class,onShow 刷一次原生导航栏 import { cuThemeRootClass, applyThemeNavigationBar } from '@/uni_modules/colorui-x/store/theme.uts' const themeClass = computed((): string => cuThemeRootClass()) onShow(() => { applyThemeNavigationBar() }) // 切换:setter 自带持久化,页面读到 ref 自动重渲染 import { setThemeMode, setThemeBrand } from '@/uni_modules/colorui-x/store/theme.uts' setThemeMode('dark') setThemeBrand('purple')
<scroll-view :class="'cu-page ' + themeClass" style="flex: 1" direction="vertical"> <!-- 页面内容 --> </scroll-view>
03 / Notes
注意事项
模板表达式不能直接调用外部导入的函数(实测报「找不到名称 invoke」),所以页面要包一层 computed;它读到 ref,主题一变整页自动重渲染。cuThemeRootClass() 只拼单一 class 串,不依赖复合 / 后代选择器;变量值在 theme/theme.css、颜色 token 在 theme/token.scss,store 不写任何色值。原生导航栏 / 状态栏不吃 CSS 变量,只能走 uni.setNavigationBarColor,且 applyThemeNavigationBar() 只在 APP 生效。持久化键为 cu-theme-mode / cu-theme-brand;App 用 uni.onOsThemeChange + uni.setAppTheme,Web / 小程序用 uni.onHostThemeChange。目前只有 App-Android 实测,iOS / 鸿蒙 / 小程序未测。