文档 / 工具库 API / theme.uts

theme 主题

11 个导出:外观三档(浅色 / 深色 / 跟随系统)与六套品牌色,页面根节点挂一个 class 即可,不重编译、不换包。

01 / Signature

导出

导出类型说明
cuThemeModeListstring[]可选主题模式:light / dark / auto
cuThemeBrandListstring[]可选品牌色:blue / purple / cyan / green / orange / pink
cuThemeModeRef<string>当前主题模式,默认 auto
cuThemeBrandRef<string>当前品牌色,默认 blue
cuSystemDarkRef<boolean>系统(或宿主)当前是否深色
cuThemeIsDark()boolean显式模式优先;auto 时看 cuSystemDark
cuThemeRootClass()string页面根节点要挂的 class 串
applyThemeNavigationBar()void原生导航栏 / 状态栏配色,仅 APP 生效
setThemeMode(mode)void切主题模式并持久化
setThemeBrand(brand)void切品牌色并持久化
initTheme()void启动时调一次:读持久化、取系统主题、订阅变化
02 / Setup

接入

theme
// 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')
template
<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 / 鸿蒙 / 小程序未测。