From bb39ebccc9001bde0c932fd432680379c1c613ee Mon Sep 17 00:00:00 2001 From: Xmader Date: Thu, 31 Dec 2020 13:13:26 -0500 Subject: [PATCH] fix: btn position when window resizes --- src/btn.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/btn.ts b/src/btn.ts index bea4890..bcb603c 100644 --- a/src/btn.ts +++ b/src/btn.ts @@ -104,6 +104,16 @@ export class BtnList { return btnTpl } + private _positionBtns (newParent: HTMLDivElement) { + try { + const anchorDiv = this.getBtnParent() + const { top } = anchorDiv.getBoundingClientRect() + newParent.style.top = `${top}px` + } catch (err) { + console.error(err) + } + } + private _commit () { const btnParent = document.querySelector('div') as HTMLDivElement const shadow = attachShadow(btnParent) @@ -121,13 +131,10 @@ export class BtnList { newParent.append(...this.list.map(e => cloneBtn(e))) shadow.append(newParent) - try { - const anchorDiv = this.getBtnParent() - const { top } = anchorDiv.getBoundingClientRect() - newParent.style.top = `${top}px` - } catch (err) { - console.error(err) - } + const pos = () => this._positionBtns(newParent) + pos() + document.addEventListener('readystatechange', pos) + window.addEventListener('resize', pos) return btnParent }