Skip to content

Commit 5021c7e

Browse files
author
Mitko Georgiev
committed
Synchronized switching of all tabs
1 parent 96cf59b commit 5021c7e

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ __markdown content__
4747

4848
~~~
4949

50+
### With options
51+
If you have a lot of tabbed sections in your documentation you might want
52+
to see all of them switch together when one is changed.
53+
To enable this behavior, just set the `sync` option to `true`:
54+
55+
```js
56+
module.exports = {
57+
// Your remaining configuration ...
58+
plugins: [
59+
[
60+
'vuepress-plugin-element-tabs',
61+
{
62+
sync: true,
63+
},
64+
],
65+
],
66+
}
67+
```
68+
5069
## Documents
5170
> Accepted Value Like That
5271
~~~md

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const path = require('path')
22

33
module.exports = (options, context) => ({
4+
extendPageData($page) {
5+
$page.elementTabsOptions = options
6+
},
47
enhanceAppFiles: [
58
path.resolve(__dirname, './lib/client.js')
69
],
@@ -9,8 +12,8 @@ module.exports = (options, context) => ({
912
},
1013
chainMarkdown (config) {
1114
config
12-
.plugin('@superbiger/tabs')
13-
.use(require('./lib/markdownItPlugin'), [options])
14-
.end()
15+
.plugin('@superbiger/tabs')
16+
.use(require('./lib/markdownItPlugin'), [options])
17+
.end()
1518
}
1619
})

lib/components/Tabs.vue

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
},
88
props: {
99
type: String,
10-
activeName: String,
10+
activeName: {
11+
type: String,
12+
default () {
13+
if (typeof localStorage !== 'undefined') {
14+
return localStorage.getItem('vuepress-plugin-element-tabs@activeId');
15+
}
16+
}
17+
},
1118
closable: Boolean,
1219
addable: Boolean,
1320
value: {},
@@ -65,7 +72,15 @@
6572
handleTabClick(tab, tabName, event) {
6673
if (tab.disabled) return;
6774
this.setCurrentName(tabName);
75+
if (Array.every([
76+
this.$page.elementTabsOptions,
77+
this.$page.elementTabsOptions.sync,
78+
typeof localStorage !== 'undefined'
79+
])) {
80+
localStorage.setItem('vuepress-plugin-element-tabs@activeId', tabName);
81+
}
6882
this.$emit('tab-click', tab, event);
83+
this.$root.$emit('tab-change', tabName, tab, event);
6984
},
7085
handleTabRemove(pane, ev) {
7186
if (pane.disabled) return;
@@ -162,6 +177,13 @@
162177
},
163178
164179
created() {
180+
if (this.$page.elementTabsOptions.sync) {
181+
this.$root.$on('tab-change', (tabName, tab, event) => {
182+
this.setCurrentName(tabName)
183+
});
184+
} else if (typeof localStorage !== 'undefined') {
185+
localStorage.removeItem('vuepress-plugin-element-tabs@activeId')
186+
}
165187
if (!this.currentName) {
166188
this.setCurrentName('0');
167189
}

0 commit comments

Comments
 (0)