-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1578 from Shopify/fix-stacked-area-arity-bug
Fix stacked area arity bug
- Loading branch information
Showing
9 changed files
with
184 additions
and
92 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
115 changes: 115 additions & 0 deletions
115
packages/polaris-viz/src/components/StackedAreaChart/components/Area/AnimatedArea.tsx
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import {useRef} from 'react'; | ||
import {animated, useSpring} from '@react-spring/web'; | ||
import type {SpringValue} from '@react-spring/web'; | ||
import type {GradientStop} from '@shopify/polaris-viz-core'; | ||
import { | ||
LinearGradientWithStops, | ||
getColorVisionEventAttrs, | ||
COLOR_VISION_SINGLE_ITEM, | ||
getColorVisionStylesForActiveIndex, | ||
useChartContext, | ||
AREAS_LOAD_ANIMATION_CONFIG, | ||
getGradientFromColor, | ||
useSpringConfig, | ||
AREAS_TRANSITION_CONFIG, | ||
} from '@shopify/polaris-viz-core'; | ||
|
||
import type {AreaProps} from './types'; | ||
import styles from './Area.scss'; | ||
|
||
export function AnimatedArea({ | ||
activeLineIndex, | ||
animationIndex, | ||
areaGenerator, | ||
colors, | ||
data, | ||
duration, | ||
id, | ||
index, | ||
lineGenerator, | ||
selectedTheme, | ||
zeroLineValues, | ||
}: AreaProps) { | ||
const {shouldAnimate} = useChartContext(); | ||
const delay = animationIndex * (duration / 2); | ||
|
||
const mounted = useRef(false); | ||
|
||
const springConfig = useSpringConfig({ | ||
shouldAnimate, | ||
animationDelay: delay, | ||
mountedSpringConfig: AREAS_TRANSITION_CONFIG, | ||
unmountedSpringConfig: AREAS_LOAD_ANIMATION_CONFIG, | ||
}); | ||
|
||
const { | ||
animatedAreaShape, | ||
animatedLineShape, | ||
opacity, | ||
}: { | ||
animatedAreaShape: SpringValue; | ||
animatedLineShape: SpringValue; | ||
opacity: SpringValue; | ||
} = useSpring({ | ||
from: { | ||
opacity: 0, | ||
animatedAreaShape: areaGenerator(mounted.current ? data : zeroLineValues), | ||
animatedLineShape: lineGenerator(mounted.current ? data : zeroLineValues), | ||
}, | ||
to: { | ||
opacity: 0.25, | ||
animatedAreaShape: areaGenerator(data), | ||
animatedLineShape: lineGenerator(data), | ||
}, | ||
...springConfig, | ||
}); | ||
|
||
if (animatedAreaShape == null || animatedLineShape == null) { | ||
return null; | ||
} | ||
|
||
const gradient = getGradientFromColor(colors[index]); | ||
|
||
return ( | ||
<g | ||
{...getColorVisionEventAttrs({ | ||
type: COLOR_VISION_SINGLE_ITEM, | ||
index, | ||
})} | ||
tabIndex={-1} | ||
> | ||
<defs> | ||
<LinearGradientWithStops | ||
id={`area-${id}-${index}`} | ||
gradient={gradient as GradientStop[]} | ||
gradientUnits="userSpaceOnUse" | ||
y1="100%" | ||
y2="0%" | ||
/> | ||
</defs> | ||
<g | ||
style={getColorVisionStylesForActiveIndex({ | ||
activeIndex: activeLineIndex, | ||
index, | ||
})} | ||
aria-hidden="true" | ||
tabIndex={-1} | ||
className={styles.Group} | ||
> | ||
<animated.path | ||
key={`line-${index}`} | ||
d={animatedLineShape} | ||
fill="none" | ||
stroke={`url(#area-${id}-${index})`} | ||
strokeWidth={selectedTheme.line.width} | ||
/> | ||
<animated.path | ||
key={index} | ||
style={{opacity}} | ||
d={animatedAreaShape} | ||
fill={`url(#area-${id}-${index})`} | ||
/> | ||
</g> | ||
</g> | ||
); | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/polaris-viz/src/components/StackedAreaChart/components/Area/index.ts
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 +1,2 @@ | ||
export {Area} from './Area'; | ||
export {AnimatedArea} from './AnimatedArea'; |
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
18 changes: 18 additions & 0 deletions
18
packages/polaris-viz/src/components/StackedAreaChart/components/Area/types.ts
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type {Area as D3Area, Line} from 'd3-shape'; | ||
import type {Color, Theme} from '@shopify/polaris-viz-core'; | ||
|
||
import type {StackedSeries} from '../../../../types'; | ||
|
||
export interface AreaProps { | ||
activeLineIndex: number; | ||
animationIndex: number; | ||
areaGenerator: D3Area<number[]>; | ||
colors: Color[]; | ||
data: StackedSeries; | ||
zeroLineValues: StackedSeries; | ||
duration: number; | ||
id: string; | ||
index: number; | ||
lineGenerator: Line<number[]>; | ||
selectedTheme: Theme; | ||
} |
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
Oops, something went wrong.