Spaces:
Running
Running
Download components/theme-toggle.js from CAGB058/xml-excel-data-explorer: direct link, hf CLI and curl.
- Browser
- Download file 2.05 kB
-
https://huggingface.co/spaces/CAGB058/xml-excel-data-explorer/resolve/main/components/theme-toggle.js
- Command line
-
hf download hf://spaces/CAGB058/xml-excel-data-explorer/components/theme-toggle.js
-
curl -L -o theme-toggle.js https://huggingface.co/spaces/CAGB058/xml-excel-data-explorer/resolve/main/components/theme-toggle.js
2.05 kB
| class ThemeToggle extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: inline-block; | |
| cursor: pointer; | |
| } | |
| .toggle-container { | |
| display: flex; | |
| align-items: center; | |
| gap: 0.5rem; | |
| } | |
| .toggle-switch { | |
| position: relative; | |
| width: 44px; | |
| height: 24px; | |
| background: #e2e8f0; | |
| border-radius: 12px; | |
| transition: background 0.3s; | |
| } | |
| .toggle-switch::after { | |
| content: ''; | |
| position: absolute; | |
| width: 20px; | |
| height: 20px; | |
| border-radius: 50%; | |
| background: white; | |
| top: 2px; | |
| left: 2px; | |
| transition: transform 0.3s; | |
| } | |
| :host([dark]) .toggle-switch { | |
| background: #4a5568; | |
| } | |
| :host([dark]) .toggle-switch::after { | |
| transform: translateX(20px); | |
| } | |
| .icon { | |
| width: 16px; | |
| height: 16px; | |
| } | |
| </style> | |
| <div class="toggle-container"> | |
| <i data-feather="sun" class="icon"></i> | |
| <div class="toggle-switch"></div> | |
| <i data-feather="moon" class="icon"></i> | |
| </div> | |
| `; | |
| this.addEventListener('click', this.toggleTheme); | |
| this.checkTheme(); | |
| } | |
| checkTheme() { | |
| if (document.documentElement.getAttribute('data-theme') === 'dark') { | |
| this.setAttribute('dark', ''); | |
| } | |
| } | |
| toggleTheme() { | |
| const isDark = this.hasAttribute('dark'); | |
| if (isDark) { | |
| document.documentElement.setAttribute('data-theme', 'light'); | |
| this.removeAttribute('dark'); | |
| } else { | |
| document.documentElement.setAttribute('data-theme', 'dark'); | |
| this.setAttribute('dark', ''); | |
| } | |
| this.updateTheme(); | |
| } | |
| updateTheme() { | |
| const isDark = this.hasAttribute('dark'); | |
| localStorage.setItem('theme', isDark ? 'dark' : 'light'); | |
| } | |
| } | |
| customElements.define('theme-toggle', ThemeToggle); |