-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo-toggle.js
More file actions
40 lines (36 loc) · 1.06 KB
/
info-toggle.js
File metadata and controls
40 lines (36 loc) · 1.06 KB
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
class InfoToggle extends HTMLElement {
constructor() {
super()
this._isVisible = false
this.attachShadow({ mode: 'open' })
this.shadowRoot.innerHTML = `
<style>
#info-box {
display: none;
}
</style>
<button>Show</button>
<p id="info-box">
<slot></slot>
</p>
`
this._toggleButton = this.shadowRoot.querySelector('button')
this._infoBox = this.shadowRoot.querySelector('#info-box')
this._toggleButton.addEventListener('click', this._toggleInfoBox.bind(this))
}
connectedCallback() {
if (this.hasAttribute('is-visible')) {
if (this.getAttribute('is-visible') === 'true') {
this._isVisible = true
this._infoBox.style.display = 'block'
this._toggleButton.textContent = 'Hide'
}
}
}
_toggleInfoBox() {
this._isVisible = !this._isVisible
this._infoBox.style.display = this._isVisible ? 'block' : 'none'
this._toggleButton.textContent = this._isVisible ? 'Hide' : 'Show'
}
}
customElements.define('uc-info-toggle', InfoToggle)