Skip to content

主题系统

jzfw-ui 的颜色不写死在 UCSS 中,而是以 TS 令牌表维护,运行时通过 getColor() 解析为实际色值后写入内联 :style。这样既规避了原生端对 CSS 变量支持不完整的问题,也让明暗主题切换即时生效。

API

从统一入口导入:

uts
import {
  setTheme, toggleTheme, toggleAutoTheme, initTheme,
  theme, isDark, isAuto,
  getColor, getAdaptiveColor,
  getForegroundColor, getBackgroundColor, getBorderColor, getMaskColor,
  getTokens
} from "@/uni_modules/jzfw-ui";

主题状态

名称类型说明
themeRef<Theme>当前主题("light" | "dark"
isDarkComputedRef<boolean>是否暗色主题,组件内所有明暗分支的唯一判据
isAutoRef<boolean>是否处于自动主题(跟随系统),仅 App 端有意义

主题方法

方法签名说明
setTheme(value: Theme) => void设置主题。App 端须经 uni.setAppTheme 成功后再落地
toggleTheme() => void在明暗主题之间切换
toggleAutoTheme() => void切换自动主题模式(仅 App 有效):开启跟随系统,关闭锁定当前
initTheme() => void初始化主题监听,建议在 App.uvueonLaunch 调用一次
uts
setTheme("dark");     // "light" | "dark"
toggleTheme();        // 明暗切换
isDark.value;         // 当前是否暗色

颜色解析

方法说明
getColor(name)按优先级解析颜色名 → 实际色值
getAdaptiveColor(lightName, darkName)明暗主题分别取色,等价 getColor(isDark ? darkName : lightName)
getForegroundColor()当前主题默认前景色 = getColor("foreground")
getBackgroundColor()当前主题默认背景色 = getColor("background")
getBorderColor()当前主题默认边框色 = getColor("border")
getMaskColor()当前主题遮罩色 = getColor("mask")
getTokens()返回当前主题的完整令牌表 Map<string, string>

getColor(name) 的解析优先级:

  1. 空串 → 返回当前主题的默认前景色
  2. 语义别名primary / success / warn / error / info / dark / light / disabled / foreground / background / border / mask
  3. 令牌全名primary-500surface-700 等色板全名(不随主题变化)
  4. 原样返回#fffrgb(...)rgba(...) 等字面色值
uts
getColor("primary");       // 明暗主题均返回 #14b8a6
getColor("info");          // 浅色 #71717a,深色 #d4d4d8
getColor("surface-700");   // 令牌全名,取值固定
getColor("#ff0000");       // #ff0000

令牌表

主色 primary(teal 色板)

primary-50 ~ primary-950,取值固定不随主题变化。语义别名 primary = #14b8a6(teal-500)。

令牌色值令牌色值
primary-50#f0fdfaprimary-600#0d9488
primary-100#ccfbf1primary-700#0f766e
primary-200#99f6e4primary-800#115e59
primary-300#5eead4primary-900#134e4a
primary-400#2dd4bfprimary-950#042f2e
primary-500#14b8a6

中性色 surface(zinc 色板)

surface(= zinc-0 #ffffff)、surface-50 ~ surface-950,取值固定。

令牌色值令牌色值
surface#ffffffsurface-600#52525b
surface-50#fafafasurface-700#3f3f46
surface-100#f4f4f5surface-800#27272a
surface-200#e4e4e7surface-900#18181b
surface-300#d4d4d8surface-950#09090b
surface-400#a1a1aasurface-500#71717a

语义状态色(明暗共用)

别名色值
success#22c55e
warn#eab308
error#ef4444

明暗自适应别名

以下别名在明暗主题下取值不同:

别名浅色 (light)深色 (dark)
primary#14b8a6#14b8a6
info#71717a (surface-500)#d4d4d8 (surface-300)
dark#3f3f46 (surface-700)#d4d4d8 (surface-300)
light#fafafa (surface-50)#fafafa (surface-50)
disabled#a1a1aa (surface-400)#71717a (surface-500)
foreground#3f3f46 (surface-700)#ffffff
background#ffffff#18181b (surface-900)
border#e4e4e7 (surface-200)#3f3f46 (surface-700)
maskrgba(0,0,0,0.5)rgba(0,0,0,0.7)

别名优先级低于色板全名,因此 primary-500 永远取色板值。

在组件属性中使用颜色

多数组件的 color / backgroundColor 等属性都接受上述任意一种取值(语义别名 / 令牌全名 / 字面色值):

html
<jz-icon name="home-line" color="primary"></jz-icon>
<jz-text value="文本" color="surface-500"></jz-text>
<jz-tag type="error" color="#ff6b6b">自定义色</jz-tag>

平台差异

  • App 端setThemeuni.setAppTheme 成功后落地;initTheme 监听 onOsThemeChange / onAppThemeChangetoggleAutoTheme 可跟随或锁定系统主题。
  • 小程序端initTheme 监听 onHostThemeChange;主题由宿主决定。
  • H5 端onHostThemeChange 存在打包丢失问题,需由宿主在合适时机调用 setTheme 桥接。

jzfw-ui 操作手册