Spaces:
Running
Running
File size: 3,213 Bytes
50fc523 8d0c931 50fc523 8d0c931 50fc523 8d0c931 50fc523 8d0c931 50fc523 8d0c931 50fc523 8d0c931 50fc523 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | class CustomThemeToggle extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.render();
}
render() {
const isDark = document.documentElement.classList.contains('dark');
this.shadowRoot.innerHTML = `
<style>
.toggle-container {
display: flex;
align-items: center;
}
.toggle-btn {
background: none;
border: none;
cursor: pointer;
padding: 0.5rem;
border-radius: 50%;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
}
.toggle-btn {
background: rgba(0,0,0,0.05);
}
.toggle-btn:hover {
background: rgba(0,0,0,0.1);
}
.dark .toggle-btn {
background: rgba(255,255,255,0.1);
}
.dark .toggle-btn:hover {
background: rgba(255,255,255,0.2);
}
.tooltip {
position: absolute;
right: 100%;
margin-right: 8px;
padding: 4px 8px;
background: #374151;
color: white;
border-radius: 4px;
font-size: 12px;
white-space: nowrap;
opacity: 0;
transition: opacity 0.2s;
pointer-events: none;
}
.toggle-btn:hover .tooltip {
opacity: 1;
}
.dark .tooltip {
background: #1F2937;
}
.toggle-icon {
width: 20px;
height: 20px;
}
</style>
<div class="toggle-container">
<button class="toggle-btn">
<i data-feather="${isDark ? 'sun' : 'moon'}" class="toggle-icon"></i>
<span class="tooltip">${isDark ? 'Light Mode' : 'Dark Mode'}</span>
</button>
</div>
`;
this.shadowRoot.querySelector('.toggle-btn').addEventListener('click', () => {
const html = document.documentElement;
const isDark = html.classList.contains('dark');
if (isDark) {
html.classList.remove('dark');
localStorage.setItem('theme', 'light');
} else {
html.classList.add('dark');
localStorage.setItem('theme', 'dark');
}
this.render();
feather.replace();
});
feather.replace();
}
}
customElements.define('custom-theme-toggle', CustomThemeToggle); |