@automattic/charts 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/AGENTS.md +14 -1
  2. package/CHANGELOG.md +18 -0
  3. package/dist/{chunk-G3PMV62Z.js → chunk-5WRI5ZAA.js} +1 -6
  4. package/dist/{chunk-EMMSS5I5.cjs → chunk-DZUJEN5N.cjs} +2 -7
  5. package/dist/chunk-DZUJEN5N.cjs.map +1 -0
  6. package/dist/index.cjs +1571 -2377
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.css +31 -107
  9. package/dist/index.css.map +1 -1
  10. package/dist/index.d.cts +2 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.js +1722 -2528
  13. package/dist/index.js.map +1 -1
  14. package/dist/visx/group/index.cjs +1 -1
  15. package/dist/visx/group/index.js +1 -1
  16. package/dist/visx/legend/index.cjs +1 -1
  17. package/dist/visx/legend/index.js +1 -1
  18. package/dist/visx/text/index.cjs +1 -1
  19. package/dist/visx/text/index.js +1 -1
  20. package/package.json +9 -8
  21. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +19 -47
  22. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +17 -10
  23. package/src/charts/geo-chart/geo-chart.module.scss +0 -3
  24. package/src/charts/geo-chart/geo-chart.tsx +9 -4
  25. package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +1 -1
  26. package/src/charts/leaderboard-chart/leaderboard-chart.tsx +2 -2
  27. package/src/charts/line-chart/line-chart.module.scss +3 -13
  28. package/src/charts/line-chart/line-chart.tsx +9 -2
  29. package/src/charts/line-chart/private/line-chart-annotation-label-popover.tsx +5 -4
  30. package/src/charts/pie-chart/pie-chart.tsx +5 -3
  31. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +3 -3
  32. package/src/components/legend/private/base-legend.module.scss +2 -51
  33. package/src/components/legend/private/base-legend.tsx +30 -28
  34. package/src/components/tooltip/base-tooltip.module.scss +1 -1
  35. package/src/components/trend-indicator/trend-indicator.module.scss +2 -2
  36. package/src/hooks/use-chart-margin.tsx +1 -14
  37. package/src/providers/chart-context/themes.ts +7 -1
  38. package/src/types.ts +2 -0
  39. package/src/utils/index.ts +3 -0
  40. package/src/utils/resolve-font-size.ts +37 -0
  41. package/src/utils/test/resolve-css-var.test.ts +3 -5
  42. package/src/utils/test/resolve-font-size.test.ts +66 -0
  43. package/dist/chunk-EMMSS5I5.cjs.map +0 -1
  44. /package/dist/{chunk-G3PMV62Z.js.map → chunk-5WRI5ZAA.js.map} +0 -0
@@ -1,6 +1,7 @@
1
1
  import { Group } from '@visx/group';
2
2
  import { LegendItem, LegendLabel, LegendOrdinal, LegendShape } from '@visx/legend';
3
3
  import { scaleOrdinal } from '@visx/scale';
4
+ import { Stack } from '@wordpress/ui';
4
5
  import clsx from 'clsx';
5
6
  import {
6
7
  type RefAttributes,
@@ -16,10 +17,11 @@ import { valueOrIdentity, valueOrIdentityString, labelTransformFactory } from '.
16
17
  import styles from './base-legend.module.scss';
17
18
  import type { BaseLegendProps } from '../types';
18
19
 
19
- const orientationToFlexDirection = {
20
- horizontal: 'row' as const,
21
- vertical: 'column' as const,
22
- };
20
+ const ALIGNMENT_TO_FLEX = {
21
+ start: 'flex-start',
22
+ center: 'center',
23
+ end: 'flex-end',
24
+ } as const;
23
25
 
24
26
  // Component for legend text with truncation detection
25
27
  // Moved outside BaseLegend to prevent recreation on every render
@@ -159,6 +161,8 @@ export const BaseLegend: ForwardRefExoticComponent<
159
161
  [ interactive, handleLegendClick ]
160
162
  );
161
163
 
164
+ const flexAlignment = ALIGNMENT_TO_FLEX[ alignment ] ?? 'center';
165
+
162
166
  return render ? (
163
167
  render( items )
164
168
  ) : (
@@ -168,20 +172,17 @@ export const BaseLegend: ForwardRefExoticComponent<
168
172
  labelTransform={ labelTransform }
169
173
  >
170
174
  { labels => (
171
- <div
175
+ <Stack
172
176
  ref={ ref }
177
+ direction={ orientation === 'vertical' ? 'column' : 'row' }
178
+ gap={ orientation === 'vertical' ? 'sm' : 'lg' }
179
+ align={ orientation === 'vertical' ? flexAlignment : undefined }
180
+ justify={ orientation === 'horizontal' ? flexAlignment : undefined }
181
+ wrap={ orientation === 'horizontal' ? 'wrap' : undefined }
173
182
  role="list"
174
183
  data-testid={ `legend-${ orientation }` }
175
- className={ clsx(
176
- styles.legend,
177
- styles[ `legend--${ orientation }` ],
178
- styles[ `legend--alignment-${ alignment }` ],
179
- className
180
- ) }
181
- style={ {
182
- flexDirection: orientationToFlexDirection[ orientation ],
183
- ...theme.legend?.containerStyles,
184
- } }
184
+ className={ clsx( styles.legend, className ) }
185
+ style={ theme.legend?.containerStyles }
185
186
  >
186
187
  { labels.map( ( label, i ) => {
187
188
  const visible = isSeriesVisible( label.text );
@@ -257,28 +258,29 @@ export const BaseLegend: ForwardRefExoticComponent<
257
258
  labelClassName
258
259
  ) }
259
260
  style={ {
260
- justifyContent: labelJustifyContent,
261
261
  flex: labelFlex,
262
262
  margin: labelMargin,
263
263
  ...theme.legend?.labelStyles,
264
264
  } }
265
265
  >
266
- <LegendText
267
- text={ label.text }
268
- textOverflow={ textOverflow }
269
- maxWidth={ maxWidth }
270
- />
271
- { matchedItem?.value != null && matchedItem.value !== '' && (
272
- <span className={ styles[ 'legend-item-value' ] }>
273
- { '\u00A0' }
274
- { matchedItem.value }
275
- </span>
276
- ) }
266
+ <Stack align="center" gap="sm" justify={ labelJustifyContent }>
267
+ <LegendText
268
+ text={ label.text }
269
+ textOverflow={ textOverflow }
270
+ maxWidth={ maxWidth }
271
+ />
272
+ { matchedItem?.value != null && matchedItem.value !== '' && (
273
+ <span className={ styles[ 'legend-item-value' ] }>
274
+ { '\u00A0' }
275
+ { matchedItem.value }
276
+ </span>
277
+ ) }
278
+ </Stack>
277
279
  </LegendLabel>
278
280
  </LegendItem>
279
281
  );
280
282
  } ) }
281
- </div>
283
+ </Stack>
282
284
  ) }
283
285
  </LegendOrdinal>
284
286
  );
@@ -3,7 +3,7 @@
3
3
  background-color: rgba(0, 0, 0, 0.85);
4
4
  color: #fff;
5
5
  border-radius: 4px;
6
- font-size: 14px;
6
+ font-size: var(--wpds-font-size-md, 13px);
7
7
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
8
8
  position: absolute;
9
9
  pointer-events: none;
@@ -2,8 +2,8 @@
2
2
  display: inline-flex;
3
3
  align-items: center;
4
4
  gap: 0.125em;
5
- font-size: 0.875rem;
6
- font-weight: 500;
5
+ font-size: var(--wpds-font-size-md, 13px);
6
+ font-weight: var(--wpds-font-weight-medium, 499);
7
7
  line-height: 1;
8
8
 
9
9
  &--up {
@@ -1,6 +1,6 @@
1
1
  import { createScale, getTicks } from '@visx/scale';
2
2
  import { useMemo } from 'react';
3
- import { getLongestTickWidth } from '../utils';
3
+ import { getLongestTickWidth, resolveFontSize } from '../utils';
4
4
  import type { BaseChartProps, DataPointDate, SeriesData } from '../types';
5
5
  import type { XYChartTheme } from '@visx/xychart';
6
6
 
@@ -50,19 +50,6 @@ const DEFAULT_TICK_LENGTH = 8;
50
50
  */
51
51
  const DEFAULT_Y_TICK_WIDTH = 40;
52
52
 
53
- const resolveFontSize = ( val?: number | string ): number | undefined => {
54
- if ( typeof val === 'number' && ! isNaN( val ) ) {
55
- return val;
56
- }
57
-
58
- if ( typeof val === 'string' ) {
59
- const parsed = parseFloat( val );
60
- return isNaN( parsed ) ? undefined : parsed;
61
- }
62
-
63
- return undefined;
64
- };
65
-
66
53
  const getXAxisLabelMetrics = ( theme: XYChartTheme, orientation: 'top' | 'bottom' ) => {
67
54
  const xAxisStyles =
68
55
  orientation === 'top' ? theme.axisStyles?.x?.top : theme.axisStyles?.x?.bottom;
@@ -26,7 +26,13 @@ const defaultTheme: CompleteChartTheme = {
26
26
  },
27
27
  seriesLineStyles: [],
28
28
  glyphs: [],
29
- svgLabelSmall: { fill: 'var(--jp-gray-80, #2c3338)' },
29
+ // `fontFamily: 'inherit'` overrides visx's hardcoded default font stack
30
+ // (`-apple-system,BlinkMacSystemFont,Roboto,Helvetica Neue,sans-serif`)
31
+ // that `buildChartTheme` injects as an inline style on SVG `<text>`
32
+ // elements for axis labels and ticks. Setting `inherit` lets SVG text
33
+ // pick up the host application's font-family via normal CSS inheritance.
34
+ svgLabelSmall: { fill: 'var(--jp-gray-80, #2c3338)', fontFamily: 'inherit' },
35
+ svgLabelBig: { fontFamily: 'inherit' },
30
36
  annotationStyles: {
31
37
  label: {
32
38
  anchorLineStroke: 'var(--jp-gray-80, #2c3338)',
package/src/types.ts CHANGED
@@ -243,6 +243,8 @@ export type ChartTheme = {
243
243
  };
244
244
  /** Styles for small SVG text (eg. axis tick labels), passed through to the XYChart theme. */
245
245
  svgLabelSmall?: TextProps;
246
+ /** Styles for large SVG text (eg. axis titles), passed through to the XYChart theme. */
247
+ svgLabelBig?: TextProps;
246
248
  annotationStyles?: AnnotationStyles;
247
249
  /** GeoChart specific settings */
248
250
  geoChart?: {
@@ -26,3 +26,6 @@ export * from './color-utils';
26
26
 
27
27
  // CSS utilities
28
28
  export { resolveCssVariable } from './resolve-css-var';
29
+
30
+ // Font sizing utilities
31
+ export { resolveFontSize } from './resolve-font-size';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Resolve a theme `fontSize` value into a plain number suitable for
3
+ * canvas-based measurement (e.g. `getStringWidth`).
4
+ *
5
+ * Accepts:
6
+ * - A number — returned as-is
7
+ * - A pixel string like `"12px"` — parsed and returned as a number
8
+ *
9
+ * Returns `undefined` for any other input (missing value, NaN, or
10
+ * relative units like `rem`/`em`/`%`/`vh`) so callers can fall back to
11
+ * their own default. Relative units are intentionally rejected because
12
+ * we cannot resolve them to absolute pixels here without a parent
13
+ * computed style, and silently returning the unitless prefix
14
+ * (`parseFloat("0.875rem") === 0.875`) would produce nearly-zero
15
+ * widths in measurement code.
16
+ * @param val - Raw font size value from a theme, axis style, or props
17
+ * @return Parsed numeric font size in pixels, or `undefined` when unresolvable
18
+ */
19
+ export const resolveFontSize = ( val?: number | string ): number | undefined => {
20
+ if ( typeof val === 'number' ) {
21
+ return isNaN( val ) ? undefined : val;
22
+ }
23
+
24
+ if ( typeof val === 'string' ) {
25
+ // Only accept plain numeric strings or pixel values.
26
+ // Reject rem, em, %, vh, vw, etc. — they cannot be resolved to
27
+ // absolute pixels without a computed style context.
28
+ const match = val.trim().match( /^(-?\d+\.?\d*|-?\.\d+)(px)?$/ );
29
+ if ( ! match ) {
30
+ return undefined;
31
+ }
32
+ const parsed = parseFloat( match[ 1 ] );
33
+ return isNaN( parsed ) ? undefined : parsed;
34
+ }
35
+
36
+ return undefined;
37
+ };
@@ -414,9 +414,8 @@ describe( 'resolveCssVariable', () => {
414
414
  if ( element === themedElement ) {
415
415
  return {
416
416
  getPropertyValue: ( prop: string ) => {
417
- // eslint-disable-next-line @wordpress/no-unknown-ds-tokens -- Thinks this is a use rather than a test.
418
- if ( prop === '--wpds-color-bg-interactive-brand-weak' ) {
419
- return '#c029dc'; // User's custom accent color
417
+ if ( prop === '--custom-accent-color' ) {
418
+ return '#c029dc';
420
419
  }
421
420
  return '';
422
421
  },
@@ -428,8 +427,7 @@ describe( 'resolveCssVariable', () => {
428
427
  } );
429
428
  window.getComputedStyle = mockGetComputedStyle as unknown as typeof window.getComputedStyle;
430
429
 
431
- // eslint-disable-next-line @wordpress/no-unknown-ds-tokens -- Thinks this is a use rather than a test.
432
- const result = resolveCssVariable( '--wpds-color-bg-interactive-brand-weak', themedElement );
430
+ const result = resolveCssVariable( '--custom-accent-color', themedElement );
433
431
  expect( result ).toBe( '#c029dc' );
434
432
  } );
435
433
  } );
@@ -0,0 +1,66 @@
1
+ import { resolveFontSize } from '../resolve-font-size';
2
+
3
+ describe( 'resolveFontSize', () => {
4
+ describe( 'numeric input', () => {
5
+ it( 'returns whole numbers as-is', () => {
6
+ expect( resolveFontSize( 12 ) ).toBe( 12 );
7
+ expect( resolveFontSize( 0 ) ).toBe( 0 );
8
+ } );
9
+
10
+ it( 'returns decimal numbers as-is', () => {
11
+ expect( resolveFontSize( 13.5 ) ).toBe( 13.5 );
12
+ } );
13
+
14
+ it( 'returns undefined for NaN', () => {
15
+ expect( resolveFontSize( NaN ) ).toBeUndefined();
16
+ } );
17
+ } );
18
+
19
+ describe( 'string input', () => {
20
+ it( 'parses bare numeric strings', () => {
21
+ expect( resolveFontSize( '12' ) ).toBe( 12 );
22
+ expect( resolveFontSize( '13.5' ) ).toBe( 13.5 );
23
+ } );
24
+
25
+ it( 'parses pixel strings', () => {
26
+ expect( resolveFontSize( '12px' ) ).toBe( 12 );
27
+ expect( resolveFontSize( '13.5px' ) ).toBe( 13.5 );
28
+ } );
29
+
30
+ it( 'trims whitespace before parsing', () => {
31
+ expect( resolveFontSize( ' 14px ' ) ).toBe( 14 );
32
+ } );
33
+
34
+ it( 'rejects rem values rather than returning the unitless prefix', () => {
35
+ expect( resolveFontSize( '0.875rem' ) ).toBeUndefined();
36
+ } );
37
+
38
+ it( 'rejects em, %, vh, vw and other relative units', () => {
39
+ expect( resolveFontSize( '1em' ) ).toBeUndefined();
40
+ expect( resolveFontSize( '100%' ) ).toBeUndefined();
41
+ expect( resolveFontSize( '2vh' ) ).toBeUndefined();
42
+ expect( resolveFontSize( '2vw' ) ).toBeUndefined();
43
+ } );
44
+
45
+ it( 'rejects keywords like inherit, initial, unset', () => {
46
+ expect( resolveFontSize( 'inherit' ) ).toBeUndefined();
47
+ expect( resolveFontSize( 'initial' ) ).toBeUndefined();
48
+ expect( resolveFontSize( 'medium' ) ).toBeUndefined();
49
+ } );
50
+
51
+ it( 'returns undefined for empty string', () => {
52
+ expect( resolveFontSize( '' ) ).toBeUndefined();
53
+ } );
54
+
55
+ it( 'returns undefined for unparseable strings', () => {
56
+ expect( resolveFontSize( 'abc' ) ).toBeUndefined();
57
+ expect( resolveFontSize( 'pxpx' ) ).toBeUndefined();
58
+ } );
59
+ } );
60
+
61
+ describe( 'missing input', () => {
62
+ it( 'returns undefined for undefined', () => {
63
+ expect( resolveFontSize( undefined ) ).toBeUndefined();
64
+ } );
65
+ } );
66
+ } );
@@ -1 +0,0 @@
1
- {"version":3,"sources":["/home/runner/work/jetpack/jetpack/projects/js-packages/charts/dist/chunk-EMMSS5I5.cjs"],"names":[],"mappings":"AAAA,6EAAI,SAAS,EAAE,MAAM,CAAC,MAAM;AAC5B,IAAI,UAAU,EAAE,MAAM,CAAC,cAAc;AACrC,IAAI,iBAAiB,EAAE,MAAM,CAAC,wBAAwB;AACtD,IAAI,kBAAkB,EAAE,MAAM,CAAC,mBAAmB;AAClD,IAAI,aAAa,EAAE,MAAM,CAAC,cAAc;AACxC,IAAI,aAAa,EAAE,MAAM,CAAC,SAAS,CAAC,cAAc;AAClD,IAAI,WAAW,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,SAAS,SAAS,CAAC,EAAE;AACnD,EAAE,OAAO,IAAI,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,GAAG,CAAC,OAAO;AACpG,CAAC;AACD,IAAI,SAAS,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG;AAChC,EAAE,IAAI,CAAC,IAAI,KAAK,GAAG,GAAG;AACtB,IAAI,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;AACjE,CAAC;AACD,IAAI,YAAY,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG;AAC9C,EAAE,GAAG,CAAC,KAAK,GAAG,OAAO,KAAK,IAAI,SAAS,GAAG,OAAO,KAAK,IAAI,UAAU,EAAE;AACtE,IAAI,IAAI,CAAC,IAAI,IAAI,GAAG,iBAAiB,CAAC,IAAI,CAAC;AAC3C,MAAM,GAAG,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,EAAE,GAAG,IAAI,IAAI,MAAM;AACvD,QAAQ,SAAS,CAAC,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,KAAK,EAAE,gBAAgB,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC;AAC1H,EAAE;AACF,EAAE,OAAO,EAAE;AACX,CAAC;AACD,IAAI,QAAQ,EAAE,CAAC,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,EAAE,IAAI,GAAG,KAAK,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,WAAW;AAChH;AACA;AACA;AACA;AACA,EAAE,WAAW,GAAG,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,SAAS,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,EAAE,EAAE,MAAM;AACjH,EAAE;AACF,CAAC,CAAC;AACF;AACA;AACE;AACA;AACA;AACF,wFAAC","file":"/home/runner/work/jetpack/jetpack/projects/js-packages/charts/dist/chunk-EMMSS5I5.cjs"}