/
tabs.js
52 lines (48 loc) · 1.28 KB
/
tabs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const tabsPanelTemplate = document.createElement( "template" );
tabsPanelTemplate.innerHTML = `
<style>
.tabs{
display: flex;
justify-content: flex-start;
border-bottom: 2px solid;
}
::slotted([slot="tab"]) {
padding: 1ex;
margin: 1ex;
border: none;
}
::slotted([slot="tab"].active) {
border: solid 3px;
}
::slotted([slot="content"]){
display: none;
}
::slotted([slot="content"].active){
display: block;
}
</style>
<slot name="tab" class="tabs"></slot>
<slot name="content"></slot>
`;
class TabsPanel extends HTMLElement {
constructor() {
super();
this.attachShadow( {mode:"open"} );
this.shadowRoot.appendChild( tabsPanelTemplate.content.cloneNode(true) );
}
connectedCallback() {
this.querySelectorAll('[slot="tab"]').forEach( tab => {
tab.addEventListener( "click", e => {
this.querySelectorAll("[slot]").forEach( tab => tab.classList.remove( "active" ) );
tab.classList.add( "active" );
this.querySelector( tab.dataset.target ).classList.add( "active" );
})
});
}
disconnectedCallback() {
this.querySelectorAll('[slot="tab"]').forEach( tab => {
tab.removeEventListener();
});
}
};
window.customElements.define( "tabs-panel", TabsPanel );