Skip to content

Commit

Permalink
Generate delay based on item count
Browse files Browse the repository at this point in the history
  • Loading branch information
envex committed Apr 3, 2024
1 parent ef9431d commit b027f31
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
### Changed

- Fill missing values for SparkLine charts
- Made `<DonutChart />` animation delay based on slice count, not a set `index * 100ms`.

## [12.4.1] - 2024-04-03

Expand Down
4 changes: 3 additions & 1 deletion packages/polaris-viz/src/components/Arc/Arc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ export interface ArcProps {
color: Color;
cornerRadius: number;
thickness: number;
animationDelay?: number;
index?: number;
isAnimated: boolean;
activeIndex?: number;
}

export function Arc({
animationDelay = 100,
radius,
width,
height,
Expand All @@ -52,7 +54,7 @@ export function Arc({
const gradient = getGradientFromColor(color);

const springConfig = useSpringConfig({
animationDelay: index * 100,
animationDelay: index * animationDelay,
shouldAnimate: isAnimated,
mountedSpringConfig: ARC_DATA_CHANGE_ANIMATION_CONFIG,
unmountedSpringConfig: ARC_LOAD_ANIMATION_CONFIG,
Expand Down
4 changes: 4 additions & 0 deletions packages/polaris-viz/src/components/DonutChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
Direction,
} from '@shopify/polaris-viz-core';

import {getAnimationDelayForItems} from '../../utilities/getAnimationDelayForItems';
import {getContainerAlignmentForLegend} from '../../utilities';
import type {ComparisonMetricProps} from '../ComparisonMetric';
import {LegendContainer, useLegend} from '../../components/LegendContainer';
Expand Down Expand Up @@ -245,6 +246,9 @@ export function Chart({
>
<Arc
isAnimated={shouldAnimate}
animationDelay={getAnimationDelayForItems(
pieChartData.length,
)}
index={index}
activeIndex={activeIndex}
width={diameter}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type {Story} from '@storybook/react';

import {DonutChart, DonutChartProps} from '../../../../components';
import {META} from '../meta';

export default {
...META,
title: `${META.title}/Playground`,
};

const Template: Story<DonutChartProps> = (args: DonutChartProps) => {
return <DonutChart {...args} />;
};

export const AnimationSpeed = Template.bind({});

AnimationSpeed.args = {
data: [...Array(100).keys()].map((num) => {
return {
name: `${num}`,
data: [{key: 'april - march', value: 10}],
};
}),
};
1 change: 1 addition & 0 deletions packages/polaris-viz/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ export const IS_TEST = process.env.NODE_ENV === 'test';
export const WARN_FOR_DEVELOPMENT = IS_DEVELOPMENT && !IS_TEST;
export const HOVER_TARGET_ZONE = 48;
export const CROSSHAIR_ID = 'Crosshair';
export const DEFAULT_ANIMATION_DELAY = 100;
10 changes: 10 additions & 0 deletions packages/polaris-viz/src/utilities/getAnimationDelayForItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {DEFAULT_ANIMATION_DELAY} from '../constants';

export function getAnimationDelayForItems(count: number) {
const scalingFactor = count * 0.1;

return Math.min(
DEFAULT_ANIMATION_DELAY / scalingFactor,
DEFAULT_ANIMATION_DELAY,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {getAnimationDelayForItems} from '../getAnimationDelayForItems';

describe('getAnimationDelayForItems()', () => {
it.each([
{count: 1, expected: 100},
{count: 10, expected: 100},
{count: 50, expected: 20},
{count: 100, expected: 10},
{count: 250, expected: 4},
{count: 1000, expected: 1},
])('generates a delay for $count items', ({count, expected}) => {
expect(getAnimationDelayForItems(count)).toBe(expected);
});
});

0 comments on commit b027f31

Please sign in to comment.