File size: 3,615 Bytes
63a938d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
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);