-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($theme-default): add code group and code block components (#2594)
- Loading branch information
Showing
4 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/@vuepress/theme-default/global-components/CodeBlock.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<template> | ||
<div | ||
class="theme-code-block" | ||
:class="{ 'theme-code-block__active': active }" | ||
> | ||
<slot /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'CodeBlock', | ||
props: { | ||
title: { | ||
type: String, | ||
required: true | ||
}, | ||
active: { | ||
type: Boolean, | ||
default: false | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.theme-code-block { | ||
display: none; | ||
} | ||
.theme-code-block__active { | ||
display: block; | ||
} | ||
.theme-code-block > pre { | ||
background-color: orange; | ||
} | ||
</style> |
90 changes: 90 additions & 0 deletions
90
packages/@vuepress/theme-default/global-components/CodeGroup.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<template> | ||
<div class="theme-code-group"> | ||
<div class="theme-code-group__nav"> | ||
<button | ||
v-for="(tab, i) in codeTabs" | ||
:key="tab.title" | ||
class="theme-code-group__nav-tab" | ||
:class="{'theme-code-group__nav-tab-active': i === activeCodeTabIndex}" | ||
@click="changeCodeTab(i)" | ||
> | ||
{{ tab.title }} | ||
</button> | ||
</div> | ||
<slot /> | ||
<pre | ||
v-if="codeTabs.length < 1" | ||
class="pre-blank" | ||
>// Make sure to add code blocks to your code group</pre> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'CodeGroup', | ||
data () { | ||
return { | ||
codeTabs: [], | ||
activeCodeTabIndex: -1 | ||
} | ||
}, | ||
watch: { | ||
activeCodeTabIndex (index) { | ||
this.codeTabs.forEach(tab => { | ||
tab.elm.classList.remove('theme-code-block__active') | ||
}) | ||
this.codeTabs[index].elm.classList.add('theme-code-block__active') | ||
} | ||
}, | ||
mounted () { | ||
this.codeTabs = (this.$slots.default || []).filter(slot => Boolean(slot.componentOptions)).map((slot, index) => { | ||
if (slot.componentOptions.propsData.active === '') { | ||
this.activeCodeTabIndex = index | ||
} | ||
return { | ||
title: slot.componentOptions.propsData.title, | ||
elm: slot.elm | ||
} | ||
}) | ||
if (this.activeCodeTabIndex === -1 && this.codeTabs.length > 0) { | ||
this.activeCodeTabIndex = 0 | ||
} | ||
}, | ||
methods: { | ||
changeCodeTab (index) { | ||
this.activeCodeTabIndex = index | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="stylus" scoped> | ||
.theme-code-group {} | ||
.theme-code-group__nav { | ||
margin-bottom: -35px; | ||
background-color: $codeBgColor; | ||
padding-bottom: 22px; | ||
border-top-left-radius: 6px; | ||
border-top-right-radius: 6px; | ||
padding-left: 10px; | ||
padding-top: 10px; | ||
} | ||
.theme-code-group__nav-tab { | ||
border: 0; | ||
padding: 5px; | ||
cursor: pointer; | ||
background-color: transparent; | ||
font-size: 0.85em; | ||
line-height: 1.4; | ||
color: rgba(255, 255, 255, 0.9); | ||
font-weight: 600; | ||
} | ||
.theme-code-group__nav-tab-active { | ||
border-bottom: #42b983 1px solid; | ||
} | ||
.pre-blank { | ||
color: #42b983; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters