import {OOElement} from '../oo-element' import {html} from '../../lib/html' import customEvent from '../../lib/custom-event' const ATTR = { DATA_OPEN: 'data-open' } const EVENT = { CLOSE: customEvent('close') } const asBoolean = (data: string): boolean => { switch(data) { case 'enabled': return true case 'disabled': return false default: return false } } const open: WeakMap = new WeakMap() export default class extends OOElement { static get observedAttributes() { return [ATTR.DATA_OPEN] } constructor() { super() open.set(this, asBoolean(this.getAttribute(ATTR.DATA_OPEN))) } attributeChangedCallback(attr, prev, next) { if (prev === next) { return } if (open.get(this) && asBoolean(next) === false) { this.dispatchClose() } open.set(this, asBoolean(next)) if (this.connected) { this.update() } } render() { const state = open.get(this) return html`
` } onClickClose() { this.setAttribute(ATTR.DATA_OPEN, 'disabled') } dispatchClose() { this.dispatchEvent(EVENT.CLOSE) } }