import { html } from "../util.js";
import buttonCSS from "../buttonStyles.js";
class PxmeSearch extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this._query = "";
this.shadowRoot.innerHTML = html`
`;
}
connectedCallback() {
const toggle = this.shadowRoot.querySelector(".pxme-search__toggle");
const panel = this.shadowRoot.querySelector("#panel");
const input = this.shadowRoot.querySelector("#search-input");
const resetBtn = this.shadowRoot.querySelector("#reset-btn");
toggle.addEventListener("click", () => {
const isOpen = panel.classList.toggle("pxme-search__panel--open");
toggle.classList.toggle("pxme-search__toggle--active", isOpen);
if (isOpen) {
input.focus();
}
});
// Search on Enter key
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
this._query = input.value.trim();
this._emitSearchChange();
}
});
// Reset button clears the search
resetBtn.addEventListener("click", () => {
input.value = "";
this._query = "";
this._emitSearchChange();
});
}
_emitSearchChange() {
this.dispatchEvent(
new CustomEvent("search-change", {
detail: {
search: this._query,
},
bubbles: true,
composed: true,
})
);
}
}
customElements.define("pxme-search", PxmeSearch);