refactor: commit btn list

This commit is contained in:
Xmader 2020-11-10 13:51:48 -05:00
parent df99718d74
commit a296651c6f
No known key found for this signature in database
GPG key ID: A20B97FB9EB730E4

View file

@ -73,7 +73,7 @@ export class BtnList {
return btn return btn
} }
private _commit (): Element { private _commit () {
const parent = this.getTemplateBtn().parentElement as HTMLDivElement const parent = this.getTemplateBtn().parentElement as HTMLDivElement
const shadow = parent.attachShadow({ mode: 'closed' }) const shadow = parent.attachShadow({ mode: 'closed' })
@ -87,20 +87,23 @@ export class BtnList {
newParent.append(...this.list) newParent.append(...this.list)
shadow.append(newParent) shadow.append(newParent)
return parent return {
parent,
shadowRoot: shadow,
}
} }
/** /**
* replace the template button with the list of new buttons * replace the template button with the list of new buttons
*/ */
commit (): void { commit (): void {
let el = this._commit() let el: Element = this._commit().parent
const observer = new MutationObserver(() => { const observer = new MutationObserver(() => {
// check if the buttons are still in document when dom updates // check if the buttons are still in document when dom updates
if (!document.contains(el)) { if (!document.contains(el)) {
// re-commit // re-commit
// performance issue? // performance issue?
el = this._commit() el = this._commit().parent
} }
}) })
observer.observe(document, { childList: true, subtree: true }) observer.observe(document, { childList: true, subtree: true })