Download components/button.js from Timofey10019182/dark-mode-button-wizard: direct link, hf CLI and curl.
- Browser
- Download file 3.62 kB
-
https://huggingface.co/spaces/Timofey10019182/dark-mode-button-wizard/resolve/main/components/button.js
- Command line
-
hf download hf://spaces/Timofey10019182/dark-mode-button-wizard/components/button.js
-
curl -L -o button.js https://huggingface.co/spaces/Timofey10019182/dark-mode-button-wizard/resolve/main/components/button.js
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); |