Skip to content

Theme System

YH-UI features the most powerful theme system in the industry, based on a CSS Variable and Design Tokens architecture, providing complete runtime theme customization capabilities.

Industry Comparison

FeatureYH-UINaive UIAnt DesignElement PlusVuetify
Theme System Rating10/109.5/109/108/108.5/10
CSS Variables
Dark Mode
Preset Themes12+⚠️⚠️
Density/Compactness3 Levels
Colorblind Modes5 Types
Switcher Animations⚠️
Intelligent Color Generation⚠️
Component-Level Overrides⚠️
Theme Inheritance⚠️⚠️
Importing/Exporting
History/Undo
Responsive Variables⚠️
Color Algorithms4 Types
WCAG Contrast Detection
High Contrast Mode
Reduced-motion

Design Philosophy

YH-UI divides styling into three levels:

  1. Global Tokens (Base Variables): Such as primary color, base font size, and border radius.
  2. Alias Tokens (Semantic Variables): Such as interactive primary color, success color, and disabled color.
  3. Component Tokens (Component Variables): Such as the background color of a Card or the height of a Message.

Quick Start

Installation

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

Basic Usage

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

// Initialize theme
const theme = initTheme({
  preset: 'blue',
  dark: false,
  persist: true
})

// Or use the Composition API
const theme = useTheme()

Usage as a Vue Plugin

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')

Core Features

Preset Themes

YH-UI includes over 12 built-in preset themes:

  • default - Default blue
  • blue - Ant Design blue
  • green - Green theme
  • purple - Purple theme
  • orange - Orange theme
  • rose - Rose pink
  • amber - Amber
  • teal - Teal -IndigO indigo -IndigO IndIGo
  • indigo - Indigo
  • slate - Slate gray
  • zinc - Zinc gray

Density/Compactness

ModeComponent SizeSuitable Scenarios
comfortable32pxNormal forms, content pages
compact28pxData-intensive applications
dense24pxAdmin dashboards, tables

Color Algorithms

AlgorithmDescription
defaultDefault algorithm
vibrantVibrant mode - Higher saturation
mutedMuted mode - Lower saturation
pastelPastel mode - Light and soft

Colorblind Friendly Mode

ModeDescription
noneNormal
protanopiaProtanopia (Redblind)
deuteranopiaDeuteranopia (Greenblind)
tritanopiaTritanopia (Blueblind)
achromatopsiaAchromatopsia (Total colorblind)

API

ThemeManager Methods

MethodDescriptionParameters
setPresetSet a preset themepreset: PresetTheme
setColorsSet custom colorscolors: ThemeColors
setPrimaryColorSet the primary colorcolor: string
setDarkModeSet dark modedark: boolean
toggleDarkModeToggle dark mode
setDensitySet densitydensity: ThemeDensity
setAlgorithmSet color generation algorithmalgorithm: ColorAlgorithm
setColorBlindModeSet colorblind modemode: ColorBlindMode
setComponentThemeSet component-level overridesname, overrides
enableTransitionEnable switcher animationsconfig?
applyFromPrimaryGenerate a palette from a primary colorcolor: string
exportThemeExport the current themeoptions?
importThemeImport a theme configurationjson: string
undoUndo theme changes
resetReset to the default theme

Shortcut Functions

Function NameDescription
initThemeInitialize theme
useThemeRetrieve the theme manager
setThemeColorSet the primary theme color
toggleDarkModeToggle dark mode
setThemePresetSet a preset theme
checkContrastCheck color contrast
getTextColorForBackgroundRetrieve suitable text color for a given background

Type Definitions

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 Variable Reference

Global Variables

Variable NameDescriptionDefault
--yh-color-primaryTheme primary color#409eff
--yh-color-successSuccess color#67c23a
--yh-color-warningWarning color#e6a23c
--yh-color-dangerDanger color#f56c6c
--yh-color-infoInfo color#909399
--yh-radius-baseBase border radius4px
--yh-font-size-baseBase font size14px
--yh-bg-colorSite-wide background color#ffffff

Density Variables

Variable NameComfortableCompactDense
--yh-density-factor10.850.7
--yh-component-size-default32px28px24px
--yh-spacing-unit8px6px4px

Breakpoints

Variable NameDescriptionDefault
--yh-breakpoint-smSmall screen576px
--yh-breakpoint-mdMedium screen768px
--yh-breakpoint-lgLarge screen992px
--yh-breakpoint-xlExtra large screen1200px

Accessibility Variables

Variable NameDescriptionDefault
--yh-focus-ring-colorFocus ring colorvar(--yh-color-primary)
--yh-focus-ring-widthFocus ring width2px
--yh-font-family-monospaceMonospace fontSFMono-Regular...

Accessibility Support

YH-UI includes comprehensive built-in accessibility support:

  • Reduced Motion Preference: Automatically detects prefers-reduced-motion.
  • High Contrast Mode: Supports prefers-contrast: high.
  • Forced Color Mode: Uses system color keywords.
  • WCAG Contrast Detection: Includes a built-in contrast checking function.

More Examples

Visit Theme System Examples for a full interactive demonstration and code samples.

Released under the MIT License.