Files
probo/packages/cookie-banner/src/components/preference-panel.ts
Sacha Al Himdani 9ac71f948f Update contact email to hello@probo.com
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
2026-06-09 16:45:23 +02:00

92 lines
2.9 KiB
TypeScript

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
import { ProboElement } from "./base";
import type { ProboRootElement } from "./base";
import type { ProboCookieBannerRoot } from "./cookie-banner-root";
const REQUIRED_CHILDREN = [
"probo-category-list",
"probo-save-button",
] as const;
export class ProboPreferencePanel extends ProboElement {
private root: ProboRootElement | null = null;
private onStateChange = (e: Event): void => {
const { state } = (e as CustomEvent).detail;
this.hidden = state !== "panel";
if (state === "panel") {
(this.root as ProboCookieBannerRoot).resetDraft();
this.focusFirst();
}
};
connectedCallback(): void {
this.hidden = true;
this.root = this.findAncestor<ProboCookieBannerRoot>("probo-cookie-banner-root");
if (this.root) {
this.root.addEventListener("probo-state", this.onStateChange);
}
this.scheduleValidation(() => this.validate());
}
disconnectedCallback(): void {
if (this.root) {
this.root.removeEventListener("probo-state", this.onStateChange);
}
}
private validate(): void {
const missing: string[] = [];
for (const tag of REQUIRED_CHILDREN) {
if (!this.querySelector(tag)) {
missing.push(tag);
}
}
if (missing.length > 0) {
this.warn(`<probo-preference-panel> is missing required children: ${missing.join(", ")}`);
this.emitValidation(missing);
}
}
}
export class ProboSaveButton extends ProboElement {
private root: ProboRootElement | null = null;
connectedCallback(): void {
this.root = this.findAncestor<ProboCookieBannerRoot>("probo-cookie-banner-root");
this.addEventListener("click", this.handleClick);
}
disconnectedCallback(): void {
this.removeEventListener("click", this.handleClick);
}
private handleClick = (): void => {
if (!this.root) return;
const draft = { ...this.root.consentDraft };
this.root.client.customize(draft);
this.root.setState("hidden");
this.root.dispatchEvent(
new CustomEvent("probo-consent", {
bubbles: true,
composed: true,
detail: { action: "CUSTOMIZE", consent_data: draft },
}),
);
};
}