Separated tab-switcher into a reusable component. This depends on JSX addition

This commit is contained in:
Henry Jameson 2018-08-27 22:22:25 +03:00
parent b4cc1e020b
commit eacbd9b500
4 changed files with 215 additions and 126 deletions

View file

@ -0,0 +1,44 @@
import Vue from 'vue'
import './tab_switcher.scss'
export default Vue.component('tab-switcher', {
name: 'TabSwitcher',
data () {
return {
active: 0
}
},
methods: {
activateTab(index) {
return () => this.active = index;
}
},
render(h) {
const tabs = this.$slots.default
.filter(slot => slot.data)
.map((slot, index) => {
const classes = ['tab']
if (index === this.active) {
classes.push('active')
}
return (<button onClick={this.activateTab(index)} class={ classes.join(' ') }>{slot.data.attrs.title}</button>)
});
const contents = (
<div>
{this.$slots.default.filter(slot => slot.data)[this.active]}
</div>
);
return (
<div class="tab-switcher">
<div class="tabs">
{tabs}
</div>
<div class="contents">
{contents}
</div>
</div>
)
}
})

View file

@ -0,0 +1,47 @@
@import '../../_variables.scss';
.tab-switcher {
.tabs {
display: flex;
position: relative;
justify-content: center;
width: 100%;
overflow: hidden;
padding-top: 5px;
&::after, &::before {
display: block;
content: '';
flex: 1 1 auto;
}
.tab, &::after, &::before {
border-bottom: 1px solid;
border-bottom-color: $fallback--btn;
border-bottom-color: var(--btn, $fallback--btn);
}
.tab {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
padding: .3em 1em;
&::-moz-focus-inner {
border: none;
}
&:not(.active) {
border-bottom: 1px solid;
border-bottom-color: $fallback--btn;
border-bottom-color: var(--btn, $fallback--btn);
z-index: 4;
}
&.active {
background: transparent;
border-bottom: none;
z-index: 5;
}
}
}
}