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 not properly animating when hydrating #14355

Merged
merged 14 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
10 changes: 7 additions & 3 deletions packages/x-charts/src/LineChart/AnimatedArea.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { styled } from '@mui/material/styles';
import { animated, useSpring } from '@react-spring/web';
import { animated, useTransition } from '@react-spring/web';
import { color as d3Color } from '@mui/x-charts-vendor/d3-color';
import { useAnimatedPath } from '../internals/useAnimatedPath';
import { cleanId } from '../internals/cleanId';
Expand Down Expand Up @@ -49,9 +49,11 @@ function AnimatedArea(props: AnimatedAreaProps) {

const path = useAnimatedPath(d, skipAnimation);

const { animatedWidth } = useSpring({
const transitions = useTransition([1], {
from: { animatedWidth: left },
to: { animatedWidth: width + left + right },
enter: { animatedWidth: width + left + right },
leave: { animatedWidth: left },
reset: false,
immediate: skipAnimation,
});
Expand All @@ -60,7 +62,9 @@ function AnimatedArea(props: AnimatedAreaProps) {
return (
<React.Fragment>
<clipPath id={clipId}>
<animated.rect x={0} y={0} width={animatedWidth} height={top + height + bottom} />
{transitions((style) => (
<animated.rect x={0} y={0} width={style.animatedWidth} height={top + height + bottom} />
))}
</clipPath>
<g clipPath={`url(#${clipId})`}>
<AreaElementPath {...other} ownerState={ownerState} d={path} />
Expand Down
10 changes: 7 additions & 3 deletions packages/x-charts/src/LineChart/AnimatedLine.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { animated, useSpring } from '@react-spring/web';
import { animated, useTransition } from '@react-spring/web';
import { color as d3Color } from '@mui/x-charts-vendor/d3-color';
import { styled } from '@mui/material/styles';
import { useAnimatedPath } from '../internals/useAnimatedPath';
Expand Down Expand Up @@ -52,9 +52,11 @@ function AnimatedLine(props: AnimatedLineProps) {

const path = useAnimatedPath(d, skipAnimation);

const { animatedWidth } = useSpring({
const transitions = useTransition([1], {
from: { animatedWidth: left },
to: { animatedWidth: width + left + right },
enter: { animatedWidth: width + left + right },
leave: { animatedWidth: left },
reset: false,
immediate: skipAnimation,
});
Expand All @@ -63,7 +65,9 @@ function AnimatedLine(props: AnimatedLineProps) {
return (
<React.Fragment>
<clipPath id={clipId}>
<animated.rect x={0} y={0} width={animatedWidth} height={top + height + bottom} />
{transitions((style) => (
<animated.rect x={0} y={0} width={style.animatedWidth} height={top + height + bottom} />
))}
</clipPath>
<g clipPath={`url(#${clipId})`}>
<LineElementPath {...other} ownerState={ownerState} d={path} />
Expand Down
29 changes: 15 additions & 14 deletions packages/x-charts/src/hooks/useReducedMotion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { useIsomorphicLayoutEffect, Globals } from '@react-spring/web';

const handleMediaChange = (e: { matches: boolean | undefined }) => {
Globals.assign({
// Modification such the react-spring implementation such that this hook can remove animation but never activate animation.
skipAnimation: e.matches || undefined,
});
};

/**
* Returns `boolean` or `null`, used to automatically
* set skipAnimations to the value of the user's
Expand All @@ -8,24 +15,18 @@ import { useIsomorphicLayoutEffect, Globals } from '@react-spring/web';
* The return value, post-effect, is the value of their preferred setting
*/
export const useReducedMotion = () => {
// Taken from: https://github.com/pmndrs/react-spring/blob/02ec877bbfab0df46da0e4a47d5f68d3e731206a/packages/shared/src/hooks/useReducedMotion.ts#L13
// Taken from: https://github.com/pmndrs/react-spring/blob/fd65b605b85c3a24143c4ce9dd322fdfca9c66be/packages/shared/src/hooks/useReducedMotion.ts

useIsomorphicLayoutEffect(() => {
if (!window.matchMedia) {
// skip animation in environments where `window.matchMedia` would not be available (i.e. test/jsdom)
Globals.assign({
skipAnimation: true,
});
return () => {};
// Skip animation test/jsdom
const shouldSkipAnimation = !window?.matchMedia;

if (shouldSkipAnimation) {
handleMediaChange({ matches: true });
return undefined;
}
const mql = window.matchMedia('(prefers-reduced-motion)');

const handleMediaChange = (event: MediaQueryListEvent | MediaQueryList) => {
Globals.assign({
// Modification such the react-spring implementation such that this hook can remove animation but never activate animation.
skipAnimation: event.matches || undefined,
});
};
const mql = window.matchMedia('(prefers-reduced-motion)');

handleMediaChange(mql);

Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading