Skip to content

Theme System

YH-UI provides a theme system based on CSS variables and design tokens, with runtime theme customization APIs exposed by @yh-ui/theme.

Overview

The current theme package includes:

  • preset themes
  • dark mode APIs
  • density control
  • color algorithm switching
  • colorblind mode helpers
  • component-level theme overrides
  • theme import/export and history APIs
  • responsive variable helpers

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 12 built-in preset themes:

  • default - Default blue
  • dark - Dark preset
  • blue - Blue preset
  • green - Green theme
  • purple - Purple theme
  • orange - Orange theme
  • rose - Rose pink
  • amber - Amber
  • teal - Teal
  • 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
setThemePresetAlias for setting 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
clearComponentThemeClear component-level overridesname
enableTransitionEnable switcher animationsconfig?
disableTransitionDisable switcher animations
applyFromPrimaryGenerate a palette from a primary colorcolor: string
exportThemeExport the current themeoptions?
importThemeImport a theme configurationjson: string
exportAsCssExport the current theme as CSS
createThemeCreate a theme configuration objectconfig
applyFullConfigApply a full theme configurationconfig
undoUndo theme changes
getHistoryRetrieve theme history
clearHistoryClear theme history
setResponsiveVarSet a responsive CSS variablename, values
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
useThemeVarsRead current theme CSS variables
provideComponentThemesProvide component theme overrides
useComponentThemeRead component-level theme overrides

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-border-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-xsExtra small screen0
--yh-breakpoint-smSmall screen576px
--yh-breakpoint-mdMedium screen768px
--yh-breakpoint-lgLarge screen992px
--yh-breakpoint-xlExtra large screen1200px
--yh-breakpoint-xxlDouble extra large screen1400px

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 built-in theme accessibility helpers.

More Examples

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

Released under the MIT License.