Download components/weather.js from arefamer/codegenius-wizardry-wand: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/spaces/arefamer/codegenius-wizardry-wand/resolve/main/components/weather.js
- Command line
-
hf download hf://spaces/arefamer/codegenius-wizardry-wand/components/weather.js
-
curl -L -o weather.js https://huggingface.co/spaces/arefamer/codegenius-wizardry-wand/resolve/main/components/weather.js
4.28 kB
| class WeatherWidget extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: block; | |
| font-family: 'Inter', sans-serif; | |
| } | |
| .weather-card { | |
| background: linear-gradient(135deg, #667eea, #764ba2); | |
| border-radius: 16px; | |
| padding: 24px; | |
| color: white; | |
| box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1); | |
| max-width: 350px; | |
| margin: 0 auto; | |
| } | |
| .weather-header { | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: center; | |
| margin-bottom: 20px; | |
| } | |
| .location { | |
| font-size: 1.5rem; | |
| font-weight: 600; | |
| } | |
| .date { | |
| font-size: 0.9rem; | |
| opacity: 0.8; | |
| } | |
| .weather-main { | |
| display: flex; | |
| align-items: center; | |
| margin-bottom: 20px; | |
| } | |
| .temperature { | |
| font-size: 3.5rem; | |
| font-weight: 300; | |
| margin-right: 20px; | |
| } | |
| .weather-icon { | |
| width: 80px; | |
| height: 80px; | |
| } | |
| .weather-details { | |
| display: grid; | |
| grid-template-columns: repeat(2, 1fr); | |
| gap: 15px; | |
| } | |
| .detail-item { | |
| display: flex; | |
| align-items: center; | |
| } | |
| .detail-icon { | |
| margin-right: 8px; | |
| font-size: 1.2rem; | |
| } | |
| .loading { | |
| text-align: center; | |
| padding: 20px; | |
| } | |
| </style> | |
| <div class="weather-card"> | |
| <div class="weather-header"> | |
| <div class="location">Loading...</div> | |
| <div class="date">${new Date().toLocaleDateString()}</div> | |
| </div> | |
| <div class="weather-main"> | |
| <div class="temperature">--ยฐ</div> | |
| <img class="weather-icon" src="" alt="Weather icon"> | |
| </div> | |
| <div class="weather-details"> | |
| <div class="detail-item"> | |
| <i class="detail-icon fas fa-tint"></i> | |
| <span class="humidity">--%</span> | |
| </div> | |
| <div class="detail-item"> | |
| <i class="detail-icon fas fa-wind"></i> | |
| <span class="wind">-- km/h</span> | |
| </div> | |
| <div class="detail-item"> | |
| <i class="detail-icon fas fa-cloud"></i> | |
| <span class="clouds">--%</span> | |
| </div> | |
| <div class="detail-item"> | |
| <i class="detail-icon fas fa-compress-alt"></i> | |
| <span class="pressure">-- hPa</span> | |
| </div> | |
| </div> | |
| </div> | |
| `; | |
| this.fetchWeather(); | |
| } | |
| async fetchWeather() { | |
| try { | |
| // Get user's location | |
| const position = await new Promise((resolve, reject) => { | |
| navigator.geolocation.getCurrentPosition(resolve, reject); | |
| }); | |
| // Fetch weather data | |
| const response = await fetch( | |
| `https://api.openweathermap.org/data/2.5/weather?lat=${position.coords.latitude}&lon=${position.coords.longitude}&units=metric&appid=YOUR_API_KEY` | |
| ); | |
| if (!response.ok) throw new Error('Weather data not available'); | |
| const data = await response.json(); | |
| this.updateWeather(data); | |
| } catch (error) { | |
| console.error('Error fetching weather:', error); | |
| this.shadowRoot.querySelector('.location').textContent = 'Weather unavailable'; | |
| } | |
| } | |
| updateWeather(data) { | |
| const shadow = this.shadowRoot; | |
| shadow.querySelector('.location').textContent = data.name; | |
| shadow.querySelector('.temperature').textContent = `${Math.round(data.main.temp)}ยฐC`; | |
| shadow.querySelector('.humidity').textContent = `${data.main.humidity}%`; | |
| shadow.querySelector('.wind').textContent = `${Math.round(data.wind.speed * 3.6)} km/h`; | |
| shadow.querySelector('.clouds').textContent = `${data.clouds.all}%`; | |
| shadow.querySelector('.pressure').textContent = `${data.main.pressure} hPa`; | |
| const iconUrl = `https://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`; | |
| shadow.querySelector('.weather-icon').src = iconUrl; | |
| shadow.querySelector('.weather-icon').alt = data.weather[0].description; | |
| } | |
| } | |
| customElements.define('weather-widget', WeatherWidget); |