@danilosimonatto/ionicons-minimal-weather-widget 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Danilo Simonatto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Ionicons Minimal Weather Widget
2
+
3
+ A tiny, framework-agnostic weather widget custom element powered by Ionicons. Displays current weather for a city using beautiful icons.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @danilosimonatto/ionicons-minimal-weather-widget
9
+ # or
10
+ yarn add @danilosimonatto/ionicons-minimal-weather-widget
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Just include the bundled JS file on your site/app:
16
+
17
+ ```js
18
+ import "@danilosimonatto/ionicons-minimal-weather-widget";
19
+ ```
20
+
21
+ You can then use the custom element in your HTML:
22
+
23
+ ```html
24
+ <weather-widget
25
+ city="Milan"
26
+ scale="C"
27
+ icon-style="filled"
28
+ api-key="YOUR_OPENWEATHER_API_KEY"
29
+ ></weather-widget>
30
+ ```
31
+
32
+ ## Options
33
+
34
+ | Attribute | Type | Description | Example |
35
+ | ------------ | -------------------------------- | -------------------------------------------------- | ------------ |
36
+ | `city` | `string` (required) | City name to fetch weather for | `Milan` |
37
+ | `scale` | `"C"`/`"F"` | Temperature units. Celsius (default) or Fahrenheit | `F` |
38
+ | `icon-style` | `"filled"`/`"outline"`/`"sharp"` | Ionicon style (default: `filled`) | `outline` |
39
+ | `api-key` | `string` (required) | OpenWeatherMap API key | `yourApiKey` |
40
+
41
+ ## Example
42
+
43
+ ```html
44
+ <weather-widget
45
+ city="San Francisco"
46
+ scale="F"
47
+ icon-style="outline"
48
+ api-key="YOUR_OPENWEATHER_API_KEY"
49
+ ></weather-widget>
50
+ ```
51
+
52
+ ## Features
53
+
54
+ - 🌀 Framework-agnostic — use anywhere (Astro, React, Vue, plain HTML, etc.)
55
+ - ⛅ Uses Ionicons for beautiful weather icons
56
+ - 🎨 Customizable icon style and units
57
+ - 🔐 Uses your OpenWeather API key via the `api-key` attribute
58
+
59
+ ## License
60
+
61
+ MIT
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@danilosimonatto/ionicons-minimal-weather-widget",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "author": "Danilo Simonatto",
7
+ "files": [
8
+ "weather-widget.js",
9
+ "weather-widget.d.ts",
10
+ "README.md",
11
+ "LICENSE"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "types": "./weather-widget.d.ts",
16
+ "default": "./weather-widget.js"
17
+ }
18
+ },
19
+ "dependencies": {
20
+ "ionicons": "^8.0.13"
21
+ }
22
+ }
@@ -0,0 +1,37 @@
1
+ export {};
2
+
3
+ type WeatherScale = "C" | "F";
4
+ type IoniconStyle = "outline" | "filled" | "sharp";
5
+
6
+ /**
7
+ * Type definitions for the framework-agnostic `<weather-widget>` custom element.
8
+ *
9
+ * This file is meant to be shipped alongside `weather-widget.js` when publishing to npm,
10
+ * so TypeScript users get autocomplete + type-checking.
11
+ */
12
+ declare class WeatherWidgetElement extends HTMLElement {
13
+ static observedAttributes: readonly [
14
+ "city",
15
+ "scale",
16
+ "icon-style",
17
+ "api-key"
18
+ ];
19
+
20
+ /** City name to search (required). Example: "Milan" */
21
+ get city(): string;
22
+
23
+ /** Temperature scale (defaults to "C") */
24
+ get scale(): WeatherScale;
25
+
26
+ /** Ionicon style (defaults to "filled") */
27
+ get iconStyle(): IoniconStyle;
28
+
29
+ /** Optional OpenWeather API key override */
30
+ get apiKey(): string;
31
+ }
32
+
33
+ declare global {
34
+ interface HTMLElementTagNameMap {
35
+ "weather-widget": WeatherWidgetElement;
36
+ }
37
+ }
@@ -0,0 +1,313 @@
1
+ import { defineCustomElements } from "ionicons/loader";
2
+ import { addIcons } from "ionicons";
3
+ import {
4
+ sunny,
5
+ sunnyOutline,
6
+ sunnySharp,
7
+ moon,
8
+ moonOutline,
9
+ moonSharp,
10
+ partlySunny,
11
+ partlySunnyOutline,
12
+ partlySunnySharp,
13
+ cloudy,
14
+ cloudyOutline,
15
+ cloudySharp,
16
+ cloudyNight,
17
+ cloudyNightOutline,
18
+ cloudyNightSharp,
19
+ rainy,
20
+ rainyOutline,
21
+ rainySharp,
22
+ thunderstorm,
23
+ thunderstormOutline,
24
+ thunderstormSharp,
25
+ snow,
26
+ snowOutline,
27
+ snowSharp,
28
+ helpCircle,
29
+ helpCircleOutline,
30
+ helpCircleSharp,
31
+ } from "ionicons/icons";
32
+
33
+ let ioniconsInitPromise;
34
+ function ensureIonicons() {
35
+ if (!ioniconsInitPromise) {
36
+ addIcons({
37
+ sunny,
38
+ sunnyOutline,
39
+ sunnySharp,
40
+ moon,
41
+ moonOutline,
42
+ moonSharp,
43
+ partlySunny,
44
+ partlySunnyOutline,
45
+ partlySunnySharp,
46
+ cloudy,
47
+ cloudyOutline,
48
+ cloudySharp,
49
+ cloudyNight,
50
+ cloudyNightOutline,
51
+ cloudyNightSharp,
52
+ rainy,
53
+ rainyOutline,
54
+ rainySharp,
55
+ thunderstorm,
56
+ thunderstormOutline,
57
+ thunderstormSharp,
58
+ snow,
59
+ snowOutline,
60
+ snowSharp,
61
+ helpCircle,
62
+ helpCircleOutline,
63
+ helpCircleSharp,
64
+ });
65
+ ioniconsInitPromise = defineCustomElements(window);
66
+ }
67
+ return ioniconsInitPromise;
68
+ }
69
+
70
+ const normalizeScale = (value) =>
71
+ String(value || "").toUpperCase() === "F" ? "F" : "C";
72
+
73
+ const normalizeIconStyle = (value) => {
74
+ if (value === "outline" || value === "sharp" || value === "filled")
75
+ return value;
76
+ return "filled";
77
+ };
78
+
79
+ const styleIonicon = (baseName, style) => {
80
+ if (style === "filled") return baseName;
81
+ return `${baseName}-${style}`;
82
+ };
83
+
84
+ // OpenWeather icon code mapping to Ionicons
85
+ const ICON_CODE_TO_IONICON = {
86
+ // 01d - sunny
87
+ "01d": "sunny",
88
+ "01n": "moon",
89
+
90
+ // 02d - partly-sunny
91
+ "02d": "partly-sunny",
92
+ "02n": "cloudy-night",
93
+
94
+ // 03d, 04d, 50d - cloudy
95
+ "03d": "cloudy",
96
+ "03n": "cloudy",
97
+ "04d": "cloudy",
98
+ "04n": "cloudy",
99
+ "50d": "cloudy",
100
+ "50n": "cloudy",
101
+
102
+ // 09d, 10d - rainy
103
+ "09d": "rainy",
104
+ "09n": "rainy",
105
+ "10d": "rainy",
106
+ "10n": "rainy",
107
+
108
+ // 11d - thunderstorm
109
+ "11d": "thunderstorm",
110
+ "11n": "thunderstorm",
111
+
112
+ // 13d - snow
113
+ "13d": "snow",
114
+ "13n": "snow",
115
+ };
116
+
117
+ const escapeHtml = (input) => {
118
+ return String(input)
119
+ .replaceAll("&", "&amp;")
120
+ .replaceAll("<", "&lt;")
121
+ .replaceAll(">", "&gt;")
122
+ .replaceAll('"', "&quot;")
123
+ .replaceAll("'", "&#039;");
124
+ };
125
+
126
+ class WeatherWidgetElement extends HTMLElement {
127
+ static observedAttributes = ["city", "scale", "icon-style", "api-key"];
128
+
129
+ #abort = null;
130
+ #shadow = this.attachShadow({ mode: "open" });
131
+
132
+ connectedCallback() {
133
+ ensureIonicons().then(() => {
134
+ this.#renderShell();
135
+ this.#load();
136
+ });
137
+ }
138
+
139
+ disconnectedCallback() {
140
+ if (this.#abort) this.#abort.abort();
141
+ }
142
+
143
+ attributeChangedCallback() {
144
+ if (!this.isConnected) return;
145
+ this.#load();
146
+ }
147
+
148
+ get city() {
149
+ return (this.getAttribute("city") || "").trim();
150
+ }
151
+
152
+ get scale() {
153
+ return normalizeScale(this.getAttribute("scale"));
154
+ }
155
+
156
+ get iconStyle() {
157
+ return normalizeIconStyle(this.getAttribute("icon-style"));
158
+ }
159
+
160
+ get apiKey() {
161
+ return (this.getAttribute("api-key") || "").trim();
162
+ }
163
+
164
+ #renderShell() {
165
+ this.#shadow.innerHTML = `
166
+ <style>
167
+ :host {
168
+ display: inline-block;
169
+ }
170
+ .weather-widget {
171
+ display: inline-flex;
172
+ flex-direction: column;
173
+ gap: 8px;
174
+ padding: 1.5rem;
175
+ background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
176
+ border-radius: 16px;
177
+ color: white;
178
+ text-align: center;
179
+ box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
180
+ min-width: 250px;
181
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
182
+ }
183
+ .loading,
184
+ .error {
185
+ font-size: 1rem;
186
+ padding: 1rem;
187
+ }
188
+ .error {
189
+ color: #ffebee;
190
+ background: rgba(255, 0, 0, 0.2);
191
+ border-radius: 8px;
192
+ }
193
+ .weather-content {
194
+ display: flex;
195
+ flex-direction: column;
196
+ gap: 12px;
197
+ }
198
+ .city-name {
199
+ font-size: 1.5rem;
200
+ font-weight: 600;
201
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
202
+ }
203
+ .weather {
204
+ display: flex;
205
+ gap: 12px;
206
+ align-items: center;
207
+ justify-content: center;
208
+ font-size: 2.5rem;
209
+ font-weight: 700;
210
+ }
211
+ .icon {
212
+ font-size: 3rem;
213
+ text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
214
+ }
215
+ .temperature {
216
+ text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
217
+ }
218
+ </style>
219
+ <div class="weather-widget" part="root">
220
+ <div class="loading" part="loading">Loading…</div>
221
+ </div>
222
+ `;
223
+ }
224
+
225
+ #setInner(html) {
226
+ const root = this.#shadow.querySelector(".weather-widget");
227
+ if (!root) return;
228
+ root.innerHTML = html;
229
+ }
230
+
231
+ async #load() {
232
+ const city = this.city;
233
+ if (!city) {
234
+ this.#setInner(`<div class="error" part="error">Missing city</div>`);
235
+ return;
236
+ }
237
+
238
+ if (this.#abort) this.#abort.abort();
239
+ this.#abort = new AbortController();
240
+
241
+ this.#setInner(`<div class="loading" part="loading">Loading…</div>`);
242
+
243
+ try {
244
+ // Mirrors the old Vue component's behavior:
245
+ // - OpenWeatherMap geocoding -> lat/lon
246
+ // - OpenWeatherMap weather in metric
247
+ // - Convert to Fahrenheit client-side if needed
248
+ const apiKey = this.apiKey;
249
+ if (!apiKey) throw new Error("Missing API key");
250
+
251
+ // Step 1: Geocode
252
+ const geoUrl = new URL("https://api.openweathermap.org/geo/1.0/direct");
253
+ geoUrl.searchParams.set("q", city);
254
+ geoUrl.searchParams.set("limit", "1");
255
+ geoUrl.searchParams.set("appid", apiKey);
256
+
257
+ const geoRes = await fetch(geoUrl, { signal: this.#abort.signal });
258
+ if (!geoRes.ok) throw new Error("City not found");
259
+ const geoJson = await geoRes.json();
260
+ const geo = geoJson && geoJson[0];
261
+ if (!geo) throw new Error("City not found");
262
+
263
+ // Step 2: Weather (metric, then optional conversion)
264
+ const weatherUrl = new URL(
265
+ "https://api.openweathermap.org/data/2.5/weather"
266
+ );
267
+ weatherUrl.searchParams.set("lat", String(geo.lat));
268
+ weatherUrl.searchParams.set("lon", String(geo.lon));
269
+ weatherUrl.searchParams.set("units", "metric");
270
+ weatherUrl.searchParams.set("appid", apiKey);
271
+
272
+ const weatherRes = await fetch(weatherUrl, {
273
+ signal: this.#abort.signal,
274
+ });
275
+ if (!weatherRes.ok) throw new Error("Weather not available");
276
+ const weather = await weatherRes.json();
277
+
278
+ const celsius = weather?.main?.temp;
279
+ const temp =
280
+ this.scale === "F"
281
+ ? Math.round((celsius * 9) / 5 + 32)
282
+ : Math.round(celsius);
283
+
284
+ const iconCode = weather?.weather?.[0]?.icon;
285
+ const baseIcon =
286
+ (iconCode && ICON_CODE_TO_IONICON[iconCode]) || "help-circle";
287
+ const iconName = styleIonicon(baseIcon, this.iconStyle);
288
+
289
+ const cityName = weather?.name || city;
290
+
291
+ this.#setInner(`
292
+ <div class="weather-content" part="content">
293
+ <div class="city-name" part="city">${escapeHtml(cityName)}</div>
294
+ <div class="weather" part="weather">
295
+ <ion-icon name="${iconName}" class="icon" aria-hidden="true"></ion-icon>
296
+ <span class="temperature" part="temperature">${temp}°${this.scale}</span>
297
+ </div>
298
+ </div>
299
+ `);
300
+ } catch (e) {
301
+ if (e instanceof DOMException && e.name === "AbortError") return;
302
+ const message =
303
+ e instanceof Error ? e.message : "Failed to fetch weather";
304
+ this.#setInner(
305
+ `<div class="error" part="error">${escapeHtml(message)}</div>`
306
+ );
307
+ }
308
+ }
309
+ }
310
+
311
+ if (!customElements.get("weather-widget")) {
312
+ customElements.define("weather-widget", WeatherWidgetElement);
313
+ }