Skip to content

Theme Customization

YH-UI offers flexible theme customization capabilities, allowing you to personalize component styles in multiple ways.

CSS Variables

YH-UI uses CSS variables (CSS Custom Properties) to define all design tokens. All variables are prefixed with --yh-.

Global Overriding

Override CSS variables in your style files:

css
:root {
  /* Modify primary colors */
  --yh-color-primary: #6366f1;
  --yh-color-primary-light-3: #818cf8;
  --yh-color-primary-light-5: #a5b4fc;
  --yh-color-primary-light-7: #c7d2fe;
  --yh-color-primary-light-9: #e0e7ff;
  --yh-color-primary-dark-2: #4f46e5;

  /* Modify border radius */
  --yh-border-radius-base: 8px;

  /* Modify typography */
  --yh-font-family: 'Inter', sans-serif;
}

Scoped Overriding

Variable overrides can be applied within a specific scope:

css
.my-custom-theme {
  --yh-color-primary: #8b5cf6;
  --yh-border-radius-base: 12px;
}
vue
<template>
  <div class="my-custom-theme">
    <yh-button type="primary">Custom Theme Button</yh-button>
  </div>
</template>

Dark Mode

YH-UI provides dark mode APIs through @yh-ui/theme. If you are using the theme package, prefer switching dark mode through the theme manager:

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()
theme.toggleDarkMode()

If your project manages dark mode separately, you can still override the related --yh-* variables in a scoped selector.

Customizing Dark Mode Variables

css
html.dark {
  --yh-color-primary: #60a5fa;
  --yh-bg-color: #1a1a1a;
  --yh-bg-color-page: #0f0f0f;
}

SCSS Usage

The current theme package is centered on published CSS variables. For app-level customization, the reliable approach is to override the exposed --yh-* variables directly:

scss
:root {
  --yh-color-primary: #6366f1;
  --yh-border-radius-base: 8px;
  --yh-font-family: 'Inter', sans-serif;
}

Then import the package styles as usual in your app entry:

ts
import '@yh-ui/yh-ui/css'

Available CSS Variables

Colors

Variable NameDescriptionDefault
--yh-color-primaryPrimary color#409eff
--yh-color-successSuccess color#67c23a
--yh-color-warningWarning color#e6a23c
--yh-color-dangerDanger color#f56c6c
--yh-color-infoInfo color#909399

Typography

Variable NameDescriptionDefault
--yh-text-color-primaryPrimary text color#303133
--yh-text-color-regularRegular text color#606266
--yh-text-color-secondarySecondary text color#909399
--yh-text-color-placeholderPlaceholder color#a8abb2
--yh-text-color-disabledDisabled text color#c0c4cc

Borders

Variable NameDescriptionDefault
--yh-border-colorBorder color#dcdfe6
--yh-border-color-lightLight border color#e4e7ed
--yh-border-color-lighterLighter border color#ebeef5

Border Radius

Variable NameDescriptionDefault
--yh-radius-smSmall radius2px
--yh-border-radius-baseBase radius4px
--yh-radius-mdMedium radius8px
--yh-radius-lgLarge radius12px
--yh-radius-roundRound20px

Spacing

Variable NameDescriptionDefault
--yh-spacing-xsExtra small spacing4px
--yh-spacing-smSmall spacing8px
--yh-spacing-mdMedium spacing16px
--yh-spacing-lgLarge spacing24px
--yh-spacing-xlExtra large spacing32px

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

Font Families

Variable NameDescriptionDefault
--yh-font-familyDefault fontSystem font stack
--yh-font-family-monospaceMonospace fontSFMono-Regular, Consolas...

Accessibility/Focus

Variable NameDescriptionDefault
--yh-focus-ring-colorFocus ring colorvar(--yh-color-primary)
--yh-focus-ring-widthFocus ring width2px
--yh-focus-ring-offsetFocus ring offset2px
--yh-focus-ringComplete focus ring styleComposite value

Mask Layer

Variable NameDescriptionDefault
--yh-mask-colorMask colorrgba(0, 0, 0, 0.5)
--yh-mask-color-lightLight mask colorrgba(0, 0, 0, 0.3)
--yh-mask-color-extra-lightExtra light mask colorrgba(0, 0, 0, 0.1)

Scrollbars

Variable NameDescriptionDefault
--yh-scrollbar-widthScrollbar width6px
--yh-scrollbar-thumb-colorScrollbar thumb colorvar(--yh-text-color-disabled)
--yh-scrollbar-thumb-hover-colorScrollbar thumb hover colorvar(--yh-text-color-secondary)
--yh-scrollbar-track-colorScrollbar track colortransparent

Accessibility Support

Reduced Motion Preference

YH-UI automatically detects the user's prefers-reduced-motion preference. When "reduce motion" is enabled, animations and transitions can be reduced through the exported theme variables:

css
@media (prefers-reduced-motion: reduce) {
  :root {
    --yh-duration-fast: 0ms;
    --yh-duration-base: 0ms;
    --yh-duration-slow: 0ms;
  }
}

High Contrast Mode

YH-UI supports Windows High Contrast Mode and the prefers-contrast: high media query:

css
@media (prefers-contrast: high) {
  :root {
    --yh-border-color: #000000;
    --yh-focus-ring-width: 3px;
  }
}

Forced Colors Mode

For Windows High Contrast themes, YH-UI uses system color keywords to ensure accessibility:

css
@media (forced-colors: active) {
  :root {
    --yh-color-primary: LinkText;
    --yh-bg-color: Canvas;
    --yh-text-color-primary: CanvasText;
  }
}

Advanced Theme Features

YH-UI provides a set of runtime theme management APIs in @yh-ui/theme.

Theme Manager

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

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

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

Density/Compactness Control

YH-UI supports three density modes to suit different scenario requirements:

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.setDensity('comfortable')
theme.setDensity('compact')
theme.setDensity('dense')
ModeComponent SizeSuitable Scenarios
comfortable32pxNormal forms, content pages
compact28pxData-intensive applications
dense24pxAdmin dashboards, tables

Colorblind Friendly Mode

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.setColorBlindMode('none')
theme.setColorBlindMode('protanopia')
theme.setColorBlindMode('deuteranopia')
theme.setColorBlindMode('tritanopia')
theme.setColorBlindMode('achromatopsia')

Theme Switcher Animations

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.enableTransition({
  duration: 300,
  timing: 'cubic-bezier(0.4, 0, 0.2, 1)'
})

theme.toggleDarkMode()
theme.disableTransition()

Intelligent Color Generation

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()
theme.applyFromPrimary('#8b5cf6')

Color Harmony Tools

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()
const primary = '#409eff'

const complementary = theme.getComplementary(primary)
const [analogous1, analogous2] = theme.getAnalogous(primary)
const [triadic1, triadic2] = theme.getTriadic(primary)

Component-Level Theme Overriding

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.setComponentTheme('button', {
  'border-radius': '20px',
  'font-weight': '600',
  'padding': '0 24px'
})

theme.clearComponentTheme('button')

Theme Inheritance and Extension

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

const customTheme = theme.createTheme({
  name: 'My Brand Theme',
  author: 'Your Name',
  extends: 'blue',
  colors: {
    primary: '#8b5cf6'
  },
  density: 'compact'
})

theme.applyFullConfig(customTheme)

Theme Importing/Exporting

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()
const themeJson = theme.exportTheme({
  name: 'My Theme',
  author: 'Your Name'
})

theme.importTheme(themeJson)
const css = theme.exportAsCss()

Theme History and Undo

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.setPreset('purple')
theme.setDarkMode(true)
theme.undo()

const history = theme.getHistory()
theme.clearHistory()

Responsive Theme Variables

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.setResponsiveVar('--yh-font-size-base', {
  xs: '12px',
  sm: '13px',
  md: '14px',
  lg: '15px',
  xl: '16px'
})

Color Algorithms

ts
import { useTheme } from '@yh-ui/theme'

const theme = useTheme()

theme.setAlgorithm('default')
theme.setAlgorithm('vibrant')
theme.setAlgorithm('muted')
theme.setAlgorithm('pastel')

Usage as a Vue Plugin

ts
// 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: 'blue',
  dark: false,
  persist: true,
  followSystem: true,
  density: 'comfortable'
})

app.mount('#app')

For more variables, please refer to Design Specification or view the Source Code.

Released under the MIT License.