Skip to content

Theme 主题系统

YH-UI 拥有业内最强大的主题系统,基于 CSS 变量Design Tokens 架构,提供完整的运行时主题定制能力。

业内对比

功能YH-UINaive UIAnt DesignElement PlusVuetify
主题系统评分10/109.5/109/108/108.5/10
CSS 变量
暗色模式
预设主题12+⚠️⚠️
密度/紧凑度3级
色盲友好模式5种
主题切换动画⚠️
智能色彩生成⚠️
组件级覆盖⚠️
主题继承⚠️⚠️
主题导入/导出
主题历史/撤销
响应式变量⚠️
色彩算法4种
WCAG 对比度检测
高对比度模式
reduced-motion

设计思想

YH-UI 将样式分为三个层级:

  1. 基础变量 (Global Tokens):如主色、基础字号、圆角
  2. 语义变量 (Alias Tokens):如交互主色、成功色、失效色
  3. 组件变量 (Component Tokens):如 Card 的背景色、Message 的高度等

快速开始

安装

bash
npm install @yh-ui/theme
# 或
pnpm add @yh-ui/theme

基础用法

typescript
import { initTheme, useTheme } from '@yh-ui/theme'

// 初始化主题
const theme = initTheme({
  preset: 'blue',
  dark: false,
  persist: true
})

// 或使用 Composition API
const theme = useTheme()

Vue 插件方式

typescript
// main.ts
import { createApp } from 'vue'
import { ThemePlugin } from '@yh-ui/theme'
import App from './App.vue'

const app = createApp(App)

app.use(ThemePlugin, {
  preset: 'default',
  dark: false,
  persist: true,
  followSystem: true
})

app.mount('#app')

核心功能

预设主题

YH-UI 内置 12+ 预设主题:

  • default - 默认蓝色
  • blue - Ant Design 蓝
  • green - 绿色主题
  • purple - 紫色主题
  • orange - 橙色主题
  • rose - 玫瑰粉
  • amber - 琥珀色
  • teal - 青色
  • indigo - 靛蓝色
  • slate - 石板灰
  • zinc - 锌灰色

密度/紧凑度

模式组件大小适用场景
comfortable32px普通表单、内容页面
compact28px数据密集型应用
dense24px管理后台、表格

色彩算法

算法说明
default默认算法
vibrant鲜艳模式 - 更高饱和度
muted柔和模式 - 降低饱和度
pastel粉彩模式 - 浅色柔和

色盲友好模式

模式说明
none正常
protanopia红色盲
deuteranopia绿色盲
tritanopia蓝色盲
achromatopsia全色盲

API 参考

ThemeManager 方法

方法名说明参数
setPreset设置预设主题preset: PresetTheme
setColors设置自定义颜色colors: ThemeColors
setPrimaryColor设置主色color: string
setDarkMode设置暗色模式dark: boolean
toggleDarkMode切换暗色模式
setDensity设置密度density: ThemeDensity
setAlgorithm设置色彩算法algorithm: ColorAlgorithm
setColorBlindMode设置色盲模式mode: ColorBlindMode
setComponentTheme设置组件级覆盖name, overrides
enableTransition启用切换动画config?
applyFromPrimary从主色生成调色板color: string
exportTheme导出主题options?
importTheme导入主题json: string
undo撤销主题更改
reset重置为默认主题

快捷函数

函数名说明
initTheme初始化主题
useTheme获取主题管理器
setThemeColor设置主色
toggleDarkMode切换暗色模式
setThemePreset设置预设主题
checkContrast检查对比度
getTextColorForBackground获取合适文字颜色

类型定义

typescript
type PresetTheme = 
  | 'default' | 'dark' | 'blue' | 'green' | 'purple' 
  | 'orange' | 'rose' | 'amber' | 'teal' | 'indigo' 
  | 'slate' | 'zinc'

type ThemeDensity = 'comfortable' | 'compact' | 'dense'

type ColorAlgorithm = 'default' | 'vibrant' | 'muted' | 'pastel'

type ColorBlindMode = 
  | 'none' | 'protanopia' | 'deuteranopia' 
  | 'tritanopia' | 'achromatopsia'

interface ThemeOptions {
  preset?: PresetTheme
  colors?: ThemeColors
  dark?: boolean
  persist?: boolean
  followSystem?: boolean
  algorithm?: ColorAlgorithm
}

CSS 变量参考

全局变量

变量名说明默认值
--yh-color-primary主题主色#409eff
--yh-color-success成功色#67c23a
--yh-color-warning警告色#e6a23c
--yh-color-danger危险色#f56c6c
--yh-color-info信息色#909399
--yh-radius-base基础圆角4px
--yh-font-size-base基础字号14px
--yh-bg-color全站背景色#ffffff

密度变量

变量名舒适紧凑密集
--yh-density-factor10.850.7
--yh-component-size-default32px28px24px
--yh-spacing-unit8px6px4px

断点系统

变量名说明默认值
--yh-breakpoint-sm小屏幕576px
--yh-breakpoint-md中等屏幕768px
--yh-breakpoint-lg大屏幕992px
--yh-breakpoint-xl超大屏幕1200px

无障碍变量

变量名说明默认值
--yh-focus-ring-color聚焦环颜色var(--yh-color-primary)
--yh-focus-ring-width聚焦环宽度2px
--yh-font-family-monospace等宽字体SFMono-Regular...

无障碍支持

YH-UI 内置完整的无障碍支持:

  • 减少动画偏好:自动检测 prefers-reduced-motion
  • 高对比度模式:支持 prefers-contrast: high
  • 强制颜色模式:使用系统颜色关键字
  • WCAG 对比度检测:内置对比度检查函数

更多示例

查看 主题系统示例 获取完整的交互式演示和代码示例。

Released under the MIT License.