Timofey10019182's picture
кнопка подключить не работает, улучши дизайн
63a938d verified
Raw History Blame Contribute Delete
3.62 kB
class CustomButton extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.render();
}
toggleTheme() {
const html = document.documentElement;
html.classList.toggle('dark');
// Save preference to localStorage
const isDark = html.classList.contains('dark');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
// Add pulse animation
const button = this.shadowRoot.querySelector('button');
button.classList.add('animate-pulse-once');
setTimeout(() => {
button.classList.remove('animate-pulse-once');
}, 500);
// Update icon
this.updateIcon();
}
updateIcon() {
const icon = this.shadowRoot.querySelector('i');
const isDark = document.documentElement.classList.contains('dark');
icon.setAttribute('data-feather', isDark ? 'sun' : 'moon');
feather.replace();
}
render() {
const isDark = document.documentElement.classList.contains('dark');
this.shadowRoot.innerHTML = `
<style>
button {
border: none;
outline: none;
cursor: pointer;
user-select: none;
}
.button-container {
position: relative;
width: 80px;
height: 80px;
}
.button-bg {
position: absolute;
width: 100%;
height: 100%;
border-radius: 50%;
background: linear-gradient(145deg, #e6e6e6, #ffffff);
box-shadow: 5px 5px 10px #d9d9d9,
-5px -5px 10px #ffffff;
transition: all 0.3s ease;
}
.dark .button-bg {
background: linear-gradient(145deg, #1a1a1a, #000000);
box-shadow: 5px 5px 10px #0d0d0d,
-5px -5px 10px #000000;
}
.button-content {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
border-radius: 50%;
z-index: 1;
}
i {
color: #000000;
transition: color 0.3s ease;
}
.dark i {
color: #ffffff;
}
button:hover .button-bg {
transform: scale(1.05);
}
button:active .button-bg {
transform: scale(0.95);
}
</style>
<button class="button-container" aria-label="Toggle dark mode">
<div class="button-bg"></div>
<div class="button-content">
<i data-feather="${isDark ? 'sun' : 'moon'}"></i>
</div>
</button>
`;
const button = this.shadowRoot.querySelector('button');
button.addEventListener('click', () => this.toggleTheme());
}
}
customElements.define('custom-button', CustomButton);