@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.
- package/CHANGELOG.md +14 -0
- package/dist/index.cjs +159 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +14 -0
- package/dist/index.d.cts +27 -22
- package/dist/index.d.ts +27 -22
- package/dist/index.js +159 -66
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/charts/area-chart/area-chart.tsx +2 -1
- package/src/charts/area-chart/test/area-chart.test.tsx +0 -8
- package/src/charts/area-chart/types.ts +0 -2
- package/src/charts/bar-chart/bar-chart.tsx +7 -2
- package/src/charts/bar-chart/test/bar-chart.test.tsx +0 -8
- package/src/charts/bar-chart/types.ts +0 -2
- package/src/charts/bar-list-chart/bar-list-chart.tsx +3 -0
- package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +23 -7
- package/src/charts/conversion-funnel-chart/test/conversion-funnel-chart.test.tsx +22 -0
- package/src/charts/conversion-funnel-chart/types.ts +1 -1
- package/src/charts/geo-chart/geo-chart.tsx +17 -2
- package/src/charts/geo-chart/test/geo-chart.test.tsx +9 -4
- package/src/charts/geo-chart/types.ts +1 -1
- package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +0 -6
- package/src/charts/heatmap-chart/types.ts +0 -2
- package/src/charts/leaderboard-chart/hooks/use-fitted-row-count.ts +2 -2
- package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +6 -0
- package/src/charts/leaderboard-chart/leaderboard-chart.tsx +34 -19
- package/src/charts/leaderboard-chart/test/leaderboard-chart.test.tsx +27 -1
- package/src/charts/leaderboard-chart/types.ts +1 -0
- package/src/charts/line-chart/line-chart.tsx +3 -0
- package/src/charts/line-chart/test/line-chart.test.tsx +0 -8
- package/src/charts/line-chart/types.ts +0 -2
- package/src/charts/pie-chart/pie-chart.module.scss +3 -0
- package/src/charts/pie-chart/pie-chart.tsx +0 -5
- package/src/charts/pie-chart/test/pie-chart.test.tsx +4 -8
- package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +3 -0
- package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.tsx +0 -5
- package/src/charts/private/pie-keyboard-navigation/pie-tooltip.tsx +8 -3
- package/src/charts/test/accessible-name.test.tsx +181 -0
- package/src/components/legend/hooks/test/use-chart-legend-items.test.tsx +63 -3
- package/src/components/legend/hooks/use-chart-legend-items.ts +63 -1
- package/src/components/legend/private/base-legend.tsx +27 -15
- package/src/components/legend/test/legend.test.tsx +42 -0
- package/src/components/legend/types.ts +2 -0
- package/src/hooks/test/use-xychart-theme.test.tsx +22 -0
- package/src/hooks/use-xychart-theme.ts +33 -2
- package/src/index.ts +1 -0
- package/src/types.ts +19 -1
- package/src/utils/resolve-font-size.ts +9 -10
- 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,
|
|
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
|
|
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(
|
|
29
|
-
|
|
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
|
|
15
|
-
expect( resolveFontSize(
|
|
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
|
} );
|