Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Fix LineChart transition stopping before completion #14366

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions packages/x-charts/src/internals/useAnimatedPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,39 @@ import { interpolateString } from '@mui/x-charts-vendor/d3-interpolate';
import { useSpring, to } from '@react-spring/web';

function usePrevious<T>(value: T) {
const ref = React.useRef<T | null>(null);
const ref = React.useRef<{ current: T; previous?: T }>({
current: value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe renaming it with actual or currentValue to avoid the confusion with the current of the ref

previous: undefined,
});
React.useEffect(() => {
ref.current = value;
ref.current = {
current: value,
previous: ref.current.current,
};
}, [value]);
return ref.current;
}

// Taken from Nivo
export const useAnimatedPath = (path: string, skipAnimation?: boolean) => {
const previousPath = usePrevious(path);
const memoryRef = usePrevious(path);

const interpolator = React.useMemo(
() => (previousPath ? interpolateString(previousPath, path) : () => path),
[previousPath, path],
() =>
memoryRef.previous
? interpolateString(memoryRef.previous, memoryRef.current)
: () => memoryRef.current,
[memoryRef],
);

const { value } = useSpring({
from: { value: 0 },
to: { value: 1 },
reset: true,
immediate: skipAnimation,
});
const [{ value }] = useSpring(
{
from: { value: 0 },
to: { value: 1 },
reset: true,
immediate: skipAnimation,
},
[memoryRef.current],
);

return to([value], interpolator);
};
Loading