@danilosimonatto/ionicons-minimal-weather-widget 0.2.0 → 0.2.1
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/dist/weather-widget.js +42 -68
- package/package.json +1 -1
- package/src/styles.css +4 -30
package/dist/weather-widget.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { defineCustomElements as
|
|
2
|
-
import { addIcons as
|
|
3
|
-
import { helpCircleSharp as
|
|
1
|
+
import { defineCustomElements as w } from "ionicons/loader";
|
|
2
|
+
import { addIcons as f } from "ionicons";
|
|
3
|
+
import { helpCircleSharp as S, helpCircleOutline as b, helpCircle as C, snowSharp as O, snowOutline as x, snow as E, thunderstormSharp as k, thunderstormOutline as I, thunderstorm as v, rainySharp as A, rainyOutline as P, rainy as q, cloudyNightSharp as M, cloudyNightOutline as N, cloudyNight as U, cloudySharp as L, cloudyOutline as $, cloudy as F, partlySunnySharp as R, partlySunnyOutline as T, partlySunny as W, moonSharp as j, moonOutline as z, moon as K, sunnySharp as _, sunnyOutline as D, sunny as H } from "ionicons/icons";
|
|
4
4
|
const B = `:host {
|
|
5
|
+
--color-primary: #000;
|
|
5
6
|
display: inline-block;
|
|
6
7
|
}
|
|
7
8
|
|
|
@@ -9,23 +10,13 @@ const B = `:host {
|
|
|
9
10
|
display: inline-flex;
|
|
10
11
|
flex-direction: column;
|
|
11
12
|
gap: 8px;
|
|
12
|
-
|
|
13
|
-
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
|
14
|
-
border-radius: 16px;
|
|
15
|
-
color: white;
|
|
13
|
+
color: var(--color-primary, #000);
|
|
16
14
|
text-align: center;
|
|
17
|
-
|
|
18
|
-
min-width: 250px;
|
|
15
|
+
font-size: 1rem;
|
|
19
16
|
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
|
|
20
17
|
Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
21
18
|
}
|
|
22
19
|
|
|
23
|
-
.loading,
|
|
24
|
-
.error {
|
|
25
|
-
font-size: 1rem;
|
|
26
|
-
padding: 1rem;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
20
|
.error {
|
|
30
21
|
color: #ffebee;
|
|
31
22
|
background: rgba(255, 0, 0, 0.2);
|
|
@@ -35,27 +26,10 @@ const B = `:host {
|
|
|
35
26
|
.weather-content {
|
|
36
27
|
display: flex;
|
|
37
28
|
flex-direction: row;
|
|
38
|
-
gap:
|
|
39
|
-
font-size: 2.5rem;
|
|
40
|
-
font-weight: 700;
|
|
29
|
+
gap: 5px;
|
|
41
30
|
align-items: center;
|
|
42
31
|
justify-content: center;
|
|
43
32
|
}
|
|
44
|
-
|
|
45
|
-
.city-name {
|
|
46
|
-
font-size: 1.5rem;
|
|
47
|
-
font-weight: 600;
|
|
48
|
-
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
.icon {
|
|
52
|
-
font-size: 3rem;
|
|
53
|
-
text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
.temperature {
|
|
57
|
-
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
|
|
58
|
-
}
|
|
59
33
|
`, J = `<div
|
|
60
34
|
class="weather-widget"
|
|
61
35
|
part="root"
|
|
@@ -95,19 +69,19 @@ const B = `:host {
|
|
|
95
69
|
`;
|
|
96
70
|
let a;
|
|
97
71
|
function G() {
|
|
98
|
-
return a || (
|
|
72
|
+
return a || (f({
|
|
99
73
|
sunny: H,
|
|
100
74
|
sunnyOutline: D,
|
|
101
75
|
sunnySharp: _,
|
|
102
76
|
moon: K,
|
|
103
|
-
moonOutline:
|
|
104
|
-
moonSharp:
|
|
105
|
-
partlySunny:
|
|
106
|
-
partlySunnyOutline:
|
|
107
|
-
partlySunnySharp:
|
|
108
|
-
cloudy:
|
|
109
|
-
cloudyOutline:
|
|
110
|
-
cloudySharp:
|
|
77
|
+
moonOutline: z,
|
|
78
|
+
moonSharp: j,
|
|
79
|
+
partlySunny: W,
|
|
80
|
+
partlySunnyOutline: T,
|
|
81
|
+
partlySunnySharp: R,
|
|
82
|
+
cloudy: F,
|
|
83
|
+
cloudyOutline: $,
|
|
84
|
+
cloudySharp: L,
|
|
111
85
|
cloudyNight: U,
|
|
112
86
|
cloudyNightOutline: N,
|
|
113
87
|
cloudyNightSharp: M,
|
|
@@ -118,14 +92,14 @@ function G() {
|
|
|
118
92
|
thunderstormOutline: I,
|
|
119
93
|
thunderstormSharp: k,
|
|
120
94
|
snow: E,
|
|
121
|
-
snowOutline:
|
|
122
|
-
snowSharp:
|
|
123
|
-
helpCircle:
|
|
124
|
-
helpCircleOutline:
|
|
125
|
-
helpCircleSharp:
|
|
126
|
-
}), a =
|
|
95
|
+
snowOutline: x,
|
|
96
|
+
snowSharp: O,
|
|
97
|
+
helpCircle: C,
|
|
98
|
+
helpCircleOutline: b,
|
|
99
|
+
helpCircleSharp: S
|
|
100
|
+
}), a = w(window)), a;
|
|
127
101
|
}
|
|
128
|
-
const Q = (
|
|
102
|
+
const Q = (e) => String(e || "").toUpperCase() === "F" ? "F" : "C", V = (e) => e === "outline" || e === "sharp" || e === "filled" ? e : "filled", X = (e, t) => t === "filled" ? e : `${e}-${t}`, Y = {
|
|
129
103
|
// 01d - sunny
|
|
130
104
|
"01d": "sunny",
|
|
131
105
|
"01n": "moon",
|
|
@@ -153,8 +127,8 @@ const Q = (n) => String(n || "").toUpperCase() === "F" ? "F" : "C", V = (n) => n
|
|
|
153
127
|
};
|
|
154
128
|
class Z extends HTMLElement {
|
|
155
129
|
static observedAttributes = ["city", "scale", "icon-style", "api-key"];
|
|
156
|
-
#
|
|
157
|
-
#
|
|
130
|
+
#e = null;
|
|
131
|
+
#n = this.attachShadow({ mode: "open" });
|
|
158
132
|
#t = null;
|
|
159
133
|
connectedCallback() {
|
|
160
134
|
G().then(() => {
|
|
@@ -162,7 +136,7 @@ class Z extends HTMLElement {
|
|
|
162
136
|
});
|
|
163
137
|
}
|
|
164
138
|
disconnectedCallback() {
|
|
165
|
-
this.#
|
|
139
|
+
this.#e && this.#e.abort();
|
|
166
140
|
}
|
|
167
141
|
attributeChangedCallback() {
|
|
168
142
|
this.isConnected && this.#i();
|
|
@@ -180,8 +154,8 @@ class Z extends HTMLElement {
|
|
|
180
154
|
return (this.getAttribute("api-key") || "").trim();
|
|
181
155
|
}
|
|
182
156
|
#o() {
|
|
183
|
-
this.#
|
|
184
|
-
const t = this.#
|
|
157
|
+
this.#n.innerHTML = `<style>${B}</style>${J}`;
|
|
158
|
+
const t = this.#n.querySelector(".weather-widget");
|
|
185
159
|
t && (this.#t = {
|
|
186
160
|
root: t,
|
|
187
161
|
loading: t.querySelector('[part="loading"]'),
|
|
@@ -198,8 +172,8 @@ class Z extends HTMLElement {
|
|
|
198
172
|
#r(t) {
|
|
199
173
|
this.#t && (this.#t.loading.hidden = !0, this.#t.content.hidden = !0, this.#t.error.hidden = !1, this.#t.error.textContent = t);
|
|
200
174
|
}
|
|
201
|
-
#a({ cityName: t, iconName:
|
|
202
|
-
this.#t && (this.#t.loading.hidden = !0, this.#t.error.hidden = !0, this.#t.content.hidden = !1, this.#t.city.textContent = t, this.#t.icon.setAttribute("name",
|
|
175
|
+
#a({ cityName: t, iconName: n, tempText: r }) {
|
|
176
|
+
this.#t && (this.#t.loading.hidden = !0, this.#t.error.hidden = !0, this.#t.content.hidden = !1, this.#t.city.textContent = t, this.#t.icon.setAttribute("name", n), this.#t.temp.textContent = r);
|
|
203
177
|
}
|
|
204
178
|
async #i() {
|
|
205
179
|
const t = this.city;
|
|
@@ -207,33 +181,33 @@ class Z extends HTMLElement {
|
|
|
207
181
|
this.#r("Missing city");
|
|
208
182
|
return;
|
|
209
183
|
}
|
|
210
|
-
this.#
|
|
184
|
+
this.#e && this.#e.abort(), this.#e = new AbortController(), this.#s();
|
|
211
185
|
try {
|
|
212
|
-
const
|
|
213
|
-
if (!
|
|
186
|
+
const n = this.apiKey;
|
|
187
|
+
if (!n) throw new Error("Missing API key");
|
|
214
188
|
const r = new URL("https://api.openweathermap.org/geo/1.0/direct");
|
|
215
|
-
r.searchParams.set("q", t), r.searchParams.set("limit", "1"), r.searchParams.set("appid",
|
|
216
|
-
const c = await fetch(r, { signal: this.#
|
|
189
|
+
r.searchParams.set("q", t), r.searchParams.set("limit", "1"), r.searchParams.set("appid", n);
|
|
190
|
+
const c = await fetch(r, { signal: this.#e.signal });
|
|
217
191
|
if (!c.ok) throw new Error("City not found");
|
|
218
192
|
const l = await c.json(), o = l && l[0];
|
|
219
193
|
if (!o) throw new Error("City not found");
|
|
220
194
|
const i = new URL(
|
|
221
195
|
"https://api.openweathermap.org/data/2.5/weather"
|
|
222
196
|
);
|
|
223
|
-
i.searchParams.set("lat", String(o.lat)), i.searchParams.set("lon", String(o.lon)), i.searchParams.set("units", "metric"), i.searchParams.set("appid",
|
|
197
|
+
i.searchParams.set("lat", String(o.lat)), i.searchParams.set("lon", String(o.lon)), i.searchParams.set("units", "metric"), i.searchParams.set("appid", n);
|
|
224
198
|
const h = await fetch(i, {
|
|
225
|
-
signal: this.#
|
|
199
|
+
signal: this.#e.signal
|
|
226
200
|
});
|
|
227
201
|
if (!h.ok) throw new Error("Weather not available");
|
|
228
|
-
const s = await h.json(), d = s?.main?.temp,
|
|
202
|
+
const s = await h.json(), d = s?.main?.temp, y = this.scale === "F" ? Math.round(d * 9 / 5 + 32) : Math.round(d), u = s?.weather?.[0]?.icon, p = u && Y[u] || "help-circle", m = X(p, this.iconStyle), g = s?.name || t;
|
|
229
203
|
this.#a({
|
|
230
204
|
cityName: g,
|
|
231
205
|
iconName: m,
|
|
232
|
-
tempText: `${
|
|
206
|
+
tempText: `${y}°${this.scale}`
|
|
233
207
|
});
|
|
234
|
-
} catch (
|
|
235
|
-
if (
|
|
236
|
-
const r =
|
|
208
|
+
} catch (n) {
|
|
209
|
+
if (n instanceof DOMException && n.name === "AbortError") return;
|
|
210
|
+
const r = n instanceof Error ? n.message : "Failed to fetch weather";
|
|
237
211
|
this.#r(r);
|
|
238
212
|
}
|
|
239
213
|
}
|
package/package.json
CHANGED
package/src/styles.css
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
:host {
|
|
2
|
+
--color-primary: #000;
|
|
2
3
|
display: inline-block;
|
|
3
4
|
}
|
|
4
5
|
|
|
@@ -6,23 +7,13 @@
|
|
|
6
7
|
display: inline-flex;
|
|
7
8
|
flex-direction: column;
|
|
8
9
|
gap: 8px;
|
|
9
|
-
|
|
10
|
-
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
|
|
11
|
-
border-radius: 16px;
|
|
12
|
-
color: white;
|
|
10
|
+
color: var(--color-primary, #000);
|
|
13
11
|
text-align: center;
|
|
14
|
-
|
|
15
|
-
min-width: 250px;
|
|
12
|
+
font-size: 1rem;
|
|
16
13
|
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
|
|
17
14
|
Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
18
15
|
}
|
|
19
16
|
|
|
20
|
-
.loading,
|
|
21
|
-
.error {
|
|
22
|
-
font-size: 1rem;
|
|
23
|
-
padding: 1rem;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
17
|
.error {
|
|
27
18
|
color: #ffebee;
|
|
28
19
|
background: rgba(255, 0, 0, 0.2);
|
|
@@ -32,24 +23,7 @@
|
|
|
32
23
|
.weather-content {
|
|
33
24
|
display: flex;
|
|
34
25
|
flex-direction: row;
|
|
35
|
-
gap:
|
|
36
|
-
font-size: 2.5rem;
|
|
37
|
-
font-weight: 700;
|
|
26
|
+
gap: 5px;
|
|
38
27
|
align-items: center;
|
|
39
28
|
justify-content: center;
|
|
40
29
|
}
|
|
41
|
-
|
|
42
|
-
.city-name {
|
|
43
|
-
font-size: 1.5rem;
|
|
44
|
-
font-weight: 600;
|
|
45
|
-
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
.icon {
|
|
49
|
-
font-size: 3rem;
|
|
50
|
-
text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
.temperature {
|
|
54
|
-
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
|
|
55
|
-
}
|