//switch const smSwitch = document.createElement('template') smSwitch.innerHTML = ` ` customElements.define('sm-switch', class extends HTMLElement { constructor() { super() this.attachShadow({ mode: 'open' }).append(smSwitch.content.cloneNode(true)) this.switch = this.shadowRoot.querySelector('.switch'); this.input = this.shadowRoot.querySelector('input') this.isChecked = false this.isDisabled = false this.dispatch = this.dispatch.bind(this) } static get observedAttributes() { return ['disabled', 'checked'] } get disabled() { return this.isDisabled } set disabled(val) { if (val) { this.setAttribute('disabled', '') } else { this.removeAttribute('disabled') } } get checked() { return this.isChecked } set checked(value) { if (value) { this.setAttribute('checked', '') } else { this.removeAttribute('checked') } } get value() { return this.isChecked } reset() { } dispatch() { this.dispatchEvent(new CustomEvent('change', { bubbles: true, composed: true, detail: { value: this.isChecked } })) } connectedCallback() { this.addEventListener('keydown', e => { if (e.key === ' ' && !this.isDisabled) { e.preventDefault() this.input.click() } }) this.input.addEventListener('click', e => { if (this.input.checked) this.checked = true else this.checked = false this.dispatch() }) } attributeChangedCallback(name, oldValue, newValue) { if (oldValue !== newValue) { if (name === 'disabled') { if (this.hasAttribute('disabled')) { this.disabled = true } else { this.disabled = false } } else if (name === 'checked') { if (this.hasAttribute('checked')) { this.isChecked = true this.input.checked = true } else { this.isChecked = false this.input.checked = false } } } } })