@opencode/ui 2.2.5 → 2.2.6

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 (117) hide show
  1. package/dist/actions/button/button.d.ts +2 -2
  2. package/dist/actions/icon-button/icon-button.d.ts +1 -1
  3. package/dist/components/animated-number-machine.d.ts +95 -0
  4. package/dist/components/file-icon-machine.d.ts +40 -0
  5. package/dist/components/file-icon.d.ts +0 -2
  6. package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
  7. package/dist/components/image-flight-machine.d.ts +135 -0
  8. package/dist/components/image-zoom-machine.d.ts +95 -0
  9. package/dist/components/image-zoom.d.ts +6 -3
  10. package/dist/components/list.d.ts +2 -6
  11. package/dist/components/menu-scroll.d.ts +9 -3
  12. package/dist/components/popover-machine.d.ts +96 -0
  13. package/dist/components/popover.d.ts +2 -2
  14. package/dist/components/resize-handle-machine.d.ts +71 -0
  15. package/dist/components/resize-handle.d.ts +2 -0
  16. package/dist/components/scroll-view-machine.d.ts +68 -0
  17. package/dist/components/scroll-view.d.ts +2 -2
  18. package/dist/components/spinner.d.ts +11 -3
  19. package/dist/components/text-field-machine.d.ts +62 -0
  20. package/dist/components/text-reveal-machine.d.ts +90 -0
  21. package/dist/components/typewriter-machine.d.ts +62 -0
  22. package/dist/context/dialog-machine.d.ts +125 -0
  23. package/dist/context/dialog.d.ts +17 -16
  24. package/dist/data-display/line-comment/machine.d.ts +157 -0
  25. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  26. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  27. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  28. package/dist/feedback/toast/machine.d.ts +57 -0
  29. package/dist/feedback/toast/toast.d.ts +1 -1
  30. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  31. package/dist/forms/select/machine.d.ts +45 -0
  32. package/dist/forms/select/select.d.ts +4 -0
  33. package/dist/forms/typeahead/machine.d.ts +85 -0
  34. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  35. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  36. package/dist/hooks/list-machine.d.ts +150 -0
  37. package/dist/hooks/use-filtered-list.d.ts +21 -35
  38. package/dist/i18n/en.d.ts +5 -0
  39. package/dist/icons/catalog.d.ts +8 -0
  40. package/dist/navigation/menu/machine.d.ts +49 -0
  41. package/dist/navigation/menu/menu.d.ts +5 -3
  42. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  43. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  44. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  45. package/dist/overlays/tooltip/machine.d.ts +146 -0
  46. package/dist/theme/context.d.ts +38 -14
  47. package/dist/theme/machine.d.ts +174 -0
  48. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  49. package/package.json +19 -3
  50. package/src/actions/button/button.tsx +11 -6
  51. package/src/actions/icon-button/icon-button.tsx +17 -4
  52. package/src/components/animated-number-machine.ts +214 -0
  53. package/src/components/animated-number.tsx +134 -83
  54. package/src/components/file-icon-machine.ts +78 -0
  55. package/src/components/file-icon.tsx +96 -579
  56. package/src/components/file-icons/choose-icon-name.ts +574 -0
  57. package/src/components/image-flight-machine.ts +247 -0
  58. package/src/components/image-preview.css +5 -2
  59. package/src/components/image-preview.tsx +390 -252
  60. package/src/components/image-zoom-machine.ts +191 -0
  61. package/src/components/image-zoom.ts +22 -14
  62. package/src/components/list.tsx +46 -137
  63. package/src/components/menu-scroll.css +19 -8
  64. package/src/components/menu-scroll.tsx +111 -34
  65. package/src/components/popover-machine.ts +198 -0
  66. package/src/components/popover.tsx +157 -89
  67. package/src/components/resize-handle-machine.ts +174 -0
  68. package/src/components/resize-handle.tsx +116 -67
  69. package/src/components/scroll-view-machine.ts +244 -0
  70. package/src/components/scroll-view.tsx +163 -116
  71. package/src/components/spinner.css +18 -5
  72. package/src/components/spinner.tsx +28 -14
  73. package/src/components/text-field-machine.ts +102 -0
  74. package/src/components/text-field.tsx +78 -24
  75. package/src/components/text-reveal-machine.ts +184 -0
  76. package/src/components/text-reveal.tsx +96 -93
  77. package/src/components/typewriter-machine.ts +134 -0
  78. package/src/components/typewriter.tsx +50 -39
  79. package/src/context/dialog-machine.ts +280 -0
  80. package/src/context/dialog.tsx +168 -177
  81. package/src/data-display/line-comment/line-comment.tsx +157 -178
  82. package/src/data-display/line-comment/machine.ts +391 -0
  83. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  84. package/src/data-display/project-avatar/animation.ts +67 -38
  85. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  86. package/src/feedback/toast/machine.ts +115 -0
  87. package/src/feedback/toast/toast.tsx +55 -59
  88. package/src/forms/checkbox/checkbox-compact.css +169 -0
  89. package/src/forms/checkbox/checkbox.tsx +93 -31
  90. package/src/forms/search-list/search-list.css +5 -1
  91. package/src/forms/search-list/search-list.tsx +4 -4
  92. package/src/forms/select/machine.ts +81 -0
  93. package/src/forms/select/select.css +59 -17
  94. package/src/forms/select/select.tsx +67 -31
  95. package/src/forms/typeahead/machine.ts +253 -0
  96. package/src/forms/typeahead/typeahead.tsx +70 -62
  97. package/src/hooks/auto-scroll-machine.ts +261 -0
  98. package/src/hooks/create-auto-scroll.tsx +110 -205
  99. package/src/hooks/list-machine.ts +359 -0
  100. package/src/hooks/use-filtered-list.tsx +101 -112
  101. package/src/i18n/en.ts +6 -1
  102. package/src/icons/catalog.ts +8 -0
  103. package/src/navigation/menu/machine.ts +109 -0
  104. package/src/navigation/menu/menu.tsx +130 -22
  105. package/src/navigation/segmented-control/machine.ts +72 -0
  106. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  107. package/src/overlays/dialog/dialog.tsx +7 -1
  108. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  109. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  110. package/src/overlays/tooltip/machine.ts +303 -0
  111. package/src/overlays/tooltip/tooltip.tsx +157 -92
  112. package/src/styles/theme.css +10 -2
  113. package/src/theme/context.tsx +192 -269
  114. package/src/theme/machine.ts +508 -0
  115. package/src/typography/text-shimmer/machine.ts +90 -0
  116. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  117. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -0,0 +1,391 @@
1
+ export const MENTION_LIMIT = 10
2
+
3
+ /** A file search that never answers stops holding Enter and Tab after this long. */
4
+ export const MENTION_TIMEOUT_MS = 5_000
5
+
6
+ /** The `@query` the caret sits at the end of, and where it starts and ends in the value. */
7
+ export type MentionToken = {
8
+ readonly query: string
9
+ readonly start: number
10
+ readonly end: number
11
+ }
12
+
13
+ /** The editor props the machine reads, captured when the event happens. */
14
+ export type LineCommentEditorConfig = {
15
+ readonly value: string
16
+ readonly allowEmpty: boolean
17
+ readonly variant: "default" | "compact"
18
+ readonly alternateSubmit: { readonly disabled: boolean } | null
19
+ readonly submitting: boolean
20
+ }
21
+
22
+ /**
23
+ * `alternate` is whether Mod is held over a compact editor with an alternate submit. `attempt` counts mention
24
+ * searches; only the search a `mention-loading` state started may settle it.
25
+ */
26
+ export type LineCommentEditorState =
27
+ | {
28
+ readonly status: "editing"
29
+ readonly attempt: number
30
+ readonly alternate: boolean
31
+ /** A token the user closed with Escape, or that found nothing, stays closed while the caret stays on it. */
32
+ readonly dismissed: { readonly start: number; readonly query: string } | null
33
+ }
34
+ | {
35
+ readonly status: "mention-loading"
36
+ readonly attempt: number
37
+ readonly alternate: boolean
38
+ readonly token: MentionToken
39
+ /** The previous results while the user refines the same token, so the list does not flash between keystrokes. */
40
+ readonly items: readonly string[]
41
+ readonly activeIndex: number
42
+ }
43
+ | {
44
+ readonly status: "mention-open"
45
+ readonly attempt: number
46
+ readonly alternate: boolean
47
+ readonly token: MentionToken
48
+ /** Never empty, and at most `MENTION_LIMIT`: every item is on screen. */
49
+ readonly items: readonly string[]
50
+ readonly activeIndex: number
51
+ }
52
+ /** The owner set `submitting`: the draft is read-only until it clears. */
53
+ | { readonly status: "submitting"; readonly attempt: number; readonly alternate: boolean }
54
+ | { readonly status: "disposed" }
55
+
56
+ export type LineCommentEditorKey = {
57
+ readonly key: string
58
+ readonly ctrl: boolean
59
+ readonly meta: boolean
60
+ readonly alt: boolean
61
+ readonly shift: boolean
62
+ readonly repeat: boolean
63
+ }
64
+
65
+ export type LineCommentEditorEvent =
66
+ /** Props changed, or the owner replaced the value; `token` is read from the textarea after the change. */
67
+ | { readonly type: "props.synced"; readonly config: LineCommentEditorConfig; readonly token: MentionToken | null }
68
+ /** The user typed or moved the caret. `token` is null without a `mention` prop. */
69
+ | { readonly type: "mention.synced"; readonly token: MentionToken | null }
70
+ | { readonly type: "mention.loaded"; readonly attempt: number; readonly items: readonly string[] }
71
+ | { readonly type: "mention.failed"; readonly attempt: number }
72
+ | { readonly type: "mention.timeout"; readonly attempt: number }
73
+ | { readonly type: "mention.hovered"; readonly index: number }
74
+ | { readonly type: "mention.selected"; readonly index: number }
75
+ | { readonly type: "modifier.changed"; readonly active: boolean; readonly config: LineCommentEditorConfig }
76
+ | { readonly type: "window.blurred" }
77
+ /** A key outside an IME composition, in the textarea or on the compact action. */
78
+ | (LineCommentEditorKey & {
79
+ readonly type: "key.down"
80
+ readonly target: "textarea" | "action"
81
+ readonly config: LineCommentEditorConfig
82
+ })
83
+ | { readonly type: "action.clicked"; readonly modified: boolean; readonly config: LineCommentEditorConfig }
84
+ /** The default layout's Submit button. */
85
+ | { readonly type: "submit.requested"; readonly config: LineCommentEditorConfig }
86
+ /** The default layout's Cancel button. */
87
+ | { readonly type: "cancel.requested" }
88
+ | { readonly type: "disposed" }
89
+
90
+ export type LineCommentEditorEffect =
91
+ | { readonly type: "mention.load"; readonly attempt: number; readonly query: string }
92
+ | { readonly type: "mention.insert"; readonly path: string; readonly token: MentionToken }
93
+ | { readonly type: "submit"; readonly mode: "primary" | "alternate"; readonly value: string }
94
+ | { readonly type: "cancel"; readonly blur: boolean }
95
+
96
+ export type LineCommentEditorTransition = {
97
+ readonly state: LineCommentEditorState
98
+ readonly effects: readonly LineCommentEditorEffect[]
99
+ /** The key event belongs to the editor: the driver calls `preventDefault()`. */
100
+ readonly handled: boolean
101
+ }
102
+
103
+ type Live = Exclude<LineCommentEditorState, { status: "disposed" | "submitting" }>
104
+
105
+ const NO_EFFECTS: readonly LineCommentEditorEffect[] = Object.freeze([])
106
+
107
+ export function createLineCommentEditorState(): LineCommentEditorState {
108
+ return { status: "editing", attempt: 0, alternate: false, dismissed: null }
109
+ }
110
+
111
+ /** The mention token the caret sits at the end of, or null for a selection, a bare `@`, or no `@` at all. */
112
+ export function parseMentionToken(
113
+ value: string,
114
+ selectionStart: number | null,
115
+ selectionEnd: number | null,
116
+ ): MentionToken | null {
117
+ if (selectionStart === null || selectionStart !== selectionEnd) return null
118
+ const match = value.slice(0, selectionStart).match(/@(\S*)$/)
119
+
120
+ if (!match?.[1]?.trim()) return null
121
+
122
+ return { query: match[1], start: selectionStart - match[0].length, end: selectionStart }
123
+ }
124
+
125
+ export function transitionLineCommentEditor(
126
+ state: LineCommentEditorState,
127
+ event: LineCommentEditorEvent,
128
+ ): LineCommentEditorTransition {
129
+ if (state.status === "disposed") return unchanged(state)
130
+
131
+ if (event.type === "disposed") return { state: { status: "disposed" }, effects: NO_EFFECTS, handled: false }
132
+
133
+ if (event.type === "props.synced") return syncProps(state, event.config, event.token)
134
+
135
+ if (event.type === "modifier.changed") {
136
+ return withAlternate(state, event.active && allowsAlternate(event.config))
137
+ }
138
+
139
+ if (event.type === "window.blurred") return withAlternate(state, false)
140
+
141
+ if (state.status === "submitting") {
142
+ if (event.type !== "key.down") return unchanged(state)
143
+
144
+ return { state, effects: NO_EFFECTS, handled: ownsKey(event, event.target, event.config) }
145
+ }
146
+
147
+ if (event.type === "mention.synced") return syncToken(state, event.token)
148
+
149
+ if (event.type === "mention.loaded") {
150
+ if (state.status !== "mention-loading" || event.attempt !== state.attempt) return unchanged(state)
151
+ const items = event.items.slice(0, MENTION_LIMIT)
152
+
153
+ if (items.length === 0) return settle(editing(state, state.token))
154
+
155
+ return settle({ ...state, status: "mention-open", items, activeIndex: 0 })
156
+ }
157
+
158
+ if (event.type === "mention.failed" || event.type === "mention.timeout") {
159
+ if (state.status !== "mention-loading" || event.attempt !== state.attempt) return unchanged(state)
160
+
161
+ return settle(editing(state, null))
162
+ }
163
+
164
+ if (event.type === "mention.hovered") {
165
+ if (state.status === "editing" || !state.items[event.index] || state.activeIndex === event.index) {
166
+ return unchanged(state)
167
+ }
168
+
169
+ return settle({ ...state, activeIndex: event.index })
170
+ }
171
+
172
+ if (event.type === "mention.selected") {
173
+ if (state.status === "editing") return unchanged(state)
174
+
175
+ return insert(state, event.index)
176
+ }
177
+
178
+ if (event.type === "action.clicked") {
179
+ const alternate = event.modified && event.config.alternateSubmit !== null
180
+
181
+ if (!alternate && !canSubmit(event.config)) return cancel(state, false)
182
+
183
+ return submit(state, event.config, alternate)
184
+ }
185
+
186
+ if (event.type === "submit.requested") return submit(state, event.config, false)
187
+
188
+ if (event.type === "cancel.requested") return cancel(state, false)
189
+
190
+ if (event.target === "action") return actionKey(state, event, event.config)
191
+
192
+ return textareaKey(state, event, event.config)
193
+ }
194
+
195
+ export function viewLineCommentEditor(state: LineCommentEditorState, config: LineCommentEditorConfig) {
196
+ const submitting = state.status === "submitting"
197
+ const submittable = canSubmit(config)
198
+ const alternateActive = state.status !== "disposed" && state.alternate && config.alternateSubmit !== null
199
+ const mentionItems = state.status === "mention-open" || state.status === "mention-loading" ? state.items : []
200
+
201
+ return {
202
+ submitting,
203
+ canSubmit: submittable,
204
+ compactCancel: !submittable,
205
+ alternateActive,
206
+ trackModifiers: state.status !== "disposed" && allowsAlternate(config),
207
+ compactVariant: alternateActive || submitting ? "submit" : config.value.trim() ? "neutral" : "ghost-muted",
208
+ compactDisabled: submitting || (alternateActive && !!config.alternateSubmit?.disabled),
209
+ mentionItems,
210
+ activeMentionPath:
211
+ state.status === "mention-open" || state.status === "mention-loading"
212
+ ? (state.items[state.activeIndex] ?? null)
213
+ : null,
214
+ } as const
215
+ }
216
+
217
+ function textareaKey(state: Live, key: LineCommentEditorKey, config: LineCommentEditorConfig) {
218
+ // The list is on screen: a fresh result, or the previous one while a refined query loads.
219
+ const listed = state.status !== "editing" && state.items.length > 0
220
+
221
+ if (key.key === "Escape") {
222
+ if (listed) return settle(editing(state, state.token), true)
223
+
224
+ return cancel(state, true)
225
+ }
226
+
227
+ if (key.key === "Enter" && !key.shift) {
228
+ if (modifiedKey(key) && config.alternateSubmit) return key.repeat ? handled(state) : submit(state, config, true)
229
+
230
+ if (state.status === "mention-open") return insert(state, state.activeIndex)
231
+
232
+ // The previous query's list is on screen while the refined one loads: picking from it would insert a stale path.
233
+ if (listed) return handled(state)
234
+
235
+ return key.repeat ? handled(state) : submit(state, config, false)
236
+ }
237
+
238
+ if (key.key === "Tab" && !key.alt && !key.ctrl && !key.meta) {
239
+ if (state.status === "mention-open") return insert(state, state.activeIndex)
240
+
241
+ if (listed) return handled(state)
242
+
243
+ return unchanged(state)
244
+ }
245
+
246
+ const step = navigation(key)
247
+
248
+ if (step === 0 || !listed) return unchanged(state)
249
+
250
+ return settle({ ...state, activeIndex: (state.activeIndex + step + state.items.length) % state.items.length }, true)
251
+ }
252
+
253
+ function actionKey(state: Live, key: LineCommentEditorKey, config: LineCommentEditorConfig) {
254
+ if (key.key === "Enter" && modifiedKey(key) && config.alternateSubmit) {
255
+ return key.repeat ? handled(state) : submit(state, config, true)
256
+ }
257
+
258
+ if (key.key === "Escape") return cancel(state, false)
259
+
260
+ return unchanged(state)
261
+ }
262
+
263
+ /** Whether a read-only, submitting editor still swallows the key so it cannot leave through a parent handler. */
264
+ function ownsKey(key: LineCommentEditorKey, target: "textarea" | "action", config: LineCommentEditorConfig) {
265
+ if (key.key === "Escape") return true
266
+
267
+ if (target === "textarea") return key.key === "Enter" && !key.shift
268
+
269
+ return key.key === "Enter" && modifiedKey(key) && config.alternateSubmit !== null
270
+ }
271
+
272
+ function syncProps(
273
+ state: Exclude<LineCommentEditorState, { status: "disposed" }>,
274
+ config: LineCommentEditorConfig,
275
+ token: MentionToken | null,
276
+ ): LineCommentEditorTransition {
277
+ const alternate = state.alternate && allowsAlternate(config)
278
+
279
+ if (config.submitting) {
280
+ if (state.status === "submitting" && state.alternate === alternate) return unchanged(state)
281
+
282
+ return settle({ status: "submitting", attempt: state.attempt, alternate })
283
+ }
284
+
285
+ if (state.status === "submitting")
286
+ return settle({ status: "editing", attempt: state.attempt, alternate, dismissed: null })
287
+
288
+ return syncToken(alternate === state.alternate ? state : { ...state, alternate }, token)
289
+ }
290
+
291
+ function syncToken(state: Live, token: MentionToken | null): LineCommentEditorTransition {
292
+ if (!token) {
293
+ if (state.status === "editing" && state.dismissed === null) return unchanged(state)
294
+
295
+ return settle(editing(state, null))
296
+ }
297
+
298
+ if (state.status === "editing" && state.dismissed?.start === token.start && state.dismissed.query === token.query) {
299
+ return unchanged(state)
300
+ }
301
+
302
+ if (state.status !== "editing" && state.token.start === token.start && state.token.query === token.query) {
303
+ return unchanged(state)
304
+ }
305
+
306
+ const attempt = state.attempt + 1
307
+ const items = state.status !== "editing" && state.token.start === token.start ? state.items : []
308
+
309
+ return {
310
+ state: { status: "mention-loading", attempt, alternate: state.alternate, token, items, activeIndex: 0 },
311
+ effects: [{ type: "mention.load", attempt, query: token.query }],
312
+ handled: false,
313
+ }
314
+ }
315
+
316
+ function submit(state: Live, config: LineCommentEditorConfig, alternate: boolean): LineCommentEditorTransition {
317
+ const mode = alternate && config.alternateSubmit ? "alternate" : "primary"
318
+
319
+ if (!canSubmit(config) || (mode === "alternate" && config.alternateSubmit?.disabled)) return handled(state)
320
+
321
+ return {
322
+ state: editing(state, null),
323
+ effects: [{ type: "submit", mode, value: config.value.trim() }],
324
+ handled: true,
325
+ }
326
+ }
327
+
328
+ function cancel(state: Live, blur: boolean): LineCommentEditorTransition {
329
+ return { state: editing(state, null), effects: [{ type: "cancel", blur }], handled: true }
330
+ }
331
+
332
+ function insert(state: Exclude<Live, { status: "editing" }>, index: number): LineCommentEditorTransition {
333
+ const path = state.items[index]
334
+
335
+ if (path === undefined) return unchanged(state)
336
+
337
+ return { state: editing(state, null), effects: [{ type: "mention.insert", path, token: state.token }], handled: true }
338
+ }
339
+
340
+ function editing(state: Live, dismissed: MentionToken | null): LineCommentEditorState {
341
+ return {
342
+ status: "editing",
343
+ attempt: state.attempt,
344
+ alternate: state.alternate,
345
+ dismissed: dismissed ? { start: dismissed.start, query: dismissed.query } : null,
346
+ }
347
+ }
348
+
349
+ function withAlternate(state: Exclude<LineCommentEditorState, { status: "disposed" }>, alternate: boolean) {
350
+ if (state.alternate === alternate) return unchanged(state)
351
+
352
+ return settle({ ...state, alternate })
353
+ }
354
+
355
+ function navigation(key: LineCommentEditorKey) {
356
+ if (!key.alt && !key.meta && key.key === "ArrowDown") return 1
357
+
358
+ if (!key.alt && !key.meta && key.key === "ArrowUp") return -1
359
+
360
+ if (!key.ctrl || key.meta || key.alt || key.shift) return 0
361
+
362
+ if (key.key === "n") return 1
363
+
364
+ if (key.key === "p") return -1
365
+
366
+ return 0
367
+ }
368
+
369
+ function allowsAlternate(config: LineCommentEditorConfig) {
370
+ return config.variant === "compact" && config.alternateSubmit !== null
371
+ }
372
+
373
+ function canSubmit(config: LineCommentEditorConfig) {
374
+ return config.allowEmpty || config.value.trim().length > 0
375
+ }
376
+
377
+ function modifiedKey(key: LineCommentEditorKey) {
378
+ return (key.meta || key.ctrl) && !key.alt && !key.shift
379
+ }
380
+
381
+ function unchanged(state: LineCommentEditorState): LineCommentEditorTransition {
382
+ return { state, effects: NO_EFFECTS, handled: false }
383
+ }
384
+
385
+ function handled(state: LineCommentEditorState): LineCommentEditorTransition {
386
+ return { state, effects: NO_EFFECTS, handled: true }
387
+ }
388
+
389
+ function settle(state: LineCommentEditorState, keyHandled = false): LineCommentEditorTransition {
390
+ return { state, effects: NO_EFFECTS, handled: keyHandled }
391
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * A hovered generated avatar's animation. It runs once per frame, so it never allocates: every state and transition is
3
+ * a frozen module singleton, events are reused by the driver, and the continuous time lives in an `AvatarClock` that
4
+ * only `transitionAvatarAnimation` writes.
5
+ */
6
+
7
+ // The animation starts once the pointer rests on the avatar's row, so passing over a list does no drawing.
8
+ export const AVATAR_REST_MS = 150
9
+
10
+ export const AVATAR_MAX_STEP_S = 0.1
11
+
12
+ export const AVATAR_DECAY_BASE = 0.002
13
+
14
+ export const AVATAR_SETTLE_EPSILON = 0.01
15
+
16
+ export type AvatarAnimationState =
17
+ | { readonly status: "static" }
18
+ /** Hovered, waiting out the rest delay before the first animated frame. */
19
+ | { readonly status: "resting" }
20
+ | { readonly status: "animating" }
21
+ /** The pointer left; the animation slows back to the static frame. */
22
+ | { readonly status: "easing" }
23
+ | { readonly status: "disposed" }
24
+
25
+ export type AvatarClock = {
26
+ entered: number
27
+ last: number
28
+ time: number
29
+ }
30
+
31
+ export type AvatarAnimationEvent =
32
+ | { readonly type: "pointer.entered"; now: number; reduced: boolean }
33
+ | { readonly type: "pointer.left" }
34
+ | { readonly type: "frame"; now: number }
35
+ | { readonly type: "motion.changed"; reduced: boolean }
36
+ | { readonly type: "refresh" }
37
+ | { readonly type: "disposed" }
38
+
39
+ export type AvatarAnimationEffect = "none" | "paint-static" | "paint-frame"
40
+
41
+ export type AvatarAnimationTransition = {
42
+ readonly state: AvatarAnimationState
43
+ readonly effect: AvatarAnimationEffect
44
+ }
45
+
46
+ const STATIC: AvatarAnimationState = Object.freeze({ status: "static" })
47
+
48
+ const RESTING: AvatarAnimationState = Object.freeze({ status: "resting" })
49
+
50
+ const ANIMATING: AvatarAnimationState = Object.freeze({ status: "animating" })
51
+
52
+ const EASING: AvatarAnimationState = Object.freeze({ status: "easing" })
53
+
54
+ const DISPOSED: AvatarAnimationState = Object.freeze({ status: "disposed" })
55
+
56
+ export const INITIAL_AVATAR_ANIMATION_STATE = STATIC
57
+
58
+ const EFFECTS: readonly AvatarAnimationEffect[] = ["none", "paint-static", "paint-frame"]
59
+
60
+ const TRANSITIONS = new Map(
61
+ [STATIC, RESTING, ANIMATING, EASING, DISPOSED].map((state) => [
62
+ state,
63
+ new Map(EFFECTS.map((effect) => [effect, Object.freeze({ state, effect })])),
64
+ ]),
65
+ )
66
+
67
+ const to = (state: AvatarAnimationState, effect: AvatarAnimationEffect): AvatarAnimationTransition =>
68
+ TRANSITIONS.get(state)!.get(effect)!
69
+
70
+ export const AVATAR_POINTER_LEFT: AvatarAnimationEvent = Object.freeze({ type: "pointer.left" })
71
+
72
+ export const AVATAR_REFRESH: AvatarAnimationEvent = Object.freeze({ type: "refresh" })
73
+
74
+ export const AVATAR_DISPOSED: AvatarAnimationEvent = Object.freeze({ type: "disposed" })
75
+
76
+ export function createAvatarClock(): AvatarClock {
77
+ return { entered: 0, last: 0, time: 0 }
78
+ }
79
+
80
+ export function transitionAvatarAnimation(
81
+ state: AvatarAnimationState,
82
+ clock: AvatarClock,
83
+ event: AvatarAnimationEvent,
84
+ ): AvatarAnimationTransition {
85
+ if (state === DISPOSED) return to(state, "none")
86
+
87
+ if (event.type === "disposed") {
88
+ reset(clock)
89
+
90
+ return to(DISPOSED, "none")
91
+ }
92
+
93
+ if (event.type === "motion.changed") {
94
+ if (!event.reduced || state === STATIC) return to(state, "none")
95
+ reset(clock)
96
+
97
+ // A resting avatar still shows its static frame.
98
+ return to(STATIC, state === RESTING ? "none" : "paint-static")
99
+ }
100
+
101
+ if (event.type === "refresh") return to(state, state === STATIC || state === RESTING ? "paint-static" : "none")
102
+
103
+ if (state === STATIC) {
104
+ if (event.type !== "pointer.entered" || event.reduced) return to(state, "none")
105
+ clock.entered = clock.last = event.now
106
+ clock.time = 0
107
+
108
+ return to(RESTING, "none")
109
+ }
110
+
111
+ if (state === RESTING) {
112
+ if (event.type === "pointer.left") {
113
+ reset(clock)
114
+
115
+ return to(STATIC, "none")
116
+ }
117
+
118
+ if (event.type !== "frame") return to(state, "none")
119
+
120
+ if (event.now - clock.entered < AVATAR_REST_MS) {
121
+ clock.last = event.now
122
+
123
+ return to(state, "none")
124
+ }
125
+
126
+ clock.time += step(event.now - clock.last)
127
+ clock.entered = 0
128
+ clock.last = event.now
129
+
130
+ return to(ANIMATING, "paint-frame")
131
+ }
132
+
133
+ if (state === ANIMATING) {
134
+ if (event.type === "pointer.left") return to(EASING, "none")
135
+
136
+ if (event.type !== "frame") return to(state, "none")
137
+ clock.time += step(event.now - clock.last)
138
+ clock.last = event.now
139
+
140
+ return to(state, "paint-frame")
141
+ }
142
+
143
+ if (event.type === "pointer.entered") {
144
+ if (event.reduced) return to(state, "none")
145
+ clock.last = event.now
146
+
147
+ return to(ANIMATING, "none")
148
+ }
149
+
150
+ if (event.type !== "frame") return to(state, "none")
151
+ const time = clock.time * AVATAR_DECAY_BASE ** step(event.now - clock.last)
152
+
153
+ if (time < AVATAR_SETTLE_EPSILON) {
154
+ reset(clock)
155
+
156
+ return to(STATIC, "paint-static")
157
+ }
158
+
159
+ clock.time = time
160
+ clock.last = event.now
161
+
162
+ return to(state, "paint-frame")
163
+ }
164
+
165
+ /** Hovered or easing: the driver runs the frame loop and follows the reduce motion setting. */
166
+ export function selectAvatarRunning(state: AvatarAnimationState) {
167
+ return state === RESTING || state === ANIMATING || state === EASING
168
+ }
169
+
170
+ export function selectAvatarPaintTime(state: AvatarAnimationState, clock: Readonly<AvatarClock>) {
171
+ return state === ANIMATING || state === EASING ? clock.time : 0
172
+ }
173
+
174
+ function step(ms: number) {
175
+ return Math.max(0, Math.min(ms / 1000, AVATAR_MAX_STEP_S))
176
+ }
177
+
178
+ function reset(clock: AvatarClock) {
179
+ clock.entered = 0
180
+ clock.last = 0
181
+ clock.time = 0
182
+ }