@justanarthur/modo-ui-notch 0.2.0 → 0.4.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.
Files changed (148) hide show
  1. package/README.md +11 -11
  2. package/blocks/activities/agenda/examples.mdx +29 -5
  3. package/blocks/activities/agenda/index.tsx +438 -181
  4. package/blocks/activities/clock/examples.mdx +89 -0
  5. package/blocks/activities/clock/index.tsx +917 -0
  6. package/blocks/activities/now-playing/examples.mdx +40 -13
  7. package/blocks/activities/now-playing/index.tsx +399 -120
  8. package/blocks/activities/weather/examples.mdx +123 -0
  9. package/blocks/activities/weather/index.tsx +863 -0
  10. package/blocks/app/expanded-island/examples.mdx +8 -7
  11. package/blocks/app/expanded-island/index.tsx +22 -4
  12. package/blocks/app/settings-window/examples.mdx +14 -2
  13. package/blocks/app/settings-window/index.tsx +33 -10
  14. package/blocks/system/clipboard/examples.mdx +91 -0
  15. package/blocks/system/clipboard/index.tsx +773 -0
  16. package/blocks/system/recent-files/examples.mdx +80 -0
  17. package/blocks/system/recent-files/index.tsx +627 -0
  18. package/blocks/system/system-stats/examples.mdx +46 -9
  19. package/blocks/system/system-stats/index.tsx +445 -104
  20. package/components/indicators/media-scrubber/examples.mdx +8 -0
  21. package/components/indicators/media-scrubber/index.tsx +37 -13
  22. package/components/island/island/examples.mdx +4 -4
  23. package/components/island/island/index.tsx +2 -2
  24. package/components/island/island-flash/examples.mdx +15 -18
  25. package/components/island/island-flash/index.tsx +4 -4
  26. package/components/island/island-tabs/examples.mdx +5 -5
  27. package/components/island/widget/examples.mdx +76 -0
  28. package/components/island/widget/index.tsx +465 -0
  29. package/dist/blocks/activities/agenda/index.d.ts +2 -1
  30. package/dist/blocks/activities/agenda/index.js +3 -3
  31. package/dist/blocks/activities/clock/index.d.ts +3 -0
  32. package/dist/blocks/activities/clock/index.js +25 -0
  33. package/dist/blocks/activities/now-playing/index.d.ts +4 -2
  34. package/dist/blocks/activities/now-playing/index.js +5 -3
  35. package/dist/blocks/activities/weather/index.d.ts +3 -0
  36. package/dist/blocks/activities/weather/index.js +21 -0
  37. package/dist/blocks/app/expanded-island/index.d.ts +1 -1
  38. package/dist/blocks/app/expanded-island/index.js +4 -3
  39. package/dist/blocks/app/settings-window/index.d.ts +1 -1
  40. package/dist/blocks/app/settings-window/index.js +1 -1
  41. package/dist/blocks/system/clipboard/index.d.ts +3 -0
  42. package/dist/blocks/system/clipboard/index.js +17 -0
  43. package/dist/blocks/system/recent-files/index.d.ts +3 -0
  44. package/dist/blocks/system/recent-files/index.js +19 -0
  45. package/dist/blocks/system/system-stats/index.d.ts +3 -2
  46. package/dist/blocks/system/system-stats/index.js +4 -1
  47. package/dist/components/indicators/media-scrubber/index.d.ts +1 -1
  48. package/dist/components/indicators/media-scrubber/index.js +1 -1
  49. package/dist/components/island/island/index.d.ts +1 -1
  50. package/dist/components/island/island/index.js +2 -2
  51. package/dist/components/island/island-flash/index.d.ts +1 -1
  52. package/dist/components/island/island-flash/index.js +1 -1
  53. package/dist/components/island/island-tabs/index.js +2 -2
  54. package/dist/components/island/widget/index.d.ts +2 -0
  55. package/dist/components/island/widget/index.js +32 -0
  56. package/dist/index.d.ts +15 -15
  57. package/dist/index.js +104 -58
  58. package/dist/lib/artwork.d.ts +2 -0
  59. package/dist/lib/artwork.js +8 -0
  60. package/dist/lib/geometry.js +1 -1
  61. package/dist/shared/{chunk-yngkkxz2.d.ts → chunk-0ym9mcqc.d.ts} +10 -6
  62. package/dist/shared/chunk-2b6gpn8k.js +444 -0
  63. package/dist/shared/chunk-3hcnf4yx.js +481 -0
  64. package/dist/shared/chunk-42wab62s.d.ts +197 -0
  65. package/dist/shared/{chunk-zvh0d3qb.js → chunk-61t2dfte.js} +1 -1
  66. package/dist/shared/{chunk-9nhpg9t8.d.ts → chunk-6tnq6p89.d.ts} +9 -1
  67. package/dist/shared/{chunk-hpsabfsd.d.ts → chunk-81fpptwp.d.ts} +1 -1
  68. package/dist/shared/chunk-93dtpjz7.js +789 -0
  69. package/dist/shared/{chunk-wwjxs75m.js → chunk-9xtcv3sy.js} +26 -12
  70. package/dist/shared/chunk-a6njb8db.d.ts +129 -0
  71. package/dist/shared/chunk-ab8mdg0n.d.ts +149 -0
  72. package/dist/shared/chunk-evqpf92s.d.ts +168 -0
  73. package/dist/shared/{chunk-6c1jbefs.d.ts → chunk-fexwzycv.d.ts} +3 -3
  74. package/dist/shared/chunk-gcyspvgr.js +582 -0
  75. package/dist/shared/chunk-gejenfap.d.ts +25 -0
  76. package/dist/shared/chunk-gkj0hjrn.d.ts +136 -0
  77. package/dist/shared/{chunk-rr5e0g15.d.ts → chunk-j755a1a5.d.ts} +7 -1
  78. package/dist/shared/chunk-n739am8j.d.ts +112 -0
  79. package/dist/shared/{chunk-78w7qwp0.js → chunk-ndf79pv5.js} +1 -1
  80. package/dist/shared/chunk-nwzpekvy.js +774 -0
  81. package/dist/shared/{chunk-rpfwv3pz.js → chunk-r471sknr.js} +1 -1
  82. package/dist/shared/chunk-sbrtesdg.d.ts +138 -0
  83. package/dist/shared/chunk-swc7dqpw.d.ts +115 -0
  84. package/dist/shared/chunk-t9wqhgzt.js +765 -0
  85. package/dist/shared/chunk-w5h6ay93.js +72 -0
  86. package/dist/shared/{chunk-mp1nx0ez.js → chunk-wayv5rhr.js} +15 -4
  87. package/dist/shared/chunk-wf0jtzhs.js +295 -0
  88. package/dist/shared/{chunk-sbnrhy5m.js → chunk-y0188bbn.js} +2 -2
  89. package/dist/shared/chunk-yq5svpk7.js +524 -0
  90. package/dist/shared/{chunk-s33y1pcq.js → chunk-z3tj1ydg.js} +23 -10
  91. package/dist/styles.css +151 -49
  92. package/index.ts +7 -6
  93. package/lib/artwork.ts +112 -0
  94. package/lib/geometry.ts +1 -1
  95. package/mock/activities.tsx +9 -32
  96. package/mock/artwork.tsx +13 -1
  97. package/mock/clipboard.tsx +113 -0
  98. package/mock/clock.tsx +77 -0
  99. package/mock/data.ts +78 -44
  100. package/mock/files.tsx +83 -0
  101. package/mock/island-content.tsx +79 -122
  102. package/mock/screen.tsx +76 -0
  103. package/mock/settings.tsx +370 -91
  104. package/mock/system.ts +49 -0
  105. package/mock/weather.tsx +135 -0
  106. package/package.json +7 -2
  107. package/blocks/activities/shelf/examples.mdx +0 -33
  108. package/blocks/activities/shelf/index.tsx +0 -289
  109. package/blocks/activities/timer/examples.mdx +0 -52
  110. package/blocks/activities/timer/index.tsx +0 -292
  111. package/blocks/system/controls/examples.mdx +0 -34
  112. package/blocks/system/controls/index.tsx +0 -167
  113. package/blocks/system/devices/examples.mdx +0 -25
  114. package/blocks/system/devices/index.tsx +0 -172
  115. package/blocks/system/power-alert/examples.mdx +0 -32
  116. package/blocks/system/power-alert/index.tsx +0 -89
  117. package/components/indicators/battery-glyph/examples.mdx +0 -22
  118. package/components/indicators/battery-glyph/index.tsx +0 -91
  119. package/dist/blocks/activities/shelf/index.d.ts +0 -2
  120. package/dist/blocks/activities/shelf/index.js +0 -14
  121. package/dist/blocks/activities/timer/index.d.ts +0 -2
  122. package/dist/blocks/activities/timer/index.js +0 -19
  123. package/dist/blocks/system/controls/index.d.ts +0 -2
  124. package/dist/blocks/system/controls/index.js +0 -10
  125. package/dist/blocks/system/devices/index.d.ts +0 -2
  126. package/dist/blocks/system/devices/index.js +0 -12
  127. package/dist/blocks/system/power-alert/index.d.ts +0 -2
  128. package/dist/blocks/system/power-alert/index.js +0 -9
  129. package/dist/components/indicators/battery-glyph/index.d.ts +0 -2
  130. package/dist/components/indicators/battery-glyph/index.js +0 -9
  131. package/dist/shared/chunk-2qpcsv5h.d.ts +0 -45
  132. package/dist/shared/chunk-45gytfr1.d.ts +0 -84
  133. package/dist/shared/chunk-50ffk4r7.d.ts +0 -69
  134. package/dist/shared/chunk-5w0yck07.d.ts +0 -32
  135. package/dist/shared/chunk-7hr53mby.js +0 -116
  136. package/dist/shared/chunk-b8jb1w72.js +0 -145
  137. package/dist/shared/chunk-cpewy6yg.js +0 -178
  138. package/dist/shared/chunk-e3e4s4ds.d.ts +0 -77
  139. package/dist/shared/chunk-enm6td0p.js +0 -280
  140. package/dist/shared/chunk-fvedaas2.d.ts +0 -33
  141. package/dist/shared/chunk-gex38j8x.d.ts +0 -82
  142. package/dist/shared/chunk-hwtxpzg0.js +0 -69
  143. package/dist/shared/chunk-phb53x1f.js +0 -252
  144. package/dist/shared/chunk-r8esmjfs.d.ts +0 -42
  145. package/dist/shared/chunk-rcp1g9ca.js +0 -70
  146. package/dist/shared/chunk-rx9sphz2.js +0 -266
  147. package/dist/shared/chunk-xdeek16r.d.ts +0 -58
  148. package/dist/shared/chunk-xkzjk1tq.js +0 -206
@@ -0,0 +1,863 @@
1
+ /*!
2
+ * Local addition: a notch app's weather, the temperature now, the next
3
+ * hours and tomorrow, with the location and loading states around it and a
4
+ * flash before rain. Presentational: the forecast comes in as props, shaped
5
+ * so a host can fill it from Open-Meteo (`conditionFromCode` reads its WMO
6
+ * weather codes).
7
+ */
8
+
9
+ 'use client'
10
+
11
+ import Button from '@justanarthur/modo-ui/components/actions/button'
12
+ import Sparkline from '@justanarthur/modo-ui/components/data-display/sparkline'
13
+ import { type SizeVariant, useSize } from '@justanarthur/modo-ui/lib/size-context'
14
+ import { SURFACE_BG } from '@justanarthur/modo-ui/lib/surface-classes'
15
+ import { useLift } from '@justanarthur/modo-ui/lib/surface-context'
16
+ import { cn } from '@justanarthur/modo-ui/lib/utils'
17
+ import Skeleton from '@justanarthur/modo-ui/primitives/content/skeleton'
18
+ import ScrollArea from '@justanarthur/modo-ui/primitives/layout/scroll-area'
19
+ import {
20
+ ArrowDown,
21
+ ArrowUp,
22
+ Cloud,
23
+ CloudDrizzle,
24
+ CloudFog,
25
+ CloudHail,
26
+ CloudLightning,
27
+ CloudMoon,
28
+ CloudOff,
29
+ CloudRain,
30
+ CloudRainWind,
31
+ CloudSnow,
32
+ CloudSun,
33
+ Droplets,
34
+ LocateOff,
35
+ type LucideIcon,
36
+ MapPin,
37
+ Moon,
38
+ RotateCw,
39
+ Sun,
40
+ Sunrise,
41
+ Sunset,
42
+ Thermometer,
43
+ Umbrella,
44
+ WifiOff,
45
+ Wind,
46
+ } from 'lucide-react'
47
+ import type { ReactNode } from 'react'
48
+ import IslandFlash from '../../../components/island/island-flash'
49
+ import Widget, { type WidgetLayout } from '../../../components/island/widget'
50
+ import { MINUTE, useEdgeClock } from '../../../lib/clock'
51
+
52
+ type WeatherCondition =
53
+ | 'clear'
54
+ | 'partly-cloudy'
55
+ | 'cloudy'
56
+ | 'fog'
57
+ | 'drizzle'
58
+ | 'rain'
59
+ | 'heavy-rain'
60
+ | 'sleet'
61
+ | 'snow'
62
+ | 'thunderstorm'
63
+
64
+ type WeatherUnit = 'celsius' | 'fahrenheit'
65
+ type WeatherAccess = 'granted' | 'undetermined' | 'denied'
66
+ type WeatherStatus = 'ready' | 'loading' | 'error' | 'offline'
67
+
68
+ /** The weather now: Open-Meteo's `current` block. Temperatures in °C, wind in km/h. */
69
+ interface WeatherNow {
70
+ /** When it was read, in epoch ms: the "updated" line counts from it. */
71
+ time: number
72
+ temperature: number
73
+ /** How warm it feels, in °C: `apparent_temperature`. */
74
+ apparent?: number
75
+ condition: WeatherCondition
76
+ /** Daylight, for the sun or the moon in the glyph: `is_day`. Defaults to `true`. */
77
+ isDay?: boolean
78
+ /** Wind speed in km/h. */
79
+ wind?: number
80
+ /** Relative humidity, 0–100. */
81
+ humidity?: number
82
+ /** The UV index. */
83
+ uv?: number
84
+ }
85
+
86
+ /** One hour of the forecast: Open-Meteo's `hourly` arrays, zipped. */
87
+ interface WeatherHour {
88
+ /** The hour's start, in epoch ms. */
89
+ time: number
90
+ temperature: number
91
+ condition: WeatherCondition
92
+ /** Defaults to `true`. */
93
+ isDay?: boolean
94
+ /** The chance of rain or snow, 0–100: `precipitation_probability`. */
95
+ precipitation?: number
96
+ }
97
+
98
+ /** One day of the forecast: Open-Meteo's `daily` arrays, zipped. */
99
+ interface WeatherDay {
100
+ /** The day's start, in epoch ms. */
101
+ date: number
102
+ condition: WeatherCondition
103
+ high: number
104
+ low: number
105
+ /** The day's highest chance of rain or snow, 0–100: `precipitation_probability_max`. */
106
+ precipitation?: number
107
+ /** When the sun rises, in epoch ms: `sunrise`. The tall layout lists today's. */
108
+ sunrise?: number
109
+ /** When the sun sets, in epoch ms: `sunset`. The tall layout lists today's. */
110
+ sunset?: number
111
+ }
112
+
113
+ interface WeatherProps {
114
+ /** Where the forecast is for: a city's name. */
115
+ location?: string
116
+ /** The weather now; without it the widget shows its state (`status`, `access`) or loads. */
117
+ current?: WeatherNow | null
118
+ /** The hourly forecast, in any order: the hours from now on fill the strip. */
119
+ hours?: WeatherHour[]
120
+ /** The daily forecast, today first: today gives the high and low, the next day the Tomorrow row. */
121
+ days?: WeatherDay[]
122
+ /** Shows temperatures in this unit (and wind in mph for `'fahrenheit'`); the data stays in °C and km/h. Defaults to `'celsius'`. */
123
+ unit?: WeatherUnit
124
+ /** Whether the location may be read: undetermined asks, denied points to the system's settings. Defaults to `'granted'`. */
125
+ access?: WeatherAccess
126
+ /** How the last fetch went. `loading` shows placeholders until there is a `current`; `error` and `offline` keep the last forecast up and say so, or explain without one. Defaults to `'ready'`. */
127
+ status?: WeatherStatus
128
+ /** The moment the forecast is read at, in epoch ms. Defaults to now, re-read on each minute. */
129
+ now?: number
130
+ /** Formats the hours. Defaults to the browser's locale. */
131
+ locale?: Intl.LocalesArgument
132
+ /** Called by the refresh button, and by Try again after an error; without it neither shows. */
133
+ onRefresh?: () => void
134
+ /** Called by Allow location while access is undetermined. */
135
+ onRequestAccess?: () => void
136
+ /** Called by Open System Settings while access is denied. */
137
+ onOpenSettings?: () => void
138
+ /** `tall` is the open island's tall snap: the hero larger, the next 24 hours as a scrolling strip with a temperature curve and the chance of rain, the week as rows with a range bar, and the wind, humidity, UV and the sun's times as details. Defaults to `'fit'`. */
139
+ layout?: WidgetLayout
140
+ /** Classes for the root, which fills its container. */
141
+ className?: string
142
+ }
143
+
144
+ const LABEL: Record<WeatherCondition, string> = {
145
+ clear: 'Clear',
146
+ 'partly-cloudy': 'Partly cloudy',
147
+ cloudy: 'Cloudy',
148
+ fog: 'Fog',
149
+ drizzle: 'Drizzle',
150
+ rain: 'Rain',
151
+ 'heavy-rain': 'Heavy rain',
152
+ sleet: 'Sleet',
153
+ snow: 'Snow',
154
+ thunderstorm: 'Thunderstorm',
155
+ }
156
+
157
+ const GLYPH: Record<WeatherCondition, LucideIcon> = {
158
+ clear: Sun,
159
+ 'partly-cloudy': CloudSun,
160
+ cloudy: Cloud,
161
+ fog: CloudFog,
162
+ drizzle: CloudDrizzle,
163
+ rain: CloudRain,
164
+ 'heavy-rain': CloudRainWind,
165
+ sleet: CloudHail,
166
+ snow: CloudSnow,
167
+ thunderstorm: CloudLightning,
168
+ }
169
+
170
+ // Literal table: UnoCSS only generates the classes it can read whole. The hue is the condition's: sun, water, the rest plain.
171
+ const HUE: Record<WeatherCondition, string> = {
172
+ clear: 'text-status-warning',
173
+ 'partly-cloudy': 'text-status-warning',
174
+ cloudy: 'text-foreground',
175
+ fog: 'text-foreground',
176
+ drizzle: 'text-status-info',
177
+ rain: 'text-status-info',
178
+ 'heavy-rain': 'text-status-info',
179
+ sleet: 'text-status-info',
180
+ snow: 'text-foreground',
181
+ thunderstorm: 'text-status-info',
182
+ }
183
+
184
+ const HERO_GLYPH: Record<SizeVariant, number> = { default: 30, compact: 24 }
185
+ const STRIP_GLYPH: Record<SizeVariant, number> = { default: 16, compact: 14 }
186
+
187
+ const SHEET_GLYPH: Record<SizeVariant, number> = { default: 44, compact: 36 }
188
+
189
+ /** A chance of rain worth showing, in %. */
190
+ const WET = 30
191
+ /** Hours the strip shows. */
192
+ const HOURS = 6
193
+ /** Hours the tall layout's strip scrolls through. */
194
+ const DAY_HOURS = 24
195
+ /** Days the tall layout lists. */
196
+ const WEEK = 7
197
+
198
+ /**
199
+ * Open-Meteo's `weather_code` (WMO 4677) as a condition.
200
+ *
201
+ * @example conditionFromCode(61) // 'rain'
202
+ */
203
+ function conditionFromCode(code: number): WeatherCondition {
204
+ if (code === 0) return 'clear'
205
+ if (code <= 2) return 'partly-cloudy'
206
+ if (code === 3) return 'cloudy'
207
+ if (code === 45 || code === 48) return 'fog'
208
+ if (code >= 51 && code <= 55) return 'drizzle'
209
+ if (code === 56 || code === 57 || code === 66 || code === 67) return 'sleet'
210
+ if (code === 65 || code === 82) return 'heavy-rain'
211
+ if ((code >= 61 && code <= 63) || code === 80 || code === 81) return 'rain'
212
+ if ((code >= 71 && code <= 77) || code === 85 || code === 86) return 'snow'
213
+ if (code >= 95) return 'thunderstorm'
214
+ return 'cloudy'
215
+ }
216
+
217
+ /** The glyph for a condition, the moon's at night. */
218
+ function glyphFor(condition: WeatherCondition, isDay = true): LucideIcon {
219
+ if (!isDay && condition === 'clear') return Moon
220
+ if (!isDay && condition === 'partly-cloudy') return CloudMoon
221
+ return GLYPH[condition]
222
+ }
223
+
224
+ /** The hue for a condition: the sun's only by day. */
225
+ function hueFor(condition: WeatherCondition, isDay = true) {
226
+ if (!isDay && (condition === 'clear' || condition === 'partly-cloudy')) return 'text-foreground'
227
+ return HUE[condition]
228
+ }
229
+
230
+ const degrees = (celsius: number, unit: WeatherUnit) =>
231
+ Math.round(unit === 'fahrenheit' ? (celsius * 9) / 5 + 32 : celsius)
232
+
233
+ const temp = (celsius: number, unit: WeatherUnit) => `${degrees(celsius, unit)}°`
234
+
235
+ const speed = (kmh: number, unit: WeatherUnit) =>
236
+ unit === 'fahrenheit' ? `${Math.round(kmh / 1.609)} mph` : `${Math.round(kmh)} km/h`
237
+
238
+ const hourOf = (ms: number, locale?: Intl.LocalesArgument) =>
239
+ new Date(ms).toLocaleTimeString(locale, { hour: 'numeric' })
240
+
241
+ const clockOf = (ms: number, locale?: Intl.LocalesArgument) =>
242
+ new Date(ms).toLocaleTimeString(locale, { hour: 'numeric', minute: '2-digit' })
243
+
244
+ /** The UV index in the WHO's words. */
245
+ function uvLevel(uv: number) {
246
+ if (uv < 3) return 'Low'
247
+ if (uv < 6) return 'Moderate'
248
+ if (uv < 8) return 'High'
249
+ if (uv < 11) return 'Very high'
250
+ return 'Extreme'
251
+ }
252
+
253
+ function ago(ms: number) {
254
+ const minutes = Math.floor(ms / MINUTE)
255
+ if (minutes < 1) return 'Just now'
256
+ if (minutes < 60) return `${minutes} min ago`
257
+ return `${Math.floor(minutes / 60)} h ago`
258
+ }
259
+
260
+ /** The first hour, from `now`, with a chance of rain worth saying, while it isn't raining already. */
261
+ function rainAhead(current: WeatherNow, hours: WeatherHour[]) {
262
+ if (HUE[current.condition] === 'text-status-info') return undefined
263
+ return hours.find(hour => (hour.precipitation ?? 0) >= 50)
264
+ }
265
+
266
+ interface GlyphProps {
267
+ condition: WeatherCondition
268
+ isDay?: boolean
269
+ size: number
270
+ className?: string
271
+ }
272
+
273
+ function Glyph({ condition, isDay, size, className }: GlyphProps) {
274
+ const Icon = glyphFor(condition, isDay)
275
+ return <Icon aria-hidden size={size} strokeWidth={1.5} className={cn('shrink-0', className)} />
276
+ }
277
+
278
+ function Hours({ hours, unit, locale }: { hours: WeatherHour[]; unit: WeatherUnit; locale?: Intl.LocalesArgument }) {
279
+ const size = useSize()
280
+ const wet = hours.some(hour => (hour.precipitation ?? 0) >= WET)
281
+ return (
282
+ <div className="flex min-w-0 justify-between">
283
+ {hours.map((hour, i) => (
284
+ <div key={hour.time} className="flex min-w-0 flex-1 flex-col items-center gap-1 tabular-nums">
285
+ <span className="whitespace-nowrap text-caption text-muted-foreground">
286
+ {i === 0 ? 'Now' : hourOf(hour.time, locale)}
287
+ </span>
288
+ <Glyph
289
+ condition={hour.condition}
290
+ isDay={hour.isDay}
291
+ size={STRIP_GLYPH[size.variant]}
292
+ className="text-foreground"
293
+ />
294
+ <span className="text-body text-foreground">{temp(hour.temperature, unit)}</span>
295
+ {wet && (
296
+ <span className="h-4 text-caption text-status-info">
297
+ {(hour.precipitation ?? 0) >= WET && `${hour.precipitation}%`}
298
+ </span>
299
+ )}
300
+ </div>
301
+ ))}
302
+ </div>
303
+ )
304
+ }
305
+
306
+ function Tomorrow({ day, unit }: { day: WeatherDay; unit: WeatherUnit }) {
307
+ return (
308
+ <Widget.Row
309
+ leading="Tomorrow"
310
+ trailing={
311
+ <>
312
+ {(day.precipitation ?? 0) >= WET && <span className="text-status-info">{day.precipitation}%</span>}
313
+ <span className="text-foreground">{temp(day.high, unit)}</span>
314
+ <span>{temp(day.low, unit)}</span>
315
+ </>
316
+ }
317
+ >
318
+ <span className="flex min-w-0 items-center gap-2">
319
+ <Glyph condition={day.condition} size={14} className="text-muted-foreground" />
320
+ <span className="truncate">{LABEL[day.condition]}</span>
321
+ </span>
322
+ </Widget.Row>
323
+ )
324
+ }
325
+
326
+ interface StripProps {
327
+ hours: WeatherHour[]
328
+ unit: WeatherUnit
329
+ locale?: Intl.LocalesArgument
330
+ }
331
+
332
+ /** The tall layout's day ahead: a column per hour (hour, glyph, temperature, chance of rain) with the temperature's curve through them, scrolling sideways. */
333
+ function HourStrip({ hours, unit, locale }: StripProps) {
334
+ const size = useSize()
335
+ const temperatures = hours.map(hour => hour.temperature)
336
+ const low = Math.min(...temperatures)
337
+ const wet = hours.some(hour => (hour.precipitation ?? 0) >= WET)
338
+ return (
339
+ <ScrollArea orientation="horizontal" viewportClassName="scroll-fade-x">
340
+ <div className="flex w-max flex-col gap-1.5 pb-3 tabular-nums">
341
+ <div className="flex">
342
+ {hours.map((hour, i) => (
343
+ <div key={hour.time} className="flex w-12 shrink-0 flex-col items-center gap-1.5">
344
+ <span className="whitespace-nowrap text-caption text-muted-foreground">
345
+ {i === 0 ? 'Now' : hourOf(hour.time, locale)}
346
+ </span>
347
+ <Glyph
348
+ condition={hour.condition}
349
+ isDay={hour.isDay}
350
+ size={STRIP_GLYPH[size.variant]}
351
+ className={hueFor(hour.condition, hour.isDay)}
352
+ />
353
+ </div>
354
+ ))}
355
+ </div>
356
+ {/* Inset half a column, so each point sits over its hour. */}
357
+ <div className="px-6">
358
+ <Sparkline
359
+ values={temperatures.map(value => value - low)}
360
+ max={Math.max(...temperatures) - low}
361
+ label={`Temperature over the next ${hours.length} hours`}
362
+ tone="muted"
363
+ />
364
+ </div>
365
+ <div className="flex">
366
+ {hours.map(hour => (
367
+ <div key={hour.time} className="flex w-12 shrink-0 flex-col items-center gap-0.5">
368
+ <span className="text-body text-foreground">{temp(hour.temperature, unit)}</span>
369
+ {wet && (
370
+ <span className="h-4 text-caption text-status-info">
371
+ {(hour.precipitation ?? 0) >= WET && `${hour.precipitation}%`}
372
+ </span>
373
+ )}
374
+ </div>
375
+ ))}
376
+ </div>
377
+ </div>
378
+ </ScrollArea>
379
+ )
380
+ }
381
+
382
+ interface WeekProps {
383
+ days: WeatherDay[]
384
+ unit: WeatherUnit
385
+ locale?: Intl.LocalesArgument
386
+ }
387
+
388
+ /** The tall layout's week: a row per day, its low and high on a bar spanning the week's range. */
389
+ function Week({ days, unit, locale }: WeekProps) {
390
+ const low = Math.min(...days.map(day => day.low))
391
+ const span = Math.max(...days.map(day => day.high)) - low || 1
392
+ return days.map((day, i) => (
393
+ <div key={day.date} className="flex h-8 min-w-0 items-center gap-3 text-body tabular-nums">
394
+ <span className="w-12 shrink-0 truncate text-foreground">
395
+ {i === 0 ? 'Today' : new Date(day.date).toLocaleDateString(locale, { weekday: 'short' })}
396
+ </span>
397
+ <Glyph condition={day.condition} size={16} className={hueFor(day.condition)} />
398
+ <span className="w-9 shrink-0 text-caption text-status-info">
399
+ {(day.precipitation ?? 0) >= WET && `${day.precipitation}%`}
400
+ </span>
401
+ <span className="w-8 shrink-0 text-right text-muted-foreground">{temp(day.low, unit)}</span>
402
+ <span className="relative h-1 min-w-12 flex-1 rounded-full bg-active">
403
+ <span
404
+ className="absolute inset-y-0 rounded-full bg-foreground"
405
+ style={{ left: `${((day.low - low) / span) * 100}%`, width: `${((day.high - day.low) / span) * 100}%` }}
406
+ />
407
+ </span>
408
+ <span className="w-8 shrink-0 text-foreground">{temp(day.high, unit)}</span>
409
+ </div>
410
+ ))
411
+ }
412
+
413
+ interface DetailProps {
414
+ icon: LucideIcon
415
+ label: string
416
+ children: ReactNode
417
+ }
418
+
419
+ /** One fact of the tall layout's details: a glyph, what it is, muted, and its value. */
420
+ function Detail({ icon: Icon, label, children }: DetailProps) {
421
+ return (
422
+ <div className="flex h-8 min-w-0 items-center gap-2 text-body">
423
+ <Icon aria-hidden size={14} strokeWidth={1.5} className="shrink-0 text-muted-foreground" />
424
+ <span className="min-w-0 flex-1 truncate text-muted-foreground">{label}</span>
425
+ <span className="shrink-0 whitespace-nowrap tabular-nums text-foreground">{children}</span>
426
+ </div>
427
+ )
428
+ }
429
+
430
+ /** The widget's shape while the first forecast loads: the header, the hero and the strip as placeholders. */
431
+ function Loading({ location, className }: { location?: string; className?: string }) {
432
+ return (
433
+ <Widget
434
+ className={className}
435
+ aside={
436
+ <div aria-hidden className="flex flex-col gap-3">
437
+ <Skeleton className="h-3 w-20" />
438
+ <div className="flex justify-between">
439
+ {Array.from({ length: HOURS }, (_, i) => (
440
+ <Skeleton key={i} className="h-14 w-7" />
441
+ ))}
442
+ </div>
443
+ <Skeleton className="h-3 w-full" />
444
+ </div>
445
+ }
446
+ >
447
+ <div aria-busy className="flex min-w-0 flex-col gap-2">
448
+ <Widget.Header icon={MapPin}>{location ?? 'Finding you…'}</Widget.Header>
449
+ <Skeleton className="h-8 w-28" />
450
+ <Skeleton className="h-4 w-40" />
451
+ <Skeleton className="h-3 w-56" />
452
+ </div>
453
+ </Widget>
454
+ )
455
+ }
456
+
457
+ interface PartProps {
458
+ /** The weather now. */
459
+ current: WeatherNow
460
+ }
461
+
462
+ /** The compact island's left wing: the condition's glyph in its hue. */
463
+ function WeatherLeading({ current }: PartProps) {
464
+ return (
465
+ <Glyph
466
+ condition={current.condition}
467
+ isDay={current.isDay}
468
+ size={16}
469
+ className={hueFor(current.condition, current.isDay)}
470
+ />
471
+ )
472
+ }
473
+
474
+ /** The compact island's right wing: the temperature. */
475
+ function WeatherTrailing({ current, unit = 'celsius' }: PartProps & { unit?: WeatherUnit }) {
476
+ return (
477
+ <span className="text-caption weight-semibold text-foreground tabular-nums">{temp(current.temperature, unit)}</span>
478
+ )
479
+ }
480
+
481
+ /** The split island's bubble: the condition's glyph on a disc. */
482
+ function WeatherMinimal({ current }: PartProps) {
483
+ const level = useLift(1)
484
+ return (
485
+ <span
486
+ role="img"
487
+ aria-label={LABEL[current.condition]}
488
+ className={cn(
489
+ 'flex size-6 items-center justify-center rounded-full',
490
+ SURFACE_BG[level],
491
+ hueFor(current.condition, current.isDay),
492
+ )}
493
+ >
494
+ <Glyph condition={current.condition} isDay={current.isDay} size={14} />
495
+ </span>
496
+ )
497
+ }
498
+
499
+ type WeatherAlertSeverity = 'notice' | 'warning' | 'severe'
500
+
501
+ // Literal tables: UnoCSS only generates the classes it can read whole.
502
+ const ALERT_TILE: Record<WeatherAlertSeverity, string> = {
503
+ notice: 'bg-status-info/16 text-status-info',
504
+ warning: 'bg-status-warning/16 text-status-warning',
505
+ severe: 'bg-status-error/16 text-status-error',
506
+ }
507
+
508
+ interface WeatherAlertProps {
509
+ /** What is coming: its glyph, and the title's noun. */
510
+ condition: WeatherCondition
511
+ /** When it starts, in epoch ms: the title counts down to it, `Rain in 15 min`, on each minute. */
512
+ startsAt?: number
513
+ /** The title in place of the countdown, for a warning: `Thunderstorm warning`. */
514
+ title?: string
515
+ /** One line more: how long, how much. */
516
+ caption?: string
517
+ /** How serious: the tile's hue, info, warning or error. Defaults to `'notice'`. */
518
+ severity?: WeatherAlertSeverity
519
+ /** The button's label. Defaults to `'Details'`. */
520
+ actionLabel?: string
521
+ /** Called by the button; without it the button hides. */
522
+ onAction?: () => void
523
+ }
524
+
525
+ /** The island's flash before the weather turns: `Rain in 15 min`, or a warning, with one action. */
526
+ function WeatherAlert({
527
+ condition,
528
+ startsAt,
529
+ title,
530
+ caption,
531
+ severity = 'notice',
532
+ actionLabel = 'Details',
533
+ onAction,
534
+ }: WeatherAlertProps) {
535
+ const now = useEdgeClock(startsAt ?? 0, startsAt !== undefined && title === undefined, MINUTE)
536
+ const left = startsAt === undefined ? 0 : Math.ceil((startsAt - now) / MINUTE)
537
+ const heading = title ?? (left > 0 ? `${LABEL[condition]} in ${left} min` : `${LABEL[condition]} now`)
538
+ return (
539
+ <IslandFlash
540
+ icon={
541
+ <span className={cn('flex size-9 items-center justify-center rounded-lg', ALERT_TILE[severity])}>
542
+ <Glyph condition={condition} size={16} />
543
+ </span>
544
+ }
545
+ title={heading}
546
+ caption={caption}
547
+ action={
548
+ onAction && (
549
+ <Button variant="secondary" size="compact" onClick={onAction}>
550
+ {actionLabel}
551
+ </Button>
552
+ )
553
+ }
554
+ />
555
+ )
556
+ }
557
+
558
+ /**
559
+ * A notch app's weather: the temperature now as the hero, the next hours
560
+ * and tomorrow beside it.
561
+ *
562
+ * The hero is the temperature in display type after the condition's glyph,
563
+ * in its hue (the sun's amber, rain's blue, the rest plain); under it the
564
+ * condition, how warm it feels and, when the next hours turn wet, around
565
+ * when. The meta line carries today's high and low, the chance of rain,
566
+ * the wind, and the humidity and UV index when given, the last giving way
567
+ * first. The header names the place and how long ago the forecast was
568
+ * read, with a refresh button when `onRefresh` is given. Beside it, past a
569
+ * hairline, the next six hours as a strip of hour, glyph and temperature,
570
+ * with the chance of rain under each once any hour is wet, then tomorrow as
571
+ * a row.
572
+ *
573
+ * The data is Open-Meteo's shape, zipped into objects: `current`, `hours`
574
+ * and `days` (today first), in °C and km/h; `unit="fahrenheit"` converts
575
+ * what it shows, wind in mph. `conditionFromCode` turns a WMO weather code
576
+ * into a condition.
577
+ *
578
+ * Before the location may be read it asks for it (`access="undetermined"`),
579
+ * and when access is off it says where to turn it on (`"denied"`). The
580
+ * first fetch shows placeholders (`status="loading"`); a failed one says so
581
+ * with Try again (`"error"`), and offline it says it will update when back
582
+ * (`"offline"`). Once there is a forecast, those keep it up and say so in
583
+ * the header instead.
584
+ *
585
+ * At the open island's tall snap (`layout="tall"`) it becomes a sheet: the
586
+ * hero larger, the condition, how warm it feels and today's high and low
587
+ * beside it; then the next 24 hours as a strip that scrolls sideways, each
588
+ * hour's glyph and temperature with the temperature's curve running through
589
+ * them and the chance of rain under each once any hour is wet; then, split
590
+ * by a hairline, the week as rows of day, glyph, chance of rain and the low
591
+ * and high on a bar spanning the week's range, and beside it the details:
592
+ * how warm it feels, the wind, the humidity, the UV index in words and
593
+ * today's sunrise and sunset (`sunrise` / `sunset` on the day).
594
+ *
595
+ * The other forms are parts: `Weather.Leading` and `Weather.Trailing` for
596
+ * the compact island's wings (the glyph and the temperature),
597
+ * `Weather.Minimal`, the glyph on a disc, for the split island's bubble,
598
+ * and `Weather.Alert`, the flash before rain or a warning. Nothing ticks but
599
+ * the minute of the "updated" line.
600
+ *
601
+ * @example {@include ./examples.mdx}
602
+ */
603
+ const Weather = Object.assign(
604
+ function Weather({
605
+ location,
606
+ current,
607
+ hours = [],
608
+ days = [],
609
+ unit = 'celsius',
610
+ access = 'granted',
611
+ status = 'ready',
612
+ now: nowProp,
613
+ locale,
614
+ onRefresh,
615
+ onRequestAccess,
616
+ onOpenSettings,
617
+ layout = 'fit',
618
+ className,
619
+ }: WeatherProps) {
620
+ const size = useSize()
621
+ const ticking = useEdgeClock(current?.time ?? 0, nowProp === undefined, MINUTE)
622
+ const now = nowProp ?? ticking
623
+
624
+ if (access === 'undetermined')
625
+ return (
626
+ <Widget.Message
627
+ icon={MapPin}
628
+ title="Weather where you are"
629
+ description="The temperature now, the next hours and tomorrow, for your location."
630
+ actions={
631
+ <Button variant="primary" size="compact" onClick={onRequestAccess}>
632
+ Allow location
633
+ </Button>
634
+ }
635
+ className={className}
636
+ />
637
+ )
638
+
639
+ if (access === 'denied')
640
+ return (
641
+ <Widget.Message
642
+ icon={LocateOff}
643
+ title="Location access is off"
644
+ description="Turn it on in System Settings › Privacy & Security › Location Services."
645
+ actions={
646
+ <Button variant="secondary" size="compact" onClick={onOpenSettings}>
647
+ Open System Settings
648
+ </Button>
649
+ }
650
+ className={className}
651
+ />
652
+ )
653
+
654
+ if (!current && status === 'error')
655
+ return (
656
+ <Widget.Message
657
+ icon={CloudOff}
658
+ title="Couldn't load the weather"
659
+ description="The forecast service didn't answer. It tries again on its own in a few minutes."
660
+ actions={
661
+ onRefresh && (
662
+ <Button variant="secondary" size="compact" onClick={onRefresh}>
663
+ Try again
664
+ </Button>
665
+ )
666
+ }
667
+ className={className}
668
+ />
669
+ )
670
+
671
+ if (!current && status === 'offline')
672
+ return (
673
+ <Widget.Message
674
+ icon={WifiOff}
675
+ title="You're offline"
676
+ description="The forecast loads as soon as you're back online."
677
+ className={className}
678
+ />
679
+ )
680
+
681
+ if (!current) return <Loading location={location} className={className} />
682
+
683
+ const hourStart = new Date(now).setMinutes(0, 0, 0)
684
+ const ahead = hours.filter(hour => hour.time >= hourStart).sort((a, b) => a.time - b.time)
685
+ const [today, tomorrow] = days
686
+ const rain = rainAhead(current, ahead)
687
+ const fresh = ago(Math.max(0, now - current.time))
688
+ const freshness =
689
+ status === 'offline' ? `Offline · ${fresh}` : status === 'error' ? `Not updated · ${fresh}` : fresh
690
+ const feels = current.apparent !== undefined && `Feels like ${temp(current.apparent, unit)}`
691
+ const soon = rain && `${LABEL[rain.condition]} around ${hourOf(rain.time, locale)}`
692
+ const header = (
693
+ <Widget.Header
694
+ icon={MapPin}
695
+ trailing={
696
+ <>
697
+ <span className="whitespace-nowrap pr-1 text-caption">{freshness}</span>
698
+ {onRefresh && <Widget.Action icon={RotateCw} label="Refresh" onClick={onRefresh} />}
699
+ </>
700
+ }
701
+ >
702
+ {location ?? 'Current location'}
703
+ </Widget.Header>
704
+ )
705
+
706
+ if (layout === 'tall') {
707
+ const week = days.slice(0, WEEK)
708
+ return (
709
+ <Widget.Sheet className={className}>
710
+ <div className="flex min-w-0 flex-col gap-2">
711
+ {header}
712
+ <div className="flex min-w-0 items-center gap-5">
713
+ <div className="flex shrink-0 items-center gap-3">
714
+ <Glyph
715
+ condition={current.condition}
716
+ isDay={current.isDay}
717
+ size={SHEET_GLYPH[size.variant]}
718
+ className={hueFor(current.condition, current.isDay)}
719
+ />
720
+ <Widget.Figure value={degrees(current.temperature, unit)} unit={unit === 'fahrenheit' ? '°F' : '°C'} />
721
+ </div>
722
+ <div className="flex min-w-0 flex-1 flex-col gap-0.5">
723
+ <Widget.Title>{LABEL[current.condition]}</Widget.Title>
724
+ {today && (
725
+ <Widget.Subtitle>
726
+ {[`H ${temp(today.high, unit)}`, `L ${temp(today.low, unit)}`, soon].filter(Boolean).join(' · ')}
727
+ </Widget.Subtitle>
728
+ )}
729
+ </div>
730
+ </div>
731
+ </div>
732
+
733
+ {ahead.length > 1 && (
734
+ <Widget.Section label="Next 24 hours">
735
+ <HourStrip hours={ahead.slice(0, DAY_HOURS)} unit={unit} locale={locale} />
736
+ </Widget.Section>
737
+ )}
738
+
739
+ <div className="flex min-w-0 items-start gap-5">
740
+ {week.length > 0 && (
741
+ <Widget.Section label={`${week.length}-day forecast`} className="flex-1">
742
+ <Week days={week} unit={unit} locale={locale} />
743
+ </Widget.Section>
744
+ )}
745
+ <Widget.Section label="Details" className="w-[38%] shrink-0 border-l border-border pl-5">
746
+ {current.apparent !== undefined && (
747
+ <Detail icon={Thermometer} label="Feels like">
748
+ {temp(current.apparent, unit)}
749
+ </Detail>
750
+ )}
751
+ {today?.precipitation !== undefined && (
752
+ <Detail icon={Umbrella} label="Chance of rain">
753
+ {today.precipitation}%
754
+ </Detail>
755
+ )}
756
+ {current.wind !== undefined && (
757
+ <Detail icon={Wind} label="Wind">
758
+ {speed(current.wind, unit)}
759
+ </Detail>
760
+ )}
761
+ {current.humidity !== undefined && (
762
+ <Detail icon={Droplets} label="Humidity">
763
+ {Math.round(current.humidity)}%
764
+ </Detail>
765
+ )}
766
+ {current.uv !== undefined && (
767
+ <Detail icon={Sun} label="UV index">
768
+ {Math.round(current.uv)} · {uvLevel(current.uv)}
769
+ </Detail>
770
+ )}
771
+ {today?.sunrise !== undefined && (
772
+ <Detail icon={Sunrise} label="Sunrise">
773
+ {clockOf(today.sunrise, locale)}
774
+ </Detail>
775
+ )}
776
+ {today?.sunset !== undefined && (
777
+ <Detail icon={Sunset} label="Sunset">
778
+ {clockOf(today.sunset, locale)}
779
+ </Detail>
780
+ )}
781
+ </Widget.Section>
782
+ </div>
783
+ </Widget.Sheet>
784
+ )
785
+ }
786
+
787
+ return (
788
+ <Widget
789
+ className={className}
790
+ aside={
791
+ ahead.length > 0 && (
792
+ <>
793
+ <Widget.Section label="Next hours">
794
+ <Hours hours={ahead.slice(0, HOURS)} unit={unit} locale={locale} />
795
+ </Widget.Section>
796
+ {tomorrow && (
797
+ <div className="mt-1.5">
798
+ <Tomorrow day={tomorrow} unit={unit} />
799
+ </div>
800
+ )}
801
+ </>
802
+ )
803
+ }
804
+ >
805
+ {header}
806
+
807
+ <div className="flex min-w-0 items-center gap-2.5">
808
+ <Glyph
809
+ condition={current.condition}
810
+ isDay={current.isDay}
811
+ size={HERO_GLYPH[size.variant]}
812
+ className={hueFor(current.condition, current.isDay)}
813
+ />
814
+ <Widget.Figure value={degrees(current.temperature, unit)} unit={unit === 'fahrenheit' ? '°F' : '°C'} />
815
+ </div>
816
+
817
+ <div className="flex min-w-0 flex-col gap-0.5">
818
+ <Widget.Title>{LABEL[current.condition]}</Widget.Title>
819
+ {(feels || soon) && <Widget.Subtitle>{[feels, soon].filter(Boolean).join(' · ')}</Widget.Subtitle>}
820
+ </div>
821
+
822
+ <Widget.Meta className="mt-1">
823
+ {today && (
824
+ <>
825
+ <Widget.MetaItem icon={ArrowUp}>{temp(today.high, unit)}</Widget.MetaItem>
826
+ <Widget.MetaItem icon={ArrowDown}>{temp(today.low, unit)}</Widget.MetaItem>
827
+ </>
828
+ )}
829
+ {today?.precipitation !== undefined && (
830
+ <Widget.MetaItem icon={Umbrella}>{today.precipitation}%</Widget.MetaItem>
831
+ )}
832
+ {current.wind !== undefined && <Widget.MetaItem icon={Wind}>{speed(current.wind, unit)}</Widget.MetaItem>}
833
+ {current.humidity !== undefined && (
834
+ <Widget.MetaItem icon={Droplets}>{Math.round(current.humidity)}%</Widget.MetaItem>
835
+ )}
836
+ {current.uv !== undefined && <Widget.MetaItem icon={Sun}>UV {Math.round(current.uv)}</Widget.MetaItem>}
837
+ </Widget.Meta>
838
+ </Widget>
839
+ )
840
+ },
841
+ {
842
+ Leading: WeatherLeading,
843
+ Trailing: WeatherTrailing,
844
+ Minimal: WeatherMinimal,
845
+ Alert: WeatherAlert,
846
+ },
847
+ )
848
+
849
+ export default Weather
850
+
851
+ export type {
852
+ WeatherAccess,
853
+ WeatherAlertProps,
854
+ WeatherAlertSeverity,
855
+ WeatherCondition,
856
+ WeatherDay,
857
+ WeatherHour,
858
+ WeatherNow,
859
+ WeatherProps,
860
+ WeatherStatus,
861
+ WeatherUnit,
862
+ }
863
+ export { conditionFromCode, Weather, WeatherAlert, WeatherLeading, WeatherMinimal, WeatherTrailing }