class PricingCard extends HTMLElement {
constructor() {
super();
this.isYearly = false;
}
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
this.bindEvents();
}
render() {
const plan = this.getAttribute('plan') || 'Basic';
const price = this.getAttribute('price') || '0';
const popular = this.hasAttribute('popular');
const features = JSON.parse(this.getAttribute('features') || '[]');
const yearly = this.getAttribute('yearly') === 'true';
const monthlyPrice = parseInt(price);
const yearlyPrice = Math.round(monthlyPrice * 12 * 0.8); // 20% discount
const savings = Math.round((1 - 0.8) * 100);
this.shadowRoot.innerHTML = `
${popular ? '
Most Popular' : ''}
${plan}
Perfect for ${plan.toLowerCase()} teams
$${yearly ? yearlyPrice : monthlyPrice}
${yearly ? `
$${monthlyPrice * 12}` : ''}
Save ${savings}%
per ${yearly ? 'year' : 'month'}
${features.map(feature => `- ${feature}
`).join('')}
`;
}
bindEvents() {
this.shadowRoot.querySelector('.btn-choose').addEventListener('click', () => {
// Handle plan selection
const event = new CustomEvent('planselected', {
detail: { plan: this.getAttribute('plan') }
});
window.dispatchEvent(event);
});
}
static get observedAttributes() {
return ['yearly'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'yearly') {
this.isYearly = newValue === 'true';
this.render();
}
}
}
customElements.define('custom-pricing-card', PricingCard);