fix: btn position when logged in
This commit is contained in:
parent
ae08ff847a
commit
9c05dd5d3d
1 changed files with 20 additions and 7 deletions
27
src/btn.ts
27
src/btn.ts
|
@ -58,6 +58,21 @@ function getScrollParent (node: HTMLElement): HTMLElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onPageRendered (getEl: () => HTMLElement) {
|
||||||
|
return new Promise<HTMLElement>((resolve) => {
|
||||||
|
const observer = new MutationObserver(() => {
|
||||||
|
try {
|
||||||
|
const el = getEl()
|
||||||
|
if (el) {
|
||||||
|
observer.disconnect()
|
||||||
|
resolve(el)
|
||||||
|
}
|
||||||
|
} catch { }
|
||||||
|
})
|
||||||
|
observer.observe(document.querySelector('div > section') ?? document.body, { childList: true, subtree: true })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
interface BtnOptions {
|
interface BtnOptions {
|
||||||
readonly name: string;
|
readonly name: string;
|
||||||
readonly action: BtnAction;
|
readonly action: BtnAction;
|
||||||
|
@ -141,8 +156,10 @@ export class BtnList {
|
||||||
newParent.append(...this.list.map(e => cloneBtn(e)))
|
newParent.append(...this.list.map(e => cloneBtn(e)))
|
||||||
shadow.append(newParent)
|
shadow.append(newParent)
|
||||||
|
|
||||||
try {
|
// default position
|
||||||
const anchorDiv = this.getBtnParent()
|
newParent.style.top = `${window.innerHeight - newParent.getBoundingClientRect().height}px`
|
||||||
|
|
||||||
|
void onPageRendered(this.getBtnParent).then((anchorDiv: HTMLDivElement) => {
|
||||||
const pos = () => this._positionBtns(anchorDiv, newParent)
|
const pos = () => this._positionBtns(anchorDiv, newParent)
|
||||||
pos()
|
pos()
|
||||||
|
|
||||||
|
@ -152,11 +169,7 @@ export class BtnList {
|
||||||
// reposition btns when scrolling
|
// reposition btns when scrolling
|
||||||
const scroll = getScrollParent(anchorDiv)
|
const scroll = getScrollParent(anchorDiv)
|
||||||
scroll.addEventListener('scroll', pos, { passive: true })
|
scroll.addEventListener('scroll', pos, { passive: true })
|
||||||
} catch (err) {
|
})
|
||||||
console.error(err)
|
|
||||||
// default position
|
|
||||||
newParent.style.top = `${window.innerHeight - newParent.getBoundingClientRect().height}px`
|
|
||||||
}
|
|
||||||
|
|
||||||
return btnParent
|
return btnParent
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue