mirror of https://github.com/vuejs/vitepress
parent
7ceaf344d2
commit
e1fe1ded40
@ -0,0 +1,32 @@
|
|||||||
|
<template>
|
||||||
|
<div v-show="!_internal ? true : active">
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script lang="ts">
|
||||||
|
import { defineComponent, PropType } from "vue";
|
||||||
|
|
||||||
|
export default defineComponent({
|
||||||
|
props: {
|
||||||
|
title: {
|
||||||
|
type: String,
|
||||||
|
required: true
|
||||||
|
} as PropType<string>,
|
||||||
|
active: {
|
||||||
|
type: [Boolean, String],
|
||||||
|
},
|
||||||
|
_internal: {
|
||||||
|
type: Boolean,
|
||||||
|
default() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
name: "CodeBlock"
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
|
||||||
|
</style>
|
@ -0,0 +1,70 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { defineComponent, h } from "vue";
|
||||||
|
import { CodeGroupTabState } from "/@theme/components/global/types";
|
||||||
|
import CodeBlock from "/@theme/components/global/CodeBlock.vue";
|
||||||
|
|
||||||
|
export default defineComponent({
|
||||||
|
name: "CodeGroup",
|
||||||
|
components: { CodeBlock },
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
tabs: [] as CodeGroupTabState[],
|
||||||
|
activeTabIndex: -1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
mounted() {
|
||||||
|
let activeTabIndex = -1;
|
||||||
|
const tabs: CodeGroupTabState[] = [];
|
||||||
|
|
||||||
|
const blocks = (this.$slots.default?.() || []).filter(s => s.type.name === 'CodeBlock');
|
||||||
|
|
||||||
|
blocks.forEach(({ props: { title, active = false }, children }, index) => {
|
||||||
|
if (activeTabIndex === -1 && typeof active === 'string') {
|
||||||
|
activeTabIndex = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
tabs.push({
|
||||||
|
index: index,
|
||||||
|
title: title,
|
||||||
|
content: children?.length
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
this.tabs = tabs
|
||||||
|
this.activeTabIndex = activeTabIndex
|
||||||
|
console.log(this)
|
||||||
|
},
|
||||||
|
render() {
|
||||||
|
const hCodeBlocks = this.tabs.map(
|
||||||
|
tab => h(
|
||||||
|
CodeBlock,
|
||||||
|
{
|
||||||
|
active: this.activeTabIndex === tab.index,
|
||||||
|
title: tab.title,
|
||||||
|
_internal: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
default: (props) => {
|
||||||
|
const children = this.$slots.default ? this.$slots.default(props) : []
|
||||||
|
|
||||||
|
return children[tab.index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
const hTabNav = h('div', { class: 'code-group__nav' },
|
||||||
|
this.tabs.map(tab =>
|
||||||
|
h(
|
||||||
|
'button',
|
||||||
|
{
|
||||||
|
class: `code-group__nav-button ${this.activeTabIndex === tab.index ? 'code-group__nav-button-active' : ''}`,
|
||||||
|
onClick: () => (this.activeTabIndex = tab.index)
|
||||||
|
},
|
||||||
|
tab.title
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return h('div', { class: 'code-group' }, [hTabNav, ...hCodeBlocks])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
@ -0,0 +1,5 @@
|
|||||||
|
export interface CodeGroupTabState {
|
||||||
|
title: string
|
||||||
|
index: number
|
||||||
|
content: string
|
||||||
|
}
|
Loading…
Reference in new issue