document.addEventListener('DOMContentLoaded', function() {
lucide.createIcons();
initTheme();
initScrollAnimations();
initSmoothScroll();
});
function initTheme() {
const themeToggle = document.getElementById('theme-toggle');
const themeSun = document.getElementById('theme-sun');
const themeMoon = document.getElementById('theme-moon');
const savedTheme = localStorage.getItem('theme');
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const isDark = savedTheme ? savedTheme === 'dark' : systemDark;
if (isDark) {
document.documentElement.classList.remove('light-mode');
themeSun?.classList.remove('hidden');
themeMoon?.classList.add('hidden');
} else {
document.documentElement.classList.add('light-mode');
themeSun?.classList.add('hidden');
themeMoon?.classList.remove('hidden');
}
themeToggle?.addEventListener('click', toggleTheme);
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
applyTheme(e.matches ? 'dark' : 'light');
}
});
}
function toggleTheme() {
const isDark = !document.documentElement.classList.contains('light-mode');
const newTheme = isDark ? 'light' : 'dark';
applyTheme(newTheme);
localStorage.setItem('theme', newTheme);
}
function applyTheme(theme) {
const themeSun = document.getElementById('theme-sun');
const themeMoon = document.getElementById('theme-moon');
if (theme === 'light') {
document.documentElement.classList.add('light-mode');
themeSun?.classList.add('hidden');
themeMoon?.classList.remove('hidden');
} else {
document.documentElement.classList.remove('light-mode');
themeSun?.classList.remove('hidden');
themeMoon?.classList.add('hidden');
}
lucide.createIcons();
}
function copyCode(text) {
navigator.clipboard.writeText(text).then(() => {
const btn = event.target.closest('.copy-btn');
if (!btn) return;
const originalHTML = btn.innerHTML;
btn.innerHTML = '<i data-lucide="check" class="w-3 h-3"></i><span>Copied!</span>';
btn.classList.add('text-github-accent');
lucide.createIcons();
setTimeout(() => {
btn.innerHTML = originalHTML;
btn.classList.remove('text-github-accent');
lucide.createIcons();
}, 2000);
}).catch(err => {
console.error('Failed to copy:', err);
});
}
function initSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const target = document.querySelector(targetId);
if (target) {
const headerOffset = 80;
const elementPosition = target.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
});
});
}
function initScrollAnimations() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('is-visible');
observer.unobserve(entry.target);
}
});
}, observerOptions);
document.querySelectorAll('.feature-card').forEach((el, index) => {
el.style.transitionDelay = `${index * 0.1}s`;
observer.observe(el);
});
}
document.querySelectorAll('a[target="_blank"]').forEach(link => {
link.addEventListener('click', function(e) {
if (!this.rel || !this.rel.includes('noopener')) {
this.rel = this.rel ? `${this.rel} noopener` : 'noopener';
}
});
});
document.addEventListener('keydown', function(e) {
if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
e.preventDefault();
const featuresSection = document.getElementById('features');
if (featuresSection) {
featuresSection.scrollIntoView({ behavior: 'smooth' });
}
}
});