Skip to content

Commit 51277f8

Browse files
authoredFeb 10, 2021
fix($core): component CodeGroup loads correctly on clientfix #2711 (#2794)
* fix($core): wrap code group in ClientOnly * fix($core): component CodeGroup loads correctly on client * fix($core): component CodeGroup loads correctly on client * fix($core): activate codetabs whenever we update the arr
1 parent 50388d9 commit 51277f8

File tree

2 files changed

+62
-42
lines changed

2 files changed

+62
-42
lines changed
 

‎packages/@vuepress/theme-default/global-components/CodeBlock.vue

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default {
1919
type: Boolean,
2020
default: false
2121
}
22+
},
23+
mounted () {
24+
if (this.$parent && this.$parent.loadTabs) {
25+
this.$parent.loadTabs()
26+
}
2227
}
2328
}
2429
</script>

‎packages/@vuepress/theme-default/global-components/CodeGroup.vue

+57-42
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<template>
2-
<div class="theme-code-group">
3-
<div class="theme-code-group__nav">
4-
<ul class="theme-code-group__ul">
5-
<li
6-
v-for="(tab, i) in codeTabs"
7-
:key="tab.title"
8-
class="theme-code-group__li"
9-
>
10-
<button
11-
class="theme-code-group__nav-tab"
12-
:class="{
13-
'theme-code-group__nav-tab-active': i === activeCodeTabIndex,
14-
}"
15-
@click="changeCodeTab(i)"
2+
<ClientOnly>
3+
<div class="theme-code-group">
4+
<div class="theme-code-group__nav">
5+
<ul class="theme-code-group__ul">
6+
<li
7+
v-for="(tab, i) in codeTabs"
8+
:key="tab.title"
9+
class="theme-code-group__li"
1610
>
17-
{{ tab.title }}
18-
</button>
19-
</li>
20-
</ul>
11+
<button
12+
class="theme-code-group__nav-tab"
13+
:class="{
14+
'theme-code-group__nav-tab-active': i === activeCodeTabIndex,
15+
}"
16+
@click="changeCodeTab(i)"
17+
>
18+
{{ tab.title }}
19+
</button>
20+
</li>
21+
</ul>
22+
</div>
23+
<slot />
24+
<pre
25+
v-if="codeTabs.length < 1"
26+
class="pre-blank"
27+
>// Make sure to add code blocks to your code group</pre>
2128
</div>
22-
<slot />
23-
<pre
24-
v-if="codeTabs.length < 1"
25-
class="pre-blank"
26-
>// Make sure to add code blocks to your code group</pre>
27-
</div>
29+
</ClientOnly>
2830
</template>
2931

3032
<script>
@@ -38,31 +40,44 @@ export default {
3840
},
3941
watch: {
4042
activeCodeTabIndex (index) {
41-
this.codeTabs.forEach(tab => {
42-
tab.elm.classList.remove('theme-code-block__active')
43-
})
44-
this.codeTabs[index].elm.classList.add('theme-code-block__active')
43+
this.activateCodeTab(index)
4544
}
4645
},
4746
mounted () {
48-
this.codeTabs = (this.$slots.default || []).filter(slot => Boolean(slot.componentOptions)).map((slot, index) => {
49-
if (slot.componentOptions.propsData.active === '') {
50-
this.activeCodeTabIndex = index
51-
}
52-
53-
return {
54-
title: slot.componentOptions.propsData.title,
55-
elm: slot.elm
56-
}
57-
})
58-
59-
if (this.activeCodeTabIndex === -1 && this.codeTabs.length > 0) {
60-
this.activeCodeTabIndex = 0
61-
}
47+
this.loadTabs()
6248
},
6349
methods: {
6450
changeCodeTab (index) {
6551
this.activeCodeTabIndex = index
52+
},
53+
loadTabs () {
54+
this.codeTabs = (this.$slots.default || []).filter(slot => Boolean(slot.componentOptions)).map((slot, index) => {
55+
if (slot.componentOptions.propsData.active === '') {
56+
this.activeCodeTabIndex = index
57+
}
58+
59+
return {
60+
title: slot.componentOptions.propsData.title,
61+
elm: slot.elm
62+
}
63+
})
64+
65+
if (this.activeCodeTabIndex === -1 && this.codeTabs.length > 0) {
66+
this.activeCodeTabIndex = 0
67+
}
68+
69+
this.activateCodeTab(0)
70+
},
71+
activateCodeTab (index) {
72+
this.codeTabs.forEach(tab => {
73+
if (tab.elm) {
74+
tab.elm.classList.remove('theme-code-block__active')
75+
}
76+
})
77+
78+
if (this.codeTabs[index].elm) {
79+
this.codeTabs[index].elm.classList.add('theme-code-block__active')
80+
}
6681
}
6782
}
6883
}

0 commit comments

Comments
 (0)