@automattic/charts 1.0.1 → 1.1.0

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 (43) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/{chunk-G3PMV62Z.js → chunk-5WRI5ZAA.js} +1 -6
  3. package/dist/{chunk-EMMSS5I5.cjs → chunk-DZUJEN5N.cjs} +2 -7
  4. package/dist/chunk-DZUJEN5N.cjs.map +1 -0
  5. package/dist/index.cjs +602 -1386
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.css +36 -52
  8. package/dist/index.css.map +1 -1
  9. package/dist/index.d.cts +3 -0
  10. package/dist/index.d.ts +3 -0
  11. package/dist/index.js +699 -1483
  12. package/dist/index.js.map +1 -1
  13. package/dist/visx/group/index.cjs +1 -1
  14. package/dist/visx/group/index.js +1 -1
  15. package/dist/visx/legend/index.cjs +1 -1
  16. package/dist/visx/legend/index.js +1 -1
  17. package/dist/visx/text/index.cjs +1 -1
  18. package/dist/visx/text/index.js +1 -1
  19. package/package.json +12 -11
  20. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +26 -42
  21. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +18 -5
  22. package/src/charts/conversion-funnel-chart/test/conversion-funnel-chart.test.tsx +11 -0
  23. package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +1 -1
  24. package/src/charts/leaderboard-chart/leaderboard-chart.tsx +2 -2
  25. package/src/charts/line-chart/line-chart.module.scss +3 -3
  26. package/src/charts/line-chart/private/line-chart-annotation-label-popover.tsx +2 -2
  27. package/src/charts/pie-chart/pie-chart.tsx +5 -3
  28. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +3 -3
  29. package/src/components/legend/private/base-legend.module.scss +2 -2
  30. package/src/components/tooltip/base-tooltip.module.scss +1 -1
  31. package/src/components/trend-indicator/trend-indicator.module.scss +2 -2
  32. package/src/hooks/use-chart-margin.tsx +1 -14
  33. package/src/providers/chart-context/global-charts-provider.tsx +13 -0
  34. package/src/providers/chart-context/test/chart-context.test.tsx +51 -0
  35. package/src/providers/chart-context/themes.ts +7 -1
  36. package/src/providers/chart-context/types.ts +1 -0
  37. package/src/types.ts +2 -0
  38. package/src/utils/index.ts +3 -0
  39. package/src/utils/resolve-font-size.ts +37 -0
  40. package/src/utils/test/resolve-css-var.test.ts +3 -5
  41. package/src/utils/test/resolve-font-size.test.ts +66 -0
  42. package/dist/chunk-EMMSS5I5.cjs.map +0 -1
  43. /package/dist/{chunk-G3PMV62Z.js.map → chunk-5WRI5ZAA.js.map} +0 -0
@@ -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,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