export class ScrollButton extends HTMLElement {
constructor() {
super();
this.handleScroll = this.handleScroll.bind(this);
this.scrollToTop = this.scrollToTop.bind(this);
}
connectedCallback() {
// Insert Tailwind-styled button in light DOM
this.innerHTML = `
`;
this._button = this.querySelector(".scroll-to-top");
window.addEventListener("scroll", this.handleScroll);
this._button.addEventListener("click", this.scrollToTop);
}
disconnectedCallback() {
window.removeEventListener("scroll", this.handleScroll);
this._button.removeEventListener("click", this.scrollToTop);
}
handleScroll() {
const scrollTop = window.scrollY || document.documentElement.scrollTop;
if (scrollTop > 300) {
this._button.classList.remove("hidden");
} else {
this._button.classList.add("hidden");
}
}
scrollToTop() {
window.scrollTo({ top: 0, behavior: "smooth" });
}
}