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:
1 parent
10ad48cc13
commit
6a358a1e91
3 files changed
+23
-10
No files matched your search
@@ -33,12 +33,13 @@ interface IndicatorGeometry {
|
|||||||
top: number;
|
top: number;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
|
baseWidth: number;
|
||||||
animate: boolean;
|
animate: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sameGeometry(
|
function sameGeometry(
|
||||||
previous: IndicatorGeometry,
|
previous: IndicatorGeometry,
|
||||||
next: Omit<IndicatorGeometry, "animate">,
|
next: Omit<IndicatorGeometry, "animate" | "baseWidth">,
|
||||||
): boolean {
|
): boolean {
|
||||||
return previous.left === next.left
|
return previous.left === next.left
|
||||||
&& previous.top === next.top
|
&& previous.top === next.top
|
||||||
@@ -83,7 +84,9 @@ export function SegmentedControl<T extends string>({
|
|||||||
};
|
};
|
||||||
setIndicator((previous) => {
|
setIndicator((previous) => {
|
||||||
if (previous && sameGeometry(previous, next)) return 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={{
|
style={{
|
||||||
top: indicator.top,
|
top: indicator.top,
|
||||||
width: indicator.width,
|
width: indicator.baseWidth,
|
||||||
height: indicator.height,
|
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}
|
) : null}
|
||||||
|
|||||||
@@ -1387,7 +1387,7 @@
|
|||||||
@layer components {
|
@layer components {
|
||||||
@media (prefers-reduced-motion: no-preference) {
|
@media (prefers-reduced-motion: no-preference) {
|
||||||
.segmented-control-indicator[data-animate="true"] {
|
.segmented-control-indicator[data-animate="true"] {
|
||||||
transition-property: transform, width;
|
transition-property: translate, scale;
|
||||||
transition-duration: 220ms, 160ms;
|
transition-duration: 220ms, 160ms;
|
||||||
transition-timing-function:
|
transition-timing-function:
|
||||||
cubic-bezier(0.2, 0.95, 0.25, 1.08),
|
cubic-bezier(0.2, 0.95, 0.25, 1.08),
|
||||||
|
|||||||
@@ -59,20 +59,28 @@ describe("SegmentedControl", () => {
|
|||||||
top: "4px",
|
top: "4px",
|
||||||
width: "64px",
|
width: "64px",
|
||||||
height: "24px",
|
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("aria-hidden", "true");
|
||||||
expect(indicator).toHaveAttribute("data-animate", "false");
|
expect(indicator).toHaveAttribute("data-animate", "false");
|
||||||
|
|
||||||
fireEvent.click(diff);
|
fireEvent.click(diff);
|
||||||
|
|
||||||
expect(indicator).toHaveStyle({
|
expect(indicator).toHaveStyle({ width: "64px" });
|
||||||
width: "48px",
|
expect(indicator?.style.translate).toBe("72px 0");
|
||||||
transform: "translate3d(72px, 0, 0)",
|
expect(indicator?.style.scale).toBe("0.75 1");
|
||||||
});
|
|
||||||
expect(indicator).toHaveAttribute("data-animate", "true");
|
expect(indicator).toHaveAttribute("data-animate", "true");
|
||||||
expect(summary).toHaveAttribute("aria-pressed", "false");
|
expect(summary).toHaveAttribute("aria-pressed", "false");
|
||||||
expect(diff).toHaveAttribute("aria-pressed", "true");
|
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", () => {
|
it("uses roving focus and arrow-key activation for tabs", () => {
|
||||||
|
|||||||
Reference in new issue
Block a user