Skip to content

VitePress 主题

VitePress 提供了灵活的主题系统,您可以使用官方主题、社区主题,或者创建自己的自定义主题。

🎨 官方主题

默认主题

  • 官方主题详解 - VitePress 默认主题的完整指南
  • 现代化的设计风格
  • 响应式布局支持
  • 深色模式切换
  • 丰富的配置选项

主题特性

  • 导航系统 - 顶部导航和侧边栏导航
  • 搜索功能 - 内置全文搜索
  • 多语言 - 完整的国际化支持
  • 代码高亮 - 语法高亮和代码复制
  • 自定义页面 - 首页、团队页面等

🌟 社区主题

热门主题

  • 社区主题推荐 - 优秀的社区主题集合
  • vitepress-theme-demoblock - 支持代码演示的主题
  • vitepress-theme-hope - 功能丰富的博客主题
  • vitepress-theme-curve - 现代化曲线设计主题
  • vitepress-theme-minimalist - 极简风格主题

主题分类

文档类主题

  • 适合技术文档和 API 文档
  • 清晰的层级结构
  • 强大的搜索功能
  • 代码示例支持

博客类主题

  • 适合个人博客和技术分享
  • 文章分类和标签
  • 评论系统集成
  • 社交媒体分享

企业类主题

  • 适合公司官网和产品文档
  • 品牌定制支持
  • 营销页面模板
  • 联系表单集成

🛠️ 自定义主题

主题开发基础

javascript
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
import './custom.css'

export default {
  ...DefaultTheme,
  enhanceApp({ app, router, siteData }) {
    // 应用级别的增强
  }
}

样式定制

css
/* .vitepress/theme/custom.css */
:root {
  --vp-c-brand: #646cff;
  --vp-c-brand-light: #747bff;
  --vp-c-brand-lighter: #9499ff;
  --vp-c-brand-lightest: #bcc0ff;
  --vp-c-brand-dark: #535bf2;
  --vp-c-brand-darker: #454ce1;
  --vp-c-brand-dimm: #363447;
}

组件扩展

vue
<!-- .vitepress/theme/components/CustomComponent.vue -->
<template>
  <div class="custom-component">
    <h2>{{ title }}</h2>
    <slot />
  </div>
</template>

<script setup>
defineProps(['title'])
</script>

🎯 主题选择指南

选择标准

  • 项目类型 - 文档、博客、企业网站
  • 设计风格 - 现代、简约、专业
  • 功能需求 - 搜索、评论、分析
  • 维护状态 - 活跃更新、社区支持

性能考虑

  • 加载速度 - 主题对页面加载的影响
  • 包大小 - 主题资源的体积
  • 兼容性 - 与 VitePress 版本的兼容
  • 移动端 - 响应式设计质量

🔧 主题安装

使用 npm 安装

bash
# 安装主题
npm install theme-name

# 或使用 yarn
yarn add theme-name

配置主题

javascript
// .vitepress/config.js
import { defineConfig } from 'vitepress'

export default defineConfig({
  // 主题配置
  themeConfig: {
    // 主题特定配置
  }
})

主题切换

javascript
// .vitepress/theme/index.js
import CustomTheme from 'theme-name'
import './style.css'

export default CustomTheme

🎨 主题定制

CSS 变量定制

css
:root {
  /* 颜色定制 */
  --vp-c-brand: #your-brand-color;
  --vp-c-bg: #your-bg-color;
  
  /* 字体定制 */
  --vp-font-family-base: 'Your Font', sans-serif;
  
  /* 布局定制 */
  --vp-layout-max-width: 1200px;
}

组件覆盖

javascript
// .vitepress/theme/index.js
import DefaultTheme from 'vitepress/theme'
import CustomLayout from './CustomLayout.vue'

export default {
  ...DefaultTheme,
  Layout: CustomLayout
}

插槽定制

vue
<!-- .vitepress/theme/Layout.vue -->
<template>
  <Layout>
    <template #nav-bar-title-before>
      <CustomLogo />
    </template>
    <template #sidebar-nav-before>
      <CustomSidebar />
    </template>
  </Layout>
</template>

📱 响应式设计

断点配置

css
/* 移动端 */
@media (max-width: 768px) {
  .custom-component {
    padding: 1rem;
  }
}

/* 平板端 */
@media (min-width: 769px) and (max-width: 1024px) {
  .custom-component {
    padding: 2rem;
  }
}

/* 桌面端 */
@media (min-width: 1025px) {
  .custom-component {
    padding: 3rem;
  }
}

移动端优化

  • 触摸友好的交互设计
  • 合适的字体大小和行高
  • 优化的导航菜单
  • 快速的页面加载

🌙 深色模式

深色模式支持

css
/* 浅色模式 */
:root {
  --text-color: #2c3e50;
  --bg-color: #ffffff;
}

/* 深色模式 */
.dark {
  --text-color: #ffffff;
  --bg-color: #1a1a1a;
}

自动切换

javascript
// 主题切换逻辑
const toggleTheme = () => {
  const isDark = document.documentElement.classList.contains('dark')
  document.documentElement.classList.toggle('dark', !isDark)
  localStorage.setItem('theme', isDark ? 'light' : 'dark')
}

🔗 主题资源

官方资源

社区资源

设计灵感

💡 最佳实践

主题开发

  • 遵循 VitePress 的设计原则
  • 保持良好的性能表现
  • 提供完整的文档和示例
  • 确保跨浏览器兼容性

主题维护

  • 定期更新依赖版本
  • 修复已知问题和 bug
  • 收集用户反馈并改进
  • 保持与 VitePress 的兼容性

用户体验

  • 优化页面加载速度
  • 提供清晰的导航结构
  • 确保内容的可读性
  • 支持无障碍访问

🚀 主题发布

准备发布

  1. 完善主题文档
  2. 添加使用示例
  3. 配置 package.json
  4. 编写 README 文件

发布到 npm

bash
# 登录 npm
npm login

# 发布主题
npm publish

社区推广

  • 提交到 Awesome VitePress
  • 在社区论坛分享
  • 编写介绍文章
  • 制作演示视频

选择合适的主题可以让您的 VitePress 网站更加美观和专业。无论是使用现有主题还是创建自定义主题,都要注重用户体验和性能表现。

vitepress开发指南