主题配置

自定义应用的颜色、圆角和字体

类型定义

interface ThemeConfig {
  /**
   * 颜色覆盖
   */
  colors?: Partial<ThemeColors>;

  /**
   * 圆角预设
   */
  radius?: 'none' | 'sm' | 'md' | 'lg' | 'full';

  /**
   * 字体覆盖
   */
  font?: {
    sans?: string;
    mono?: string;
  };

  /**
   * 自定义 CSS 变量
   */
  cssVariables?: Record<string, string>;
}

interface ThemeColors {
  primary: string;
  secondary: string;
  background: string;
  foreground: string;
  muted: string;
  mutedForeground: string;
  border: string;
  ring: string;
  accent: string;
  accentForeground: string;
  destructive: string;
  destructiveForeground: string;
}

颜色配置

覆盖主题颜色:

theme: {
  colors: {
    primary: '#3b82f6',         // 主色
    secondary: '#64748b',       // 次要色
    destructive: '#ef4444',     // 危险操作色
  },
},

完整颜色列表

颜色说明使用场景
primary主色按钮、链接、高亮
secondary次要色次要按钮、辅助信息
background背景色页面背景
foreground前景色文本颜色
muted柔和背景禁用状态、次要区域
mutedForeground柔和前景次要文本
border边框色边框、分隔线
ring焦点环聚焦状态
accent强调色悬停状态
accentForeground强调前景强调区域文本
destructive危险色删除按钮、错误
destructiveForeground危险前景危险按钮文本

圆角配置

预设圆角大小:

theme: {
  radius: 'md',  // 'none' | 'sm' | 'md' | 'lg' | 'full'
},
效果
none无圆角 (0)
sm小圆角 (0.25rem)
md中等圆角 (0.5rem)
lg大圆角 (0.75rem)
full完全圆角 (9999px)

字体配置

自定义字体:

theme: {
  font: {
    sans: '"Inter", "Noto Sans SC", sans-serif',
    mono: '"Fira Code", monospace',
  },
},

CSS 变量

添加自定义 CSS 变量:

theme: {
  cssVariables: {
    '--header-height': '48px',
    '--sidebar-width': '200px',
    '--custom-spacing': '1.5rem',
  },
},

在 CSS 中使用:

.my-component {
  height: var(--header-height);
  margin-left: var(--sidebar-width);
}

明暗模式

基座自动支持明暗模式:

  • 使用 useTheme hook 获取和设置主题
  • 颜色会根据主题自动切换
  • 系统模式跟随操作系统设置
import { useTheme } from '@linch-tech/desktop-core';

function ThemeToggle() {
  const { theme, setTheme } = useTheme();

  return (
    <select value={theme} onChange={(e) => setTheme(e.target.value)}>
      <option value="light">浅色</option>
      <option value="dark">深色</option>
      <option value="system">跟随系统</option>
    </select>
  );
}

完整示例

theme: {
  colors: {
    primary: '#8b5cf6',      // 紫色主题
    secondary: '#a78bfa',
    destructive: '#f43f5e',
  },
  radius: 'lg',
  font: {
    sans: '"Inter", sans-serif',
    mono: '"JetBrains Mono", monospace',
  },
  cssVariables: {
    '--header-height': '52px',
  },
},