UNPKG

2.78 kBPlain TextView Raw
1<template>
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"
10 >
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>
28 </div>
29 </ClientOnly>
30</template>
31
32<script>
33export default {
34 name: 'CodeGroup',
35 data () {
36 return {
37 codeTabs: [],
38 activeCodeTabIndex: -1
39 }
40 },
41 watch: {
42 activeCodeTabIndex (index) {
43 this.activateCodeTab(index)
44 }
45 },
46 mounted () {
47 this.loadTabs()
48 },
49 methods: {
50 changeCodeTab (index) {
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 }
81 }
82 }
83}
84</script>
85
86<style lang="stylus" scoped>
87 .theme-code-group {}
88 .theme-code-group__nav {
89 margin-bottom: -35px;
90 background-color: $codeBgColor;
91 padding-bottom: 22px;
92 border-top-left-radius: 6px;
93 border-top-right-radius: 6px;
94 padding-left: 10px;
95 padding-top: 10px;
96 }
97 .theme-code-group__ul {
98 margin: auto 0;
99 padding-left: 0;
100 display: inline-flex;
101 list-style: none;
102 }
103 .theme-code-group__li {}
104 .theme-code-group__nav-tab {
105 border: 0;
106 padding: 5px;
107 cursor: pointer;
108 background-color: transparent;
109 font-size: 0.85em;
110 line-height: 1.4;
111 color: rgba(255, 255, 255, 0.9);
112 font-weight: 600;
113 }
114 .theme-code-group__nav-tab-active {
115 border-bottom: #42b983 1px solid;
116 }
117 .pre-blank {
118 color: #42b983;
119 }
120</style>