Skip to content

Commit 0441cf2

Browse files
committed
fix(ui): read AnimatedNumber once, in order, in any direction
Expose the value as one sr-only text node and draw every digit with CSS in an aria-hidden layer, render digits in reading order keyed by place value, and pin the row to ltr so RTL documents keep numeric order. Fixes anomalyco#54653 Fixes anomalyco#43643
1 parent d80a4ca commit 0441cf2

3 files changed

Lines changed: 47 additions & 19 deletions

File tree

‎packages/session-ui/component-tests/tool-group.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,35 @@ story("summarizes subagents as Agent while retaining their card titles", async (
109109
await expect(group.locator('[data-component="task-tool-title"]')).toHaveText(["General", "Explore"])
110110
})
111111

112+
for (const direction of ["ltr", "rtl"]) {
113+
story(`reads and shows a two-digit count once, in order, in ${direction}`, async ({ mount }) => {
114+
const root = await mount("current-tool-group--sticky-header", { globals: { direction } })
115+
const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]')
116+
await expect(count).toHaveText("37")
117+
expect(await count.ariaSnapshot()).toBe('- text: "37"')
118+
119+
const shown = await count.locator('[data-slot="animated-number-static"]').evaluateAll((digits) =>
120+
digits
121+
.toSorted((a, b) => a.getBoundingClientRect().left - b.getBoundingClientRect().left)
122+
.map((digit) => digit.getAttribute("data-digit"))
123+
.join(""),
124+
)
125+
126+
expect(shown).toBe("37")
127+
})
128+
}
129+
130+
story("rolls the count and exposes only its current value while rolling", async ({ mount }) => {
131+
const root = await mount("current-tool-group--mixed-reasoning")
132+
const count = root.locator('[data-component="context-tool-group-trigger"] [data-slot="context-tool-group-count"]')
133+
await expect(count).toHaveText("4")
134+
await root.getByRole("button", { name: "Append follow-up read" }).click()
135+
await expect(count.locator('[data-animating="true"]')).toHaveCount(1)
136+
// One read mid-roll: a retrying assertion would pass once the strip hides again.
137+
expect(await count.ariaSnapshot()).toBe('- text: "5"')
138+
await expect(count).toHaveText("5")
139+
})
140+
112141
for (const width of [840, 390]) {
113142
story(`keeps grouped cards inside their trigger bounds at ${width}px`, async ({ mount, page }) => {
114143
await page.setViewportSize({ width, height: 600 })

‎packages/ui/src/components/animated-number.css‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
[data-slot="animated-number-value"] {
99
display: inline-flex;
10-
flex-direction: row-reverse;
10+
/* Digits keep numeric order in RTL documents. */
11+
direction: ltr;
1112
align-items: baseline;
1213
justify-content: flex-end;
1314
line-height: inherit;
@@ -56,6 +57,7 @@
5657
line-height: 1em;
5758
}
5859

60+
[data-slot="animated-number-static"]::before,
5961
[data-slot="animated-number-cell"]::before {
6062
content: attr(data-digit);
6163
}

‎packages/ui/src/components/animated-number.tsx‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { For, Index, createEffect, createMemo, on } from "solid-js"
1+
import { For, createEffect, createMemo, on } from "solid-js"
22
import { createStore } from "solid-js/store"
33

44
const TRACK = Array.from({ length: 30 }, (_, index) => index % 10)
@@ -50,10 +50,9 @@ function Digit(props: { value: number; direction: 1 | -1 }) {
5050

5151
return (
5252
<span data-slot="animated-number-digit" data-animating={animating() ? "true" : "false"}>
53-
<span data-slot="animated-number-static">{props.value}</span>
53+
<span data-slot="animated-number-static" data-digit={props.value} />
5454
<span
5555
data-slot="animated-number-strip"
56-
aria-hidden="true"
5756
onTransitionEnd={() => {
5857
setState("animating", false)
5958
setState("step", (value) => normalize(value) + 10)
@@ -63,7 +62,6 @@ function Digit(props: { value: number; direction: 1 | -1 }) {
6362
"--animated-number-duration": `var(--tool-motion-odometer-ms, ${DURATION}ms)`,
6463
}}
6564
>
66-
{/* Generated digits keep the strip out of the number's text content. */}
6765
<For each={TRACK}>{(value) => <span data-slot="animated-number-cell" data-digit={value} />}</For>
6866
</span>
6967
</span>
@@ -102,22 +100,21 @@ export function AnimatedNumber(props: { value: number; class?: string }) {
102100

103101
const label = createMemo(() => value().toString())
104102

105-
const digits = createMemo(() =>
106-
Array.from(label(), (char) => {
107-
const code = char.charCodeAt(0) - 48
108-
109-
if (code < 0 || code > 9) return 0
110-
111-
return code
112-
}).reverse(),
113-
)
114-
115-
const width = createMemo(() => `${digits().length}ch`)
103+
// Place values, most significant first. Keying by place keeps the ones digit rolling when a tens digit appears.
104+
const places = createMemo(() => Array.from(label(), (_, index) => label().length - 1 - index))
116105

117106
return (
118-
<span data-component="animated-number" class={props.class} aria-label={label()}>
119-
<span data-slot="animated-number-value" style={{ "--animated-number-width": width() }}>
120-
<Index each={digits()}>{(digit) => <Digit value={digit()} direction={direction()} />}</Index>
107+
<span data-component="animated-number" class={props.class}>
108+
{/* The value is the only text: screen readers and copy read it once, in order. CSS draws the digits. */}
109+
<span class="sr-only">{label()}</span>
110+
<span
111+
data-slot="animated-number-value"
112+
aria-hidden="true"
113+
style={{ "--animated-number-width": `${label().length}ch` }}
114+
>
115+
<For each={places()}>
116+
{(place) => <Digit value={Math.floor(value() / 10 ** place) % 10} direction={direction()} />}
117+
</For>
121118
</span>
122119
</span>
123120
)

0 commit comments

Comments
 (0)