1
1
<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"
16
10
>
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 >
21
28
</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 >
28
30
</template >
29
31
30
32
<script >
@@ -38,31 +40,44 @@ export default {
38
40
},
39
41
watch: {
40
42
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)
45
44
}
46
45
},
47
46
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 ()
62
48
},
63
49
methods: {
64
50
changeCodeTab (index ) {
65
51
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
+ }
66
81
}
67
82
}
68
83
}
0 commit comments