|
| 1 | +# Making a Theme Extendable |
| 2 | + |
| 3 | +Sometimes users might want make some minor changes to a theme, but they don't want to fork and modify the entire project. |
| 4 | + |
| 5 | +With the help of [Theme API](../../reference/theme-api.md), you can make your theme extendable, allowing users to make their own modifications easily. |
| 6 | + |
| 7 | +You must have known that how to [extend default theme](../../reference/default-theme/extending.md). Here we'll introduce how to make your own theme extendable like default theme. |
| 8 | + |
| 9 | +## Layout Slots |
| 10 | + |
| 11 | +This approach requires you to determine which parts of your theme could be extended. It is more suitable for those common customizations like page footer or header. |
| 12 | + |
| 13 | +You just need to provide slots in your layouts, and tell users how to make use of them: |
| 14 | + |
| 15 | +```vue |
| 16 | +<template> |
| 17 | + <div class="my-theme-layout"> |
| 18 | + <slot name="page-header" /> |
| 19 | + <Content /> |
| 20 | + <slot name="page-footer" /> |
| 21 | + </div> |
| 22 | +</template> |
| 23 | +``` |
| 24 | + |
| 25 | +## Component Aliases |
| 26 | + |
| 27 | +This approach requires you to consider which components of your theme should be replaceable, and you also need to split components into a suitable granularity. |
| 28 | + |
| 29 | +First, set `alias` for replaceable components of you theme: |
| 30 | + |
| 31 | +```js |
| 32 | +module.exports = { |
| 33 | + name: 'vuepress-theme-foo', |
| 34 | + alias: { |
| 35 | + // set alias for replaceable components |
| 36 | + '@theme/Navbar.vue': path.resolve(__dirname, 'components/Navbar.vue'), |
| 37 | + '@theme/Sidebar.vue': path.resolve(__dirname, 'components/Sidebar.vue'), |
| 38 | + }, |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +Next, use those components via aliases in your theme: |
| 43 | + |
| 44 | +```vue |
| 45 | +<script setup lang="ts"> |
| 46 | +import Navbar from '@theme/Navbar.vue' |
| 47 | +import Sidebar from '@theme/Sidebar.vue' |
| 48 | +</script> |
| 49 | +
|
| 50 | +<template> |
| 51 | + <div class="my-theme-layout"> |
| 52 | + <Navbar /> |
| 53 | + <Sidebar /> |
| 54 | + <Content /> |
| 55 | + </div> |
| 56 | +</template> |
| 57 | +``` |
| 58 | + |
| 59 | +Then, users can replace specific components by overriding the `alias` when extending or using your theme. |
0 commit comments