@automattic/charts 4.9.0 → 4.10.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 (50) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/index.cjs +159 -66
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.css +14 -0
  5. package/dist/index.d.cts +27 -22
  6. package/dist/index.d.ts +27 -22
  7. package/dist/index.js +159 -66
  8. package/dist/index.js.map +1 -1
  9. package/package.json +1 -1
  10. package/src/charts/area-chart/area-chart.tsx +2 -1
  11. package/src/charts/area-chart/test/area-chart.test.tsx +0 -8
  12. package/src/charts/area-chart/types.ts +0 -2
  13. package/src/charts/bar-chart/bar-chart.tsx +7 -2
  14. package/src/charts/bar-chart/test/bar-chart.test.tsx +0 -8
  15. package/src/charts/bar-chart/types.ts +0 -2
  16. package/src/charts/bar-list-chart/bar-list-chart.tsx +3 -0
  17. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +23 -7
  18. package/src/charts/conversion-funnel-chart/test/conversion-funnel-chart.test.tsx +22 -0
  19. package/src/charts/conversion-funnel-chart/types.ts +1 -1
  20. package/src/charts/geo-chart/geo-chart.tsx +17 -2
  21. package/src/charts/geo-chart/test/geo-chart.test.tsx +9 -4
  22. package/src/charts/geo-chart/types.ts +1 -1
  23. package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +0 -6
  24. package/src/charts/heatmap-chart/types.ts +0 -2
  25. package/src/charts/leaderboard-chart/hooks/use-fitted-row-count.ts +2 -2
  26. package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +6 -0
  27. package/src/charts/leaderboard-chart/leaderboard-chart.tsx +34 -19
  28. package/src/charts/leaderboard-chart/test/leaderboard-chart.test.tsx +27 -1
  29. package/src/charts/leaderboard-chart/types.ts +1 -0
  30. package/src/charts/line-chart/line-chart.tsx +3 -0
  31. package/src/charts/line-chart/test/line-chart.test.tsx +0 -8
  32. package/src/charts/line-chart/types.ts +0 -2
  33. package/src/charts/pie-chart/pie-chart.module.scss +3 -0
  34. package/src/charts/pie-chart/pie-chart.tsx +0 -5
  35. package/src/charts/pie-chart/test/pie-chart.test.tsx +4 -8
  36. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +3 -0
  37. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.tsx +0 -5
  38. package/src/charts/private/pie-keyboard-navigation/pie-tooltip.tsx +8 -3
  39. package/src/charts/test/accessible-name.test.tsx +181 -0
  40. package/src/components/legend/hooks/test/use-chart-legend-items.test.tsx +63 -3
  41. package/src/components/legend/hooks/use-chart-legend-items.ts +63 -1
  42. package/src/components/legend/private/base-legend.tsx +27 -15
  43. package/src/components/legend/test/legend.test.tsx +42 -0
  44. package/src/components/legend/types.ts +2 -0
  45. package/src/hooks/test/use-xychart-theme.test.tsx +22 -0
  46. package/src/hooks/use-xychart-theme.ts +33 -2
  47. package/src/index.ts +1 -0
  48. package/src/types.ts +19 -1
  49. package/src/utils/resolve-font-size.ts +9 -10
  50. package/src/utils/test/resolve-font-size.test.ts +11 -2
@@ -1,13 +1,16 @@
1
+ const toFontSize = ( size: number ): number | undefined =>
2
+ Number.isFinite( size ) && size >= 0 ? size : undefined;
3
+
1
4
  /**
2
5
  * Resolve a theme `fontSize` value into a plain number suitable for
3
6
  * canvas-based measurement (e.g. `getStringWidth`).
4
7
  *
5
8
  * Accepts:
6
- * - A number — returned as-is
9
+ * - A finite, non-negative number — returned as-is
7
10
  * - A pixel string like `"12px"` — parsed and returned as a number
8
11
  *
9
- * Returns `undefined` for any other input (missing value, NaN, or
10
- * relative units like `rem`/`em`/`%`/`vh`) so callers can fall back to
12
+ * Returns `undefined` for any other input (missing value, a non-finite or
13
+ * negative size, or relative units like `rem`/`em`/`%`/`vh`) so callers can fall back to
11
14
  * their own default. Relative units are intentionally rejected because
12
15
  * we cannot resolve them to absolute pixels here without a parent
13
16
  * computed style, and silently returning the unitless prefix
@@ -18,19 +21,15 @@
18
21
  */
19
22
  export const resolveFontSize = ( val?: number | string ): number | undefined => {
20
23
  if ( typeof val === 'number' ) {
21
- return isNaN( val ) ? undefined : val;
24
+ return toFontSize( val );
22
25
  }
23
26
 
24
27
  if ( typeof val === 'string' ) {
25
28
  // Only accept plain numeric strings or pixel values.
26
29
  // Reject rem, em, %, vh, vw, etc. — they cannot be resolved to
27
30
  // 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;
31
+ const match = val.trim().match( /^\+?((?:\d*\.)?\d+)(px)?$/ );
32
+ return match ? toFontSize( parseFloat( match[ 1 ] ) ) : undefined;
34
33
  }
35
34
 
36
35
  return undefined;
@@ -11,8 +11,8 @@ describe( 'resolveFontSize', () => {
11
11
  expect( resolveFontSize( 13.5 ) ).toBe( 13.5 );
12
12
  } );
13
13
 
14
- it( 'returns undefined for NaN', () => {
15
- expect( resolveFontSize( NaN ) ).toBeUndefined();
14
+ it.each( [ NaN, Infinity, -12 ] )( 'returns undefined for %p', val => {
15
+ expect( resolveFontSize( val ) ).toBeUndefined();
16
16
  } );
17
17
  } );
18
18
 
@@ -25,8 +25,17 @@ describe( 'resolveFontSize', () => {
25
25
  it( 'parses pixel strings', () => {
26
26
  expect( resolveFontSize( '12px' ) ).toBe( 12 );
27
27
  expect( resolveFontSize( '13.5px' ) ).toBe( 13.5 );
28
+ expect( resolveFontSize( '.5px' ) ).toBe( 0.5 );
29
+ expect( resolveFontSize( '+12px' ) ).toBe( 12 );
28
30
  } );
29
31
 
32
+ it.each( [ '12.px', '12.', '-12px', '-.5', '9'.repeat( 400 ) + 'px' ] )(
33
+ 'rejects %p, which is not a valid font size',
34
+ val => {
35
+ expect( resolveFontSize( val ) ).toBeUndefined();
36
+ }
37
+ );
38
+
30
39
  it( 'trims whitespace before parsing', () => {
31
40
  expect( resolveFontSize( ' 14px ' ) ).toBe( 14 );
32
41
  } );