perf(webui): animate segmented indicator without layout (#6403)

* perf(webui): animate segmented indicator without layout

* test(webui): keep segmented indicator coverage focused
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-23 00:45:58 +08:00
1 parent 10ad48cc13
commit 6a358a1e91
3 files changed
+23 -10

No files matched your search

@@ -33,12 +33,13 @@ interface IndicatorGeometry {
top: number;
width: number;
height: number;
baseWidth: number;
animate: boolean;
}
function sameGeometry(
previous: IndicatorGeometry,
next: Omit<IndicatorGeometry, "animate">,
next: Omit<IndicatorGeometry, "animate" | "baseWidth">,
): boolean {
return previous.left === next.left
&& previous.top === next.top
@@ -83,7 +84,9 @@ export function SegmentedControl<T extends string>({
};
setIndicator((previous) => {
if (previous && sameGeometry(previous, next)) return previous;
return { ...next, animate: Boolean(previous) && animate };
// Keep the box fixed while switching; rebase it when the option geometry changes without motion.
const baseWidth = !animate || !previous?.width ? Math.max(next.width, 1) : previous.baseWidth;
return { ...next, baseWidth, animate: Boolean(previous?.width) && animate };
});
};
@@ -151,9 +154,11 @@ export function SegmentedControl<T extends string>({
)}
style={{
top: indicator.top,
width: indicator.width,
width: indicator.baseWidth,
height: indicator.height,
transform: `translate3d(${indicator.left}px, 0, 0)`,
transformOrigin: "left center",
translate: `${indicator.left}px 0`,
scale: `${indicator.width / indicator.baseWidth} 1`,
}}
/>
) : null}
+1 -1
View File
@@ -1387,7 +1387,7 @@
@layer components {
@media (prefers-reduced-motion: no-preference) {
.segmented-control-indicator[data-animate="true"] {
transition-property: transform, width;
transition-property: translate, scale;
transition-duration: 220ms, 160ms;
transition-timing-function:
cubic-bezier(0.2, 0.95, 0.25, 1.08),
+13 -5
View File
@@ -59,20 +59,28 @@ describe("SegmentedControl", () => {
top: "4px",
width: "64px",
height: "24px",
transform: "translate3d(4px, 0, 0)",
});
expect(indicator?.style.translate).toBe("4px 0");
expect(indicator?.style.scale).toBe("1 1");
expect(indicator).toHaveAttribute("aria-hidden", "true");
expect(indicator).toHaveAttribute("data-animate", "false");
fireEvent.click(diff);
expect(indicator).toHaveStyle({
width: "48px",
transform: "translate3d(72px, 0, 0)",
});
expect(indicator).toHaveStyle({ width: "64px" });
expect(indicator?.style.translate).toBe("72px 0");
expect(indicator?.style.scale).toBe("0.75 1");
expect(indicator).toHaveAttribute("data-animate", "true");
expect(summary).toHaveAttribute("aria-pressed", "false");
expect(diff).toHaveAttribute("aria-pressed", "true");
fireEvent(window, new Event("resize"));
expect(indicator).toHaveStyle({ width: "64px" });
setGeometry(diff, { left: 72, width: 96 });
fireEvent(window, new Event("resize"));
expect(indicator).toHaveStyle({ width: "96px" });
expect(indicator?.style.scale).toBe("1 1");
expect(indicator).toHaveAttribute("data-animate", "false");
});
it("uses roving focus and arrow-key activation for tabs", () => {