-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Fix
LineChart
transition stopping before completion (#14366)
- Loading branch information
Showing
2 changed files
with
30 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,42 @@ | ||
import * as React from 'react'; | ||
import { interpolateString } from '@mui/x-charts-vendor/d3-interpolate'; | ||
import { useSpring, to } from '@react-spring/web'; | ||
import { useSpring } from '@react-spring/web'; | ||
|
||
function usePrevious<T>(value: T) { | ||
const ref = React.useRef<T | null>(null); | ||
React.useEffect(() => { | ||
ref.current = value; | ||
}, [value]); | ||
const ref = React.useRef<{ currentPath: T; previousPath?: T }>({ | ||
currentPath: value, | ||
previousPath: undefined, | ||
}); | ||
if (ref.current.currentPath !== value) { | ||
ref.current = { | ||
currentPath: value, | ||
previousPath: ref.current.currentPath, | ||
}; | ||
} | ||
|
||
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.previousPath | ||
? interpolateString(memoryRef.previousPath, memoryRef.currentPath) | ||
: () => memoryRef.currentPath, | ||
[memoryRef.currentPath, memoryRef.previousPath], | ||
); | ||
|
||
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.currentPath], | ||
); | ||
|
||
return to([value], interpolator); | ||
return value.to(interpolator); | ||
}; |