Skip to content

Commit 97afd65

Browse files
authored
[charts] Fix LineChart transition stopping before completion (#14366)
1 parent 8e82705 commit 97afd65

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

packages/x-charts/src/LineChart/MarkElement.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function MarkElement(props: MarkElementProps) {
107107
});
108108
const { axis } = React.useContext(InteractionContext);
109109

110-
const position = useSpring({ x, y, immediate: skipAnimation });
110+
const position = useSpring({ to: { x, y }, immediate: skipAnimation });
111111
const ownerState = {
112112
id,
113113
classes: innerClasses,
Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
import * as React from 'react';
22
import { interpolateString } from '@mui/x-charts-vendor/d3-interpolate';
3-
import { useSpring, to } from '@react-spring/web';
3+
import { useSpring } from '@react-spring/web';
44

55
function usePrevious<T>(value: T) {
6-
const ref = React.useRef<T | null>(null);
7-
React.useEffect(() => {
8-
ref.current = value;
9-
}, [value]);
6+
const ref = React.useRef<{ currentPath: T; previousPath?: T }>({
7+
currentPath: value,
8+
previousPath: undefined,
9+
});
10+
if (ref.current.currentPath !== value) {
11+
ref.current = {
12+
currentPath: value,
13+
previousPath: ref.current.currentPath,
14+
};
15+
}
16+
1017
return ref.current;
1118
}
1219

13-
// Taken from Nivo
1420
export const useAnimatedPath = (path: string, skipAnimation?: boolean) => {
15-
const previousPath = usePrevious(path);
21+
const memoryRef = usePrevious(path);
22+
1623
const interpolator = React.useMemo(
17-
() => (previousPath ? interpolateString(previousPath, path) : () => path),
18-
[previousPath, path],
24+
() =>
25+
memoryRef.previousPath
26+
? interpolateString(memoryRef.previousPath, memoryRef.currentPath)
27+
: () => memoryRef.currentPath,
28+
[memoryRef.currentPath, memoryRef.previousPath],
1929
);
2030

21-
const { value } = useSpring({
22-
from: { value: 0 },
23-
to: { value: 1 },
24-
reset: true,
25-
immediate: skipAnimation,
26-
});
31+
const [{ value }] = useSpring(
32+
{
33+
from: { value: 0 },
34+
to: { value: 1 },
35+
reset: true,
36+
immediate: skipAnimation,
37+
},
38+
[memoryRef.currentPath],
39+
);
2740

28-
return to([value], interpolator);
41+
return value.to(interpolator);
2942
};

0 commit comments

Comments
 (0)