/**
* Horizontal Scroll Nav Component
* Pill-style category buttons in a horizontal scroller
*/
class HorizontalScrollNav extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.categories = [
{ id: "all", label: "All", active: true },
{ id: "fiction", label: "Fiction", active: false },
{ id: "non-fiction", label: "Non-Fiction", active: false },
{ id: "mystery", label: "Mystery", active: false },
{ id: "romance", label: "Romance", active: false },
{ id: "sci-fi", label: "Sci-Fi", active: false },
{ id: "biography", label: "Biography", active: false },
{ id: "history", label: "History", active: false },
];
}
connectedCallback() {
this.render();
this.addEventListeners();
}
addEventListeners() {
const buttons = this.shadowRoot.querySelectorAll(".nav-pill");
buttons.forEach((button) => {
button.addEventListener("click", (e) => {
this.setActiveCategory(e.target.dataset.id);
});
});
}
setActiveCategory(id) {
this.categories = this.categories.map((cat) => ({
...cat,
active: cat.id === id,
}));
this.render();
this.addEventListeners();
// Dispatch custom event for parent components
this.dispatchEvent(
new CustomEvent("category-change", {
detail: { categoryId: id },
bubbles: true,
composed: true,
})
);
}
render() {
this.shadowRoot.innerHTML = `
`;
}
}
customElements.define("horizontal-scroll-nav", HorizontalScrollNav);