Spaces:
Running
Running
File size: 1,869 Bytes
ec5a33c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | // Theme toggle functionality
document.addEventListener('DOMContentLoaded', () => {
const themeToggle = document.getElementById('theme-toggle');
const html = document.documentElement;
// Check for saved theme preference or use system preference
const savedTheme = localStorage.getItem('theme') ||
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
html.classList.add(savedTheme);
// Update toggle state
if (themeToggle) {
themeToggle.checked = savedTheme === 'dark';
}
// Theme toggle event
if (themeToggle) {
themeToggle.addEventListener('change', () => {
const newTheme = themeToggle.checked ? 'dark' : 'light';
html.classList.remove('light', 'dark');
html.classList.add(newTheme);
localStorage.setItem('theme', newTheme);
});
}
// Initialize tooltips
const tooltipTriggers = document.querySelectorAll('[data-tooltip]');
tooltipTriggers.forEach(el => {
el.addEventListener('mouseenter', showTooltip);
el.addEventListener('mouseleave', hideTooltip);
});
});
function showTooltip(e) {
const tooltipText = this.getAttribute('data-tooltip');
const tooltip = document.createElement('div');
tooltip.className = 'absolute z-50 px-3 py-2 text-sm text-white bg-gray-800 dark:bg-gray-700 rounded-md shadow-lg whitespace-nowrap';
tooltip.textContent = tooltipText;
const rect = this.getBoundingClientRect();
tooltip.style.top = `${rect.top - 40}px`;
tooltip.style.left = `${rect.left + rect.width / 2 - tooltip.offsetWidth / 2}px`;
document.body.appendChild(tooltip);
this.tooltip = tooltip;
}
function hideTooltip() {
if (this.tooltip) {
this.tooltip.remove();
this.tooltip = null;
}
} |