toolsvana-clonecraft / components /stats-counter.js
Retese's picture
make it more beautiful, useful and functional
46925ca verified
Raw History Blame Contribute Delete
3.34 kB
class StatsCounter extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
const value = this.getAttribute('value') || '0';
const label = this.getAttribute('label') || 'Stat';
const icon = this.getAttribute('icon') || 'trending-up';
const color = this.getAttribute('color') || 'blue';
const colors = {
blue: 'from-blue-500 to-cyan-500',
green: 'from-emerald-500 to-green-500',
purple: 'from-purple-500 to-pink-500',
orange: 'from-orange-500 to-amber-500'
};
const gradient = colors[color] || colors.blue;
this.shadowRoot.innerHTML = `
<style>
.stat-container {
background: white;
border-radius: 1.5rem;
padding: 1.5rem;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
transition: all 0.3s ease;
}
.dark .stat-container {
background: #1e293b;
}
.stat-container:hover {
transform: translateY(-5px);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
.stat-value {
font-size: 2.5rem;
font-weight: 800;
background: linear-gradient(135deg, var(--primary), var(--secondary));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
}
.stat-label {
color: #64748b;
font-size: 0.95rem;
font-weight: 500;
}
.stat-icon {
width: 3rem;
height: 3rem;
border-radius: 1rem;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 1rem;
}
</style>
<div class="stat-container dark:bg-slate-800">
<div class="stat-icon bg-gradient-to-br ${gradient}">
<i data-feather="${icon}" class="text-white w-5 h-5"></i>
</div>
<div class="stat-value" id="counter">0</div>
<div class="stat-label">${label}</div>
</div>
`;
// Animate counter
this.animateCounter(value);
// Replace feather icon
if (typeof feather !== 'undefined') {
feather.replace();
}
}
animateCounter(target) {
const counter = this.shadowRoot.getElementById('counter');
const numTarget = parseInt(target.replace(/,/g, ''));
let count = 0;
const increment = Math.max(1, Math.floor(numTarget / 100));
const timer = setInterval(() => {
count += increment;
if (count >= numTarget) {
count = numTarget;
clearInterval(timer);
}
counter.textContent = count.toLocaleString();
}, 20);
}
}
customElements.define('stats-counter', StatsCounter);