refactor: commit btn list
This commit is contained in:
parent
df99718d74
commit
a296651c6f
1 changed files with 7 additions and 4 deletions
11
src/btn.ts
11
src/btn.ts
|
@ -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 })
|
||||||
|
|
Loading…
Reference in a new issue