Files
catalog/ts_web/elements/consentsoftware-cookieconsent.ts

320 lines
10 KiB
TypeScript

import { cssManager, colors } from './shared.js';
import {
DeesElement,
customElement,
html,
css,
type TemplateResult,
property,
} from '@design.estate/dees-element';
import * as csInterfaces from '@consent.software/interfaces';
import * as csWebclient from '@consent.software/webclient';
import { delayFor } from '@push.rocks/smartdelay';
declare global {
interface HTMLElementTagNameMap {
'consentsoftware-cookieconsent': ConsentsoftwareCookieconsent;
}
}
@customElement('consentsoftware-cookieconsent')
export class ConsentsoftwareCookieconsent extends DeesElement {
public static demo = () => html`<consentsoftware-cookieconsent></consentsoftware-cookieconsent>`;
public csWebclientInstance = new csWebclient.CsWebclient();
public csWebclientRan = false;
public static styles = [
cssManager.defaultStyles,
css`
:host {
font-size: 14px;
user-select: none;
}
.pageOverlay {
position: fixed;
inset: 0;
display: grid;
align-items: center;
justify-content: center;
z-index: 1000;
background: rgba(0, 0, 0, 0);
backdrop-filter: blur(0px);
transition: all 0.2s ease-out;
}
.pageOverlay.shake {
background: rgba(0, 0, 0, 0.6) !important;
}
.modalBox {
display: block;
color: ${cssManager.bdTheme(colors.light.foreground, colors.dark.foreground)};
background: ${cssManager.bdTheme(colors.light.background, colors.dark.background)};
box-shadow:
0 0 0 1px ${cssManager.bdTheme(colors.light.border, colors.dark.border)},
0 16px 70px ${cssManager.bdTheme('rgba(0, 0, 0, 0.15)', 'rgba(0, 0, 0, 0.35)')};
position: relative;
border-radius: 8px;
max-width: 520px;
min-width: 320px;
box-sizing: border-box;
overflow: hidden;
will-change: transform;
transition: all 0.2s ease-out;
transform: scale(0.96);
opacity: 0;
}
@media (max-width: 560px) {
.modalBox {
max-width: 100%;
min-width: 100%;
height: 100vh;
border-radius: 0;
}
}
.modalBox.shake {
animation: shake 120ms 2 linear;
}
@keyframes shake {
0% { transform: translateX(3px); }
50% { transform: translateX(-3px); }
100% { transform: translateX(0); }
}
:host([show='false']) { display: none; }
:host([show='true']) { display: block; }
.content {
margin: auto;
}
.text-container {
padding: 12px 16px;
font-size: 0.9em;
line-height: 1.5;
color: ${cssManager.bdTheme(colors.light.mutedForeground, colors.dark.mutedForeground)};
}
.text-container a {
color: ${cssManager.bdTheme(colors.light.foreground, colors.dark.foreground)};
text-decoration: underline;
text-underline-offset: 2px;
}
.text-container a:hover {
opacity: 0.8;
}
.button-container {
padding: 12px 16px 16px;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
@media (max-width: 560px) {
.button-container {
grid-template-columns: 1fr;
}
}
.consent-button {
border-radius: 6px;
background: ${cssManager.bdTheme(colors.light.secondary, colors.dark.secondary)};
border: 1px solid ${cssManager.bdTheme(colors.light.border, colors.dark.border)};
padding: 8px 16px;
font-size: 0.85em;
font-weight: 500;
text-align: center;
cursor: pointer;
transition: all 0.15s ease;
color: ${cssManager.bdTheme(colors.light.secondaryForeground, colors.dark.secondaryForeground)};
}
.consent-button:hover {
background: ${cssManager.bdTheme(colors.light.accent, colors.dark.accent)};
border-color: ${cssManager.bdTheme(colors.light.ring, colors.dark.ring)};
}
.consent-button:last-child {
background: ${cssManager.bdTheme(colors.light.primary, colors.dark.primary)};
color: ${cssManager.bdTheme(colors.light.primaryForeground, colors.dark.primaryForeground)};
border-color: ${cssManager.bdTheme(colors.light.primary, colors.dark.primary)};
}
.consent-button:last-child:hover {
opacity: 0.9;
}
.info-container {
text-align: center;
padding: 10px 16px;
background: ${cssManager.bdTheme(colors.light.muted, colors.dark.muted)};
border-top: 1px solid ${cssManager.bdTheme(colors.light.border, colors.dark.border)};
font-size: 0.75em;
color: ${cssManager.bdTheme(colors.light.mutedForeground, colors.dark.mutedForeground)};
}
.info-container a {
color: ${cssManager.bdTheme(colors.light.foreground, colors.dark.foreground)};
text-decoration: none;
}
.info-container a:hover {
text-decoration: underline;
}
`,
];
constructor() {
super();
// Initially hide the consent banner until needed
this.setAttribute('show', 'false');
}
public render(): TemplateResult {
return html`
<div class="pageOverlay" @click=${this.pageOverlayClick}>
<div class="modalBox">
<div class="content">
<consentsoftware-header></consentsoftware-header>
<consentsoftware-tabs></consentsoftware-tabs>
<div class="text-container">
<div class="toptext">
We use cookies to enhance your experience. Review our
<a href="https://lossless.gmbh/cookie" target="_blank">cookie policy</a>
and select your preferences.
</div>
</div>
<consentsoftware-mainselection></consentsoftware-mainselection>
<div class="button-container">
<div
class="consent-button"
@click=${(event: MouseEvent) =>
this.handleConsentButtonClick(event, ['functional'])}
>
Deny
</div>
<div
class="consent-button"
@click=${(event: MouseEvent) =>
this.handleConsentButtonClick(event, ['functional', 'analytics'])}
>
Accept selection
</div>
<div
class="consent-button"
@click=${(event: MouseEvent) =>
this.handleConsentButtonClick(event, ['functional', 'analytics', 'marketing'])}
>
Accept all cookies
</div>
</div>
<div class="info-container">
consent management powered by
<a href="https://consent.software">consent.software</a>
</div>
</div>
</div>
</div>
`;
}
/**
* Lifecycle method called when the element is connected to the DOM.
* Displays the consent banner if no cookie levels are set.
*/
public async connectedCallback() {
await super.connectedCallback();
const cookieLevel = await this.csWebclientInstance.getCookieLevels();
if (!cookieLevel) {
// Show consent banner if cookie levels haven't been set yet
this.setAttribute('show', 'true');
requestAnimationFrame(async () => {
await this.updateComplete;
const pageOverlay: HTMLDivElement = this.shadowRoot?.querySelector('.pageOverlay');
if (pageOverlay) {
// Apply subtle backdrop blur when modal appears
pageOverlay.style.background = 'rgba(0,0,0, 0.6)';
pageOverlay.style.backdropFilter = 'blur(4px)';
}
const modalBox: HTMLDivElement = this.shadowRoot?.querySelector('.modalBox');
if (modalBox) {
// Animate modal box appearance
modalBox.style.transform = `scale(1)`;
modalBox.style.opacity = '1';
}
});
} else {
// Hide banner if cookie levels are already set
this.setAttribute('show', 'false');
}
}
public async firstUpdated() {
// Run consent scripts if levels are already set
const acceptedCookieLevels = await this.csWebclientInstance.getCookieLevels();
if (!this.csWebclientRan && acceptedCookieLevels) {
this.csWebclientRan = true;
await this.csWebclientInstance.getAndRunConsentTuples();
}
}
/**
* Handles consent button clicks, sets cookie levels, and hides the banner.
*/
private async handleConsentButtonClick(
event: MouseEvent,
levelsArg: csInterfaces.TCookieLevel[]
) {
console.log(`Set level to ${levelsArg}`);
const pageOverlay: HTMLDivElement = this.shadowRoot?.querySelector('.pageOverlay');
if (pageOverlay) {
pageOverlay.style.background = 'rgba(0,0,0, 0)';
pageOverlay.style.backdropFilter = 'blur(0px)';
}
const modalBox: HTMLDivElement = this.shadowRoot?.querySelector('.modalBox');
if (modalBox) {
// Scale down and fade out modal box before hiding
modalBox.style.transform = `scale(0.95)`;
modalBox.style.opacity = '0';
}
// Save user consent preferences
await this.csWebclientInstance.setCookieLevels(levelsArg);
await delayFor(300);
this.setAttribute('show', 'false'); // Hide the consent banner
// Run consent scripts
if (!this.csWebclientRan) {
this.csWebclientRan = true;
await this.csWebclientInstance.getAndRunConsentTuples();
}
}
/**
* Handles clicks on the page overlay. If clicked outside the modal,
* triggers a shake animation as feedback.
*/
private async pageOverlayClick(e: MouseEvent) {
if (e.target === e.currentTarget) {
const pageOverlay: HTMLDivElement = this.shadowRoot?.querySelector('.pageOverlay');
const modalBox: HTMLDivElement = this.shadowRoot?.querySelector('.modalBox');
if (pageOverlay && modalBox) {
pageOverlay.classList.add('shake');
modalBox.classList.add('shake');
await delayFor(2000);
pageOverlay.classList.remove('shake');
modalBox.classList.remove('shake');
}
}
}
}