@opencode/ui 0.0.0-dev-735 → 0.0.0-dev-767

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 (56) hide show
  1. package/dist/components/animated-number-machine.d.ts +95 -0
  2. package/dist/components/image-flight-machine.d.ts +135 -0
  3. package/dist/components/image-zoom-machine.d.ts +95 -0
  4. package/dist/components/image-zoom.d.ts +6 -3
  5. package/dist/components/popover-machine.d.ts +96 -0
  6. package/dist/components/popover.d.ts +2 -2
  7. package/dist/components/resize-handle-machine.d.ts +71 -0
  8. package/dist/components/scroll-view-machine.d.ts +66 -0
  9. package/dist/components/scroll-view.d.ts +2 -2
  10. package/dist/components/text-field-machine.d.ts +62 -0
  11. package/dist/components/text-reveal-machine.d.ts +90 -0
  12. package/dist/data-display/line-comment/machine.d.ts +157 -0
  13. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  14. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  15. package/dist/feedback/toast/machine.d.ts +57 -0
  16. package/dist/feedback/toast/toast.d.ts +1 -1
  17. package/dist/forms/select/machine.d.ts +45 -0
  18. package/dist/forms/typeahead/machine.d.ts +85 -0
  19. package/dist/navigation/menu/machine.d.ts +49 -0
  20. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  21. package/dist/theme/context.d.ts +20 -9
  22. package/dist/theme/machine.d.ts +174 -0
  23. package/package.json +3 -3
  24. package/src/components/animated-number-machine.ts +214 -0
  25. package/src/components/animated-number.tsx +134 -83
  26. package/src/components/image-flight-machine.ts +247 -0
  27. package/src/components/image-preview.css +5 -2
  28. package/src/components/image-preview.tsx +390 -252
  29. package/src/components/image-zoom-machine.ts +191 -0
  30. package/src/components/image-zoom.ts +22 -14
  31. package/src/components/popover-machine.ts +198 -0
  32. package/src/components/popover.tsx +157 -89
  33. package/src/components/resize-handle-machine.ts +174 -0
  34. package/src/components/resize-handle.tsx +113 -83
  35. package/src/components/scroll-view-machine.ts +231 -0
  36. package/src/components/scroll-view.tsx +163 -116
  37. package/src/components/text-field-machine.ts +102 -0
  38. package/src/components/text-field.tsx +78 -24
  39. package/src/components/text-reveal-machine.ts +184 -0
  40. package/src/components/text-reveal.tsx +96 -93
  41. package/src/data-display/line-comment/line-comment.tsx +154 -178
  42. package/src/data-display/line-comment/machine.ts +392 -0
  43. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  44. package/src/data-display/project-avatar/animation.ts +67 -38
  45. package/src/feedback/toast/machine.ts +115 -0
  46. package/src/feedback/toast/toast.tsx +55 -59
  47. package/src/forms/select/machine.ts +81 -0
  48. package/src/forms/select/select.tsx +49 -27
  49. package/src/forms/typeahead/machine.ts +253 -0
  50. package/src/forms/typeahead/typeahead.tsx +70 -62
  51. package/src/navigation/menu/machine.ts +109 -0
  52. package/src/navigation/menu/menu.tsx +118 -20
  53. package/src/navigation/segmented-control/machine.ts +72 -0
  54. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  55. package/src/theme/context.tsx +134 -240
  56. package/src/theme/machine.ts +508 -0
@@ -0,0 +1,174 @@
1
+ import type { DesktopTheme } from "./types";
2
+ export type ColorScheme = "light" | "dark" | "system";
3
+ export type ResolvedMode = "light" | "dark";
4
+ export declare const DEFAULT_THEME_ID = "oc-2";
5
+ /** A bundled theme import that has not settled by then shows the default theme until it arrives. */
6
+ export declare const THEME_LOAD_TIMEOUT_MS = 5000;
7
+ export type ThemeSelection = {
8
+ readonly themeId: string;
9
+ readonly scheme: ColorScheme;
10
+ };
11
+ /** Overrides one or both axes of the committed selection; a preview with neither is `committed`. */
12
+ export type ThemePreview = {
13
+ readonly themeId?: string;
14
+ readonly scheme?: ColorScheme;
15
+ };
16
+ /** What the window shows. `preload` is a registered theme that may still arrive: the cached colors stay on screen. */
17
+ export type AppliedTheme = {
18
+ readonly kind: "theme";
19
+ readonly id: string;
20
+ readonly theme: DesktopTheme;
21
+ readonly mode: ResolvedMode;
22
+ readonly scheme: ColorScheme;
23
+ } | {
24
+ readonly kind: "preload";
25
+ readonly id: string;
26
+ readonly mode: ResolvedMode;
27
+ readonly scheme: ColorScheme;
28
+ };
29
+ /** The colors in localStorage that the next start paints before the app loads. `unknown` keeps the last run's. */
30
+ export type CachedTheme = {
31
+ readonly status: "unknown";
32
+ } | {
33
+ readonly status: "cleared";
34
+ } | {
35
+ readonly status: "cached";
36
+ readonly id: string;
37
+ readonly theme: DesktopTheme;
38
+ };
39
+ type ThemeData = {
40
+ readonly committed: ThemeSelection;
41
+ readonly systemMode: ResolvedMode;
42
+ /** `settled` once every theme source has registered what it will: a missing registered theme is not coming. */
43
+ readonly registration: "pending" | "settled";
44
+ /** Loaded bundled themes and registered themes, by id. */
45
+ readonly themes: Readonly<Record<string, DesktopTheme>>;
46
+ /** The attempt of each bundled theme import in flight, including one that timed out and may still arrive. */
47
+ readonly loads: Readonly<Record<string, number>>;
48
+ /**
49
+ * Bundled themes the window shows the default theme for: the import failed, or timed out and may still arrive.
50
+ * Selecting, previewing or preloading one tries again.
51
+ */
52
+ readonly failed: Readonly<Record<string, true>>;
53
+ readonly issued: number;
54
+ readonly applied: AppliedTheme | undefined;
55
+ readonly cached: CachedTheme;
56
+ };
57
+ export type ThemeMachineState = ({
58
+ readonly status: "committed";
59
+ } & ThemeData) | ({
60
+ readonly status: "previewing";
61
+ readonly preview: ThemePreview;
62
+ } & ThemeData) | ({
63
+ readonly status: "disposed";
64
+ } & ThemeData);
65
+ export type ThemeEvent = {
66
+ readonly type: "theme.selected";
67
+ readonly id: string;
68
+ } | {
69
+ readonly type: "scheme.selected";
70
+ readonly scheme: ColorScheme;
71
+ } | {
72
+ readonly type: "preview.theme";
73
+ readonly id: string;
74
+ } | {
75
+ readonly type: "preview.scheme";
76
+ readonly scheme: ColorScheme;
77
+ } | {
78
+ readonly type: "preview.committed";
79
+ } | {
80
+ readonly type: "preview.cancelled";
81
+ } | {
82
+ readonly type: "theme.registered";
83
+ readonly theme: DesktopTheme;
84
+ } | {
85
+ readonly type: "theme.unregistered";
86
+ readonly theme: DesktopTheme;
87
+ } | {
88
+ readonly type: "settled";
89
+ } | {
90
+ readonly type: "themes.preload";
91
+ } | {
92
+ readonly type: "theme.loaded";
93
+ readonly id: string;
94
+ readonly attempt: number;
95
+ readonly theme: DesktopTheme;
96
+ } | {
97
+ readonly type: "theme.load.failed";
98
+ readonly id: string;
99
+ readonly attempt: number;
100
+ } | {
101
+ readonly type: "theme.load.timed-out";
102
+ readonly id: string;
103
+ readonly attempt: number;
104
+ } | {
105
+ readonly type: "system.mode.changed";
106
+ readonly mode: ResolvedMode;
107
+ } | {
108
+ readonly type: "storage.theme.changed";
109
+ readonly id: string;
110
+ } | {
111
+ readonly type: "storage.scheme.changed";
112
+ readonly scheme: ColorScheme;
113
+ } | {
114
+ readonly type: "disposed";
115
+ };
116
+ export type ThemeEffect = {
117
+ readonly type: "load-theme";
118
+ readonly id: string;
119
+ readonly attempt: number;
120
+ readonly timeoutMs: number;
121
+ } | {
122
+ readonly type: "cancel-load-timer";
123
+ readonly attempt: number;
124
+ } | {
125
+ readonly type: "cancel-all-loads";
126
+ } | {
127
+ readonly type: "persist-theme";
128
+ readonly id: string;
129
+ } | {
130
+ readonly type: "persist-scheme";
131
+ readonly scheme: ColorScheme;
132
+ } | {
133
+ readonly type: "cache-theme";
134
+ readonly id: string;
135
+ readonly theme: DesktopTheme;
136
+ } | {
137
+ readonly type: "clear-cache";
138
+ } | {
139
+ readonly type: "apply-theme";
140
+ readonly id: string;
141
+ readonly theme: DesktopTheme;
142
+ readonly mode: ResolvedMode;
143
+ readonly scheme: ColorScheme;
144
+ } | {
145
+ readonly type: "notify-applied";
146
+ readonly mode: ResolvedMode;
147
+ readonly scheme: ColorScheme;
148
+ } | {
149
+ readonly type: "warn-missing-theme";
150
+ readonly id: string;
151
+ };
152
+ export type ThemeTransition = {
153
+ readonly state: ThemeMachineState;
154
+ readonly effects: readonly ThemeEffect[];
155
+ };
156
+ export declare function parseColorScheme(value: string | null | undefined): ColorScheme;
157
+ export declare function createThemeState(input: {
158
+ readonly themeId: string;
159
+ readonly scheme: ColorScheme;
160
+ readonly systemMode: ResolvedMode;
161
+ readonly defaultTheme: DesktopTheme;
162
+ readonly bundled: ReadonlySet<string>;
163
+ }): ThemeTransition;
164
+ export declare function transitionTheme(state: ThemeMachineState, event: ThemeEvent, bundled: ReadonlySet<string>): ThemeTransition;
165
+ /** The theme the window should show when nothing is previewed. */
166
+ export declare function selectCommittedThemeId(state: ThemeMachineState, bundled: ReadonlySet<string>): string;
167
+ export declare function selectThemeId(state: ThemeMachineState, bundled: ReadonlySet<string>): string;
168
+ /** The scheme the user chose; a preview does not change it. */
169
+ export declare function selectColorScheme(state: ThemeMachineState): ColorScheme;
170
+ /** The light or dark mode the window shows, preview included. */
171
+ export declare function selectMode(state: ThemeMachineState): ResolvedMode;
172
+ /** An import is still within its timeout. */
173
+ export declare function selectThemesLoading(state: ThemeMachineState): boolean;
174
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-735",
3
+ "version": "0.0.0-dev-767",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -273,7 +273,7 @@
273
273
  "generate:v2-oc2": "bun run script/build-oc2-v2-overrides.ts"
274
274
  },
275
275
  "devDependencies": {
276
- "@hona/strobe": "0.7.0",
276
+ "@hona/strobe": "0.7.3",
277
277
  "@solidjs/meta": "0.29.4",
278
278
  "@tsconfig/node22": "22.0.2",
279
279
  "@types/bun": "1.4.3",
@@ -288,7 +288,7 @@
288
288
  "dependencies": {
289
289
  "@kitlangton/solid-motion": "0.2.2",
290
290
  "@kobalte/core": "0.13.13",
291
- "@opencode/util": "0.0.0-dev-735",
291
+ "@opencode/util": "0.0.0-dev-767",
292
292
  "@pierre/diffs": "1.6.1",
293
293
  "@pierre/highlights": "0.2.0",
294
294
  "@solid-primitives/event-listener": "2.4.6",
@@ -0,0 +1,214 @@
1
+ export const ANIMATED_NUMBER_TRACK_LENGTH = 30
2
+
3
+ /** Added to the measured transition time before a roll or resize that never reported its end settles anyway. */
4
+ export const ANIMATED_NUMBER_FALLBACK_PAD_MS = 100
5
+
6
+ /**
7
+ * `resizing` eases the width to `targetDigits` while extra leading places stay mounted to roll in or out. `attempt`
8
+ * names each eased change, so a fallback timer from an earlier one cannot cut the current one short.
9
+ */
10
+ export type AnimatedNumberState =
11
+ | {
12
+ readonly status: "static"
13
+ readonly attempt: number
14
+ readonly value: number
15
+ readonly direction: 1 | -1
16
+ readonly places: number
17
+ }
18
+ | {
19
+ readonly status: "resizing"
20
+ readonly attempt: number
21
+ readonly value: number
22
+ readonly direction: 1 | -1
23
+ readonly places: number
24
+ readonly targetDigits: number
25
+ }
26
+ | { readonly status: "disposed" }
27
+
28
+ /** `canAnimate` is false while detached or under reduced motion, where no transition runs to report its end. */
29
+ export type AnimatedNumberEvent =
30
+ | { readonly type: "value.changed"; readonly value: number; readonly canAnimate: boolean }
31
+ | { readonly type: "resize.ended" }
32
+ | { readonly type: "resize.timed-out"; readonly attempt: number }
33
+ | { readonly type: "disposed" }
34
+
35
+ export type AnimatedDigitState =
36
+ | { readonly status: "static"; readonly attempt: number; readonly digit: number; readonly step: number }
37
+ | { readonly status: "rolling"; readonly attempt: number; readonly digit: number; readonly step: number }
38
+ | { readonly status: "disposed" }
39
+
40
+ export type AnimatedDigitEvent =
41
+ | { readonly type: "digit.changed"; readonly digit: number; readonly direction: 1 | -1; readonly canAnimate: boolean }
42
+ | { readonly type: "roll.ended" }
43
+ | { readonly type: "roll.timed-out"; readonly attempt: number }
44
+ | { readonly type: "disposed" }
45
+
46
+ /** `watch` arms the fallback for an eased change; `clear` drops it once the change has settled. */
47
+ export type AnimatedNumberEffect = { readonly type: "watch"; readonly attempt: number } | { readonly type: "clear" }
48
+
49
+ export type AnimatedNumberTransition<State> = {
50
+ readonly state: State
51
+ readonly effects: readonly AnimatedNumberEffect[]
52
+ }
53
+
54
+ const NONE: readonly AnimatedNumberEffect[] = Object.freeze([])
55
+
56
+ const CLEAR: readonly AnimatedNumberEffect[] = Object.freeze([Object.freeze({ type: "clear" })])
57
+
58
+ const DISPOSED = Object.freeze({ status: "disposed" as const })
59
+
60
+ export function createAnimatedNumberState(value: number): AnimatedNumberState {
61
+ return { status: "static", attempt: 0, value, direction: 1, places: digits(value) + 1 }
62
+ }
63
+
64
+ export function transitionAnimatedNumber(
65
+ state: AnimatedNumberState,
66
+ event: AnimatedNumberEvent,
67
+ ): AnimatedNumberTransition<AnimatedNumberState> {
68
+ if (state.status === "disposed") return { state, effects: NONE }
69
+
70
+ switch (event.type) {
71
+ case "disposed":
72
+ return { state: DISPOSED, effects: state.status === "resizing" ? CLEAR : NONE }
73
+ case "value.changed": {
74
+ if (event.value === state.value) return { state, effects: NONE }
75
+ const direction = event.value > state.value ? 1 : -1
76
+ const count = digits(event.value)
77
+
78
+ if (!event.canAnimate)
79
+ return {
80
+ state: { status: "static", attempt: state.attempt, value: event.value, direction, places: count + 1 },
81
+ effects: state.status === "resizing" ? CLEAR : NONE,
82
+ }
83
+
84
+ // Only a change to the digit count eases the width, so a late font swap or zoom never animates it.
85
+ if (count === (state.status === "resizing" ? state.targetDigits : digits(state.value)))
86
+ return { state: { ...state, value: event.value, direction }, effects: NONE }
87
+
88
+ const attempt = state.attempt + 1
89
+
90
+ return {
91
+ state: {
92
+ status: "resizing",
93
+ attempt,
94
+ value: event.value,
95
+ direction,
96
+ places: Math.max(state.places, count + 1),
97
+ targetDigits: count,
98
+ },
99
+ effects: [{ type: "watch", attempt }],
100
+ }
101
+ }
102
+
103
+ case "resize.ended":
104
+ return settleNumber(state)
105
+ case "resize.timed-out":
106
+ if (state.status !== "resizing" || event.attempt !== state.attempt) return { state, effects: NONE }
107
+
108
+ return settleNumber(state)
109
+ }
110
+ }
111
+
112
+ export function createAnimatedDigitState(digit: number): AnimatedDigitState {
113
+ return { status: "static", attempt: 0, digit, step: digit + 10 }
114
+ }
115
+
116
+ export function transitionAnimatedDigit(
117
+ state: AnimatedDigitState,
118
+ event: AnimatedDigitEvent,
119
+ ): AnimatedNumberTransition<AnimatedDigitState> {
120
+ if (state.status === "disposed") return { state, effects: NONE }
121
+
122
+ switch (event.type) {
123
+ case "disposed":
124
+ return { state: DISPOSED, effects: state.status === "rolling" ? CLEAR : NONE }
125
+ case "digit.changed": {
126
+ const delta = spin(state.digit, event.digit, event.direction)
127
+
128
+ // An unchanged digit keeps any roll it is in the middle of.
129
+ if (!delta) return { state, effects: NONE }
130
+ const target = state.step + delta
131
+
132
+ // Rapid changes can run off the track, so they settle at once like a digit that cannot animate.
133
+ if (!event.canAnimate || target < 0 || target >= ANIMATED_NUMBER_TRACK_LENGTH)
134
+ return {
135
+ state: { status: "static", attempt: state.attempt, digit: event.digit, step: event.digit + 10 },
136
+ effects: state.status === "rolling" ? CLEAR : NONE,
137
+ }
138
+
139
+ const attempt = state.attempt + 1
140
+
141
+ return {
142
+ state: { status: "rolling", attempt, digit: event.digit, step: target },
143
+ effects: [{ type: "watch", attempt }],
144
+ }
145
+ }
146
+
147
+ case "roll.ended":
148
+ return settleDigit(state)
149
+ case "roll.timed-out":
150
+ if (state.status !== "rolling" || event.attempt !== state.attempt) return { state, effects: NONE }
151
+
152
+ return settleDigit(state)
153
+ }
154
+ }
155
+
156
+ export function selectAnimatedNumberValue(state: AnimatedNumberState) {
157
+ return state.status === "disposed" ? 0 : state.value
158
+ }
159
+
160
+ export function selectAnimatedNumberDirection(state: AnimatedNumberState) {
161
+ return state.status === "disposed" ? 1 : state.direction
162
+ }
163
+
164
+ export function selectAnimatedNumberPlaces(state: AnimatedNumberState) {
165
+ return state.status === "disposed" ? 1 : state.places
166
+ }
167
+
168
+ export function selectAnimatedNumberResizing(state: AnimatedNumberState) {
169
+ return state.status === "resizing"
170
+ }
171
+
172
+ export function selectAnimatedDigitRolling(state: AnimatedDigitState) {
173
+ return state.status === "rolling"
174
+ }
175
+
176
+ export function selectAnimatedDigitStep(state: AnimatedDigitState) {
177
+ return state.status === "disposed" ? 10 : state.step
178
+ }
179
+
180
+ function settleNumber(state: Exclude<AnimatedNumberState, { status: "disposed" }>) {
181
+ if (state.status !== "resizing") return { state, effects: NONE }
182
+
183
+ return {
184
+ state: {
185
+ status: "static" as const,
186
+ attempt: state.attempt,
187
+ value: state.value,
188
+ direction: state.direction,
189
+ places: state.targetDigits + 1,
190
+ },
191
+ effects: CLEAR,
192
+ }
193
+ }
194
+
195
+ function settleDigit(state: Exclude<AnimatedDigitState, { status: "disposed" }>) {
196
+ if (state.status !== "rolling") return { state, effects: NONE }
197
+
198
+ return {
199
+ state: { status: "static" as const, attempt: state.attempt, digit: state.digit, step: state.digit + 10 },
200
+ effects: CLEAR,
201
+ }
202
+ }
203
+
204
+ function digits(value: number) {
205
+ return String(value).length
206
+ }
207
+
208
+ function spin(from: number, to: number, direction: 1 | -1) {
209
+ if (from === to) return 0
210
+
211
+ if (direction > 0) return (to - from + 10) % 10
212
+
213
+ return -((from - to + 10) % 10)
214
+ }
@@ -1,70 +1,73 @@
1
- import { For, createEffect, createMemo, on } from "solid-js"
2
- import { createStore } from "solid-js/store"
3
-
4
- const TRACK = Array.from({ length: 30 }, (_, index) => index % 10)
1
+ import { For, createEffect, createMemo, createSignal, on, onCleanup, untrack } from "solid-js"
2
+ import { prefersReducedMotion } from "../motion/reduced"
3
+ import {
4
+ ANIMATED_NUMBER_FALLBACK_PAD_MS,
5
+ ANIMATED_NUMBER_TRACK_LENGTH,
6
+ createAnimatedDigitState,
7
+ createAnimatedNumberState,
8
+ selectAnimatedDigitRolling,
9
+ selectAnimatedDigitStep,
10
+ selectAnimatedNumberDirection,
11
+ selectAnimatedNumberPlaces,
12
+ selectAnimatedNumberResizing,
13
+ selectAnimatedNumberValue,
14
+ transitionAnimatedDigit,
15
+ transitionAnimatedNumber,
16
+ type AnimatedDigitEvent,
17
+ type AnimatedNumberEffect,
18
+ type AnimatedNumberEvent,
19
+ } from "./animated-number-machine"
20
+
21
+ const TRACK = Array.from({ length: ANIMATED_NUMBER_TRACK_LENGTH }, (_, index) => index % 10)
5
22
 
6
23
  const DURATION = 600
7
24
 
8
- function normalize(value: number) {
9
- return ((value % 10) + 10) % 10
10
- }
25
+ const ROLL_ENDED: AnimatedDigitEvent = { type: "roll.ended" }
11
26
 
12
- function spin(from: number, to: number, direction: 1 | -1) {
13
- if (from === to) return 0
27
+ const RESIZE_ENDED: AnimatedNumberEvent = { type: "resize.ended" }
14
28
 
15
- if (direction > 0) return (to - from + 10) % 10
29
+ function Digit(props: { value: number; direction: 1 | -1 }) {
30
+ const [digit, setDigit] = createSignal(createAnimatedDigitState(props.value))
31
+ let strip: HTMLSpanElement | undefined
16
32
 
17
- return -((from - to + 10) % 10)
18
- }
33
+ const fallback = createFallback(
34
+ () => strip,
35
+ (attempt) => dispatch({ type: "roll.timed-out", attempt }),
36
+ )
19
37
 
20
- function Digit(props: { value: number; direction: 1 | -1 }) {
21
- const [state, setState] = createStore({
22
- step: props.value + 10,
23
- animating: false,
24
- })
38
+ const dispatch = (event: AnimatedDigitEvent) => {
39
+ const prev = untrack(digit)
40
+ const next = transitionAnimatedDigit(prev, event)
25
41
 
26
- const step = () => state.step
27
- const animating = () => state.animating
28
- let last = props.value
29
- let strip: HTMLSpanElement | undefined
42
+ if (next.state !== prev) setDigit(next.state)
43
+ next.effects.forEach(fallback)
44
+ }
30
45
 
31
46
  createEffect(
32
47
  on(
33
48
  () => props.value,
34
- (next) => {
35
- const delta = spin(last, next, props.direction)
36
- last = next
37
-
38
- // An unchanged digit keeps any roll it is in the middle of.
39
- if (!delta) return
40
-
41
- const target = step() + delta
42
-
43
- // A detached digit never finishes a roll, and rapid changes can run off the track, so both settle at once.
44
- if (!strip?.isConnected || target < 0 || target >= TRACK.length) {
45
- setState({ animating: false, step: next + 10 })
46
-
47
- return
48
- }
49
-
50
- setState({ animating: true, step: target })
51
- },
49
+ (value) =>
50
+ dispatch({ type: "digit.changed", digit: value, direction: props.direction, canAnimate: canAnimate(strip) }),
52
51
  { defer: true },
53
52
  ),
54
53
  )
55
54
 
55
+ onCleanup(() => dispatch({ type: "disposed" }))
56
+
57
+ const ended = (event: TransitionEvent) => {
58
+ if (transitionEnded(event, strip, "top")) dispatch(ROLL_ENDED)
59
+ }
60
+
56
61
  return (
57
- <span data-slot="animated-number-digit" data-animating={animating() ? "true" : "false"}>
62
+ <span data-slot="animated-number-digit" data-animating={selectAnimatedDigitRolling(digit()) ? "true" : "false"}>
58
63
  <span data-slot="animated-number-static" data-digit={props.value} />
59
64
  <span
60
65
  ref={strip}
61
66
  data-slot="animated-number-strip"
62
- onTransitionEnd={() => {
63
- setState("animating", false)
64
- setState("step", (value) => normalize(value) + 10)
65
- }}
67
+ onTransitionEnd={ended}
68
+ onTransitionCancel={ended}
66
69
  style={{
67
- "--animated-number-offset": `${step()}`,
70
+ "--animated-number-offset": `${selectAnimatedDigitStep(digit())}`,
68
71
  "--animated-number-duration": `var(--tool-motion-odometer-ms, ${DURATION}ms)`,
69
72
  }}
70
73
  >
@@ -83,50 +86,39 @@ export function AnimatedNumber(props: { value: number; class?: string }) {
83
86
 
84
87
  // A spare leading zero waits outside the clipped width to roll in. A digit that leaves stays until the width has
85
88
  // shrunk past it, so it rolls to zero instead of vanishing.
86
- const [state, setState] = createStore<{ value: number; direction: 1 | -1; places: number; resizing: boolean }>({
87
- value: target(),
88
- direction: 1,
89
- places: String(target()).length + 1,
90
- resizing: false,
91
- })
92
-
93
- const value = () => state.value
94
- const direction = () => state.direction
89
+ const [number, setNumber] = createSignal(createAnimatedNumberState(target()))
95
90
  let box: HTMLSpanElement | undefined
96
91
 
97
- createEffect(
98
- on(
99
- target,
100
- (next) => {
101
- const current = value()
102
-
103
- if (next === current) return
104
-
105
- const digits = String(next).length
92
+ const fallback = createFallback(
93
+ () => box,
94
+ (attempt) => dispatch({ type: "resize.timed-out", attempt }),
95
+ )
106
96
 
107
- // A detached number cannot ease its width and would never hear it end, so it settles at once.
108
- if (!box?.isConnected) {
109
- setState({ direction: next > current ? 1 : -1, value: next, places: digits + 1, resizing: false })
97
+ const dispatch = (event: AnimatedNumberEvent) => {
98
+ const prev = untrack(number)
99
+ const next = transitionAnimatedNumber(prev, event)
110
100
 
111
- return
112
- }
101
+ if (next.state !== prev) setNumber(next.state)
102
+ next.effects.forEach(fallback)
103
+ }
113
104
 
114
- // Only a change to the digit count eases the width, so a late font swap or zoom never animates it.
115
- setState({
116
- direction: next > current ? 1 : -1,
117
- value: next,
118
- places: Math.max(state.places, digits + 1),
119
- resizing: state.resizing || digits !== String(current).length,
120
- })
121
- },
122
- { defer: true },
123
- ),
105
+ createEffect(
106
+ on(target, (value) => dispatch({ type: "value.changed", value, canAnimate: canAnimate(box) }), { defer: true }),
124
107
  )
125
108
 
109
+ onCleanup(() => dispatch({ type: "disposed" }))
110
+
111
+ const value = () => selectAnimatedNumberValue(number())
112
+ const direction = () => selectAnimatedNumberDirection(number())
126
113
  const label = createMemo(() => value().toString())
114
+ const count = createMemo(() => selectAnimatedNumberPlaces(number()))
127
115
 
128
116
  // Place values, most significant first. Keying by place keeps each digit rolling as digits appear and leave.
129
- const places = createMemo(() => Array.from({ length: state.places }, (_, index) => state.places - 1 - index))
117
+ const places = createMemo(() => Array.from({ length: count() }, (_, index) => count() - 1 - index))
118
+
119
+ const ended = (event: TransitionEvent) => {
120
+ if (transitionEnded(event, box, "width")) dispatch(RESIZE_ENDED)
121
+ }
130
122
 
131
123
  return (
132
124
  <span data-component="animated-number" class={props.class}>
@@ -135,12 +127,11 @@ export function AnimatedNumber(props: { value: number; class?: string }) {
135
127
  <span
136
128
  ref={box}
137
129
  data-slot="animated-number-value"
138
- data-resizing={state.resizing ? "true" : "false"}
130
+ data-resizing={selectAnimatedNumberResizing(number()) ? "true" : "false"}
139
131
  aria-hidden="true"
140
132
  style={{ "--animated-number-width": `${label().length}ch` }}
141
- onTransitionEnd={(event) => {
142
- if (event.target === event.currentTarget) setState({ places: label().length + 1, resizing: false })
143
- }}
133
+ onTransitionEnd={ended}
134
+ onTransitionCancel={ended}
144
135
  >
145
136
  <For each={places()}>
146
137
  {(place) => <Digit value={Math.floor(value() / 10 ** place) % 10} direction={direction()} />}
@@ -149,3 +140,63 @@ export function AnimatedNumber(props: { value: number; class?: string }) {
149
140
  </span>
150
141
  )
151
142
  }
143
+
144
+ // A detached element never runs a transition, and reduced motion runs them in 0 ms, which reports no end.
145
+ function canAnimate(element: HTMLElement | undefined) {
146
+ return element?.isConnected === true && !prefersReducedMotion()
147
+ }
148
+
149
+ // A change that retargets a running transition cancels the old one while the new one keeps going; only a transition
150
+ // with nothing left running on the property has ended.
151
+ function transitionEnded(event: TransitionEvent, element: HTMLElement | undefined, property: string) {
152
+ if (!element || event.target !== element || event.propertyName !== property) return false
153
+
154
+ if (event.type !== "transitioncancel") return true
155
+
156
+ return !element
157
+ .getAnimations()
158
+ .some(
159
+ (animation) =>
160
+ animation instanceof CSSTransition &&
161
+ animation.transitionProperty === property &&
162
+ animation.playState === "running",
163
+ )
164
+ }
165
+
166
+ /** Settles an eased change whose end event never arrives, a little after its transition should have finished. */
167
+ function createFallback(element: () => HTMLElement | undefined, timeout: (attempt: number) => void) {
168
+ let timer: ReturnType<typeof setTimeout> | undefined
169
+
170
+ return (effect: AnimatedNumberEffect) => {
171
+ clearTimeout(timer)
172
+ timer = undefined
173
+
174
+ if (effect.type === "clear") return
175
+ // The change's transition styles apply when Solid flushes this update, so measure after it.
176
+ queueMicrotask(() => {
177
+ clearTimeout(timer)
178
+ timer = setTimeout(() => timeout(effect.attempt), transitionMs(element()) + ANIMATED_NUMBER_FALLBACK_PAD_MS)
179
+ })
180
+ }
181
+ }
182
+
183
+ function transitionMs(element: HTMLElement | undefined) {
184
+ if (!element?.isConnected) return 0
185
+ const style = getComputedStyle(element)
186
+ const delays = style.transitionDelay.split(",")
187
+
188
+ return Math.max(
189
+ 0,
190
+ ...style.transitionDuration
191
+ .split(",")
192
+ .map((duration, index) => milliseconds(duration) + milliseconds(delays[index % delays.length])),
193
+ )
194
+ }
195
+
196
+ function milliseconds(value: string) {
197
+ const time = Number.parseFloat(value)
198
+
199
+ if (!Number.isFinite(time)) return 0
200
+
201
+ return value.trim().endsWith("ms") ? time : time * 1000
202
+ }