@justanarthur/modo-ui-notch 0.0.0-stage → 0.3.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 (141) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -2
  3. package/blocks/activities/agenda/examples.mdx +74 -0
  4. package/blocks/activities/agenda/index.tsx +441 -0
  5. package/blocks/activities/clock/examples.mdx +73 -0
  6. package/blocks/activities/clock/index.tsx +619 -0
  7. package/blocks/activities/now-playing/examples.mdx +68 -0
  8. package/blocks/activities/now-playing/index.tsx +411 -0
  9. package/blocks/activities/weather/examples.mdx +115 -0
  10. package/blocks/activities/weather/index.tsx +634 -0
  11. package/blocks/app/expanded-island/examples.mdx +46 -0
  12. package/blocks/app/expanded-island/index.tsx +143 -0
  13. package/blocks/app/settings-window/examples.mdx +34 -0
  14. package/blocks/app/settings-window/index.tsx +375 -0
  15. package/blocks/system/clipboard/examples.mdx +83 -0
  16. package/blocks/system/clipboard/index.tsx +563 -0
  17. package/blocks/system/recent-files/examples.mdx +66 -0
  18. package/blocks/system/recent-files/index.tsx +542 -0
  19. package/blocks/system/system-stats/examples.mdx +52 -0
  20. package/blocks/system/system-stats/index.tsx +312 -0
  21. package/components/indicators/countdown/examples.mdx +25 -0
  22. package/components/indicators/countdown/index.tsx +83 -0
  23. package/components/indicators/equalizer/examples.mdx +20 -0
  24. package/components/indicators/equalizer/index.tsx +116 -0
  25. package/components/indicators/media-scrubber/examples.mdx +29 -0
  26. package/components/indicators/media-scrubber/index.tsx +122 -0
  27. package/components/island/island/examples.mdx +57 -0
  28. package/components/island/island/index.tsx +415 -0
  29. package/components/island/island-flash/examples.mdx +52 -0
  30. package/components/island/island-flash/index.tsx +79 -0
  31. package/components/island/island-tabs/examples.mdx +53 -0
  32. package/components/island/island-tabs/index.tsx +285 -0
  33. package/components/island/widget/examples.mdx +76 -0
  34. package/components/island/widget/index.tsx +431 -0
  35. package/dist/blocks/activities/agenda/index.d.ts +2 -0
  36. package/dist/blocks/activities/agenda/index.js +21 -0
  37. package/dist/blocks/activities/clock/index.d.ts +2 -0
  38. package/dist/blocks/activities/clock/index.js +25 -0
  39. package/dist/blocks/activities/now-playing/index.d.ts +3 -0
  40. package/dist/blocks/activities/now-playing/index.js +24 -0
  41. package/dist/blocks/activities/weather/index.d.ts +2 -0
  42. package/dist/blocks/activities/weather/index.js +21 -0
  43. package/dist/blocks/app/expanded-island/index.d.ts +3 -0
  44. package/dist/blocks/app/expanded-island/index.js +11 -0
  45. package/dist/blocks/app/settings-window/index.d.ts +2 -0
  46. package/dist/blocks/app/settings-window/index.js +7 -0
  47. package/dist/blocks/system/clipboard/index.d.ts +2 -0
  48. package/dist/blocks/system/clipboard/index.js +17 -0
  49. package/dist/blocks/system/recent-files/index.d.ts +2 -0
  50. package/dist/blocks/system/recent-files/index.js +19 -0
  51. package/dist/blocks/system/system-stats/index.d.ts +2 -0
  52. package/dist/blocks/system/system-stats/index.js +15 -0
  53. package/dist/components/indicators/countdown/index.d.ts +2 -0
  54. package/dist/components/indicators/countdown/index.js +8 -0
  55. package/dist/components/indicators/equalizer/index.d.ts +2 -0
  56. package/dist/components/indicators/equalizer/index.js +7 -0
  57. package/dist/components/indicators/media-scrubber/index.d.ts +2 -0
  58. package/dist/components/indicators/media-scrubber/index.js +8 -0
  59. package/dist/components/island/island/index.d.ts +3 -0
  60. package/dist/components/island/island/index.js +10 -0
  61. package/dist/components/island/island-flash/index.d.ts +2 -0
  62. package/dist/components/island/island-flash/index.js +7 -0
  63. package/dist/components/island/island-tabs/index.d.ts +2 -0
  64. package/dist/components/island/island-tabs/index.js +9 -0
  65. package/dist/components/island/widget/index.d.ts +2 -0
  66. package/dist/components/island/widget/index.js +30 -0
  67. package/dist/index.d.ts +21 -0
  68. package/dist/index.js +195 -0
  69. package/dist/lib/artwork.d.ts +2 -0
  70. package/dist/lib/artwork.js +8 -0
  71. package/dist/lib/clock.d.ts +2 -0
  72. package/dist/lib/clock.js +14 -0
  73. package/dist/lib/geometry.d.ts +2 -0
  74. package/dist/lib/geometry.js +24 -0
  75. package/dist/lib/island-context.d.ts +3 -0
  76. package/dist/lib/island-context.js +8 -0
  77. package/dist/lib/motion.d.ts +21 -0
  78. package/dist/lib/motion.js +16 -0
  79. package/dist/shared/chunk-152gpyq3.d.ts +13 -0
  80. package/dist/shared/chunk-2818esrq.d.ts +140 -0
  81. package/dist/shared/chunk-2f7zxrbk.js +65 -0
  82. package/dist/shared/chunk-2m5dz8nf.d.ts +88 -0
  83. package/dist/shared/chunk-2s4q6mt0.js +27 -0
  84. package/dist/shared/chunk-3thgcfcq.d.ts +151 -0
  85. package/dist/shared/chunk-416tsgbc.d.ts +175 -0
  86. package/dist/shared/chunk-502rmtap.js +54 -0
  87. package/dist/shared/chunk-52gb3g6r.d.ts +12 -0
  88. package/dist/shared/chunk-5en5aaj1.d.ts +33 -0
  89. package/dist/shared/chunk-61t2dfte.js +63 -0
  90. package/dist/shared/chunk-7fvnrtz4.d.ts +105 -0
  91. package/dist/shared/chunk-81fpptwp.d.ts +75 -0
  92. package/dist/shared/chunk-9c7ewvkk.js +381 -0
  93. package/dist/shared/chunk-9m0xbg5v.js +112 -0
  94. package/dist/shared/chunk-9xtcv3sy.js +88 -0
  95. package/dist/shared/chunk-bvtyjcce.d.ts +123 -0
  96. package/dist/shared/chunk-cc9nwbtf.js +315 -0
  97. package/dist/shared/chunk-cfzmfnyh.d.ts +63 -0
  98. package/dist/shared/chunk-d5qm9cps.js +10 -0
  99. package/dist/shared/chunk-da177e8r.d.ts +114 -0
  100. package/dist/shared/chunk-fexwzycv.d.ts +36 -0
  101. package/dist/shared/chunk-gb56m10p.js +39 -0
  102. package/dist/shared/chunk-gejenfap.d.ts +25 -0
  103. package/dist/shared/chunk-hrpxbqmx.d.ts +28 -0
  104. package/dist/shared/chunk-j755a1a5.d.ts +41 -0
  105. package/dist/shared/chunk-j77cx8dp.js +498 -0
  106. package/dist/shared/chunk-mt0k6v8x.js +474 -0
  107. package/dist/shared/chunk-ndf79pv5.js +58 -0
  108. package/dist/shared/chunk-pfhy5e4q.d.ts +53 -0
  109. package/dist/shared/chunk-qdzemt2v.js +477 -0
  110. package/dist/shared/chunk-r471sknr.js +209 -0
  111. package/dist/shared/chunk-s2jeqqan.js +243 -0
  112. package/dist/shared/chunk-t0hcxh89.d.ts +124 -0
  113. package/dist/shared/chunk-w48eyvfr.d.ts +56 -0
  114. package/dist/shared/chunk-w5h6ay93.js +72 -0
  115. package/dist/shared/chunk-wkg17nxs.d.ts +101 -0
  116. package/dist/shared/chunk-x7f3g82r.js +285 -0
  117. package/dist/shared/chunk-xq5hc5gv.js +333 -0
  118. package/dist/shared/chunk-y0188bbn.js +331 -0
  119. package/dist/shared/chunk-ym0xbses.js +518 -0
  120. package/dist/styles.css +264 -0
  121. package/index.ts +22 -0
  122. package/lib/artwork.ts +112 -0
  123. package/lib/clock.ts +53 -0
  124. package/lib/geometry.ts +130 -0
  125. package/lib/island-context.tsx +24 -0
  126. package/lib/motion.ts +42 -0
  127. package/mock/activities.tsx +77 -0
  128. package/mock/artwork.tsx +37 -0
  129. package/mock/clipboard.tsx +79 -0
  130. package/mock/clock.tsx +77 -0
  131. package/mock/data.ts +139 -0
  132. package/mock/desktop.tsx +83 -0
  133. package/mock/files.tsx +70 -0
  134. package/mock/fit.ts +26 -0
  135. package/mock/island-content.tsx +142 -0
  136. package/mock/scene.tsx +52 -0
  137. package/mock/screen.tsx +76 -0
  138. package/mock/settings.tsx +489 -0
  139. package/mock/system.ts +46 -0
  140. package/mock/weather.tsx +105 -0
  141. package/package.json +122 -4
@@ -0,0 +1,619 @@
1
+ /*!
2
+ * Local addition: a notch app's clock, the time, the date and a few world
3
+ * clocks, with a Pomodoro cycle that takes the widget over while it runs.
4
+ * The run is a value the host may own and persist.
5
+ */
6
+
7
+ 'use client'
8
+
9
+ import Button from '@justanarthur/modo-ui/components/actions/button'
10
+ import Meter from '@justanarthur/modo-ui/components/feedback/meter'
11
+ import { useControllableState } from '@justanarthur/modo-ui/lib/use-controllable-state'
12
+ import { cn } from '@justanarthur/modo-ui/lib/utils'
13
+ import {
14
+ CalendarDays,
15
+ Clock3,
16
+ Coffee,
17
+ Moon,
18
+ Pause,
19
+ Play,
20
+ Plus,
21
+ Repeat,
22
+ RotateCcw,
23
+ SkipForward,
24
+ Sun,
25
+ Target,
26
+ Timer,
27
+ } from 'lucide-react'
28
+ import { useEffect } from 'react'
29
+ import IslandFlash from '../../../components/island/island-flash'
30
+ import Widget from '../../../components/island/widget'
31
+ import { formatClock, MINUTE, SECOND, useEdgeClock } from '../../../lib/clock'
32
+
33
+ type PomodoroPhase = 'focus' | 'short-break' | 'long-break'
34
+
35
+ /** A Pomodoro cycle's lengths, in minutes, and how many focus rounds come before the long break. */
36
+ interface PomodoroSettings {
37
+ focus: number
38
+ shortBreak: number
39
+ longBreak: number
40
+ rounds: number
41
+ }
42
+
43
+ /** A Pomodoro phase: running while it has `endsAt`, paused while it has `remaining`. */
44
+ interface PomodoroRun {
45
+ phase: PomodoroPhase
46
+ /** The focus round it belongs to, from 1: a break follows its round. */
47
+ round: number
48
+ /** The phase's whole length, in ms. */
49
+ total: number
50
+ /** When it ends, in epoch ms, while it runs. */
51
+ endsAt?: number
52
+ /** The time left, in ms, while it is paused. */
53
+ remaining?: number
54
+ }
55
+
56
+ /** A world clock: a city and its IANA time zone. */
57
+ interface ClockZone {
58
+ city: string
59
+ /** An IANA time zone: `America/Chicago`. */
60
+ timeZone: string
61
+ }
62
+
63
+ interface ClockProps {
64
+ /** The world clocks beside the time, two or three. */
65
+ zones?: ClockZone[]
66
+ /** Shows the seconds, re-rendering on each second's edge instead of each minute's. Defaults to `false`. */
67
+ seconds?: boolean
68
+ /** A 12-hour clock (`true`) or a 24-hour one (`false`). Defaults to the locale's. */
69
+ hour12?: boolean
70
+ /** The time zone of the main clock. Defaults to the system's. */
71
+ timeZone?: string
72
+ /** Formats the time and the date. Defaults to the browser's locale. */
73
+ locale?: Intl.LocalesArgument
74
+ /** The moment the clock shows, in epoch ms. Defaults to now, re-read on each minute (or second). */
75
+ now?: number
76
+ /** The Pomodoro cycle's lengths, merged over 25 / 5 / 15 minutes and 4 rounds; `false` leaves the Pomodoro out. */
77
+ pomodoro?: Partial<PomodoroSettings> | false
78
+ /** The Pomodoro run (controlled); `null` shows the clock. */
79
+ value?: PomodoroRun | null
80
+ /** The run at first (uncontrolled). Defaults to `null`. */
81
+ defaultValue?: PomodoroRun | null
82
+ /** Called with the run a start, pause, resume, skip, +1 min or reset leaves (`null` after a reset). */
83
+ onValueChange?: (run: PomodoroRun | null) => void
84
+ /** Called when a phase reaches zero, with the next one, which waits paused to be started: show `Clock.PhaseDone`. */
85
+ onPhaseEnd?: (ended: PomodoroRun, next: PomodoroRun) => void
86
+ /** Classes for the root, which fills its container. */
87
+ className?: string
88
+ }
89
+
90
+ const DEFAULTS: PomodoroSettings = { focus: 25, shortBreak: 5, longBreak: 15, rounds: 4 }
91
+
92
+ // Literal tables: UnoCSS only generates the classes it can read whole. Focus is warm, a break green.
93
+ const RING: Record<PomodoroPhase, string> = {
94
+ focus: '[&_circle:last-child]:text-status-warning',
95
+ 'short-break': '[&_circle:last-child]:text-status-success',
96
+ 'long-break': '[&_circle:last-child]:text-status-success',
97
+ }
98
+ const DOT: Record<PomodoroPhase, string> = {
99
+ focus: 'bg-status-warning',
100
+ 'short-break': 'bg-status-success',
101
+ 'long-break': 'bg-status-success',
102
+ }
103
+ const TILE: Record<PomodoroPhase, string> = {
104
+ focus: 'bg-status-warning/16 text-status-warning',
105
+ 'short-break': 'bg-status-success/16 text-status-success',
106
+ 'long-break': 'bg-status-success/16 text-status-success',
107
+ }
108
+ const MARK: Record<PomodoroPhase, string> = {
109
+ focus: 'var(--status-warning)',
110
+ 'short-break': 'var(--status-success)',
111
+ 'long-break': 'var(--status-success)',
112
+ }
113
+ const GLYPH = { focus: Target, 'short-break': Coffee, 'long-break': Coffee }
114
+
115
+ const settingsOf = (pomodoro?: Partial<PomodoroSettings> | false): PomodoroSettings => ({
116
+ ...DEFAULTS,
117
+ ...(pomodoro || {}),
118
+ })
119
+
120
+ const lengthOf = (phase: PomodoroPhase, settings: PomodoroSettings) =>
121
+ (phase === 'focus' ? settings.focus : phase === 'short-break' ? settings.shortBreak : settings.longBreak) * MINUTE
122
+
123
+ /** The phase after `run`'s and its round: a break after focus (the long one after the last round), then the next round. */
124
+ function following(run: Pick<PomodoroRun, 'phase' | 'round'>, settings: PomodoroSettings) {
125
+ if (run.phase === 'focus')
126
+ return { phase: run.round >= settings.rounds ? 'long-break' : 'short-break', round: run.round } as const
127
+ return { phase: 'focus', round: run.phase === 'long-break' ? 1 : run.round + 1 } as const
128
+ }
129
+
130
+ /** The first focus round, running from now. */
131
+ function startPomodoro(pomodoro?: Partial<PomodoroSettings>): PomodoroRun {
132
+ const total = lengthOf('focus', settingsOf(pomodoro))
133
+ return { phase: 'focus', round: 1, total, endsAt: Date.now() + total }
134
+ }
135
+
136
+ /** The phase after `run`, paused at its whole length until it is started: what a phase reaching zero leaves. */
137
+ function advancePomodoro(run: PomodoroRun, pomodoro?: Partial<PomodoroSettings>): PomodoroRun {
138
+ const settings = settingsOf(pomodoro)
139
+ const next = following(run, settings)
140
+ const total = lengthOf(next.phase, settings)
141
+ return { ...next, total, remaining: total }
142
+ }
143
+
144
+ /** `run` running from now, from the time it had left. */
145
+ function resumePomodoro(run: PomodoroRun): PomodoroRun {
146
+ return { phase: run.phase, round: run.round, total: run.total, endsAt: Date.now() + (run.remaining ?? run.total) }
147
+ }
148
+
149
+ const pause = (run: PomodoroRun, left: number): PomodoroRun => ({
150
+ phase: run.phase,
151
+ round: run.round,
152
+ total: run.total,
153
+ remaining: left,
154
+ })
155
+
156
+ const addMinute = (run: PomodoroRun): PomodoroRun =>
157
+ run.endsAt === undefined
158
+ ? { ...run, total: run.total + MINUTE, remaining: (run.remaining ?? run.total) + MINUTE }
159
+ : { ...run, total: run.total + MINUTE, endsAt: run.endsAt + MINUTE }
160
+
161
+ /** The time `run` has left, re-read on each second's edge while it runs, never in between. */
162
+ function useLeft(run: PomodoroRun) {
163
+ const end = run.endsAt ?? 0
164
+ const now = useEdgeClock(end, run.endsAt !== undefined && Date.now() < end + SECOND)
165
+ return run.endsAt === undefined ? (run.remaining ?? run.total) : Math.max(0, end - now)
166
+ }
167
+
168
+ function phaseName(run: Pick<PomodoroRun, 'phase' | 'round'>, rounds: number) {
169
+ if (run.phase === 'focus') return `Focus ${run.round} of ${rounds}`
170
+ return run.phase === 'long-break' ? 'Long break' : 'Short break'
171
+ }
172
+
173
+ /** A time split into its figure and its day period: `4:05` and `PM`, or `16:05` alone. */
174
+ function timeParts(
175
+ ms: number,
176
+ { locale, hour12, timeZone, seconds }: Pick<ClockProps, 'locale' | 'hour12' | 'timeZone' | 'seconds'>,
177
+ ) {
178
+ const parts = new Intl.DateTimeFormat(locale, {
179
+ hour: 'numeric',
180
+ minute: '2-digit',
181
+ second: seconds ? '2-digit' : undefined,
182
+ hour12,
183
+ timeZone,
184
+ }).formatToParts(ms)
185
+ const period = parts.find(part => part.type === 'dayPeriod')?.value
186
+ const text = parts
187
+ .filter(part => part.type !== 'dayPeriod')
188
+ .map(part => part.value)
189
+ .join('')
190
+ .trim()
191
+ return { text, period }
192
+ }
193
+
194
+ const shortTime = (ms: number, options: Pick<ClockProps, 'locale' | 'hour12' | 'timeZone'>) =>
195
+ new Date(ms).toLocaleTimeString(options.locale, {
196
+ hour: 'numeric',
197
+ minute: '2-digit',
198
+ hour12: options.hour12,
199
+ timeZone: options.timeZone,
200
+ })
201
+
202
+ /** A zone's offset from UTC at `ms`, in minutes; the system's without a zone. */
203
+ function offsetOf(ms: number, timeZone?: string) {
204
+ if (!timeZone) return -new Date(ms).getTimezoneOffset()
205
+ const name = new Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'longOffset' })
206
+ .formatToParts(ms)
207
+ .find(part => part.type === 'timeZoneName')?.value
208
+ const match = name?.match(/([+-])(\d{2}):(\d{2})/)
209
+ if (!match) return 0
210
+ const minutes = Number(match[2]) * 60 + Number(match[3])
211
+ return match[1] === '-' ? -minutes : minutes
212
+ }
213
+
214
+ /** An offset difference as `+9h`, `−6h`, `+5:30`. */
215
+ function offsetLabel(minutes: number) {
216
+ if (minutes === 0) return 'Same time'
217
+ const sign = minutes > 0 ? '+' : '−'
218
+ const h = Math.floor(Math.abs(minutes) / 60)
219
+ const m = Math.abs(minutes) % 60
220
+ return m ? `${sign}${h}:${String(m).padStart(2, '0')}` : `${sign}${h}h`
221
+ }
222
+
223
+ const dayKey = (ms: number, timeZone?: string) =>
224
+ new Intl.DateTimeFormat('en-CA', { timeZone, year: 'numeric', month: '2-digit', day: '2-digit' }).format(ms)
225
+
226
+ const hourIn = (ms: number, timeZone: string) =>
227
+ Number(new Intl.DateTimeFormat('en-US', { timeZone, hour: 'numeric', hourCycle: 'h23' }).format(ms))
228
+
229
+ interface ZoneRowProps extends Pick<ClockProps, 'locale' | 'hour12' | 'timeZone'> {
230
+ zone: ClockZone
231
+ now: number
232
+ }
233
+
234
+ function ZoneRow({ zone, now, locale, hour12, timeZone }: ZoneRowProps) {
235
+ const there = dayKey(now, zone.timeZone)
236
+ const here = dayKey(now, timeZone)
237
+ const day = there > here ? 'Tomorrow' : there < here ? 'Yesterday' : undefined
238
+ const hour = hourIn(now, zone.timeZone)
239
+ const DayGlyph = hour >= 6 && hour < 18 ? Sun : Moon
240
+ return (
241
+ <Widget.Row
242
+ leading={shortTime(now, { locale, hour12, timeZone: zone.timeZone })}
243
+ trailing={
244
+ <>
245
+ {day && <span>{day} ·</span>}
246
+ <span className="tabular-nums">{offsetLabel(offsetOf(now, zone.timeZone) - offsetOf(now, timeZone))}</span>
247
+ <DayGlyph size={12} strokeWidth={1.5} aria-label={DayGlyph === Sun ? 'Day' : 'Night'} className="ml-1" />
248
+ </>
249
+ }
250
+ >
251
+ {zone.city}
252
+ </Widget.Row>
253
+ )
254
+ }
255
+
256
+ interface RingProps {
257
+ run: PomodoroRun
258
+ rounds?: number
259
+ className: string
260
+ }
261
+
262
+ /** The time left as a Meter ring in the phase's hue. */
263
+ function PhaseRing({ run, rounds = DEFAULTS.rounds, className }: RingProps) {
264
+ const left = useLeft(run)
265
+ return (
266
+ <Meter
267
+ variant="ring"
268
+ label={`${phaseName(run, rounds)}, ${formatClock(Math.ceil(left / SECOND))} left`}
269
+ value={run.total ? left / run.total : 0}
270
+ tone="default"
271
+ animate={false}
272
+ className={cn(RING[run.phase], className)}
273
+ />
274
+ )
275
+ }
276
+
277
+ interface PartProps {
278
+ /** The Pomodoro run; without one the part shows nothing. */
279
+ run?: PomodoroRun | null
280
+ }
281
+
282
+ /** The compact island's left wing: the ring of the phase's time left. */
283
+ function ClockLeading({ run }: PartProps) {
284
+ if (!run) return null
285
+ return <PhaseRing run={run} className="size-4" />
286
+ }
287
+
288
+ function TimeLeft({ run }: { run: PomodoroRun }) {
289
+ const left = useLeft(run)
290
+ return (
291
+ <span
292
+ className={cn(
293
+ 'text-caption weight-semibold tabular-nums',
294
+ run.endsAt === undefined ? 'text-muted-foreground' : 'text-foreground',
295
+ )}
296
+ >
297
+ {formatClock(Math.ceil(left / SECOND))}
298
+ </span>
299
+ )
300
+ }
301
+
302
+ /** The compact island's right wing: the phase's time left as `mm:ss`, muted while paused. */
303
+ function ClockTrailing({ run }: PartProps) {
304
+ if (!run) return null
305
+ return <TimeLeft run={run} />
306
+ }
307
+
308
+ /** The split island's bubble: the ring of the phase's time left. */
309
+ function ClockMinimal({ run }: PartProps) {
310
+ if (!run) return null
311
+ return <PhaseRing run={run} className="size-5.5" />
312
+ }
313
+
314
+ interface ClockPhaseDoneProps {
315
+ /** The phase that reached zero. */
316
+ ended: Pick<PomodoroRun, 'phase' | 'round'>
317
+ /** The cycle's lengths, as given to the Clock. */
318
+ pomodoro?: Partial<PomodoroSettings>
319
+ /** Called by the button that starts the next phase; without it the button hides. */
320
+ onStart?: () => void
321
+ }
322
+
323
+ /** The island's flash when a phase reaches zero: "Focus done — break time" or "Break over", and a button to start the next. */
324
+ function ClockPhaseDone({ ended, pomodoro, onStart }: ClockPhaseDoneProps) {
325
+ const settings = settingsOf(pomodoro)
326
+ const next = following(ended, settings)
327
+ const minutes = lengthOf(next.phase, settings) / MINUTE
328
+ const Glyph = GLYPH[next.phase]
329
+ const breakNext = next.phase !== 'focus'
330
+ const title = !breakNext
331
+ ? 'Break over'
332
+ : next.phase === 'long-break'
333
+ ? 'Focus done — long break'
334
+ : 'Focus done — break time'
335
+ const caption = !breakNext
336
+ ? `${phaseName(next, settings.rounds)} · ${minutes} min`
337
+ : next.phase === 'long-break'
338
+ ? `${minutes} min break · all ${settings.rounds} rounds done`
339
+ : `${minutes} min break · ${ended.round} of ${settings.rounds} rounds done`
340
+ return (
341
+ <IslandFlash
342
+ icon={
343
+ <span className={cn('flex size-9 items-center justify-center rounded-lg', TILE[next.phase])}>
344
+ <Glyph size={16} strokeWidth={1.5} />
345
+ </span>
346
+ }
347
+ title={title}
348
+ caption={caption}
349
+ action={
350
+ onStart && (
351
+ <Button variant="secondary" size="compact" onClick={onStart}>
352
+ {breakNext ? 'Start break' : 'Start focus'}
353
+ </Button>
354
+ )
355
+ }
356
+ />
357
+ )
358
+ }
359
+
360
+ interface RunningProps extends Pick<ClockProps, 'locale' | 'hour12' | 'timeZone' | 'onPhaseEnd' | 'className'> {
361
+ run: PomodoroRun
362
+ settings: PomodoroSettings
363
+ onChange: (run: PomodoroRun | null) => void
364
+ }
365
+
366
+ /** The coming phases from `run`'s end, each with when it starts. */
367
+ function schedule(run: PomodoroRun, start: number, settings: PomodoroSettings, count: number) {
368
+ const phases: { phase: PomodoroPhase; round: number; start: number; length: number }[] = []
369
+ let at: Pick<PomodoroRun, 'phase' | 'round'> = run
370
+ let when = start
371
+ for (let i = 0; i < count; i++) {
372
+ at = following(at, settings)
373
+ const length = lengthOf(at.phase, settings)
374
+ phases.push({ ...at, start: when, length })
375
+ when += length
376
+ }
377
+ return phases
378
+ }
379
+
380
+ function Running({ run, settings, onChange, onPhaseEnd, locale, hour12, timeZone, className }: RunningProps) {
381
+ const left = useLeft(run)
382
+ const running = run.endsAt !== undefined
383
+ const now = running ? (run.endsAt ?? 0) - left : Date.now()
384
+
385
+ useEffect(() => {
386
+ if (!running || left > 0) return
387
+ const next = advancePomodoro(run, settings)
388
+ onPhaseEnd?.(run, next)
389
+ onChange(next)
390
+ }, [running, left, run, settings, onChange, onPhaseEnd])
391
+
392
+ const ends = now + left
393
+ const ready = !running && left === run.total
394
+ const next = following(run, settings)
395
+ const options = { locale, hour12, timeZone }
396
+ const status = running
397
+ ? `Until ${shortTime(ends, options)}`
398
+ : ready
399
+ ? `Ready · ${Math.round(run.total / MINUTE)} min`
400
+ : 'Paused'
401
+ const nextLabel =
402
+ next.phase === 'focus'
403
+ ? `${phaseName(next, settings.rounds)} next`
404
+ : `${lengthOf(next.phase, settings) / MINUTE} min ${next.phase === 'long-break' ? 'long break' : 'break'} next`
405
+
406
+ return (
407
+ <Widget
408
+ className={className}
409
+ media={<PhaseRing run={run} rounds={settings.rounds} className="size-24" />}
410
+ aside={
411
+ <Widget.Section
412
+ label="Up next"
413
+ trailing={<span className="tabular-nums">{shortTime(Date.now(), options)}</span>}
414
+ >
415
+ {schedule(run, ends, settings, 3).map(phase => (
416
+ <Widget.Row
417
+ key={`${phase.phase}-${phase.round}-${phase.start}`}
418
+ leading={shortTime(phase.start, options)}
419
+ color={MARK[phase.phase]}
420
+ trailing={`${phase.length / MINUTE} min`}
421
+ >
422
+ {phaseName(phase, settings.rounds)}
423
+ </Widget.Row>
424
+ ))}
425
+ </Widget.Section>
426
+ }
427
+ >
428
+ <Widget.Header
429
+ icon={Timer}
430
+ trailing={<Widget.Action icon={RotateCcw} label="Reset" onClick={() => onChange(null)} />}
431
+ >
432
+ Pomodoro
433
+ </Widget.Header>
434
+ <div className="flex min-w-0 items-center justify-between gap-3">
435
+ <Widget.Figure value={formatClock(Math.ceil(left / SECOND))} />
436
+ <div className="-mr-1 flex shrink-0 items-center gap-1">
437
+ <Widget.Action icon={Plus} label="Add a minute" onClick={() => onChange(addMinute(run))} />
438
+ <Widget.Action
439
+ icon={running ? Pause : Play}
440
+ label={running ? 'Pause' : ready ? 'Start' : 'Resume'}
441
+ emphasis="primary"
442
+ filled
443
+ onClick={() => onChange(running ? pause(run, left) : resumePomodoro(run))}
444
+ />
445
+ <Widget.Action
446
+ icon={SkipForward}
447
+ label={next.phase === 'focus' ? 'Skip to focus' : 'Skip to break'}
448
+ filled
449
+ onClick={() => onChange(resumePomodoro(advancePomodoro(run, settings)))}
450
+ />
451
+ </div>
452
+ </div>
453
+ <div className="flex min-w-0 flex-col gap-0.5">
454
+ <Widget.Title leading={<span aria-hidden className={cn('size-2.5 shrink-0 rounded-full', DOT[run.phase])} />}>
455
+ {phaseName(run, settings.rounds)}
456
+ </Widget.Title>
457
+ <Widget.Meta className="pl-4.5">
458
+ <Widget.MetaItem icon={running ? Clock3 : Pause}>{status}</Widget.MetaItem>
459
+ <Widget.MetaItem icon={GLYPH[next.phase]}>{nextLabel}</Widget.MetaItem>
460
+ </Widget.Meta>
461
+ </div>
462
+ </Widget>
463
+ )
464
+ }
465
+
466
+ /**
467
+ * A notch app's clock: the time as the hero, the date above it, the world
468
+ * clocks beside it, and a Pomodoro cycle that takes over while it runs.
469
+ *
470
+ * The time is set large in hours and minutes (`seconds` adds them, muted,
471
+ * and a tick on each second), on the locale's clock or the one `hour12`
472
+ * picks, with the time zone's name under it and the date on the line above.
473
+ * Beside it, past a hairline, the world clocks (`zones`): each city's time,
474
+ * how far it is from here (`−6h`), whether it's day or night there, and
475
+ * `Tomorrow` or `Yesterday` when its date differs. Nothing ticks between
476
+ * minutes; `now` pins the moment.
477
+ *
478
+ * Under the time the Pomodoro waits with its lengths (`pomodoro`: focus,
479
+ * short and long break in minutes, and the rounds before the long one) and
480
+ * a Start focus button. A run takes the widget over: the phase's ring in
481
+ * its hue (focus warm, a break green) as the picture, the time left as the
482
+ * hero, its round (`Focus 2 of 4`), when it ends and what comes next, and
483
+ * the next phases with their start times beside it. Pause / resume is the
484
+ * main action, with +1 min and skip beside it and reset at the top.
485
+ *
486
+ * The run is a value (`value` / `onValueChange`, or kept here from
487
+ * `defaultValue`): a running phase carries when it ends, a paused one the
488
+ * time it had left, so a host can persist it and pick it up after a
489
+ * reload. When a phase reaches zero the next one waits paused at its whole
490
+ * length and `onPhaseEnd` is called; the helpers `startPomodoro`,
491
+ * `advancePomodoro` and `resumePomodoro` build the same runs for a host
492
+ * that keeps time itself.
493
+ *
494
+ * The other forms are parts: `Clock.Leading` and `Clock.Trailing` for the
495
+ * compact island's wings (the ring and `mm:ss`, nothing without a run),
496
+ * `Clock.Minimal`, the ring, for the split island's bubble, and
497
+ * `Clock.PhaseDone`, the flash at a phase's end with a button to start the
498
+ * next.
499
+ *
500
+ * @example {@include ./examples.mdx}
501
+ */
502
+ const Clock = Object.assign(
503
+ function Clock({
504
+ zones = [],
505
+ seconds = false,
506
+ hour12,
507
+ timeZone,
508
+ locale,
509
+ now: nowProp,
510
+ pomodoro,
511
+ value,
512
+ defaultValue = null,
513
+ onValueChange,
514
+ onPhaseEnd,
515
+ className,
516
+ }: ClockProps) {
517
+ const [run, setRun] = useControllableState(value, defaultValue, onValueChange)
518
+ const ticking = useEdgeClock(0, nowProp === undefined && !run, seconds ? SECOND : MINUTE)
519
+ const settings = settingsOf(pomodoro)
520
+
521
+ if (run)
522
+ return (
523
+ <Running
524
+ run={run}
525
+ settings={settings}
526
+ onChange={setRun}
527
+ onPhaseEnd={onPhaseEnd}
528
+ locale={locale}
529
+ hour12={hour12}
530
+ timeZone={timeZone}
531
+ className={className}
532
+ />
533
+ )
534
+
535
+ const now = nowProp ?? ticking
536
+ const time = timeParts(now, { locale, hour12, timeZone })
537
+ const second = seconds && new Intl.DateTimeFormat('en-US', { second: '2-digit', timeZone }).format(now)
538
+ const date = new Date(now).toLocaleDateString(locale, { weekday: 'long', month: 'long', day: 'numeric', timeZone })
539
+ const zoneName = new Intl.DateTimeFormat(locale, { timeZone, timeZoneName: 'long' })
540
+ .formatToParts(now)
541
+ .find(part => part.type === 'timeZoneName')?.value
542
+
543
+ return (
544
+ <Widget
545
+ className={className}
546
+ aside={
547
+ zones.length > 0 && (
548
+ <Widget.Section label="World clock">
549
+ {zones.slice(0, 3).map(zone => (
550
+ <ZoneRow
551
+ key={zone.timeZone + zone.city}
552
+ zone={zone}
553
+ now={now}
554
+ locale={locale}
555
+ hour12={hour12}
556
+ timeZone={timeZone}
557
+ />
558
+ ))}
559
+ </Widget.Section>
560
+ )
561
+ }
562
+ >
563
+ <Widget.Header icon={CalendarDays}>{date}</Widget.Header>
564
+ <div className="flex min-w-0 items-end justify-between gap-3">
565
+ <Widget.Figure
566
+ value={
567
+ <>
568
+ {time.text}
569
+ {second && <span className="text-muted-foreground">:{second.padStart(2, '0')}</span>}
570
+ </>
571
+ }
572
+ unit={time.period}
573
+ />
574
+ {pomodoro !== false && (
575
+ <Button
576
+ variant="secondary"
577
+ size="compact"
578
+ leadingIcon={Play}
579
+ className="mb-1"
580
+ onClick={() => setRun(startPomodoro(settings))}
581
+ >
582
+ Start focus
583
+ </Button>
584
+ )}
585
+ </div>
586
+ {zoneName && <Widget.Subtitle>{zoneName}</Widget.Subtitle>}
587
+ {pomodoro !== false && (
588
+ <Widget.Meta className="mt-1">
589
+ <Widget.MetaItem icon={Target}>{settings.focus} min focus</Widget.MetaItem>
590
+ <Widget.MetaItem icon={Coffee}>
591
+ {settings.shortBreak} / {settings.longBreak} min breaks
592
+ </Widget.MetaItem>
593
+ <Widget.MetaItem icon={Repeat}>{settings.rounds} rounds</Widget.MetaItem>
594
+ </Widget.Meta>
595
+ )}
596
+ </Widget>
597
+ )
598
+ },
599
+ {
600
+ Leading: ClockLeading,
601
+ Trailing: ClockTrailing,
602
+ Minimal: ClockMinimal,
603
+ PhaseDone: ClockPhaseDone,
604
+ },
605
+ )
606
+
607
+ export default Clock
608
+
609
+ export type { ClockPhaseDoneProps, ClockProps, ClockZone, PomodoroPhase, PomodoroRun, PomodoroSettings }
610
+ export {
611
+ advancePomodoro,
612
+ Clock,
613
+ ClockLeading,
614
+ ClockMinimal,
615
+ ClockPhaseDone,
616
+ ClockTrailing,
617
+ resumePomodoro,
618
+ startPomodoro,
619
+ }
@@ -0,0 +1,68 @@
1
+ import NowPlaying from './index'
2
+ import Button from '@justanarthur/modo-ui/components/actions/button'
3
+ import { ARTWORK_TINT, MockArtwork } from '../../../mock/artwork'
4
+ import { FlashIsland, PLAYING, WidgetIsland, WingsIsland } from '../../../mock/island-content'
5
+ import { TRACK } from '../../../mock/data'
6
+
7
+ # Player
8
+
9
+ The artwork as the hero's picture with a glow of its tint, the title, artist and album, the scrubber ticking on by itself, the transport, the app and the device it plays on, and shuffle, repeat and the heart.
10
+
11
+ <WidgetIsland tab="now-playing">
12
+ <NowPlaying track={PLAYING} defaultShuffle={false} defaultRepeat="all" defaultLiked />
13
+ </WidgetIsland>
14
+
15
+ # Paused, no extras
16
+
17
+ Without shuffle, repeat or like values or handlers, their buttons stay out; paused, the bars settle.
18
+
19
+ <WidgetIsland tab="now-playing">
20
+ <NowPlaying track={{ ...PLAYING, output: undefined }} defaultPlaying={false} />
21
+ </WidgetIsland>
22
+
23
+ # Without artwork
24
+
25
+ `artwork: null` says the track has none: the note placeholder takes the artwork's place. Left out, the artwork is still loading, and the last picture stays up.
26
+
27
+ <WidgetIsland tab="now-playing">
28
+ <NowPlaying track={{ ...TRACK, artwork: null }} defaultPlaying={false} />
29
+ </WidgetIsland>
30
+
31
+ # A stream
32
+
33
+ A `duration` of `0` hides the scrubber.
34
+
35
+ <WidgetIsland tab="now-playing">
36
+ <NowPlaying
37
+ track={{ title: 'Lofi beats to code to', artist: 'Radio', source: 'Safari', output: 'Living room', outputKind: 'airplay', duration: 0, position: 0, artwork: <MockArtwork variant="night" />, tint: ARTWORK_TINT.night }}
38
+ />
39
+ </WidgetIsland>
40
+
41
+ # Nothing playing
42
+
43
+ <WidgetIsland tab="now-playing">
44
+ <NowPlaying track={null} emptyActions={<Button variant="secondary" size="compact">Open Music</Button>} />
45
+ </WidgetIsland>
46
+
47
+ # Compact
48
+
49
+ `NowPlaying.Leading` and `NowPlaying.Trailing` in the wings: the artwork, and the bars in its tint, settling low while paused.
50
+
51
+ <div className="flex w-full flex-col gap-3">
52
+ <WingsIsland leading={<NowPlaying.Leading track={PLAYING} />} trailing={<NowPlaying.Trailing track={PLAYING} />} />
53
+ <WingsIsland leading={<NowPlaying.Leading track={PLAYING} />} trailing={<NowPlaying.Trailing track={PLAYING} playing={false} />} />
54
+ </div>
55
+
56
+ # Minimal
57
+
58
+ `NowPlaying.Minimal`, the artwork as a disc, in the split island's bubble.
59
+
60
+ <WingsIsland leading={<NowPlaying.Leading track={PLAYING} />} trailing={<NowPlaying.Trailing track={PLAYING} />} minimal={<NowPlaying.Minimal track={PLAYING} />} />
61
+
62
+ # Track change
63
+
64
+ `NowPlaying.Flash` announces the next track for a moment.
65
+
66
+ <FlashIsland>
67
+ <NowPlaying.Flash track={{ ...PLAYING, title: 'Teardrop', artist: 'Massive Attack', album: 'Mezzanine', artwork: <MockArtwork variant="night" />, tint: ARTWORK_TINT.night }} />
68
+ </FlashIsland>