Skip to content

Commit a52b3b6

Browse files
committed
perf(ui): run the spinner and the text shimmer on the compositor
1 parent bf0ef80 commit a52b3b6

6 files changed

Lines changed: 114 additions & 60 deletions

File tree

‎packages/session-ui/src/v2/components/session-progress-indicator-v2.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Spinner } from "@opencode/ui/spinner"
33
import "./session-progress-indicator-v2.css"
44

55
// The app's one activity spinner, shared by the activity chip, subagent rows, task cards and loading tabs.
6-
export function SessionProgressIndicatorV2(props: ComponentProps<"svg">) {
6+
export function SessionProgressIndicatorV2(props: ComponentProps<typeof Spinner>) {
77
return (
88
<Spinner
99
{...props}
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1+
/* `width` and `height` props arrive as variables, so classes and other CSS still size the spinner over them, as they
2+
did over an SVG's attributes. */
3+
[data-slot="spinner-dots"] {
4+
position: relative;
5+
display: block;
6+
width: var(--spinner-width, 18px);
7+
height: var(--spinner-height, auto);
8+
aspect-ratio: 1;
9+
}
10+
111
[data-component="spinner"] {
212
color: inherit;
313
flex-shrink: 0;
4-
width: 18px;
5-
aspect-ratio: 1;
614
}
715

816
/* Each dot rests at its frozen pose, so reduced motion only has to stop the animation. */
9-
[data-slot="spinner-dots"] rect {
17+
[data-slot="spinner-dots"] > svg {
18+
position: absolute;
19+
inset: 0;
20+
width: 100%;
21+
height: 100%;
22+
overflow: visible;
1023
opacity: var(--still);
1124
}
1225

1326
/* syncSpinPhase marks spinners out of view; `!important` beats the inline animation each dot carries. */
14-
[data-slot="spinner-dots"][data-offscreen] rect {
27+
[data-slot="spinner-dots"][data-offscreen] > svg {
1528
animation-play-state: paused !important;
1629
}
1730

18-
:root[data-motion="reduce"] [data-slot="spinner-dots"] rect {
31+
:root[data-motion="reduce"] [data-slot="spinner-dots"] > svg {
1932
/* Beats the inline animation each dot carries. */
2033
animation: none !important;
2134
}
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { For, splitProps, type ComponentProps } from "solid-js"
1+
import { For, splitProps, type ComponentProps, type JSX } from "solid-js"
22
import { syncSpinPhase } from "../hooks/spin-phase"
33
import { radarDots, radarKeyframes, radarLoop } from "./spinner-radar"
44

@@ -12,8 +12,20 @@ const dots = radarDots()
1212
// The keyframes are added to the document once, the first time a spinner renders.
1313
let added = false
1414

15-
export function Spinner(props: ComponentProps<"svg"> & { "data-component"?: string }) {
16-
const [local, rest] = splitProps(props, ["class", "classList", "data-component"])
15+
/**
16+
* Each dot is its own SVG over the whole box, drawn exactly where it sat in one SVG. The compositor runs the opacity
17+
* animation of an SVG element but not of a shape inside one, so a spinner no longer restyles and repaints on every
18+
* frame. `width` and `height` size it like an SVG's attributes, below any CSS size.
19+
*/
20+
export function Spinner(
21+
props: Omit<ComponentProps<"span">, "style"> & {
22+
"data-component"?: string
23+
width?: number
24+
height?: number
25+
style?: JSX.CSSProperties
26+
},
27+
) {
28+
const [local, rest] = splitProps(props, ["class", "classList", "data-component", "width", "height", "style"])
1729

1830
if (!added) {
1931
added = true
@@ -23,32 +35,34 @@ export function Spinner(props: ComponentProps<"svg"> & { "data-component"?: stri
2335
}
2436

2537
return (
26-
<svg
38+
<span
2739
{...rest}
2840
ref={syncSpinPhase}
29-
viewBox="0 0 16 16"
3041
data-component={local["data-component"] ?? "spinner"}
3142
data-slot="spinner-dots"
3243
classList={{ ...local.classList, [local.class ?? ""]: !!local.class }}
33-
fill="currentColor"
44+
style={{
45+
...local.style,
46+
"--spinner-width": local.width === undefined ? undefined : `${local.width}px`,
47+
"--spinner-height": local.height === undefined ? undefined : `${local.height}px`,
48+
}}
3449
>
3550
<For each={dots}>
3651
{(dot) => (
37-
<rect
38-
x={dot.x}
39-
y={dot.y}
40-
width={dot.size}
41-
height={dot.size}
42-
rx={dot.size * 0.24}
52+
<svg
53+
viewBox="0 0 16 16"
54+
fill="currentColor"
4355
style={{
4456
"--still": dot.still,
4557
animation: dot.steady
4658
? undefined
4759
: `${keyframes}-${dot.shape} calc(${radarLoop}s * var(--spinner-time, 1)) linear calc(${-radarLoop * (1 - dot.at)}s * var(--spinner-time, 1)) infinite`,
4860
}}
49-
/>
61+
>
62+
<rect x={dot.x} y={dot.y} width={dot.size} height={dot.size} rx={dot.size * 0.24} />
63+
</svg>
5064
)}
5165
</For>
52-
</svg>
66+
</span>
5367
)
5468
}

‎packages/ui/src/feedback/activity-ring/activity-ring.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type ComponentProps } from "solid-js"
22
import { Spinner } from "../../components/spinner"
33
import "./activity-ring.css"
44

5-
export function ActivityRing(props: ComponentProps<"svg">) {
5+
export function ActivityRing(props: ComponentProps<typeof Spinner>) {
66
return (
77
<Spinner
88
{...props}

‎packages/ui/src/typography/text-shimmer/text-shimmer.css‎

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@
33
--text-shimmer-duration: 1200ms;
44
--text-shimmer-swap: 220ms;
55
--text-shimmer-index: 0;
6-
--text-shimmer-angle: 90deg;
76
--text-shimmer-spread: 5.2ch;
87
--text-shimmer-gutter: calc(var(--text-shimmer-spread) + 1ch);
9-
--text-shimmer-size: calc(200% + var(--text-shimmer-gutter) + var(--text-shimmer-gutter));
108
--text-shimmer-base-color: var(--text-weak);
119
--text-shimmer-peak-color: var(--text-strong);
12-
--text-shimmer-sweep: linear-gradient(
13-
var(--text-shimmer-angle),
14-
transparent calc(50% - var(--text-shimmer-spread)),
15-
var(--text-shimmer-peak-color) 50%,
16-
transparent calc(50% + var(--text-shimmer-spread))
17-
);
18-
--text-shimmer-base: linear-gradient(var(--text-shimmer-base-color), var(--text-shimmer-base-color));
1910

2011
display: inline-flex;
2112
align-items: baseline;
@@ -47,57 +38,89 @@
4738
opacity: 1;
4839
}
4940

41+
/* Clipped to its box, where the old background sweep painted, so the sweep's travel never adds overflow. */
5042
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"] {
51-
color: var(--text-weaker);
43+
position: relative;
44+
overflow: clip;
45+
color: var(--text-shimmer-base-color);
5246
opacity: 0;
5347
}
5448

5549
[data-component="text-shimmer"][data-active="true"] [data-slot="text-shimmer-char-shimmer"] {
5650
opacity: 1;
5751
}
5852

59-
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"][data-run="true"] {
60-
animation-name: text-shimmer-sweep;
53+
[data-component="text-shimmer"][data-active="true"] [data-slot="text-shimmer-char-base"] {
54+
opacity: 0;
55+
}
56+
57+
/* The sweep is the text again in the peak colour, seen through a soft window that crosses it. The window and the text
58+
inside it move in opposite directions by the same amount, so the text stays put while the window travels: both are
59+
transforms, which the compositor runs, where an animated background position restyled and repainted the text on
60+
every frame. The window's centre runs from one gutter before the text to one gutter past it. */
61+
[data-component="text-shimmer"] [data-slot="text-shimmer-sweep"] {
62+
position: absolute;
63+
top: 0;
64+
bottom: 0;
65+
left: calc(-1 * var(--text-shimmer-spread));
66+
right: calc(-1 * var(--text-shimmer-spread));
67+
display: none;
68+
pointer-events: none;
69+
user-select: none;
70+
mask-image: linear-gradient(
71+
90deg,
72+
transparent calc(50% - var(--text-shimmer-spread)),
73+
#000 50%,
74+
transparent calc(50% + var(--text-shimmer-spread))
75+
);
76+
}
77+
78+
[data-component="text-shimmer"] [data-slot="text-shimmer-sweep-text"] {
79+
display: block;
80+
margin-left: var(--text-shimmer-spread);
81+
margin-right: var(--text-shimmer-spread);
82+
white-space: pre;
83+
color: var(--text-shimmer-peak-color);
84+
}
85+
86+
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"][data-run="true"] > [data-slot="text-shimmer-sweep"] {
87+
display: block;
88+
}
89+
90+
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"][data-run="true"] :is([data-slot="text-shimmer-sweep"], [data-slot="text-shimmer-sweep-text"]) {
6191
animation-duration: var(--text-shimmer-duration);
6292
animation-iteration-count: infinite;
6393
animation-timing-function: linear;
6494
animation-fill-mode: both;
6595
animation-delay: calc(var(--text-shimmer-step) * var(--text-shimmer-index) * -1);
66-
will-change: background-position;
6796
}
6897

69-
@keyframes text-shimmer-sweep {
98+
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"][data-run="true"] [data-slot="text-shimmer-sweep"] {
99+
animation-name: text-shimmer-window;
100+
}
101+
102+
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"][data-run="true"] [data-slot="text-shimmer-sweep-text"] {
103+
animation-name: text-shimmer-text;
104+
}
105+
106+
/* The window is one spread wider than the text on each side, so its percentages carry the spread back out. */
107+
@keyframes text-shimmer-window {
70108
0% {
71-
background-position:
72-
100% 0,
73-
0 0;
109+
translate: calc(-50% + var(--text-shimmer-spread) - var(--text-shimmer-gutter)) 0;
74110
}
75111

76112
100% {
77-
background-position:
78-
0% 0,
79-
0 0;
113+
translate: calc(50% - var(--text-shimmer-spread) + var(--text-shimmer-gutter)) 0;
80114
}
81115
}
82116

83-
@supports ((-webkit-background-clip: text) or (background-clip: text)) {
84-
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"] {
85-
color: transparent;
86-
-webkit-text-fill-color: transparent;
87-
background-image: var(--text-shimmer-sweep), var(--text-shimmer-base);
88-
background-size:
89-
var(--text-shimmer-size) 100%,
90-
100% 100%;
91-
background-position:
92-
100% 0,
93-
0 0;
94-
background-repeat: no-repeat;
95-
-webkit-background-clip: text;
96-
background-clip: text;
117+
@keyframes text-shimmer-text {
118+
0% {
119+
translate: calc(50% + var(--text-shimmer-gutter)) 0;
97120
}
98121

99-
[data-component="text-shimmer"][data-active="true"] [data-slot="text-shimmer-char-base"] {
100-
opacity: 0;
122+
100% {
123+
translate: calc(-50% - var(--text-shimmer-gutter)) 0;
101124
}
102125
}
103126

@@ -108,10 +131,11 @@
108131
}
109132

110133
[data-component="text-shimmer"] [data-slot="text-shimmer-char-shimmer"] {
111-
animation: none !important;
112134
color: inherit;
113-
-webkit-text-fill-color: currentColor;
114-
background-image: none;
135+
}
136+
137+
[data-component="text-shimmer"] [data-slot="text-shimmer-sweep"] {
138+
display: none !important;
115139
}
116140

117141
[data-component="text-shimmer"] [data-slot="text-shimmer-char-base"] {

‎packages/ui/src/typography/text-shimmer/text-shimmer.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export const TextShimmer = <T extends ValidComponent = "span">(props: {
5757
</span>
5858
<span data-slot="text-shimmer-char-shimmer" data-run={run() ? "true" : "false"} aria-hidden="true">
5959
{text()}
60+
<span data-slot="text-shimmer-sweep">
61+
<span data-slot="text-shimmer-sweep-text">{text()}</span>
62+
</span>
6063
</span>
6164
</span>
6265
</Dynamic>

0 commit comments

Comments
 (0)