@opencode/ui 2.2.5 → 2.2.7

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 (119) 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 +128 -111
  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 +59 -59
  88. package/src/forms/checkbox/checkbox-compact.css +169 -0
  89. package/src/forms/checkbox/checkbox.tsx +93 -31
  90. package/src/forms/inline-input/inline-input.css +0 -1
  91. package/src/forms/search-list/search-list.css +5 -1
  92. package/src/forms/search-list/search-list.tsx +18 -10
  93. package/src/forms/select/machine.ts +81 -0
  94. package/src/forms/select/select.css +59 -17
  95. package/src/forms/select/select.tsx +74 -32
  96. package/src/forms/typeahead/machine.ts +253 -0
  97. package/src/forms/typeahead/typeahead.tsx +70 -62
  98. package/src/hooks/auto-scroll-machine.ts +261 -0
  99. package/src/hooks/create-auto-scroll.tsx +110 -205
  100. package/src/hooks/list-machine.ts +359 -0
  101. package/src/hooks/use-filtered-list.tsx +101 -112
  102. package/src/i18n/en.ts +6 -1
  103. package/src/i18n/uk.ts +3 -3
  104. package/src/icons/catalog.ts +8 -0
  105. package/src/navigation/menu/machine.ts +109 -0
  106. package/src/navigation/menu/menu.tsx +133 -22
  107. package/src/navigation/segmented-control/machine.ts +72 -0
  108. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  109. package/src/overlays/dialog/dialog.tsx +7 -1
  110. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  111. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  112. package/src/overlays/tooltip/machine.ts +303 -0
  113. package/src/overlays/tooltip/tooltip.tsx +159 -92
  114. package/src/styles/theme.css +10 -2
  115. package/src/theme/context.tsx +192 -269
  116. package/src/theme/machine.ts +508 -0
  117. package/src/typography/text-shimmer/machine.ts +90 -0
  118. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  119. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -1,11 +1,11 @@
1
- import { Root } from "@kobalte/core/button"
1
+ import type { Root } from "@kobalte/core/button"
2
2
  import { type ComponentProps, Show, splitProps } from "solid-js"
3
3
  import { Icon, type IconProps } from "@opencode/ui/icon"
4
4
  import "./button.css"
5
5
 
6
6
  export interface ButtonProps
7
- extends ComponentProps<typeof Root>,
8
- Pick<ComponentProps<"button">, "class" | "classList" | "children"> {
7
+ extends Omit<ComponentProps<typeof Root>, "type">,
8
+ Pick<ComponentProps<"button">, "class" | "classList" | "children" | "type"> {
9
9
  size?: "small" | "normal" | "large"
10
10
  variant?:
11
11
  | "neutral"
@@ -22,11 +22,16 @@ export interface ButtonProps
22
22
  icon?: IconProps["name"]
23
23
  }
24
24
 
25
+ // A native button with the attributes Kobalte's Button sets on one, without its per-instance computations.
25
26
  export function Button(props: ButtonProps) {
26
- const [split, rest] = splitProps(props, ["variant", "size", "icon", "class", "classList"])
27
+ const [split, rest] = splitProps(props, ["variant", "size", "icon", "class", "classList", "disabled"])
27
28
 
28
29
  return (
29
- <Root
30
+ <button
31
+ // A type the caller passes in `rest` replaces this default.
32
+ type="button"
33
+ disabled={split.disabled}
34
+ data-disabled={split.disabled ? "" : undefined}
30
35
  {...rest}
31
36
  data-component="button-v2"
32
37
  data-size={split.size || "normal"}
@@ -39,6 +44,6 @@ export function Button(props: ButtonProps) {
39
44
  >
40
45
  <Show when={split.icon}>{(icon) => <Icon name={icon()} />}</Show>
41
46
  {props.children}
42
- </Root>
47
+ </button>
43
48
  )
44
49
  }
@@ -1,4 +1,4 @@
1
- import { Root } from "@kobalte/core/button"
1
+ import type { Root } from "@kobalte/core/button"
2
2
  import { type ComponentProps, splitProps } from "solid-js"
3
3
  import { JSX } from "solid-js"
4
4
  import "./icon-button.css"
@@ -12,11 +12,24 @@ export interface IconButtonProps
12
12
  state?: "rest" | "hover" | "pressed"
13
13
  }
14
14
 
15
+ // A native button with the attributes Kobalte's Button sets on one, without its per-instance computations.
15
16
  export function IconButton(props: ComponentProps<"button"> & IconButtonProps) {
16
- const [local, rest] = splitProps(props, ["variant", "size", "icon", "class", "classList", "state"])
17
+ const [local, rest] = splitProps(props, [
18
+ "variant",
19
+ "size",
20
+ "icon",
21
+ "class",
22
+ "classList",
23
+ "state",
24
+ "type",
25
+ "disabled",
26
+ ])
17
27
 
18
28
  return (
19
- <Root
29
+ <button
30
+ type={local.type ?? "button"}
31
+ disabled={local.disabled}
32
+ data-disabled={local.disabled ? "" : undefined}
20
33
  {...rest}
21
34
  data-component="icon-button-v2"
22
35
  data-size={local.size || "normal"}
@@ -28,6 +41,6 @@ export function IconButton(props: ComponentProps<"button"> & IconButtonProps) {
28
41
  }}
29
42
  >
30
43
  {local.icon}
31
- </Root>
44
+ </button>
32
45
  )
33
46
  }
@@ -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
+ }
@@ -0,0 +1,78 @@
1
+ import type { IconName } from "./file-icons/types"
2
+
3
+ export const FILE_ICON_LOAD_TIMEOUT_MS = 10_000
4
+
5
+ /** Bundled with the component, so they are always in the sprite and stand in for an icon that failed to load. */
6
+ export type FallbackIconName = "Document" | "Folder" | "FolderOpen"
7
+
8
+ /**
9
+ * Lifecycle of one lazy `<symbol>` in the in-document sprite, shared by every mounted icon with that name. It only
10
+ * moves forward: Chromium keeps a failed dynamic import failed for the page's lifetime, so a retry cannot succeed.
11
+ */
12
+ export type FileIconSymbolState = { readonly status: "unrequested" | "loading" | "failed" | "ready" }
13
+
14
+ export type FileIconSymbolEvent =
15
+ | { readonly type: "requested" }
16
+ | { readonly type: "load.succeeded"; readonly svg: string }
17
+ | { readonly type: "load.failed" }
18
+ | { readonly type: "load.timed-out" }
19
+
20
+ export type FileIconSymbolEffect =
21
+ | { readonly type: "load" }
22
+ | { readonly type: "insert-symbol"; readonly svg: string }
23
+ | { readonly type: "cancel-timeout" }
24
+
25
+ export type FileIconSymbolTransition = {
26
+ readonly state: FileIconSymbolState
27
+ readonly effects: readonly FileIconSymbolEffect[]
28
+ }
29
+
30
+ const UNREQUESTED: FileIconSymbolState = Object.freeze({ status: "unrequested" })
31
+
32
+ const LOADING: FileIconSymbolState = Object.freeze({ status: "loading" })
33
+
34
+ const FAILED: FileIconSymbolState = Object.freeze({ status: "failed" })
35
+
36
+ const READY: FileIconSymbolState = Object.freeze({ status: "ready" })
37
+
38
+ export const INITIAL_FILE_ICON_SYMBOL_STATE = UNREQUESTED
39
+
40
+ export const READY_FILE_ICON_SYMBOL_STATE = READY
41
+
42
+ export const FILE_ICON_REQUESTED: FileIconSymbolEvent = Object.freeze({ type: "requested" })
43
+
44
+ const NONE: readonly FileIconSymbolEffect[] = Object.freeze([])
45
+
46
+ const LOAD: readonly FileIconSymbolEffect[] = Object.freeze([{ type: "load" }])
47
+
48
+ const CANCEL: readonly FileIconSymbolEffect[] = Object.freeze([{ type: "cancel-timeout" }])
49
+
50
+ export function transitionFileIconSymbol(
51
+ state: FileIconSymbolState,
52
+ event: FileIconSymbolEvent,
53
+ ): FileIconSymbolTransition {
54
+ if (state.status === "unrequested" && event.type === "requested") return { state: LOADING, effects: LOAD }
55
+
56
+ // A symbol that arrives after its timeout still upgrades the icons showing the fallback.
57
+ if ((state.status === "loading" || state.status === "failed") && event.type === "load.succeeded")
58
+ return {
59
+ state: READY,
60
+ effects: [...(state.status === "loading" ? CANCEL : NONE), { type: "insert-symbol", svg: event.svg }],
61
+ }
62
+
63
+ if (state.status === "loading" && (event.type === "load.failed" || event.type === "load.timed-out"))
64
+ return { state: FAILED, effects: CANCEL }
65
+
66
+ return { state, effects: NONE }
67
+ }
68
+
69
+ export function chooseFallbackIconName(type: "directory" | "file", expanded: boolean): FallbackIconName {
70
+ if (type === "file") return "Document"
71
+
72
+ return expanded ? "FolderOpen" : "Folder"
73
+ }
74
+
75
+ /** The symbol a mounted icon shows: its own unless it failed to load. */
76
+ export function selectFileIconSymbol(state: FileIconSymbolState, name: IconName, fallback: FallbackIconName): IconName {
77
+ return state.status === "failed" ? fallback : name
78
+ }