Creating a Custom Theme
VitePress allows you to create completely custom themes to match your specific design requirements. A custom theme gives you full control over the layout, styling, and functionality of your documentation site.Theme Structure
Theme Entry File
Create a theme entry file at.vitepress/theme/index.js or .vitepress/theme/index.ts:
When VitePress detects a theme entry file, it will use your custom theme instead of the default theme.
Theme Interface
A VitePress theme must implement this interface:Basic Custom Theme
Minimal Setup
The simplest possible theme:With Type Safety
- TypeScript
- JSDoc
.vitepress/theme/index.ts
Building a Layout Component
Basic Layout
A layout needs at minimum the<Content /> component:
.vitepress/theme/Layout.vue
Handling 404 Pages
Use theuseData() composable to detect 404 pages:
Multiple Layout Types
Support different layouts via frontmatter:Using Runtime Data
Available Composables
VitePress provides several composables for accessing runtime data:- useData
- useRoute
- useRouter
Accessing Page Data
Enhancing the App
Registering Global Components
.vitepress/theme/index.js
Auto-Registering Components
Use Vite’s glob import:.vitepress/theme/index.js
Installing Vue Plugins
.vitepress/theme/index.js
Adding Global State
.vitepress/theme/index.js
SSR Compatibility
Conditional Browser Code
- onMounted
- ClientOnly Component
- Dynamic Import
Import Guards
Theme Styling
Scoped Styles
Global Styles
.vitepress/theme/index.js
CSS Variables
Distributing Your Theme
As npm Package
- Structure your package:
- Export the theme:
src/index.ts
- Package.json:
- Document usage:
README.md
As GitHub Template
Create a template repository with:Advanced Examples
Blog Theme
.vitepress/theme/Layout.vue
Documentation Theme with Sidebar
.vitepress/theme/Layout.vue
Complete Theme Example
.vitepress/theme/index.ts
Next Steps
Extending Default Theme
Build on top of the default theme instead
Runtime API
Explore available composables and utilities