-
Notifications
You must be signed in to change notification settings - Fork 872
Charts: Use Stack for layout #47981
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: Use Stack for layout #47981
Changes from all commits
83b38b4
0036a59
366c982
2fdfb42
2f9f2a6
bbe65d8
676037a
b0ff694
5227124
7d30e8d
902ec03
f5db6a3
03ca39d
58b79c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Charts: Replace ad-hoc flexbox layouts with @wordpress/ui Stack across legend, conversion funnel, line chart, geo chart, conversion funnel tooltip, and donut story. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,13 +263,13 @@ const ConversionFunnelChartInternal: FC< ConversionFunnelChartProps > = ( { | |
|
|
||
| // Default tooltip rendering function | ||
| const renderDefaultTooltip = ( step: FunnelStep ) => ( | ||
| <> | ||
| <Stack direction="column" align="flex-start" gap="xs"> | ||
| <div className={ styles[ 'tooltip-title' ] }>{ step.label }</div> | ||
| <div className={ styles[ 'tooltip-content' ] }> | ||
| { formatPercentage( step.rate ) } | ||
| { ` • ${ step.count ?? 'no' } items` } | ||
| </div> | ||
| </> | ||
| </Stack> | ||
| ); | ||
|
|
||
| // Validate data | ||
|
|
@@ -322,6 +322,7 @@ const ConversionFunnelChartInternal: FC< ConversionFunnelChartProps > = ( { | |
| <> | ||
| <Stack | ||
| direction="column" | ||
| gap="xl" | ||
| data-testid="conversion-funnel-chart" | ||
| ref={ node => { | ||
| // Set containerRef for @visx coordinate system | ||
|
|
@@ -344,27 +345,31 @@ const ConversionFunnelChartInternal: FC< ConversionFunnelChartProps > = ( { | |
| changeColor, | ||
| } ) | ||
| ) : ( | ||
| <div className={ styles[ 'main-metric' ] }>{ renderDefaultMainMetric() }</div> | ||
| <Stack direction="row" align="baseline" gap="sm" className={ styles[ 'main-metric' ] }> | ||
| { renderDefaultMainMetric() } | ||
| </Stack> | ||
| ) } | ||
|
|
||
| { /* Funnel Steps */ } | ||
| <div className={ styles[ 'funnel-container' ] }> | ||
| <Stack direction="row" align="flex-end" gap="lg" className={ styles[ 'funnel-container' ] }> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { steps.map( ( step, index ) => { | ||
| const barHeight = ( step.rate / maxRate ) * 100; | ||
| const { isBlurred } = getStepState( step.id ); | ||
|
|
||
| return ( | ||
| <div | ||
| <Stack | ||
| key={ step.id } | ||
| direction="column" | ||
| data-testid="funnel-step" | ||
| className={ clsx( | ||
| styles[ 'funnel-step' ], | ||
| isColorPaletteResolved && styles[ 'funnel-step--animated' ], | ||
| isBlurred && styles[ 'funnel-step--blurred' ] | ||
| ) } | ||
| gap="xl" | ||
| > | ||
| { /* Step Label and Rate */ } | ||
| <div className={ styles[ 'step-header' ] }> | ||
| <div> | ||
| { renderStepLabel ? ( | ||
| renderStepLabel( { | ||
| step, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Micro-cleanup suggestion: this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with it! 👍🏼
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -388,7 +393,9 @@ const ConversionFunnelChartInternal: FC< ConversionFunnelChartProps > = ( { | |
| </div> | ||
|
|
||
| { /* Funnel Bar */ } | ||
| <div | ||
| <Stack | ||
| direction="column" | ||
| justify="flex-end" | ||
| className={ styles[ 'bar-container' ] } | ||
| onClick={ stepHandlers.get( step.id )?.onClick } | ||
| onKeyDown={ stepHandlers.get( step.id )?.onKeyDown } | ||
|
|
@@ -407,11 +414,11 @@ const ConversionFunnelChartInternal: FC< ConversionFunnelChartProps > = ( { | |
| backgroundColor: barColor, | ||
| } } | ||
| /> | ||
| </div> | ||
| </div> | ||
| </Stack> | ||
| </Stack> | ||
| ); | ||
| } ) } | ||
| </div> | ||
| </Stack> | ||
| </Stack> | ||
|
|
||
| { /* Tooltip Portal */ } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,3 @@ | ||
| .container { | ||
| position: relative; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| * External dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Stack } from '@wordpress/ui'; | ||
| import clsx from 'clsx'; | ||
| import { FC, useContext, useMemo } from 'react'; | ||
| import { Chart, type GoogleChartOptions } from 'react-google-charts'; | ||
|
|
@@ -57,13 +58,15 @@ const GeoChartInternal: FC< GeoChartProps > = ( { | |
|
|
||
| // Render loading placeholder | ||
| const loadingPlaceholder = ( | ||
| <div | ||
| <Stack | ||
| align="center" | ||
| justify="center" | ||
| className={ clsx( 'geo-chart', styles.container, className ) } | ||
| data-testid="geo-chart-loading" | ||
| style={ { width, height } } | ||
| > | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both the loading placeholder and the main container use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think this is still a valid use of a Stack despite the name. I think a more appropriate name for the Stack component might be Flex, as that's what it provides essentially. That said, a centering component abstraction could be useful, I'll take a look at what sort of usage this would have.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Claude's take: a thin
Tradeoff: one more primitive to learn, and the implementation is trivially a Stack underneath. If you're renaming My take: I didn't mean we'd actually rename
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { renderPlaceholder ? renderPlaceholder() : __( 'Loading map', 'jetpack-charts' ) } | ||
| </div> | ||
| </Stack> | ||
| ); | ||
|
|
||
| // Google charts doesn't accept CSS variables, so we need to convert them to hex colors | ||
|
|
@@ -144,7 +147,9 @@ const GeoChartInternal: FC< GeoChartProps > = ( { | |
| ); | ||
|
|
||
| return ( | ||
| <div | ||
| <Stack | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| align="center" | ||
| justify="center" | ||
| className={ clsx( 'geo-chart', styles.container, className ) } | ||
| data-testid="geo-chart" | ||
| style={ { width, height, backgroundColor } } | ||
|
|
@@ -157,7 +162,7 @@ const GeoChartInternal: FC< GeoChartProps > = ( { | |
| options={ options } | ||
| loader={ loadingPlaceholder } | ||
| /> | ||
| </div> | ||
| </Stack> | ||
| ); | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { LinearGradient } from '@visx/gradient'; | |
| import { scaleTime } from '@visx/scale'; | ||
| import { XYChart, AreaSeries, Grid, Axis, DataContext } from '@visx/xychart'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Stack } from '@wordpress/ui'; | ||
| import clsx from 'clsx'; | ||
| import { differenceInHours, differenceInYears } from 'date-fns'; | ||
| import { | ||
|
|
@@ -103,12 +104,18 @@ const renderDefaultTooltip = ( params: RenderTooltipParams< DataPointDate > ) => | |
| { nearestDatum.date?.toLocaleDateString() } | ||
| </div> | ||
| { tooltipPoints.map( point => ( | ||
| <div key={ point.key } className={ styles[ 'line-chart__tooltip-row' ] }> | ||
| <Stack | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| key={ point.key } | ||
| direction="row" | ||
| align="center" | ||
| justify="space-between" | ||
| className={ styles[ 'line-chart__tooltip-row' ] } | ||
| > | ||
| <span className={ styles[ 'line-chart__tooltip-label' ] }>{ point.key }:</span> | ||
| <span className={ styles[ 'line-chart__tooltip-value' ] }> | ||
| { formatNumber( point.value ) } | ||
| </span> | ||
| </div> | ||
| </Stack> | ||
| ) ) } | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { Icon, close } from '@wordpress/icons'; | ||
| import { Stack } from '@wordpress/ui'; | ||
| import clsx from 'clsx'; | ||
| import { useEffect, useId, useRef, useState } from 'react'; | ||
| import { isSafari } from '../../../utils'; | ||
|
|
@@ -88,7 +89,7 @@ const LineChartAnnotationLabelWithPopover: FC< LineChartAnnotationLabelWithPopov | |
| ) } | ||
| data-testid="line-chart-annotation-label-popover" | ||
| > | ||
| <div className={ styles[ 'line-chart__annotation-label-popover-header' ] }> | ||
| <Stack direction="row" align="flex-start" justify="space-between"> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| <div className={ styles[ 'line-chart__annotation-label-popover-content' ] }> | ||
| { renderLabelPopover( { title, subtitle } ) } | ||
| </div> | ||
|
|
@@ -102,7 +103,7 @@ const LineChartAnnotationLabelWithPopover: FC< LineChartAnnotationLabelWithPopov | |
| > | ||
| <Icon icon={ close } size={ 16 } /> | ||
| </button> | ||
| </div> | ||
| </Stack> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||





There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as previously.