homebase-navigator / components /theme-toggle.js
Dereck234's picture
now enhance the UI with icons, implement dark and light mode options, and make things to able to collapse
8d0c931 verified
Raw
History Blame Contribute Delete
3.21 kB
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);