diff --git a/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.test.tsx b/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.test.tsx index c4ca4b60548..2bf2e63deba 100644 --- a/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.test.tsx +++ b/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.test.tsx @@ -48,7 +48,7 @@ function renderLink( } describe( 'PDFLink', () => { - it( 'links to the href with no underline', () => { + it( 'should link to the href with no underline', () => { const tree = renderLink( { href: 'https://example.com/dashboard', children: 'View dashboard', @@ -62,7 +62,7 @@ describe( 'PDFLink', () => { expect( tree.props.style ).not.toHaveProperty( 'flexGrow' ); } ); - it( 'uses the link color when no style sets a color', () => { + it( 'should use the link color when no style sets a color', () => { const linkJSON = JSON.stringify( renderLink( { href: 'https://example.com/dashboard', @@ -74,7 +74,7 @@ describe( 'PDFLink', () => { expect( linkJSON ).toContain( PDF_COLORS.CONTENT_SECONDARY ); } ); - it( 'renders the trailing icon after the link text', () => { + it( 'should render the trailing icon after the link text', () => { const tree = renderLink( { href: 'https://example.com/dashboard', trailingIcon: ( @@ -90,7 +90,7 @@ describe( 'PDFLink', () => { expect( JSON.stringify( tree ) ).toContain( 'M1 2 3 4' ); } ); - it( 'renders only the link text when no icon is passed', () => { + it( 'should render only the link text when no icon is passed', () => { const tree = renderLink( { href: 'https://example.com/dashboard', children: 'View dashboard', @@ -99,7 +99,7 @@ describe( 'PDFLink', () => { expect( tree.children ).toHaveLength( 1 ); } ); - it( 'uses the color from the style prop', () => { + it( 'should use the color from the style prop', () => { const linkJSON = JSON.stringify( renderLink( { href: 'https://example.com/dashboard', @@ -111,7 +111,7 @@ describe( 'PDFLink', () => { expect( linkJSON ).toContain( '#6c726e' ); } ); - it( 'renders plain text in the default color when the href is an empty string', () => { + it( 'should render plain text in the default color when the href is an empty string', () => { // An empty `href` is means a row with no link, so the text // should render as plain text instead of as a link. const tree = renderLink( { href: '', children: 'View dashboard' } ); @@ -123,7 +123,7 @@ describe( 'PDFLink', () => { expect( treeJSON ).not.toContain( PDF_COLORS.CONTENT_SECONDARY ); } ); - it( 'renders plain text in the default color when the href is undefined', () => { + it( 'should render plain text in the default color when the href is undefined', () => { // An undefined/missing `href` should renders plain text, // the same as an empty string above. const tree = renderLink( { @@ -138,7 +138,7 @@ describe( 'PDFLink', () => { expect( treeJSON ).not.toContain( PDF_COLORS.CONTENT_SECONDARY ); } ); - it( 'renders plain text in the default color when the href is null', () => { + it( 'should render plain text in the default color when the href is null', () => { const tree = renderLink( { href: null, children: 'View dashboard' } ); expect( tree.type ).toBe( 'pdf-text' ); @@ -148,7 +148,7 @@ describe( 'PDFLink', () => { expect( treeJSON ).not.toContain( PDF_COLORS.CONTENT_SECONDARY ); } ); - it( 'truncates the linked text to one line when truncateContent is set', () => { + it( 'should truncate the linked text to one line when truncateContent is set', () => { const tree = renderLink( { href: 'https://example.com/dashboard', truncateContent: true, @@ -171,7 +171,7 @@ describe( 'PDFLink', () => { ); } ); - it( 'truncates the plain text to one line when truncateContent is set and href is empty', () => { + it( 'should truncate the plain text to one line when truncateContent is set and href is empty', () => { const json = JSON.stringify( renderLink( { href: '', @@ -184,7 +184,7 @@ describe( 'PDFLink', () => { expect( json ).toContain( '"textOverflow":"ellipsis"' ); } ); - it( 'omits the trailing icon when the href is empty', () => { + it( 'should omit the trailing icon when the href is empty', () => { const tree = renderLink( { href: '', trailingIcon: ( @@ -197,4 +197,27 @@ describe( 'PDFLink', () => { expect( JSON.stringify( tree ) ).not.toContain( 'M1 2 3 4' ); } ); + + it( 'should pass the hyphenation callback to the text of a link and of plain text', () => { + function hyphenationCallback( word: string ) { + return [ word ]; + } + + const linkTree = renderLink( { + href: 'https://example.com/dashboard', + hyphenationCallback, + children: 'View dashboard', + } ); + const [ linkText ] = + linkTree.children as TestRenderer.ReactTestRendererJSON[]; + + expect( linkText.props.hyphenationCallback ).toBe( + hyphenationCallback + ); + + expect( + renderLink( { href: '', hyphenationCallback, children: 'Home' } ) + .props.hyphenationCallback + ).toBe( hyphenationCallback ); + } ); } ); diff --git a/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.tsx b/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.tsx index ae230fac0a6..9cf6bc3a859 100644 --- a/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.tsx +++ b/assets/js/components/pdf-export/shared-react-pdf-components/PDFLink.tsx @@ -30,6 +30,7 @@ import { FC, ReactNode } from 'react'; import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; import { PDF_COLORS } from '@/js/components/pdf-export/pdf-theme'; import PDFTypography, { + PDFTypographyProps, PDFTypographySize, PDFTypographyType, } from './PDFTypography'; @@ -60,6 +61,8 @@ export interface PDFLinkProps { style?: Style | Style[]; /** Ends the text in an ellipsis when it's too long, and limits it to one line unless the text's own `maxLines` sets another limit. */ truncateContent?: boolean; + /** Callback that splits each word into the parts a line can break between. */ + hyphenationCallback?: PDFTypographyProps[ 'hyphenationCallback' ]; } const PDFLink: FC< PDFLinkProps > = ( { @@ -69,6 +72,7 @@ const PDFLink: FC< PDFLinkProps > = ( { trailingIcon, style, truncateContent, + hyphenationCallback, children, } ) => { if ( ! href ) { @@ -78,6 +82,7 @@ const PDFLink: FC< PDFLinkProps > = ( { size={ size } style={ style } truncateContent={ truncateContent } + hyphenationCallback={ hyphenationCallback } > { children } @@ -96,6 +101,7 @@ const PDFLink: FC< PDFLinkProps > = ( { size={ size } style={ style ? [ linkColor ].concat( style ) : linkColor } truncateContent={ truncateContent } + hyphenationCallback={ hyphenationCallback } > { children } diff --git a/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.test.tsx b/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.test.tsx index 234c56f5dfe..783e5346352 100644 --- a/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.test.tsx +++ b/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.test.tsx @@ -130,6 +130,22 @@ describe( 'PDFTypography', () => { } ); } ); + it( 'should pass the hyphenation callback to the Text', () => { + function hyphenationCallback( word: string ) { + return [ word ]; + } + const tree = TestRenderer.create( + + Vacoas-Phoenix + + ).toJSON(); + if ( ! tree || Array.isArray( tree ) ) { + throw new Error( 'Unexpected render output.' ); + } + + expect( tree.props.hyphenationCallback ).toBe( hyphenationCallback ); + } ); + it( 'truncates the text to one line with an ellipsis when truncateContent is true', () => { const json = renderJSON( diff --git a/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.tsx b/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.tsx index 90ad62d47e3..d5957ea36ae 100644 --- a/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.tsx +++ b/assets/js/components/pdf-export/shared-react-pdf-components/PDFTypography.tsx @@ -22,7 +22,7 @@ */ import { Text, View } from '@react-pdf/renderer'; import type { Style } from '@react-pdf/stylesheet'; -import { FC } from 'react'; +import { ComponentProps, FC } from 'react'; /** * Internal dependencies @@ -57,6 +57,10 @@ export interface PDFTypographyProps { maxLines?: number; /** Ends the text in an ellipsis when it's too long, and limits it to one line unless `maxLines` sets another limit. */ truncateContent?: boolean; + /** Callback that splits each word into the parts a line can break between. */ + hyphenationCallback?: ComponentProps< + typeof Text + >[ 'hyphenationCallback' ]; } const PDFTypography: FC< PDFTypographyProps > = ( { @@ -65,6 +69,7 @@ const PDFTypography: FC< PDFTypographyProps > = ( { style, maxLines, truncateContent = false, + hyphenationCallback, children, } ) => { const baseStyles: Style[] = [ @@ -85,7 +90,10 @@ const PDFTypography: FC< PDFTypographyProps > = ( { } const text = ( - + { children } ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardHeader.test.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardHeader.test.tsx new file mode 100644 index 00000000000..bb6a173d5af --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardHeader.test.tsx @@ -0,0 +1,132 @@ +/** + * PDFAudienceCardHeader tests. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { ComponentProps } from 'react'; +import TestRenderer from 'react-test-renderer'; + +/** + * Internal dependencies + */ +import { scalePDFValue } from '@/js/components/pdf-export/pdf-scale'; +import { PDF_PARTIAL_DATA_BADGE_HEIGHT } from '@/js/components/pdf-export/shared-react-pdf-components/PDFPartialDataBadge'; +import { + flattenPDFStyle, + renderJSON, + renderPDFStyle, +} from '@/js/components/pdf-export/test-utils'; +import PDFAudienceCardHeader from './PDFAudienceCardHeader'; + +/** + * Renders the header and returns its title row. + * + * @since n.e.x.t + * + * @param {Object} props Props for the header. + * @return {Object} The row holding the audience name and the badge. + */ +function renderTitleRow( + props: ComponentProps< typeof PDFAudienceCardHeader > +) { + const tree = TestRenderer.create( + + ).toJSON() as TestRenderer.ReactTestRendererJSON; + + const [ titleRow ] = tree.children as TestRenderer.ReactTestRendererJSON[]; + + return titleRow; +} + +describe( 'PDFAudienceCardHeader', () => { + it( 'should render the audience name', () => { + expect( + renderJSON( + + ) + ).toContain( 'New visitors' ); + } ); + + it( 'should show the Partial data badge only when the audience is in a partial data state', () => { + expect( + renderJSON( + + ) + ).toContain( 'Partial data' ); + + expect( + renderJSON( + + ) + ).not.toContain( 'Partial data' ); + } ); + + it( 'should fill its grid cell', () => { + expect( + renderPDFStyle( + + ) + ).toMatchObject( { flexGrow: 1 } ); + } ); + + it( 'should wrap a long name in the width the badge leaves, with a gap before the badge', () => { + const titleRow = renderTitleRow( { + audienceName: + 'Visitors who completed a purchase in the last thirty days', + isAudiencePartialData: true, + } ); + const [ name, badge ] = + titleRow.children as TestRenderer.ReactTestRendererJSON[]; + + expect( flattenPDFStyle( titleRow.props.style ) ).toMatchObject( { + columnGap: scalePDFValue( 10 ), + } ); + expect( flattenPDFStyle( name.props.style ) ).toMatchObject( { + flex: 1, + } ); + expect( JSON.stringify( badge ) ).toContain( 'Partial data' ); + } ); + + it( 'should reserve the Partial data badge height on the title row with or without a badge', () => { + const minHeight = scalePDFValue( PDF_PARTIAL_DATA_BADGE_HEIGHT ); + + for ( const isAudiencePartialData of [ true, false ] ) { + const titleRow = renderTitleRow( { + audienceName: 'New visitors', + isAudiencePartialData, + } ); + + expect( flattenPDFStyle( titleRow.props.style ) ).toMatchObject( { + minHeight, + } ); + } + } ); +} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardHeader.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardHeader.tsx new file mode 100644 index 00000000000..42f5f7e9b18 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardHeader.tsx @@ -0,0 +1,77 @@ +/** + * Header for the Your visitor groups PDF audience card. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { View } from '@react-pdf/renderer'; +import { FC } from 'react'; + +/** + * Internal dependencies + */ +import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; +import { PDF_COLORS } from '@/js/components/pdf-export/pdf-theme'; +import PDFPartialDataBadge, { + PDF_PARTIAL_DATA_BADGE_HEIGHT, +} from '@/js/components/pdf-export/shared-react-pdf-components/PDFPartialDataBadge'; +import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-components/PDFTypography'; +import { audienceCardStyles } from './pdfAudienceCardStyles'; + +const styles = createPDFStyles( { + // Fills its grid cell, so the dividers line up across the cards. + header: { + flexGrow: 1, + paddingVertical: 25, + paddingHorizontal: 25, + borderBottomWidth: 1, + borderBottomColor: PDF_COLORS.SURFACES_SURFACE_1, + }, + // `minHeight` includes padding, so it sits on this row, not the header. + titleRow: { + minHeight: PDF_PARTIAL_DATA_BADGE_HEIGHT, + columnGap: 10, + }, +} ); + +interface PDFAudienceCardHeaderProps { + /** The audience display name. */ + audienceName: string; + /** Whether to show the Partial data badge beside the audience name. */ + isAudiencePartialData: boolean; +} + +const PDFAudienceCardHeader: FC< PDFAudienceCardHeaderProps > = ( { + audienceName, + isAudiencePartialData, +} ) => { + return ( + + + + + { audienceName } + + + { isAudiencePartialData && } + + + ); +}; + +export default PDFAudienceCardHeader; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardSection.test.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardSection.test.tsx new file mode 100644 index 00000000000..8ba669ac682 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardSection.test.tsx @@ -0,0 +1,63 @@ +/** + * PDFAudienceCardSection tests. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Internal dependencies + */ +import { PDFAudienceMetricIconCities } from '@/js/components/pdf-export/pdf-icons'; +import { scalePDFValue } from '@/js/components/pdf-export/pdf-scale'; +import { + renderPDFChildStyles, + renderPDFStyle, +} from '@/js/components/pdf-export/test-utils'; +import PDFAudienceCardSection from './PDFAudienceCardSection'; + +describe( 'PDFAudienceCardSection', () => { + it( 'should fill its grid cell with a divider below', () => { + expect( + renderPDFStyle( + + ) + ).toMatchObject( { + flexGrow: 1, + borderBottomWidth: scalePDFValue( 1 ), + } ); + } ); + + it( 'should drop the divider when it is the last section', () => { + expect( + renderPDFStyle( + + ) + ).toMatchObject( { borderBottomWidth: 0 } ); + } ); + + it( 'should center the icon on the section content, like the dashboard tile', () => { + const [ content ] = renderPDFChildStyles( + + ); + + expect( content ).toMatchObject( { + flexDirection: 'row', + alignItems: 'center', + } ); + } ); +} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardSection.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardSection.tsx new file mode 100644 index 00000000000..a2b7e37dcd2 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCardSection.tsx @@ -0,0 +1,64 @@ +/** + * Section for the Your visitor groups PDF audience card. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { View } from '@react-pdf/renderer'; +import { FC } from 'react'; + +/** + * Internal dependencies + */ +import { PDFIcon } from '@/js/components/pdf-export/types'; +import { audienceCardStyles } from './pdfAudienceCardStyles'; + +interface PDFAudienceCardSectionProps { + /** The section's icon. */ + Icon: PDFIcon; + /** Whether the section is the last one in the card, which drops the divider. */ + isLast?: boolean; +} + +const PDFAudienceCardSection: FC< PDFAudienceCardSectionProps > = ( { + Icon, + isLast = false, + children, +} ) => { + return ( + + + + + + { children } + + + ); +}; + +export default PDFAudienceCardSection; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCitiesRow.test.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCitiesRow.test.tsx new file mode 100644 index 00000000000..281ce08d931 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCitiesRow.test.tsx @@ -0,0 +1,112 @@ +/** + * PDFAudienceCitiesRow tests. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import TestRenderer from 'react-test-renderer'; + +/** + * Internal dependencies + */ +import { renderJSON } from '@/js/components/pdf-export/test-utils'; +import PDFAudienceCitiesRow from './PDFAudienceCitiesRow'; + +/** + * Gets the hyphenation callback the given city name's text renders with. + * + * @since n.e.x.t + * + * @param {string} name The city name. + * @return {Function|undefined} The text's hyphenation callback, if any. + */ +function getCityNameHyphenationCallback( name: string ) { + const renderer = TestRenderer.create( + + ); + const [ text ] = renderer.root.findAllByProps( { children: name } ); + + return text.props.hyphenationCallback; +} + +describe( 'PDFAudienceCitiesRow', () => { + it( 'should render each top city with its name and percentage', () => { + const json = renderJSON( + + ); + + expect( json ).toContain( 'Cities with the most visitors' ); + expect( json ).toContain( 'Dublin' ); + expect( json ).toContain( '38.8%' ); + expect( json ).toContain( 'London' ); + expect( json ).toContain( 'New York' ); + } ); + + it( 'should show a no data message when there are no top cities', () => { + expect( + renderJSON( ) + ).toContain( 'No data to show yet' ); + } ); + + it( 'should truncate a long city name to one line with an ellipsis', () => { + const json = renderJSON( + + ); + + expect( json ).toContain( 'Municipal District of Bandon - Kinsale' ); + // `@react-pdf` wraps text by default, so the city name caps at one line + // with an ellipsis. + expect( json ).toContain( '"maxLines":1' ); + expect( json ).toContain( '"textOverflow":"ellipsis"' ); + } ); + + it( 'should let a city name with no space wrap between any two characters', () => { + const hyphenationCallback = + getCityNameHyphenationCallback( 'Vacoas-Phoenix' ); + + // `@react-pdf` breaks a line at an empty part without adding a hyphen. + expect( hyphenationCallback( 'Cork' ) ).toEqual( [ + 'C', + '', + 'o', + '', + 'r', + '', + 'k', + ] ); + } ); + + it( 'should keep a city name with a space wrapping only at its spaces', () => { + expect( + getCityNameHyphenationCallback( 'Beau Bassin-Rose Hill' ) + ).toBeUndefined(); + } ); +} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCitiesRow.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCitiesRow.tsx new file mode 100644 index 00000000000..27443bf3104 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceCitiesRow.tsx @@ -0,0 +1,118 @@ +/** + * Cities row for the Your visitor groups PDF audience card. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { View } from '@react-pdf/renderer'; +import type { Style } from '@react-pdf/stylesheet'; +import { FC } from 'react'; + +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import { PDFAudienceMetricIconCities } from '@/js/components/pdf-export/pdf-icons'; +import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; +import { PDF_TYPOGRAPHY } from '@/js/components/pdf-export/pdf-typography'; +import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-components/PDFTypography'; +import { numFmt } from '@/js/util'; +import type { AudienceTileTopCity } from './buildPDFAudienceCard'; +import PDFAudienceCardSection from './PDFAudienceCardSection'; +import { audienceCardStyles } from './pdfAudienceCardStyles'; +import PDFAudienceTileNoData from './PDFAudienceTileNoData'; +import { getTruncationHyphenationCallback } from './pdfTextTruncation'; + +const styles = createPDFStyles( { + citiesRow: { + flexDirection: 'row', + columnGap: 10, + marginTop: 12, + }, + city: { + flex: 1, + overflow: 'hidden', + }, + cityName: { + maxLines: 1, + textOverflow: 'ellipsis', + }, + cityPercentage: { + marginTop: 2, + }, +} ); + +// Leaves room for the ellipsis. +const cityNameStyle: Style[] = [ + styles.cityName, + { paddingRight: PDF_TYPOGRAPHY.title.small.fontSize }, +]; + +interface PDFAudienceCitiesRowProps { + /** Up to three top cities. */ + topCities: AudienceTileTopCity[]; +} + +const PDFAudienceCitiesRow: FC< PDFAudienceCitiesRowProps > = ( { + topCities, +} ) => { + return ( + + + + { __( 'Cities with the most visitors', 'google-site-kit' ) } + + + { ! topCities.length && } + { topCities.map( ( city ) => ( + + + { city.name } + + + { numFmt( city.percentage, { + style: 'percent', + maximumFractionDigits: 1, + } ) } + + + ) ) } + + + + ); +}; + +export default PDFAudienceCitiesRow; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceMetricRow.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceMetricRow.tsx index 264ac82c405..be17f5ba07f 100644 --- a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceMetricRow.tsx +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceMetricRow.tsx @@ -1,7 +1,7 @@ /** * Metric row for the Your visitor groups PDF audience card. * - * `PDFYourVisitorGroupsTile` renders one row per audience metric. + * `PDFYourVisitorGroups` renders one row per audience metric. * * Site Kit by Google, Copyright 2026 Google LLC * @@ -35,25 +35,10 @@ import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-component import { PDFChangeType, PDFIcon } from '@/js/components/pdf-export/types'; import { calculateChange, numFmt } from '@/js/util'; import type { AudienceTileMetric } from './buildPDFAudienceCard'; +import PDFAudienceCardSection from './PDFAudienceCardSection'; +import { audienceCardStyles } from './pdfAudienceCardStyles'; const styles = createPDFStyles( { - // Each metric row is inset from the card edges and draws a divider below it. - row: { - flexDirection: 'row', - alignItems: 'center', - marginHorizontal: 24, - paddingVertical: 12, - borderBottomWidth: 1, - borderBottomColor: PDF_COLORS.SURFACES_SURFACE_1, - }, - iconColumn: { - width: 52, - alignItems: 'center', - marginRight: 4, - }, - body: { - flex: 1, - }, metricValue: { lineHeight: 32 / 28, }, @@ -114,11 +99,8 @@ const PDFAudienceMetricRow: FC< PDFAudienceMetricRowProps > = ( { const chip = getChangeChip( metric ); return ( - - - - - + + = ( { changeType={ chip.changeType } /> ) } - + ); }; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTileNoData.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTileNoData.tsx new file mode 100644 index 00000000000..f0e4d331bd4 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTileNoData.tsx @@ -0,0 +1,42 @@ +/** + * No data message for the Your visitor groups PDF audience card. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { FC } from 'react'; + +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-components/PDFTypography'; + +const PDFAudienceTileNoData: FC = () => { + return ( + + { __( 'No data to show yet', 'google-site-kit' ) } + + ); +}; + +export default PDFAudienceTileNoData; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTopContentRow.test.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTopContentRow.test.tsx new file mode 100644 index 00000000000..ac175353bd1 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTopContentRow.test.tsx @@ -0,0 +1,207 @@ +/** + * PDFAudienceTopContentRow tests. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { ComponentProps } from 'react'; +import TestRenderer from 'react-test-renderer'; + +/** + * Internal dependencies + */ +import { scalePDFValue } from '@/js/components/pdf-export/pdf-scale'; +import PDFLink from '@/js/components/pdf-export/shared-react-pdf-components/PDFLink'; +import { + flattenPDFStyle, + renderJSON, + renderPDFStyle, +} from '@/js/components/pdf-export/test-utils'; +import PDFAudienceTopContentRow from './PDFAudienceTopContentRow'; +import { splitIntoCharacters } from './pdfTextTruncation'; + +const TOP_CONTENT = [ + { + title: 'First post title', + pageviews: 847, + serviceURL: 'https://example.com/analytics-report/first-post', + }, + { + title: 'Second post title', + pageviews: 596, + serviceURL: 'https://example.com/analytics-report/second-post', + }, +]; + +/** + * Builds a `PDFAudienceTopContentRow` element with default props. + * + * @since n.e.x.t + * + * @param {Object} props Props that override the defaults. + * @return {Element} The element. + */ +function buildRow( + props: Partial< ComponentProps< typeof PDFAudienceTopContentRow > > = {} +) { + return ( + + ); +} + +/** + * Finds every node in a rendered tree that matches the predicate, in render + * order. + * + * @since n.e.x.t + * + * @param {Object} node The rendered tree, or a node within it. + * @param {Function} predicate Whether a node matches. + * @return {Array.} The matching nodes. + */ +function findNodes( + node: TestRenderer.ReactTestRendererJSON, + predicate: ( node: TestRenderer.ReactTestRendererJSON ) => boolean +): TestRenderer.ReactTestRendererJSON[] { + const children = ( node.children ?? [] ).filter( + ( child ): child is TestRenderer.ReactTestRendererJSON => + typeof child === 'object' + ); + + return [ + ...( predicate( node ) ? [ node ] : [] ), + ...children.flatMap( ( child ) => findNodes( child, predicate ) ), + ]; +} + +describe( 'PDFAudienceTopContentRow', () => { + it( 'should render each top content row with its title and pageviews', () => { + const json = renderJSON( buildRow() ); + + expect( json ).toContain( 'Top content by pageviews' ); + expect( json ).toContain( 'First post title' ); + expect( json ).toContain( '847' ); + expect( json ).toContain( 'Second post title' ); + expect( json ).toContain( '596' ); + } ); + + it( 'should show a no data message when there is no top content', () => { + expect( renderJSON( buildRow( { topContent: [] } ) ) ).toContain( + 'No data to show yet' + ); + } ); + + it( 'should show the Partial data badge only when the top content is in a partial data state', () => { + expect( + renderJSON( buildRow( { isTopContentPartialData: true } ) ) + ).toContain( 'Partial data' ); + + expect( renderJSON( buildRow() ) ).not.toContain( 'Partial data' ); + } ); + + it( 'should reserve the Partial data badge height on the title without a badge', () => { + expect( renderJSON( buildRow() ) ).toContain( + `"minHeight":${ scalePDFValue( 28 ) }` + ); + } ); + + it( 'should keep the title row free of a gap before the Partial data badge', () => { + const tree = TestRenderer.create( + buildRow( { isTopContentPartialData: true } ) + ).toJSON() as TestRenderer.ReactTestRendererJSON; + const [ titleRow ] = findNodes( + tree, + ( node ) => 'minHeight' in flattenPDFStyle( node.props.style ) + ); + const [ title ] = + titleRow.children as TestRenderer.ReactTestRendererJSON[]; + + expect( flattenPDFStyle( titleRow.props.style ) ).not.toHaveProperty( + 'columnGap' + ); + expect( flattenPDFStyle( title.props.style ) ).toMatchObject( { + flex: 1, + } ); + } ); + + it( 'should fill its grid cell with no divider below', () => { + expect( renderPDFStyle( buildRow() ) ).toMatchObject( { + flexGrow: 1, + borderBottomWidth: 0, + } ); + } ); + + it( 'should link each top content title to its Analytics report', () => { + const json = renderJSON( buildRow() ); + + expect( json ).toContain( TOP_CONTENT[ 0 ].serviceURL ); + expect( json ).toContain( TOP_CONTENT[ 1 ].serviceURL ); + // The `Link` primitive from `@react-pdf` renders as `pdf-link` under + // the test mock. So a `pdf-link` in the tree means the title rendered + // as a link, not as text that happens to hold the URL. + expect( json ).toContain( 'pdf-link' ); + } ); + + it( 'should render each top content title as plain text when a row has no link', () => { + // An empty `serviceURL` gives every row an empty link, so each title + // renders as plain text. + const json = renderJSON( + buildRow( { + topContent: TOP_CONTENT.map( ( content ) => ( { + ...content, + serviceURL: '', + } ) ), + } ) + ); + + expect( json ).toContain( 'First post title' ); + expect( json ).toContain( 'Second post title' ); + // No `pdf-link` in the tree means every title rendered as plain text. + expect( json ).not.toContain( 'pdf-link' ); + } ); + + it( 'should let a linked title with no spaces break between characters', () => { + const pagePath = '/2024/05/a-very-long-slug-with-no-spaces/'; + const renderer = TestRenderer.create( + buildRow( { + topContent: [ + { + title: pagePath, + pageviews: 12, + serviceURL: 'https://example.com/analytics-report/slug', + }, + TOP_CONTENT[ 0 ], + ], + } ) + ); + const [ pathLink, titleLink ] = renderer.root.findAllByType( PDFLink ); + + expect( pathLink.props.hyphenationCallback ).toBe( + splitIntoCharacters + ); + expect( pathLink.props.href ).toBe( + 'https://example.com/analytics-report/slug' + ); + expect( titleLink.props.hyphenationCallback ).toBeUndefined(); + expect( titleLink.props.href ).toBe( TOP_CONTENT[ 0 ].serviceURL ); + } ); +} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTopContentRow.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTopContentRow.tsx new file mode 100644 index 00000000000..79c2e7ed787 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFAudienceTopContentRow.tsx @@ -0,0 +1,128 @@ +/** + * Top content row for the Your visitor groups PDF audience card. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * External dependencies + */ +import { View } from '@react-pdf/renderer'; +import { FC } from 'react'; + +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import { PDFAudienceMetricIconTopContent } from '@/js/components/pdf-export/pdf-icons'; +import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; +import PDFLink from '@/js/components/pdf-export/shared-react-pdf-components/PDFLink'; +import PDFPartialDataBadge, { + PDF_PARTIAL_DATA_BADGE_HEIGHT, +} from '@/js/components/pdf-export/shared-react-pdf-components/PDFPartialDataBadge'; +import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-components/PDFTypography'; +import { numFmt } from '@/js/util'; +import type { AudienceTileTopContent } from './buildPDFAudienceCard'; +import PDFAudienceCardSection from './PDFAudienceCardSection'; +import { audienceCardStyles } from './pdfAudienceCardStyles'; +import PDFAudienceTileNoData from './PDFAudienceTileNoData'; +import { getTruncationHyphenationCallback } from './pdfTextTruncation'; + +const styles = createPDFStyles( { + titleRow: { + minHeight: PDF_PARTIAL_DATA_BADGE_HEIGHT, + marginBottom: 10, + }, + contentRow: { + flexDirection: 'row', + justifyContent: 'space-between', + marginBottom: 6, + }, + // The title fills the row remainder before the pageviews count. + contentTitle: { + flex: 1, + marginRight: 30, + }, + // `@react-pdf` wraps text by default, so `maxLines` keeps it to one line. + contentTitleText: { + maxLines: 1, + textOverflow: 'ellipsis', + }, +} ); + +interface PDFAudienceTopContentRowProps { + /** Up to three top content pages. */ + topContent: AudienceTileTopContent[]; + /** Whether to show the Partial data badge on the Top content title. */ + isTopContentPartialData: boolean; +} + +const PDFAudienceTopContentRow: FC< PDFAudienceTopContentRowProps > = ( { + topContent, + isTopContentPartialData, +} ) => { + return ( + + + + + + { __( + 'Top content by pageviews', + 'google-site-kit' + ) } + + + { isTopContentPartialData && } + + { ! topContent.length && } + { /* Links to the Analytics report; `PDFLink` falls back to plain text. */ } + { topContent.map( ( content, index ) => ( + + + + { content.title } + + + + { numFmt( content.pageviews ) } + + + ) ) } + + + ); +}; + +export default PDFAudienceTopContentRow; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.test.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.test.tsx index 0c289e28a56..3499f1dfb70 100644 --- a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.test.tsx +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.test.tsx @@ -25,6 +25,7 @@ import TestRenderer from 'react-test-renderer'; /** * Internal dependencies */ +import { flattenPDFStyle } from '@/js/components/pdf-export/test-utils'; import { AudiencePartialDataFlags } from '@/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/getPDFData'; import PDFYourVisitorGroups from './PDFYourVisitorGroups'; @@ -93,7 +94,7 @@ function countCards( json: string ) { } describe( 'AudienceTilesWidget PDF', () => { - it( 'renders the heading and two cards for two audiences', () => { + it( 'should render the heading and two cards for two audiences', () => { const tree = renderWidget( { data: { audiences: [ @@ -110,7 +111,7 @@ describe( 'AudienceTilesWidget PDF', () => { expect( countCards( json ) ).toBe( 2 ); } ); - it( 'renders three cards for three audiences', () => { + it( 'should render three cards for three audiences', () => { const tree = renderWidget( { data: { audiences: [ @@ -124,7 +125,7 @@ describe( 'AudienceTilesWidget PDF', () => { expect( countCards( JSON.stringify( tree ) ) ).toBe( 3 ); } ); - it( "gives each card its own audience's partial-data flags", () => { + it( "should give each card its own audience's partial-data flags", () => { const tree = renderWidget( { data: { audiences: [ @@ -141,15 +142,75 @@ describe( 'AudienceTilesWidget PDF', () => { expect( json.split( 'Partial data' ).length - 1 ).toBe( 1 ); } ); - it( 'renders nothing for an empty audiences array', () => { + it( 'should render the four metric rows in order, with the pageviews share label', () => { + const json = JSON.stringify( + renderWidget( { + data: { + audiences: [ + buildAudience( 'New' ), + buildAudience( 'Returning' ), + ], + }, + } ) + ); + + expect( json ).toContain( '50% of total pageviews' ); + expect( json.indexOf( 'Visitors' ) ).toBeLessThan( + json.indexOf( 'Visits per visitor' ) + ); + expect( json.indexOf( 'Visits per visitor' ) ).toBeLessThan( + json.indexOf( 'Pages per visit' ) + ); + expect( json.indexOf( 'Pages per visit' ) ).toBeLessThan( + json.indexOf( '50% of total pageviews' ) + ); + } ); + + it( 'should render one row per card section, with a cell per card', () => { + const tree = renderWidget( { + data: { + audiences: [ + buildAudience( 'New' ), + buildAudience( 'Returning' ), + buildAudience( 'Purchasers' ), + ], + }, + } ) as TestRenderer.ReactTestRendererJSON; + const [ , grid ] = + tree.children as TestRenderer.ReactTestRendererJSON[]; + const [ cards, ...rows ] = + grid.children as TestRenderer.ReactTestRendererJSON[]; + + // One card surface per audience, behind the sections. + expect( flattenPDFStyle( cards.props.style ) ).toMatchObject( { + position: 'absolute', + } ); + expect( cards.children ).toHaveLength( 3 ); + + // The header, four metrics, cities, and top content sections, each a + // row with one cell per card. A row's cells stretch to the tallest one, + // as `alignItems` is left at its `stretch` default. + expect( rows ).toHaveLength( 7 ); + rows.forEach( ( row ) => { + expect( flattenPDFStyle( row.props.style ) ).toMatchObject( { + flexDirection: 'row', + } ); + expect( flattenPDFStyle( row.props.style ) ).not.toHaveProperty( + 'alignItems' + ); + expect( row.children ).toHaveLength( 3 ); + } ); + } ); + + it( 'should render nothing for an empty audiences array', () => { expect( renderWidget( { data: { audiences: [] } } ) ).toBeNull(); } ); - it( 'renders nothing when data is null', () => { + it( 'should render nothing when data is null', () => { expect( renderWidget( { data: null } ) ).toBeNull(); } ); - it( 'renders nothing when data is undefined', () => { + it( 'should render nothing when data is undefined', () => { expect( renderWidget( {} ) ).toBeNull(); } ); } ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.tsx index a65389b52ff..e3acb08d40a 100644 --- a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.tsx +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroups.tsx @@ -20,44 +20,158 @@ * External dependencies */ import { View } from '@react-pdf/renderer'; -import { FC } from 'react'; +import { FC, ReactElement } from 'react'; /** * WordPress dependencies */ -import { Fragment } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies */ +import { + PDFAudienceMetricIconPagesPerVisit, + PDFAudienceMetricIconPageviews, + PDFAudienceMetricIconVisitors, + PDFAudienceMetricIconVisitsPerVisitor, +} from '@/js/components/pdf-export/pdf-icons'; import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; +import PDFCard from '@/js/components/pdf-export/shared-react-pdf-components/PDFCard'; import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-components/PDFTypography'; import { PDFWidgetComponentProps } from '@/js/googlesitekit/widgets/types'; +import { numFmt } from '@/js/util'; +import type { AudienceTilePDFData } from './buildPDFAudienceCard'; import type { AudienceTilesPDFData } from './getPDFData'; -import PDFYourVisitorGroupsTile from './PDFYourVisitorGroupsTile'; +import PDFAudienceCardHeader from './PDFAudienceCardHeader'; +import PDFAudienceCitiesRow from './PDFAudienceCitiesRow'; +import PDFAudienceMetricRow from './PDFAudienceMetricRow'; +import PDFAudienceTopContentRow from './PDFAudienceTopContentRow'; const styles = createPDFStyles( { heading: { marginBottom: 15, }, - row: { + // One row per card section, sized to its tallest cell; the card + // surfaces sit in a layer behind. + grid: { + position: 'relative', + }, + columns: { flexDirection: 'row', - // Stretch every card to the tallest card's height, for equal-height - // columns. - alignItems: 'stretch', + columnGap: 24, }, - card: { - // Equal-width columns: every card grows and shrinks from a zero basis. + column: { flexGrow: 1, flexShrink: 1, flexBasis: 0, }, - cardGap: { - width: 24, + cardLayer: { + position: 'absolute', + top: 0, + right: 0, + bottom: 0, + left: 0, + }, + card: { + paddingVertical: 0, + paddingHorizontal: 0, }, } ); +interface CardSection { + /** A key unique among the sections. */ + key: string; + /** Renders the section for one audience's card. */ + render: ( audience: AudienceTilePDFData ) => ReactElement; +} + +/** + * The sections of an audience card, top to bottom. + * + * @since n.e.x.t + */ +const CARD_SECTIONS: CardSection[] = [ + { + key: 'header', + render: ( audience ) => ( + + ), + }, + { + key: 'visitors', + render: ( { metrics } ) => ( + + ), + }, + { + key: 'visits-per-visitor', + render: ( { metrics } ) => ( + + ), + }, + { + key: 'pages-per-visit', + render: ( { metrics } ) => ( + + ), + }, + { + key: 'pageviews', + render: ( { metrics } ) => ( + + ), + }, + { + key: 'cities', + render: ( audience ) => ( + + ), + }, + { + key: 'top-content', + render: ( audience ) => ( + + ), + }, +]; + const PDFYourVisitorGroups: FC< PDFWidgetComponentProps > = ( { data } ) => { const audienceData = data as AudienceTilesPDFData[ 'data' ] | undefined; const audiences = audienceData?.audiences || []; @@ -72,25 +186,26 @@ const PDFYourVisitorGroups: FC< PDFWidgetComponentProps > = ( { data } ) => { { __( 'Your visitor groups', 'google-site-kit' ) } - - { audiences.map( ( audience, index ) => ( - - { index > 0 && } - - - - + + + { audiences.map( ( audience ) => ( + + ) ) } + + { CARD_SECTIONS.map( ( section ) => ( + + { audiences.map( ( audience ) => ( + + { section.render( audience ) } + + ) ) } + ) ) } diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroupsTile.test.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroupsTile.test.tsx deleted file mode 100644 index 4f4b493c594..00000000000 --- a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroupsTile.test.tsx +++ /dev/null @@ -1,335 +0,0 @@ -/** - * PDFYourVisitorGroupsTile tests. - * - * Site Kit by Google, Copyright 2026 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * External dependencies - */ -import { ComponentProps } from 'react'; -import TestRenderer from 'react-test-renderer'; - -/** - * Internal dependencies - */ -import { scalePDFValue } from '@/js/components/pdf-export/pdf-scale'; -import { PDF_COLORS } from '@/js/components/pdf-export/pdf-theme'; -import { flattenPDFStyle } from '@/js/components/pdf-export/test-utils'; -import PDFYourVisitorGroupsTile from './PDFYourVisitorGroupsTile'; - -const METRICS = { - visitors: { current: 24200, previous: 22000 }, - visitsPerVisitor: { current: 3, previous: 3 }, - pagesPerVisit: { current: 2, previous: 2.5 }, - pageviews: { - current: 1565, - previous: 1500, - percentageOfTotalPageViews: 0.33, - }, -}; - -const TOP_CITIES = [ - { name: 'Dublin', percentage: 0.388 }, - { name: 'London', percentage: 0.126 }, - { name: 'New York', percentage: 0.094 }, -]; - -const TOP_CONTENT = [ - { - title: 'First post title', - pageviews: 847, - serviceURL: 'https://example.com/analytics-report/first-post', - }, - { - title: 'Second post title', - pageviews: 596, - serviceURL: 'https://example.com/analytics-report/second-post', - }, -]; - -/** - * Renders `PDFYourVisitorGroupsTile` with default props, as a JSON tree string. - * - * @since 1.184.0 - * - * @param props Props that override the defaults. - * @return The rendered tree serialized to a string. - */ -function renderTile( - props: Partial< ComponentProps< typeof PDFYourVisitorGroupsTile > > = {} -) { - const renderer = TestRenderer.create( - - ); - return JSON.stringify( renderer.toJSON() ); -} - -/** - * Counts the "Partial data" badges. - * - * @since 1.186.0 - * - * @param json The rendered PDF content, serialized to a string. - * @return The number of "Partial data" badges. - */ -function countPartialDataBadges( json: string ) { - return json.split( 'Partial data' ).length - 1; -} - -/** - * Finds the first row that directly holds the given title text. - * - * @since n.e.x.t - * - * @param node The rendered tree, or a node within it. - * @param title The title text the row holds. - * @return The row, or `undefined` when no row holds the title. - */ -function findTitleRow( - node: TestRenderer.ReactTestRendererJSON, - title: string -): TestRenderer.ReactTestRendererJSON | undefined { - const children = ( node.children ?? [] ).filter( - ( child ): child is TestRenderer.ReactTestRendererJSON => - typeof child === 'object' - ); - - if ( children.some( ( child ) => child.children?.includes( title ) ) ) { - return node; - } - - for ( const child of children ) { - const row = findTitleRow( child, title ); - - if ( row ) { - return row; - } - } - - return undefined; -} - -/** - * Finds the style of the row that holds the given title text. - * - * @since n.e.x.t - * - * @param json The rendered PDF content, serialized to a string. - * @param title The title text the row holds. - * @return The row's style, flattened to one object. - */ -function findTitleRowStyle( json: string, title: string ) { - const row = findTitleRow( JSON.parse( json ), title ); - - return flattenPDFStyle( row?.props.style ); -} - -describe( 'PDFYourVisitorGroupsTile', () => { - it( 'renders the audience name header', () => { - expect( renderTile() ).toContain( 'New visitors' ); - } ); - - it( 'renders the four metric tiles in order, with the pageviews share label', () => { - const json = renderTile(); - - expect( json ).toContain( 'Visitors' ); - expect( json ).toContain( 'Visits per visitor' ); - expect( json ).toContain( 'Pages per visit' ); - expect( json ).toContain( '33% of total pageviews' ); - - expect( json.indexOf( 'Visitors' ) ).toBeLessThan( - json.indexOf( 'Visits per visitor' ) - ); - expect( json.indexOf( 'Visits per visitor' ) ).toBeLessThan( - json.indexOf( 'Pages per visit' ) - ); - expect( json.indexOf( 'Pages per visit' ) ).toBeLessThan( - json.indexOf( '33% of total pageviews' ) - ); - } ); - - it( 'colors the delta chip green when rising and red when falling', () => { - const json = renderTile(); - - // Visitors rose from 22000 to 24200. - expect( json ).toContain( '+10%' ); - expect( json ).toContain( PDF_COLORS.GREEN_G_50 ); - - // Pages per visit fell from 2.5 to 2. - expect( json ).toContain( '-20%' ); - expect( json ).toContain( PDF_COLORS.UTILITY_ERROR_CONTAINER ); - } ); - - it( 'renders each top city with its name and percentage', () => { - const json = renderTile(); - - expect( json ).toContain( 'Cities with the most visitors' ); - expect( json ).toContain( 'Dublin' ); - expect( json ).toContain( '38.8%' ); - expect( json ).toContain( 'London' ); - expect( json ).toContain( 'New York' ); - } ); - - it( 'renders each top content row with its title and pageviews', () => { - const json = renderTile(); - - expect( json ).toContain( 'Top content by pageviews' ); - expect( json ).toContain( 'First post title' ); - expect( json ).toContain( '847' ); - expect( json ).toContain( 'Second post title' ); - expect( json ).toContain( '596' ); - } ); - - it( 'shows a Partial data badge only where its flag is set', () => { - // Neither flag: no badge in either place. - expect( countPartialDataBadges( renderTile() ) ).toBe( 0 ); - - // Header flag only: one badge beside the audience name. - expect( - countPartialDataBadges( - renderTile( { isAudiencePartialData: true } ) - ) - ).toBe( 1 ); - - // Top content flag only: one badge on the Top content title. - expect( - countPartialDataBadges( - renderTile( { isTopContentPartialData: true } ) - ) - ).toBe( 1 ); - - // Both flags: a badge in each place. - expect( - countPartialDataBadges( - renderTile( { - isAudiencePartialData: true, - isTopContentPartialData: true, - } ) - ) - ).toBe( 2 ); - } ); - - it( 'should reserve the badge height on the card header with or without a badge', () => { - // Header flag set: the badge shows beside the audience name. The row - // holding the height can't be the padded header, as `minHeight` - // includes padding there. - const headerRow = findTitleRowStyle( - renderTile( { isAudiencePartialData: true } ), - 'New visitors' - ); - - expect( headerRow ).toMatchObject( { minHeight: scalePDFValue( 28 ) } ); - expect( headerRow ).not.toHaveProperty( 'paddingVertical' ); - - // Header flag unset: no badge, but the header keeps its height. - expect( - findTitleRowStyle( - renderTile( { isAudiencePartialData: false } ), - 'New visitors' - ) - ).toMatchObject( { minHeight: scalePDFValue( 28 ) } ); - } ); - - it( 'should reserve the badge height on the Top content title row with or without a badge', () => { - // Top content flag set: the badge shows beside the title. - expect( - findTitleRowStyle( - renderTile( { isTopContentPartialData: true } ), - 'Top content by pageviews' - ) - ).toMatchObject( { minHeight: scalePDFValue( 28 ) } ); - - // Top content flag unset: no badge, but the row keeps its height. - expect( - findTitleRowStyle( - renderTile( { isTopContentPartialData: false } ), - 'Top content by pageviews' - ) - ).toMatchObject( { minHeight: scalePDFValue( 28 ) } ); - } ); - - it( 'truncates a long city name to one line with an ellipsis', () => { - const json = renderTile( { - topCities: [ - { - name: 'Municipal District of Bandon - Kinsale', - percentage: 0.5, - }, - ], - } ); - - expect( json ).toContain( 'Municipal District of Bandon - Kinsale' ); - // `@react-pdf` wraps text by default, so the city name caps at one line - // with an ellipsis. - expect( json ).toContain( '"maxLines":1' ); - expect( json ).toContain( '"textOverflow":"ellipsis"' ); - } ); - - it( 'links each top content title to its Analytics report', () => { - const json = renderTile(); - - expect( json ).toContain( TOP_CONTENT[ 0 ].serviceURL ); - expect( json ).toContain( TOP_CONTENT[ 1 ].serviceURL ); - // The `Link` primitive from `@react-pdf` renders as `pdf-link` under - // the test mock. So a `pdf-link` in the tree means the title rendered - // as a link, not as text that happens to hold the URL. - expect( json ).toContain( 'pdf-link' ); - } ); - - it( 'renders each top content title as plain text when a row has no link', () => { - // An empty `serviceURL` gives every row an empty link, so each title - // renders as plain text. - const json = renderTile( { - topContent: TOP_CONTENT.map( ( content ) => ( { - ...content, - serviceURL: '', - } ) ), - } ); - - expect( json ).toContain( 'First post title' ); - expect( json ).toContain( 'Second post title' ); - // No `pdf-link` in the tree means every title rendered as plain text. - expect( json ).not.toContain( 'pdf-link' ); - } ); - - it( "hides the delta chip when the change can't be calculated", () => { - // A zero previous value leaves no calculable change for any metric. - const json = renderTile( { - metrics: { - visitors: { current: 100, previous: 0 }, - visitsPerVisitor: { current: 3, previous: 0 }, - pagesPerVisit: { current: 2, previous: 0 }, - pageviews: { - current: 1565, - previous: 0, - percentageOfTotalPageViews: 0.33, - }, - }, - } ); - - // No calculable change means no chip in either color. - expect( json ).not.toContain( PDF_COLORS.GREEN_G_50 ); - expect( json ).not.toContain( PDF_COLORS.UTILITY_ERROR_CONTAINER ); - } ); -} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroupsTile.tsx b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroupsTile.tsx deleted file mode 100644 index 7d65c898778..00000000000 --- a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/PDFYourVisitorGroupsTile.tsx +++ /dev/null @@ -1,315 +0,0 @@ -/** - * Audience card for the Your visitor groups PDF widget. - * - * `PDFYourVisitorGroups` renders one card per configured GA4 audience. - * - * Site Kit by Google, Copyright 2026 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * External dependencies - */ -import { View } from '@react-pdf/renderer'; -import { FC } from 'react'; - -/** - * WordPress dependencies - */ -import { __, sprintf } from '@wordpress/i18n'; - -/** - * Internal dependencies - */ -import { - PDFAudienceMetricIconCities, - PDFAudienceMetricIconPagesPerVisit, - PDFAudienceMetricIconPageviews, - PDFAudienceMetricIconTopContent, - PDFAudienceMetricIconVisitors, - PDFAudienceMetricIconVisitsPerVisitor, -} from '@/js/components/pdf-export/pdf-icons'; -import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; -import { PDF_COLORS } from '@/js/components/pdf-export/pdf-theme'; -import PDFCard from '@/js/components/pdf-export/shared-react-pdf-components/PDFCard'; -import PDFLink from '@/js/components/pdf-export/shared-react-pdf-components/PDFLink'; -import PDFPartialDataBadge, { - PDF_PARTIAL_DATA_BADGE_HEIGHT, -} from '@/js/components/pdf-export/shared-react-pdf-components/PDFPartialDataBadge'; -import PDFTypography from '@/js/components/pdf-export/shared-react-pdf-components/PDFTypography'; -import { numFmt } from '@/js/util'; -import type { - AudienceTilePDFData, - AudienceTileTopCity, - AudienceTileTopContent, -} from './buildPDFAudienceCard'; -import PDFAudienceMetricRow from './PDFAudienceMetricRow'; - -const styles = createPDFStyles( { - // The dashboard tile gives the card no padding, so the header sets its own - // inset and its bottom divider reaches both card edges. - card: { - paddingVertical: 0, - paddingHorizontal: 0, - }, - // The header holds the audience name and a full-width bottom divider. The - // divider sits on the header's full-width box, so it meets both card edges - // while the name stays inset. - header: { - paddingVertical: 25, - paddingHorizontal: 25, - borderBottomWidth: 1, - borderBottomColor: PDF_COLORS.SURFACES_SURFACE_1, - }, - // A row that keeps a title on the left and its Partial data badge on the - // right, both vertically centered, like the dashboard tile. The row is at - // least the badge's height. - badgeRow: { - flexDirection: 'row', - alignItems: 'center', - justifyContent: 'space-between', - minHeight: PDF_PARTIAL_DATA_BADGE_HEIGHT, - }, - // Each section is inset from the card edges and draws a divider below it. - row: { - flexDirection: 'row', - alignItems: 'center', - marginHorizontal: 24, - paddingVertical: 12, - borderBottomWidth: 1, - borderBottomColor: PDF_COLORS.SURFACES_SURFACE_1, - }, - // The last row drops its divider, so the card ends on content. - lastRow: { - borderBottomWidth: 0, - }, - iconColumn: { - width: 52, - alignItems: 'center', - marginRight: 4, - }, - body: { - flex: 1, - }, - sectionTitle: { - color: PDF_COLORS.SURFACES_ON_SURFACE_VARIANT, - }, - topContentTitleRow: { - marginBottom: 10, - }, - citiesRow: { - flexDirection: 'row', - marginTop: 12, - }, - city: { - flex: 1, - }, - cityName: { - // A long city name truncates to one line with an ellipsis. `@react-pdf` - // wraps text by default, so `maxLines` caps it at one line. - maxLines: 1, - textOverflow: 'ellipsis', - }, - cityPercentage: { - marginTop: 2, - }, - contentRow: { - flexDirection: 'row', - justifyContent: 'space-between', - marginBottom: 6, - }, - // The title fills the row remainder before the pageviews count. - contentTitle: { - flex: 1, - marginRight: 30, - }, - // A long title truncates to one line with an ellipsis. `@react-pdf` - // wraps text by default, so `maxLines` caps it at one line. - contentTitleText: { - maxLines: 1, - textOverflow: 'ellipsis', - }, -} ); - -interface PDFYourVisitorGroupsTileProps { - /** The audience display name, shown in the card header. */ - audienceName: string; - /** The four period-over-period metrics. */ - metrics: AudienceTilePDFData[ 'metrics' ]; - /** Up to three top cities. */ - topCities: AudienceTileTopCity[]; - /** Up to three top content pages. */ - topContent: AudienceTileTopContent[]; - /** Whether to show the Partial data badge beside the audience name. */ - isAudiencePartialData: boolean; - /** Whether to show the Partial data badge on the Top content title. */ - isTopContentPartialData: boolean; -} - -const PDFYourVisitorGroupsTile: FC< PDFYourVisitorGroupsTileProps > = ( { - audienceName, - metrics, - topCities, - topContent, - isAudiencePartialData, - isTopContentPartialData, -} ) => { - const metricRows = [ - { - Icon: PDFAudienceMetricIconVisitors, - label: __( 'Visitors', 'google-site-kit' ), - value: numFmt( metrics.visitors.current ), - metric: metrics.visitors, - }, - { - Icon: PDFAudienceMetricIconVisitsPerVisitor, - label: __( 'Visits per visitor', 'google-site-kit' ), - value: numFmt( metrics.visitsPerVisitor.current ), - metric: metrics.visitsPerVisitor, - }, - { - Icon: PDFAudienceMetricIconPagesPerVisit, - label: __( 'Pages per visit', 'google-site-kit' ), - value: numFmt( metrics.pagesPerVisit.current, { - style: 'decimal', - maximumFractionDigits: 2, - } ), - metric: metrics.pagesPerVisit, - }, - { - Icon: PDFAudienceMetricIconPageviews, - label: sprintf( - /* translators: %s: a percentage value such as "33.3%". */ - __( '%s of total pageviews', 'google-site-kit' ), - numFmt( metrics.pageviews.percentageOfTotalPageViews, { - style: 'percent', - maximumFractionDigits: 1, - } ) - ), - value: numFmt( metrics.pageviews.current ), - metric: metrics.pageviews, - }, - ]; - - return ( - - - { /* `minHeight` includes padding, so it goes on an inner row. */ } - - - { audienceName } - - { isAudiencePartialData && } - - - - { metricRows.map( ( row ) => ( - - ) ) } - - - - - - - - { __( - 'Cities with the most visitors', - 'google-site-kit' - ) } - - - { topCities.map( ( city ) => ( - - - { city.name } - - - { numFmt( city.percentage, { - style: 'percent', - maximumFractionDigits: 1, - } ) } - - - ) ) } - - - - - - - - - - - - { __( - 'Top content by pageviews', - 'google-site-kit' - ) } - - { isTopContentPartialData && } - - { /* If no content exists, show a "no data" message. */ } - { ! topContent.length && ( - - { __( 'No data to show yet', 'google-site-kit' ) } - - ) } - { /* - * The page title links to its Analytics report, like the - * dashboard tile. When the page has no link, `PDFLink` - * renders the title as plain text instead of as a link. - */ } - { topContent.map( ( content ) => ( - - - - { content.title } - - - - { numFmt( content.pageviews ) } - - - ) ) } - - - - ); -}; - -export default PDFYourVisitorGroupsTile; diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfAudienceCardStyles.ts b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfAudienceCardStyles.ts new file mode 100644 index 00000000000..e96fff400bd --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfAudienceCardStyles.ts @@ -0,0 +1,61 @@ +/** + * Shared styles for the Your visitor groups PDF audience card. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Internal dependencies + */ +import { createPDFStyles } from '@/js/components/pdf-export/pdf-scale'; +import { PDF_COLORS } from '@/js/components/pdf-export/pdf-theme'; + +export const audienceCardStyles = createPDFStyles( { + // Fills its grid cell, so the dividers line up across the cards. + section: { + flexGrow: 1, + marginHorizontal: 24, + borderBottomWidth: 1, + borderBottomColor: PDF_COLORS.SURFACES_SURFACE_1, + }, + lastSection: { + borderBottomWidth: 0, + }, + sectionContent: { + flexDirection: 'row', + alignItems: 'center', + paddingVertical: 12, + }, + iconColumn: { + width: 52, + alignItems: 'center', + marginRight: 4, + }, + body: { + flex: 1, + }, + sectionTitle: { + color: PDF_COLORS.SURFACES_ON_SURFACE_VARIANT, + }, + badgeRow: { + flexDirection: 'row', + alignItems: 'center', + justifyContent: 'space-between', + }, + // Takes the width the badge leaves, so a long title wraps before it. + badgeRowTitle: { + flex: 1, + }, +} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfTextTruncation.test.ts b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfTextTruncation.test.ts new file mode 100644 index 00000000000..184e7602537 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfTextTruncation.test.ts @@ -0,0 +1,63 @@ +/** + * Text truncation helper tests for the Your visitor groups PDF widget. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Internal dependencies + */ +import { + getTruncationHyphenationCallback, + splitIntoCharacters, +} from './pdfTextTruncation'; + +describe( 'splitIntoCharacters', () => { + it( 'should split a word into its characters with an empty part between each pair', () => { + expect( splitIntoCharacters( 'Cork' ) ).toEqual( [ + 'C', + '', + 'o', + '', + 'r', + '', + 'k', + ] ); + } ); + + it( 'should return no parts for an empty word', () => { + expect( splitIntoCharacters( '' ) ).toEqual( [] ); + } ); +} ); + +describe( 'getTruncationHyphenationCallback', () => { + it( 'should return the character split for text without spaces', () => { + expect( getTruncationHyphenationCallback( 'Vacoas-Phoenix' ) ).toBe( + splitIntoCharacters + ); + } ); + + it( 'should return no callback for text with spaces', () => { + expect( + getTruncationHyphenationCallback( 'Beau Bassin-Rose Hill' ) + ).toBeUndefined(); + } ); + + it( 'should return the character split for empty text', () => { + expect( getTruncationHyphenationCallback( '' ) ).toBe( + splitIntoCharacters + ); + } ); +} ); diff --git a/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfTextTruncation.ts b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfTextTruncation.ts new file mode 100644 index 00000000000..97d0f632781 --- /dev/null +++ b/assets/js/modules/analytics-4/components/audience-segmentation/dashboard/AudienceTilesWidget/pdfTextTruncation.ts @@ -0,0 +1,48 @@ +/** + * Text truncation helpers for the Your visitor groups PDF widget. + * + * Site Kit by Google, Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Splits a word into its characters, which `@react-pdf` can break the line + * between without adding a hyphen, so the word can truncate with an ellipsis. + * + * @since n.e.x.t + * + * @param {string} word The word to split. + * @return {Array.} The characters, with an empty part between each pair. + */ +export function splitIntoCharacters( word: string ): string[] { + return Array.from( word ).flatMap( ( character, index ) => + index ? [ '', character ] : [ character ] + ); +} + +/** + * Gets the hyphenation callback that lets the text truncate with an ellipsis. + * Text with spaces gets none: it already breaks at its spaces, and splitting + * it would close them up. + * + * @since n.e.x.t + * + * @param {string} text The text to truncate. + * @return {Function|undefined} The callback, or `undefined` for text with spaces. + */ +export function getTruncationHyphenationCallback( + text: string +): typeof splitIntoCharacters | undefined { + return text.includes( ' ' ) ? undefined : splitIntoCharacters; +} diff --git a/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-desktop-linux.png b/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-desktop-linux.png index 4bfc13b3f70..25eb23d7db4 100644 Binary files a/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-desktop-linux.png and b/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-desktop-linux.png differ diff --git a/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-mobile-linux.png b/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-mobile-linux.png index 4bfc13b3f70..25eb23d7db4 100644 Binary files a/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-mobile-linux.png and b/tests/playwright/specs/pdf-generation/pdf-generation.spec.ts-snapshots/PDF-Generation-Analytics-sections-downloads-the-audience-Your-visitor-groups-widget-on-its-own-1-chrome-mobile-linux.png differ