Skip to content

Commit

Permalink
Fix bar animation when sign changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carysmills committed Dec 13, 2024
1 parent 5c8bc0f commit d4ed8b3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Bar chart animation when value goes from positive to negative or vice versa

### Added

- Added support for responsive legends in `<LineChartRelational />`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DataType,
getRoundedRectPath,
useTheme,
usePrevious,
} from '@shopify/polaris-viz-core';

import {useBarSpringConfig} from '../../../../hooks/useBarSpringConfig';
Expand Down Expand Up @@ -69,15 +70,31 @@ export const VerticalBar = memo(function Bar({
const springConfig = useBarSpringConfig({animationDelay});
const rotate = `${treatAsNegative ? 'rotate(180)' : 'rotate(0)'}`;

const prevValue = usePrevious(rawValue);
const signIsChanging =
prevValue == null ? false : Math.sign(prevValue) !== Math.sign(rawValue);

const {pathD, transform} = useSpring({
from: {
pathD: getPath(1, width),
transform: `translate(0 ${zeroPosition}) ${rotate}`,
},
to: {
pathD: getPath(height, width),
transform: `translate(0 ${yPosition}) ${rotate}`,
},
to: signIsChanging
? [
{
pathD: getPath(0, width),
transform: `translate(0 ${zeroPosition}) ${rotate}`,
immediate: true,
},
{
pathD: getPath(height, width),
transform: `translate(0 ${yPosition}) ${rotate}`,
},
]
: {
pathD: getPath(height, width),
transform: `translate(0 ${yPosition}) ${rotate}`,
},
...springConfig,
});

Expand Down

0 comments on commit d4ed8b3

Please sign in to comment.