未验证 提交 394c4f66 编写于 作者: D Derek Pollard 提交者: GitHub

feat($theme-default): add code group and code block components (#2594)

上级 46189131
<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>
<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>
......@@ -13,10 +13,19 @@ The fastest way to get your VuePress project setup is to use our [create-vuepres
To use it, open up your terminal in the desired directory and run the following command:
<code-group>
<code-block title="YARN" active>
```bash
yarn create vuepress-site [optionalDirectoryName]
# OR npx create-vuepress-site [optionalDirectoryName]
```
</code-block>
<code-block title="NPM">
```bash
npx create-vuepress-site [optionalDirectoryName]
```
</code-block>
</code-group>
You will then have the opportunity to configure your VuePress site’s metadata such as:
......@@ -40,15 +49,35 @@ This section will help you build a basic VuePress documentation site from ground
2. Initialize with your preferred package manager
<code-group>
<code-block title="YARN" active>
```bash
yarn init
```
</code-block>
<code-block title="NPM">
```bash
yarn init # npm init
npm init
```
</code-block>
</code-group>
3. Install VuePress locally
<code-group>
<code-block title="YARN" active>
```bash
yarn add -D vuepress
```
</code-block>
<code-block title="NPM">
```bash
yarn add -D vuepress # npm install -D vuepress
npm install -D vuepress
```
</code-block>
</code-group>
4. Create your first document
......@@ -71,9 +100,19 @@ This section will help you build a basic VuePress documentation site from ground
6. Serve the documentation site in the local server
<code-group>
<code-block title="YARN" active>
```bash
yarn docs:dev
```
</code-block>
<code-block title="NPM">
```bash
yarn docs:dev # npm run docs:dev
npm run docs:dev
```
</code-block>
</code-group>
VuePress will start a hot-reloading development server at [http://localhost:8080](http://localhost:8080).
......
......@@ -559,7 +559,7 @@ This will render `.vuepress/components/SpecialLayout.vue` for the given page.
## Ejecting
You can copy the default theme source code into `.vuepress/theme` to fully customize the theme using the `vuepress eject [targetDir]` command. If you didn't install Vuepress globally, run `./node_modules/.bin/vuepress eject`.
You can copy the default theme source code into `.vuepress/theme` to fully customize the theme using the `vuepress eject [targetDir]` command. If you didn’t install VuePress globally, run `./node_modules/.bin/vuepress eject`.
::: warning
Once you eject, you are on your own and **won’t** be receiving future updates or bugfixes to the default theme even if you upgrade VuePress.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册