Spaces:
Running
Running
Download components/theme-toggle.js from qq5378673/url-ninja-loader: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/qq5378673/url-ninja-loader/resolve/main/components/theme-toggle.js
- Command line
-
hf download hf://spaces/qq5378673/url-ninja-loader/components/theme-toggle.js
-
curl -L -o theme-toggle.js https://huggingface.co/spaces/qq5378673/url-ninja-loader/resolve/main/components/theme-toggle.js
2.67 kB
| class ThemeToggle extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.render(); | |
| this.setupEventListeners(); | |
| } | |
| render() { | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| .toggle-container { | |
| position: fixed; | |
| bottom: 2rem; | |
| right: 2rem; | |
| z-index: 50; | |
| } | |
| .toggle-btn { | |
| background: var(--bg-color); | |
| color: var(--text-color); | |
| border: none; | |
| border-radius: 50%; | |
| width: 3rem; | |
| height: 3rem; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| cursor: pointer; | |
| box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); | |
| transition: all 0.3s ease; | |
| } | |
| .toggle-btn:hover { | |
| transform: scale(1.1); | |
| } | |
| .toggle-btn:focus { | |
| outline: none; | |
| box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5); | |
| } | |
| </style> | |
| <div class="toggle-container"> | |
| <button class="toggle-btn" id="themeToggle"> | |
| <i data-feather="moon"></i> | |
| </button> | |
| </div> | |
| `; | |
| } | |
| setupEventListeners() { | |
| const themeToggle = this.shadowRoot.getElementById('themeToggle'); | |
| // Check for saved theme preference or use system preference | |
| const savedTheme = localStorage.getItem('theme') || | |
| (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'); | |
| document.documentElement.classList.toggle('dark', savedTheme === 'dark'); | |
| // Set initial icon | |
| this.updateIcon(themeToggle, savedTheme); | |
| themeToggle.addEventListener('click', () => { | |
| const isDark = document.documentElement.classList.toggle('dark'); | |
| localStorage.setItem('theme', isDark ? 'dark' : 'light'); | |
| this.updateIcon(themeToggle, isDark ? 'dark' : 'light'); | |
| }); | |
| } | |
| updateIcon(button, theme) { | |
| const icon = button.querySelector('i'); | |
| if (theme === 'dark') { | |
| icon.setAttribute('data-feather', 'sun'); | |
| } else { | |
| icon.setAttribute('data-feather', 'moon'); | |
| } | |
| feather.replace(); | |
| } | |
| } | |
| customElements.define('theme-toggle', ThemeToggle); |