diff --git a/projects/js-packages/charts/changelog/charts-typography-tokenization b/projects/js-packages/charts/changelog/charts-typography-tokenization new file mode 100644 index 000000000000..65f025361f98 --- /dev/null +++ b/projects/js-packages/charts/changelog/charts-typography-tokenization @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Replace hardcoded typography values with WPDS design tokens for font family, size, weight, and line height. diff --git a/projects/js-packages/charts/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss b/projects/js-packages/charts/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss index 64040be4cb44..16da365f498b 100644 --- a/projects/js-packages/charts/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +++ b/projects/js-packages/charts/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss @@ -1,5 +1,4 @@ .conversion-funnel-chart { - font-family: var(--funnel-font-family, "SF Pro Text"); &--loading { opacity: 0.6; @@ -19,22 +18,20 @@ overflow: hidden; color: #1e1e1e; text-overflow: ellipsis; - font-family: var(--funnel-font-family, "SF Pro Text"); - font-size: 18px; + font-size: var(--wpds-font-size-xl, 18px); font-style: normal; - font-weight: 500; - line-height: 20px; + font-weight: var(--wpds-font-weight-medium, 499); + line-height: var(--wpds-font-line-height-sm, 20px); margin: 0; } .change-indicator { overflow: hidden; text-overflow: ellipsis; - font-family: var(--funnel-font-family, "SF Pro Text"); - font-size: 13px; + font-size: var(--wpds-font-size-md, 13px); font-style: normal; - font-weight: 500; - line-height: 20px; + font-weight: var(--wpds-font-weight-medium, 499); + line-height: var(--wpds-font-line-height-sm, 20px); margin: 0; } @@ -69,11 +66,10 @@ .step-label { color: #757575; - font-family: var(--step-font-family, "SF Pro"); - font-size: 12px; + font-size: var(--wpds-font-size-sm, 12px); font-style: normal; - font-weight: 400; - line-height: 16px; + font-weight: var(--wpds-font-weight-regular, 400); + line-height: var(--wpds-font-line-height-xs, 16px); margin: 0 0 2px 0; display: block; overflow: hidden; @@ -82,11 +78,10 @@ .step-rate { color: #1e1e1e; - font-family: var(--step-font-family, "SF Pro"); - font-size: 13px; + font-size: var(--wpds-font-size-md, 13px); font-style: normal; - font-weight: 500; - line-height: 20px; + font-weight: var(--wpds-font-weight-medium, 499); + line-height: var(--wpds-font-line-height-sm, 20px); margin: 0; display: block; } @@ -139,25 +134,23 @@ .tooltip-title { color: #1e1e1e; - font-family: "SF Pro", sans-serif; - font-size: 12px; + font-size: var(--wpds-font-size-sm, 12px); font-style: normal; - font-weight: 400; - line-height: 16px; + font-weight: var(--wpds-font-weight-regular, 400); + line-height: var(--wpds-font-line-height-xs, 16px); } .tooltip-content { color: #1e1e1e; - font-family: "SF Pro", sans-serif; - font-size: 13px; + font-size: var(--wpds-font-size-md, 13px); font-style: normal; - font-weight: 500; - line-height: 20px; + font-weight: var(--wpds-font-weight-medium, 499); + line-height: var(--wpds-font-line-height-sm, 20px); } .empty-state { text-align: center; padding: 48px 24px; color: #6b7280; - font-size: 16px; + font-size: var(--wpds-font-size-lg, 16px); } diff --git a/projects/js-packages/charts/src/charts/conversion-funnel-chart/stories/index.docs.mdx b/projects/js-packages/charts/src/charts/conversion-funnel-chart/stories/index.docs.mdx index 7858ebbcc5fe..3ca4e0b2c498 100644 --- a/projects/js-packages/charts/src/charts/conversion-funnel-chart/stories/index.docs.mdx +++ b/projects/js-packages/charts/src/charts/conversion-funnel-chart/stories/index.docs.mdx @@ -247,18 +247,6 @@ Use the `style` prop or `className` prop to customize the chart container: />` } /> -### CSS Custom Properties - -The component exposes CSS custom properties for font customization: - - - ## Theming Integration Conversion Funnel Charts integrate seamlessly with the chart theming system. The default theme has neutral colors and styling, and is automatically applied to all charts unless a custom theme is provided. A custom theme can be provided by wrapping your chart in `GlobalChartsProvider` and passing a custom theme object with the properties you want to override to the `theme` prop: diff --git a/projects/js-packages/charts/src/charts/leaderboard-chart/leaderboard-chart.module.scss b/projects/js-packages/charts/src/charts/leaderboard-chart/leaderboard-chart.module.scss index f768e11bdbc9..d950bfed08cb 100644 --- a/projects/js-packages/charts/src/charts/leaderboard-chart/leaderboard-chart.module.scss +++ b/projects/js-packages/charts/src/charts/leaderboard-chart/leaderboard-chart.module.scss @@ -73,6 +73,6 @@ padding: 32px 16px; text-align: center; color: #666; - font-size: 14px; + font-size: var(--wpds-font-size-md, 13px); font-style: italic; } diff --git a/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.docs.mdx b/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.docs.mdx index b57e4d15dffa..dbba46002ee4 100644 --- a/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.docs.mdx +++ b/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.docs.mdx @@ -421,7 +421,6 @@ Combine overlay labels with custom styling and transparent colors: primaryColor="rgba(56, 88, 233, 0.08)" // Semi-transparent style={{ '--a8c--charts--leaderboard--bar--border-radius': '4px', - fontFamily: '"SF Pro Text", sans-serif', }} />` } /> @@ -445,7 +444,6 @@ The component supports CSS custom properties for advanced styling: language="css" code={ `.myCustomChart { --a8c--charts--leaderboard--bar--border-radius: 8px; - font-family: "Custom Font", sans-serif; }` } /> diff --git a/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.stories.tsx b/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.stories.tsx index 4ca1af9e4170..60e0a5e362cd 100644 --- a/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.stories.tsx +++ b/projects/js-packages/charts/src/charts/leaderboard-chart/stories/index.stories.tsx @@ -371,7 +371,6 @@ export const OverlayLabelWithImage: Story = { loading: false, style: { '--a8c--charts--leaderboard--bar--border-radius': '4px', - fontFamily: `"SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif`, }, }, render: args => , diff --git a/projects/js-packages/charts/src/charts/line-chart/line-chart.module.scss b/projects/js-packages/charts/src/charts/line-chart/line-chart.module.scss index 007d7ad8bba3..027e9d18faac 100644 --- a/projects/js-packages/charts/src/charts/line-chart/line-chart.module.scss +++ b/projects/js-packages/charts/src/charts/line-chart/line-chart.module.scss @@ -21,7 +21,7 @@ } &__tooltip-date { - font-weight: 700; + font-weight: var(--wpds-font-weight-medium, 499); padding-bottom: 10px; } @@ -33,7 +33,7 @@ } &__tooltip-label { - font-weight: 500; + font-weight: var(--wpds-font-weight-medium, 499); padding-right: 1rem; } @@ -65,7 +65,7 @@ background: #fff; border: none; border-radius: 4px; - font-size: 14px; + font-size: var(--wpds-font-size-md, 13px); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); position: fixed; margin: 0.5rem; diff --git a/projects/js-packages/charts/src/charts/pie-chart/pie-chart.tsx b/projects/js-packages/charts/src/charts/pie-chart/pie-chart.tsx index 019679316862..bcf65f9c495c 100644 --- a/projects/js-packages/charts/src/charts/pie-chart/pie-chart.tsx +++ b/projects/js-packages/charts/src/charts/pie-chart/pie-chart.tsx @@ -20,7 +20,7 @@ import { useGlobalChartsTheme, GlobalChartsContext, } from '../../providers'; -import { attachSubComponents } from '../../utils'; +import { attachSubComponents, resolveFontSize } from '../../utils'; import { getStringWidth } from '../../visx/text'; import { ChartSVG, ChartHTML, useChartChildren } from '../private/chart-composition'; import { ChartLayout } from '../private/chart-layout'; @@ -441,10 +441,12 @@ const PieChartInternal = ( { groupProps.onMouseLeave = onMouseLeave; } - // Estimate text width more accurately for background sizing - const fontSize = 12; + const svgLabelSmall = providerTheme.svgLabelSmall; + const fontSize = resolveFontSize( svgLabelSmall?.fontSize ) ?? 12; const estimatedTextWidth = getStringWidth( arc.data.label, { fontSize, + fontFamily: svgLabelSmall?.fontFamily, + fontWeight: svgLabelSmall?.fontWeight, } ); const labelPadding = 6; const backgroundWidth = estimatedTextWidth + labelPadding * 2; diff --git a/projects/js-packages/charts/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss b/projects/js-packages/charts/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss index c1d2be2f0be6..a1ac68826817 100644 --- a/projects/js-packages/charts/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +++ b/projects/js-packages/charts/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss @@ -11,11 +11,11 @@ } .label { - font-weight: 600; - font-size: 16px; + font-weight: var(--wpds-font-weight-medium, 499); + font-size: var(--wpds-font-size-lg, 16px); } .note { - font-size: 14px; + font-size: var(--wpds-font-size-md, 13px); } } diff --git a/projects/js-packages/charts/src/charts/pie-semi-circle-chart/stories/index.docs.mdx b/projects/js-packages/charts/src/charts/pie-semi-circle-chart/stories/index.docs.mdx index 69553af36d8b..27df24a6d572 100644 --- a/projects/js-packages/charts/src/charts/pie-semi-circle-chart/stories/index.docs.mdx +++ b/projects/js-packages/charts/src/charts/pie-semi-circle-chart/stories/index.docs.mdx @@ -222,8 +222,8 @@ Segments automatically use theme colors, but you can override individual segment The chart includes built-in styling for labels and notes with appropriate typography hierarchy: -- **Label**: 16px, font-weight 600, positioned above the chart -- **Note**: 14px, positioned below the label +- **Label**: Uses `--wpds-font-size-lg` (default 16px), `--wpds-font-weight-medium` (default 499), positioned above the chart +- **Note**: Uses `--wpds-font-size-md` (default 13px), positioned below the label { - if ( typeof val === 'number' && ! isNaN( val ) ) { - return val; - } - - if ( typeof val === 'string' ) { - const parsed = parseFloat( val ); - return isNaN( parsed ) ? undefined : parsed; - } - - return undefined; -}; - const getXAxisLabelMetrics = ( theme: XYChartTheme, orientation: 'top' | 'bottom' ) => { const xAxisStyles = orientation === 'top' ? theme.axisStyles?.x?.top : theme.axisStyles?.x?.bottom; diff --git a/projects/js-packages/charts/src/providers/chart-context/themes.ts b/projects/js-packages/charts/src/providers/chart-context/themes.ts index e768a60c847a..e8cb5882eb4a 100644 --- a/projects/js-packages/charts/src/providers/chart-context/themes.ts +++ b/projects/js-packages/charts/src/providers/chart-context/themes.ts @@ -26,7 +26,13 @@ const defaultTheme: CompleteChartTheme = { }, seriesLineStyles: [], glyphs: [], - svgLabelSmall: { fill: 'var(--jp-gray-80, #2c3338)' }, + // `fontFamily: 'inherit'` overrides visx's hardcoded default font stack + // (`-apple-system,BlinkMacSystemFont,Roboto,Helvetica Neue,sans-serif`) + // that `buildChartTheme` injects as an inline style on SVG `` + // elements for axis labels and ticks. Setting `inherit` lets SVG text + // pick up the host application's font-family via normal CSS inheritance. + svgLabelSmall: { fill: 'var(--jp-gray-80, #2c3338)', fontFamily: 'inherit' }, + svgLabelBig: { fontFamily: 'inherit' }, annotationStyles: { label: { anchorLineStroke: 'var(--jp-gray-80, #2c3338)', diff --git a/projects/js-packages/charts/src/stories/chart-decorator.tsx b/projects/js-packages/charts/src/stories/chart-decorator.tsx index 71e537017db2..7067b2e2941e 100644 --- a/projects/js-packages/charts/src/stories/chart-decorator.tsx +++ b/projects/js-packages/charts/src/stories/chart-decorator.tsx @@ -161,9 +161,20 @@ const StoryChartProvider = ( { return ( - - { children } - + { /* + Storybook acts as a WPDS-themed host application so charts + inherit the design system body font through normal CSS + cascade. + */ } +
+ + { children } + +
); }; diff --git a/projects/js-packages/charts/src/types.ts b/projects/js-packages/charts/src/types.ts index faeb3b4beb7f..959bda24a76b 100644 --- a/projects/js-packages/charts/src/types.ts +++ b/projects/js-packages/charts/src/types.ts @@ -243,6 +243,8 @@ export type ChartTheme = { }; /** Styles for small SVG text (eg. axis tick labels), passed through to the XYChart theme. */ svgLabelSmall?: TextProps; + /** Styles for large SVG text (eg. axis titles), passed through to the XYChart theme. */ + svgLabelBig?: TextProps; annotationStyles?: AnnotationStyles; /** GeoChart specific settings */ geoChart?: { diff --git a/projects/js-packages/charts/src/utils/index.ts b/projects/js-packages/charts/src/utils/index.ts index c93a0794d15b..68039dc46559 100644 --- a/projects/js-packages/charts/src/utils/index.ts +++ b/projects/js-packages/charts/src/utils/index.ts @@ -26,3 +26,6 @@ export * from './color-utils'; // CSS utilities export { resolveCssVariable } from './resolve-css-var'; + +// Font sizing utilities +export { resolveFontSize } from './resolve-font-size'; diff --git a/projects/js-packages/charts/src/utils/resolve-font-size.ts b/projects/js-packages/charts/src/utils/resolve-font-size.ts new file mode 100644 index 000000000000..a96109a13c74 --- /dev/null +++ b/projects/js-packages/charts/src/utils/resolve-font-size.ts @@ -0,0 +1,37 @@ +/** + * Resolve a theme `fontSize` value into a plain number suitable for + * canvas-based measurement (e.g. `getStringWidth`). + * + * Accepts: + * - A number — returned as-is + * - A pixel string like `"12px"` — parsed and returned as a number + * + * Returns `undefined` for any other input (missing value, NaN, or + * relative units like `rem`/`em`/`%`/`vh`) so callers can fall back to + * their own default. Relative units are intentionally rejected because + * we cannot resolve them to absolute pixels here without a parent + * computed style, and silently returning the unitless prefix + * (`parseFloat("0.875rem") === 0.875`) would produce nearly-zero + * widths in measurement code. + * @param val - Raw font size value from a theme, axis style, or props + * @return Parsed numeric font size in pixels, or `undefined` when unresolvable + */ +export const resolveFontSize = ( val?: number | string ): number | undefined => { + if ( typeof val === 'number' ) { + return isNaN( val ) ? undefined : val; + } + + if ( typeof val === 'string' ) { + // Only accept plain numeric strings or pixel values. + // Reject rem, em, %, vh, vw, etc. — they cannot be resolved to + // absolute pixels without a computed style context. + const match = val.trim().match( /^(-?\d+\.?\d*|-?\.\d+)(px)?$/ ); + if ( ! match ) { + return undefined; + } + const parsed = parseFloat( match[ 1 ] ); + return isNaN( parsed ) ? undefined : parsed; + } + + return undefined; +}; diff --git a/projects/js-packages/charts/src/utils/test/resolve-font-size.test.ts b/projects/js-packages/charts/src/utils/test/resolve-font-size.test.ts new file mode 100644 index 000000000000..7c43bd1c75c6 --- /dev/null +++ b/projects/js-packages/charts/src/utils/test/resolve-font-size.test.ts @@ -0,0 +1,66 @@ +import { resolveFontSize } from '../resolve-font-size'; + +describe( 'resolveFontSize', () => { + describe( 'numeric input', () => { + it( 'returns whole numbers as-is', () => { + expect( resolveFontSize( 12 ) ).toBe( 12 ); + expect( resolveFontSize( 0 ) ).toBe( 0 ); + } ); + + it( 'returns decimal numbers as-is', () => { + expect( resolveFontSize( 13.5 ) ).toBe( 13.5 ); + } ); + + it( 'returns undefined for NaN', () => { + expect( resolveFontSize( NaN ) ).toBeUndefined(); + } ); + } ); + + describe( 'string input', () => { + it( 'parses bare numeric strings', () => { + expect( resolveFontSize( '12' ) ).toBe( 12 ); + expect( resolveFontSize( '13.5' ) ).toBe( 13.5 ); + } ); + + it( 'parses pixel strings', () => { + expect( resolveFontSize( '12px' ) ).toBe( 12 ); + expect( resolveFontSize( '13.5px' ) ).toBe( 13.5 ); + } ); + + it( 'trims whitespace before parsing', () => { + expect( resolveFontSize( ' 14px ' ) ).toBe( 14 ); + } ); + + it( 'rejects rem values rather than returning the unitless prefix', () => { + expect( resolveFontSize( '0.875rem' ) ).toBeUndefined(); + } ); + + it( 'rejects em, %, vh, vw and other relative units', () => { + expect( resolveFontSize( '1em' ) ).toBeUndefined(); + expect( resolveFontSize( '100%' ) ).toBeUndefined(); + expect( resolveFontSize( '2vh' ) ).toBeUndefined(); + expect( resolveFontSize( '2vw' ) ).toBeUndefined(); + } ); + + it( 'rejects keywords like inherit, initial, unset', () => { + expect( resolveFontSize( 'inherit' ) ).toBeUndefined(); + expect( resolveFontSize( 'initial' ) ).toBeUndefined(); + expect( resolveFontSize( 'medium' ) ).toBeUndefined(); + } ); + + it( 'returns undefined for empty string', () => { + expect( resolveFontSize( '' ) ).toBeUndefined(); + } ); + + it( 'returns undefined for unparseable strings', () => { + expect( resolveFontSize( 'abc' ) ).toBeUndefined(); + expect( resolveFontSize( 'pxpx' ) ).toBeUndefined(); + } ); + } ); + + describe( 'missing input', () => { + it( 'returns undefined for undefined', () => { + expect( resolveFontSize( undefined ) ).toBeUndefined(); + } ); + } ); +} );