theme.md 7.96 KB

主题定制

Venus Admin 提供了灵活的主题定制系统,支持亮色/暗色模式切换和自定义主题色。

主题配置

在 src/preferences.ts 中配置主题:

import { defineOverridesPreferences } from '@venus/preferences';

export const overridesPreferences = defineOverridesPreferences({
  theme: {
    // 主题模式: light | dark | auto
    mode: 'auto',
    // 主题色
    colorPrimary: '#646cff',
    // 成功色
    colorSuccess: '#52c41a',
    // 警告色
    colorWarning: '#faad14',
    // 错误色
    colorError: '#f5222d',
    // 信息色
    colorInfo: '#1890ff',
    // 圆角
    radius: '0.5rem',
  },
});

主题色定制

方式一:配置文件

在 tailwind.config.mjs 中配置主题色:

/** @type {import('tailwindcss').Config} */
export default {
  theme: {
    extend: {
      colors: {
        primary: {
          DEFAULT: '#646cff',
          50: '#f0f1ff',
          100: '#e4e5ff',
          200: '#cdcfff',
          300: '#a6aaff',
          400: '#8488ff',
          500: '#646cff',
          600: '#4d51f5',
          700: '#3b3fd8',
          800: '#3034ae',
          900: '#2c2f89',
          950: '#1b1d51',
        },
      },
    },
  },
};

方式二:CSS 变量

在全局样式文件中定义 CSS 变量:

:root {
  --color-primary: #646cff;
  --color-primary-light: #8488ff;
  --color-primary-dark: #4d51f5;

  --color-success: #52c41a;
  --color-warning: #faad14;
  --color-error: #f5222d;
  --color-info: #1890ff;

  --border-radius: 0.5rem;
  --sidebar-width: 240px;
  --header-height: 50px;
}

暗色模式

自动切换

根据系统主题自动切换:

export const overridesPreferences = defineOverridesPreferences({
  theme: {
    mode: 'auto', // 跟随系统
  },
});

手动切换

<template>
  <button @click="toggleTheme">
    <Icon :icon="isDark ? 'lucide:moon' : 'lucide:sun'" />
  </button>
</template>

<script setup lang="ts">
import { useAppStore } from '@/store/modules/app';
import { computed } from 'vue';

const appStore = useAppStore();

const isDark = computed(() => appStore.theme === 'dark');

const toggleTheme = () => {
  appStore.setTheme(isDark.value ? 'light' : 'dark');
};
</script>

暗色模式样式

使用 Tailwind CSS 的 dark 变体:

<template>
  <div class="bg-white dark:bg-gray-900 text-gray-900 dark:text-white">
    内容
  </div>
</template>

使用 CSS 变量:

:root {
  --bg-color: #ffffff;
  --text-color: #000000;
}

:root.dark {
  --bg-color: #1a1a1a;
  --text-color: #ffffff;
}

.content {
  background-color: var(--bg-color);
  color: var(--text-color);
}

布局主题

侧边栏主题

export const overridesPreferences = defineOverridesPreferences({
  sidebar: {
    // 侧边栏主题
    theme: 'dark', // light | dark
    // 背景色
    bgColor: '#001529',
    // 文字色
    textColor: '#ffffff',
  },
});

头部主题

export const overridesPreferences = defineOverridesPreferences({
  header: {
    // 头部主题
    theme: 'light', // light | dark
    // 背景色
    bgColor: '#ffffff',
  },
});

UI 框架主题

Ant Design Vue

// src/main.ts
import { ConfigProvider } from 'ant-design-vue';
import { computed } from 'vue';
import { useAppStore } from '@/store/modules/app';

const appStore = useAppStore();

const theme = computed(() => ({
  token: {
    colorPrimary: '#646cff',
    borderRadius: 8,
  },
  algorithm: appStore.theme === 'dark' 
    ? ConfigProvider.darkAlgorithm 
    : ConfigProvider.defaultAlgorithm,
}));

Element Plus

// src/main.ts
import { ElConfigProvider } from 'element-plus';

<ElConfigProvider
  :locale="locale"
  :theme-overrides="{
    colorPrimary: '#646cff',
  }"
>
  <App />
</ElConfigProvider>

Naive UI

// src/main.ts
import { NConfigProvider, darkTheme } from 'naive-ui';
import { computed } from 'vue';

const theme = computed(() => 
  appStore.theme === 'dark' ? darkTheme : null
);

const themeOverrides = {
  common: {
    primaryColor: '#646cff',
  },
};

动态主题切换

实时预览主题色

<template>
  <div>
    <input 
      type="color" 
      v-model="primaryColor" 
      @change="handleColorChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const primaryColor = ref('#646cff');

const handleColorChange = () => {
  // 更新 CSS 变量
  document.documentElement.style.setProperty(
    '--color-primary',
    primaryColor.value
  );

  // 或者更新 Tailwind 配置(需要重新编译)
};
</script>

主题预设

提供多套预设主题:

const themePresets = {
  default: {
    colorPrimary: '#646cff',
    colorSuccess: '#52c41a',
    colorWarning: '#faad14',
    colorError: '#f5222d',
  },
  green: {
    colorPrimary: '#52c41a',
    colorSuccess: '#95de64',
    colorWarning: '#faad14',
    colorError: '#ff4d4f',
  },
  blue: {
    colorPrimary: '#1890ff',
    colorSuccess: '#52c41a',
    colorWarning: '#faad14',
    colorError: '#ff4d4f',
  },
};

// 应用预设主题
const applyTheme = (preset: keyof typeof themePresets) => {
  const theme = themePresets[preset];
  Object.entries(theme).forEach(([key, value]) => {
    document.documentElement.style.setProperty(
      `--color-${key.replace('color', '').toLowerCase()}`,
      value
    );
  });
};

自定义组件样式

使用 CSS Modules

<template>
  <div :class="$style.container">
    <h1 :class="$style.title">标题</h1>
  </div>
</template>

<style module>
.container {
  padding: 20px;
  background: var(--bg-color);
}

.title {
  color: var(--color-primary);
}
</style>

使用 Scoped Styles

<template>
  <div class="container">
    <h1 class="title">标题</h1>
  </div>
</template>

<style scoped>
.container {
  padding: 20px;
}

.title {
  color: var(--color-primary);
}

/* 深度选择器 */
:deep(.ant-btn) {
  border-radius: var(--border-radius);
}
</style>

主题工具

生成主题色板

// src/utils/theme.ts

/**
 * 根据主色生成色板
 */
export function generateColorPalette(color: string) {
  // 实现色板生成逻辑
  return {
    50: lighten(color, 0.9),
    100: lighten(color, 0.8),
    200: lighten(color, 0.6),
    300: lighten(color, 0.4),
    400: lighten(color, 0.2),
    500: color,
    600: darken(color, 0.2),
    700: darken(color, 0.4),
    800: darken(color, 0.6),
    900: darken(color, 0.8),
  };
}

/**
 * 颜色加亮
 */
function lighten(color: string, amount: number): string {
  // 实现加亮逻辑
}

/**
 * 颜色加深
 */
function darken(color: string, amount: number): string {
  // 实现加深逻辑
}

主题持久化

// src/composables/useTheme.ts
import { watch } from 'vue';
import { useAppStore } from '@/store/modules/app';

export function useTheme() {
  const appStore = useAppStore();

  // 监听主题变化
  watch(
    () => appStore.theme,
    (theme) => {
      // 更新 HTML class
      document.documentElement.classList.toggle('dark', theme === 'dark');

      // 保存到本地存储
      localStorage.setItem('theme', theme);
    },
    { immediate: true }
  );

  return {
    theme: computed(() => appStore.theme),
    setTheme: appStore.setTheme,
  };
}

最佳实践

1. 使用 CSS 变量

CSS 变量便于主题切换和维护:

:root {
  --color-primary: #646cff;
}

.button {
  background: var(--color-primary);
}

2. 避免硬编码颜色

使用主题色而不是硬编码:

<!-- ❌ 不推荐 -->
<div class="text-[#646cff]">文本</div>

<!-- ✅ 推荐 -->
<div class="text-primary">文本</div>

3. 提供主题切换入口

在设置页面提供主题切换功能。

4. 测试多种主题

确保在不同主题下界面都能正常显示。

下一步