Created
May 19, 2017 19:58
-
-
Save amfischer/6b5b2366ac834d7b31acae8cdda9af72 to your computer and use it in GitHub Desktop.
Vue Tab Example
This file contains hidden or 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
Vue.component('tabs', { | |
template: ` | |
<div> | |
<div class="tabs"> | |
<ul> | |
<li v-for="tab in tabs" :class="{ 'is-active': tab.isActive }"> | |
<a :href="tab.href" @click="isSelected(tab)">{{ tab.name }}</a> | |
</li> | |
</ul> | |
</div> | |
<div class="tabs-details"> | |
<slot></slot> | |
</div> | |
</div> | |
`, | |
data() { | |
return { | |
tabs: [] | |
} | |
}, | |
created() { | |
this.tabs = this.$children; | |
}, | |
methods: { | |
isSelected(selectedTab) { | |
this.tabs.forEach(tab => { | |
tab.isActive = (tab.name == selectedTab.name) | |
}); | |
} | |
} | |
}); | |
Vue.component('tab', { | |
template: ` | |
<div v-if="isActive"><slot></slot></div> | |
`, | |
props: { | |
name: { required: true }, | |
selected: { default: false } | |
}, | |
data() { | |
return { | |
isActive: false | |
}; | |
}, | |
computed: { | |
href() { | |
return '#' + this.name.toLowerCase().replace(/ /g, '-'); | |
} | |
}, | |
mounted() { | |
this.isActive = this.selected; | |
} | |
}); | |
new Vue({ | |
el: '#root' | |
}); |
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.4.1/css/bulma.css"> | |
<style> | |
body { padding-top: 40px; } | |
</style> | |
</head> | |
<body> | |
<div id="root" class="container"> | |
<tabs> | |
<tab name="About us" :selected="true"> | |
<h1>Here is the content for the about us tab</h1> | |
</tab> | |
<tab name="About our culture"> | |
<h1>Here is the content for the about our culture tab</h1> | |
</tab> | |
<tab name="About our vision"> | |
<h1>Here is the content for the about our vision tab</h1> | |
</tab> | |
</tabs> | |
</div> | |
<script src="https://unpkg.com/[email protected]"></script> | |
<script src="main.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment