Skip to content

Commit f480bb2

Browse files
committed
feat(theme-default): make all non-global components replaceable
1 parent 2defa32 commit f480bb2

File tree

25 files changed

+560
-477
lines changed

25 files changed

+560
-477
lines changed

docs/.vuepress/configs/navbar/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export const en: NavbarConfig = [
4040
'/reference/default-theme/components.md',
4141
'/reference/default-theme/markdown.md',
4242
'/reference/default-theme/styles.md',
43+
'/reference/default-theme/extending.md',
4344
],
4445
},
4546
],

docs/.vuepress/configs/navbar/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const zh: NavbarConfig = [
3737
'/zh/reference/default-theme/components.md',
3838
'/zh/reference/default-theme/markdown.md',
3939
'/zh/reference/default-theme/styles.md',
40+
'/zh/reference/default-theme/extending.md',
4041
],
4142
},
4243
],

docs/.vuepress/configs/sidebar/en.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const en: SidebarConfig = {
3535
'/advanced/cookbook/README.md',
3636
'/advanced/cookbook/usage-of-client-app-enhance.md',
3737
'/advanced/cookbook/adding-extra-pages.md',
38-
'/advanced/cookbook/extending-a-theme.md',
38+
'/advanced/cookbook/making-a-theme-extendable.md',
3939
'/advanced/cookbook/passing-data-to-client-code.md',
4040
'/advanced/cookbook/markdown-and-vue-sfc.md',
4141
],
@@ -70,6 +70,7 @@ export const en: SidebarConfig = {
7070
'/reference/default-theme/components.md',
7171
'/reference/default-theme/markdown.md',
7272
'/reference/default-theme/styles.md',
73+
'/reference/default-theme/extending.md',
7374
],
7475
},
7576
{

docs/.vuepress/configs/sidebar/zh.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const zh: SidebarConfig = {
3535
'/zh/advanced/cookbook/README.md',
3636
'/zh/advanced/cookbook/usage-of-client-app-enhance.md',
3737
'/zh/advanced/cookbook/adding-extra-pages.md',
38-
'/zh/advanced/cookbook/extending-a-theme.md',
38+
'/zh/advanced/cookbook/making-a-theme-extendable.md',
3939
'/zh/advanced/cookbook/passing-data-to-client-code.md',
4040
'/zh/advanced/cookbook/markdown-and-vue-sfc.md',
4141
],
@@ -73,6 +73,7 @@ export const zh: SidebarConfig = {
7373
'/zh/reference/default-theme/components.md',
7474
'/zh/reference/default-theme/markdown.md',
7575
'/zh/reference/default-theme/styles.md',
76+
'/zh/reference/default-theme/extending.md',
7677
],
7778
},
7879
{

docs/advanced/cookbook/extending-a-theme.md

Lines changed: 0 additions & 222 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.

docs/guide/migration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ Renamed to `extends`.
221221

222222
You can still inherit a parent theme with `extends: 'parent-theme'`, which will extends the plugins, layouts, etc.
223223

224-
The `@theme` and `@parent-theme` aliases are not available now.
224+
The `@theme` and `@parent-theme` aliases are removed by default, but you can still replace components in default theme with similar approach.
225225

226-
Now multi-level theme inheritance is supported.
226+
You can refer to [Default Theme > Extending](../reference/default-theme/extending.md) for how to extend default theme.
227227

228228
### CLI Change
229229

0 commit comments

Comments
 (0)