From 3217b3becb6851c6642d62e3b416833783a648ee Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 17:26:35 +0800 Subject: [PATCH 1/6] refactor(premium-analytics): move the remaining widget labels onto the shared row --- .../wooa7s-1989-migrate-remaining-labels | 4 + .../components/chart-leaderboard/README.md | 10 ++- .../__tests__/leaderboard-row.test.tsx | 13 ++++ .../leaderboard-label.module.scss | 4 + .../chart-leaderboard/leaderboard-label.tsx | 14 +++- .../widgets/file-downloads/render.tsx | 23 ++---- .../widgets/file-downloads/style.module.css | 26 ------- .../widgets/search-terms/render.tsx | 11 ++- .../widgets/search-terms/style.module.css | 12 --- .../widgets/shares/render.tsx | 11 ++- .../widgets/shares/style.module.css | 12 --- .../premium-analytics/widgets/tags/render.tsx | 78 +++++++------------ .../widgets/tags/style.module.css | 35 --------- .../widgets/top-platforms/render.tsx | 10 ++- .../widgets/top-platforms/style.module.css | 4 - .../video-detail-embeds/style.module.css | 2 +- .../wooa7s-1989-migrate-remaining-labels | 4 + .../wooa7s-1989-migrate-remaining-labels | 4 + 18 files changed, 106 insertions(+), 171 deletions(-) create mode 100644 projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels create mode 100644 projects/plugins/jetpack/changelog/wooa7s-1989-migrate-remaining-labels create mode 100644 projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels diff --git a/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels new file mode 100644 index 000000000000..e78493d77075 --- /dev/null +++ b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Leaderboards: Match the remaining row labels to the other leaderboard widgets. diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md index 49be6a854378..e945f7be88a8 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md @@ -163,7 +163,7 @@ Pass `section` to `LeaderboardPostLabel` to open a named tab on the detail page, There is no `LeaderboardVideoLabel`, so a widget building a `videoLink` action takes that same window from `useWidgetNavigationSearch()` and passes it as `search`. -`LeaderboardRowMedia` provides five semantic media variants. The variant owns its size, +`LeaderboardRowMedia` provides six semantic media variants. The variant owns its size, fallback, and default alt-text policy: | Kind | Size | Missing or failed image behavior | @@ -172,8 +172,16 @@ fallback, and default alt-text policy: | `favicon` | 16 × 16px | Hidden; always decorative | | `flag` | 28px wide | Placeholder; proportional height | | `thumbnail` | 28 × 28px | Placeholder | +| `icon` | 20 × 20px | No image; takes a glyph | | `none` | No media | Renders text only | +`icon` takes a `@wordpress/icons` glyph rather than a URL, for rows whose media is a symbol +instead of a picture: + +```tsx +media: { kind: 'icon', icon: category }, +``` + Use `resolveLeaderboardRowAction` when raw data can contain both an external URL and children. It applies the shared precedence: drill-down for rows with children, external links for childless rows, and static content otherwise. diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx index aa1dc4ac9ac5..51278f3063ec 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx @@ -2,6 +2,7 @@ * External dependencies */ import { fireEvent, render, screen } from '@testing-library/react'; +import { category } from '@wordpress/icons'; /** * Internal dependencies */ @@ -66,6 +67,18 @@ describe( 'LeaderboardLabel', () => { expect( screen.getByText( 'Desktop' ) ).toBeInTheDocument(); expect( screen.queryByRole( 'img' ) ).not.toBeInTheDocument(); } ); + + it( 'renders a glyph for icon media without announcing it', () => { + const { container } = render( + + ); + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access -- the glyph is aria-hidden by design, so the DOM is the only place to assert it. + const glyphPath = container.querySelector( 'svg path' )?.getAttribute( 'd' ); + + expect( screen.getByText( 'Recipes' ) ).toBeInTheDocument(); + expect( glyphPath ).toBeTruthy(); + expect( screen.queryByRole( 'img' ) ).not.toBeInTheDocument(); + } ); } ); describe( 'resolveLeaderboardRowAction', () => { diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss index f9fa7aa9fcb3..9b09de2c89ad 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss @@ -93,3 +93,7 @@ height: var(--jpa-leaderboard-image-height); object-fit: var(--jpa-leaderboard-image-fit); } + +.icon { + flex: none; +} diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.tsx index 83a6051b56f1..70c73d73b9c4 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.tsx @@ -1,7 +1,7 @@ /** * External dependencies */ -import { Stack } from '@jetpack-premium-analytics/externals'; +import { Icon, Stack } from '@jetpack-premium-analytics/externals'; import { __, sprintf } from '@wordpress/i18n'; import clsx from 'clsx'; import { useState } from 'react'; @@ -9,12 +9,14 @@ import { useState } from 'react'; * Internal dependencies */ import styles from './leaderboard-label.module.scss'; +import type { ComponentProps } from 'react'; export type LeaderboardRowMedia = | { kind: 'avatar'; url?: string; name: string } | { kind: 'favicon'; url?: string } | { kind: 'flag'; url?: string; country: string } | { kind: 'thumbnail'; url?: string; alt: string } + | { kind: 'icon'; icon: ComponentProps< typeof Icon >[ 'icon' ] } | { kind: 'none' }; export type LeaderboardLabelProps = { @@ -30,7 +32,9 @@ export type LeaderboardLabelProps = { const DEFAULT_IMAGE_URL = 'data:image/svg+xml;utf8,'; -function getMediaDetails( media: Exclude< LeaderboardRowMedia, { kind: 'none' } > ) { +function getMediaDetails( + media: Exclude< LeaderboardRowMedia, { kind: 'none' } | { kind: 'icon' } > +) { switch ( media.kind ) { case 'avatar': return { @@ -82,7 +86,8 @@ export function LeaderboardLabel( { decorativeMedia = false, }: LeaderboardLabelProps ) { const [ failedImageUrl, setFailedImageUrl ] = useState< string >(); - const mediaDetails = media.kind === 'none' ? null : getMediaDetails( media ); + const mediaDetails = + media.kind === 'none' || media.kind === 'icon' ? null : getMediaDetails( media ); const shouldRenderImage = mediaDetails && ( mediaDetails.fallback === 'placeholder' || Boolean( mediaDetails.url ) ) && @@ -95,6 +100,9 @@ export function LeaderboardLabel( { align="center" className={ clsx( styles.container, mediaDetails?.className ) } > + { media.kind === 'icon' && ( + + ) } { shouldRenderImage && ( - { row.label } - - ) : ( - - { row.label } - + label: ( + ), currentValue: row.value, currentShare: sharePercentage( row.value, maxValue ), diff --git a/projects/packages/premium-analytics/widgets/file-downloads/style.module.css b/projects/packages/premium-analytics/widgets/file-downloads/style.module.css index 79af92d7a772..5a943c435006 100644 --- a/projects/packages/premium-analytics/widgets/file-downloads/style.module.css +++ b/projects/packages/premium-analytics/widgets/file-downloads/style.module.css @@ -1,29 +1,3 @@ -.labelLink { - display: block; - - padding-block: var(--wpds-dimension-padding-md); - padding-inline-start: var(--wpds-dimension-padding-sm); - overflow: hidden; - color: inherit; - text-decoration: none; - text-overflow: ellipsis; - white-space: nowrap; -} - -.labelLink:hover, -.labelLink:focus { - text-decoration: underline; -} - -.labelText { - display: block; - padding-block: var(--wpds-dimension-padding-md); - padding-inline-start: var(--wpds-dimension-padding-sm); - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - .root { container-type: inline-size; display: flex; diff --git a/projects/packages/premium-analytics/widgets/search-terms/render.tsx b/projects/packages/premium-analytics/widgets/search-terms/render.tsx index 8848ce263ed6..57ff444c3e02 100644 --- a/projects/packages/premium-analytics/widgets/search-terms/render.tsx +++ b/projects/packages/premium-analytics/widgets/search-terms/render.tsx @@ -7,6 +7,7 @@ import { describeError, getCombinedPeriodMax, LeaderboardChart, + LeaderboardRow, LeaderboardSkeleton, ReportLink, WidgetFooter, @@ -20,7 +21,7 @@ import { import { search } from '@jetpack-premium-analytics/icons'; import { useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { Stack, Text } from '@jetpack-premium-analytics/externals'; +import { Stack } from '@jetpack-premium-analytics/externals'; /** * Internal dependencies */ @@ -56,9 +57,11 @@ function SearchTermsInner() { return { id: `${ index }-${ term.label }`, label: ( - - { term.label } - + ), currentValue: term.views, previousValue: previousViews, diff --git a/projects/packages/premium-analytics/widgets/search-terms/style.module.css b/projects/packages/premium-analytics/widgets/search-terms/style.module.css index 29ddc2e78e23..5a943c435006 100644 --- a/projects/packages/premium-analytics/widgets/search-terms/style.module.css +++ b/projects/packages/premium-analytics/widgets/search-terms/style.module.css @@ -13,15 +13,3 @@ flex: 1 1 0; min-height: 0; } - -.itemLabel { - padding: var(--wpds-dimension-padding-sm); - min-width: 0; - overflow: hidden; -} - -.itemLabelText { - overflow: hidden; - white-space: nowrap; - text-overflow: ellipsis; -} diff --git a/projects/packages/premium-analytics/widgets/shares/render.tsx b/projects/packages/premium-analytics/widgets/shares/render.tsx index bb08b0e170c3..c86aa1e5939c 100644 --- a/projects/packages/premium-analytics/widgets/shares/render.tsx +++ b/projects/packages/premium-analytics/widgets/shares/render.tsx @@ -3,6 +3,7 @@ */ import { LeaderboardChart, + LeaderboardRow, LeaderboardSkeleton, WIDGET_ROW_LIMIT, WidgetRoot, @@ -14,7 +15,7 @@ import { import { megaphone } from '@jetpack-premium-analytics/icons'; import { useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { Stack, Text } from '@jetpack-premium-analytics/externals'; +import { Stack } from '@jetpack-premium-analytics/externals'; /** * Internal dependencies */ @@ -41,9 +42,11 @@ function SharesInner() { return data.map( ( service, index ) => ( { id: `${ index }-${ service.service }`, label: ( - - { service.label } - + ), currentValue: service.value, currentShare: sharePercentage( service.value, maxValue ), diff --git a/projects/packages/premium-analytics/widgets/shares/style.module.css b/projects/packages/premium-analytics/widgets/shares/style.module.css index 29ddc2e78e23..5a943c435006 100644 --- a/projects/packages/premium-analytics/widgets/shares/style.module.css +++ b/projects/packages/premium-analytics/widgets/shares/style.module.css @@ -13,15 +13,3 @@ flex: 1 1 0; min-height: 0; } - -.itemLabel { - padding: var(--wpds-dimension-padding-sm); - min-width: 0; - overflow: hidden; -} - -.itemLabelText { - overflow: hidden; - white-space: nowrap; - text-overflow: ellipsis; -} diff --git a/projects/packages/premium-analytics/widgets/tags/render.tsx b/projects/packages/premium-analytics/widgets/tags/render.tsx index 365cc71000c6..1cce2011c84f 100644 --- a/projects/packages/premium-analytics/widgets/tags/render.tsx +++ b/projects/packages/premium-analytics/widgets/tags/render.tsx @@ -3,6 +3,7 @@ */ import { LeaderboardChart, + LeaderboardRow, LeaderboardSkeleton, ReportLink, WIDGET_ROW_LIMIT, @@ -10,6 +11,8 @@ import { WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, + resolveLeaderboardRowAction, safeHttpUrl, sharePercentage, useWidgetDrillDown, @@ -21,7 +24,7 @@ import { tag as tagIllustration } from '@jetpack-premium-analytics/icons'; import { useEffect, useMemo } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { category, tag as tagGlyph } from '@wordpress/icons'; -import { Icon, Link, Stack } from '@jetpack-premium-analytics/externals'; +import { Stack } from '@jetpack-premium-analytics/externals'; /** * Internal dependencies */ @@ -37,10 +40,6 @@ type TagsWidgetProps = WidgetRenderProps< TagsRenderAttributes >; // row is a tag. const rowGlyph = ( labelIcon: string ) => ( labelIcon === 'folder' ? category : tagGlyph ); -// Icon + label fields shared by the leaderboard rows and the drilled-in members; -// documented on TagChildView. -type TagLabelProps = Pick< TagChildView, 'labelIcon' | 'label' | 'link' >; - interface TagGroupMembersProps { /** * The selected group's individual tags/categories. @@ -48,36 +47,6 @@ interface TagGroupMembersProps { members: TagChildView[]; } -/** - * Icon + label for a single tag/category, shared by the leaderboard rows and the - * drilled-in group members. A member with an archive URL renders an external - * link; one without renders plain text. - */ -function TagLabel( { labelIcon, label, link }: TagLabelProps ) { - const href = safeHttpUrl( link ); - - return ( - <> - - { href ? ( - - { label } - - ) : ( - - { label } - - ) } - - ); -} - /** * Drilled-in view for a grouped row: the individual tags and categories that * share the selected group. The Stats endpoint reports only the group's @@ -87,9 +56,15 @@ function TagGroupMembers( { members }: TagGroupMembersProps ) { return ( { members.map( member => ( -
- -
+ ) ) }
); @@ -130,22 +105,25 @@ function TagsInner() { return { id: row.id, - label: ( - - - - ), currentValue: row.value, currentShare: sharePercentage( row.value, maxValue ), // Grouped rows have no single archive URL, so a click drills into // their members instead. Single tag/category rows link out directly. - ...( isGroup && { - onClick: () => selectGroup( row.label ), - ariaLabel: sprintf( - /* translators: %s is the grouped tags and categories label */ - __( 'View the tags and categories in %s', 'jetpack-premium-analytics-pkg' ), - row.label - ), + ...buildLeaderboardRow( { + label: row.label, + media: { kind: 'icon', icon: rowGlyph( row.labelIcon ) }, + action: resolveLeaderboardRowAction( { + href: safeHttpUrl( row.link ) ?? undefined, + hasChildren: isGroup, + drillDown: { + onClick: () => selectGroup( row.label ), + ariaLabel: sprintf( + /* translators: %s is the grouped tags and categories label */ + __( 'View the tags and categories in %s', 'jetpack-premium-analytics-pkg' ), + row.label + ), + }, + } ), } ), }; } ); diff --git a/projects/packages/premium-analytics/widgets/tags/style.module.css b/projects/packages/premium-analytics/widgets/tags/style.module.css index c5eef7758cbb..9b90cf0d95bd 100644 --- a/projects/packages/premium-analytics/widgets/tags/style.module.css +++ b/projects/packages/premium-analytics/widgets/tags/style.module.css @@ -14,32 +14,6 @@ min-height: 0; } -/* Icon + label row for the leaderboard. min-height keeps a stable 36px row for - both single tag/category rows and grouped (drill-down) rows. */ -.itemLabel { - gap: var(--wpds-dimension-padding-sm); - min-height: 36px; - min-width: 0; - padding-inline: var(--wpds-dimension-padding-sm); - overflow: hidden; -} - -/* Keep the category/tag glyph from shrinking when the label is long. */ -.itemIcon { - flex-shrink: 0; -} - -.itemLabelText { - overflow: hidden; - white-space: nowrap; - text-overflow: ellipsis; -} - -a.itemLabelText:hover, -a.itemLabelText:focus { - text-decoration: underline; -} - /* Drilled-in group members: a scrollable list of the individual tags/categories. The Stats endpoint reports no per-member views, so these rows are links only, without leaderboard bars. */ @@ -48,12 +22,3 @@ a.itemLabelText:focus { min-height: 0; overflow-y: auto; } - -.childRow { - display: flex; - align-items: center; - gap: var(--wpds-dimension-padding-sm); - min-height: 36px; - padding-inline: var(--wpds-dimension-padding-sm); - overflow: hidden; -} diff --git a/projects/packages/premium-analytics/widgets/top-platforms/render.tsx b/projects/packages/premium-analytics/widgets/top-platforms/render.tsx index ba68cfb1ca49..adc534b3d392 100644 --- a/projects/packages/premium-analytics/widgets/top-platforms/render.tsx +++ b/projects/packages/premium-analytics/widgets/top-platforms/render.tsx @@ -6,13 +6,13 @@ import { device } from '@jetpack-premium-analytics/icons'; * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { Stack, Text } from '@jetpack-premium-analytics/externals'; import { WIDGET_ROW_LIMIT, calculateDelta, describeError, getCombinedPeriodMax, LeaderboardChart, + LeaderboardRow, LeaderboardSkeleton, sharePercentage, WidgetRoot, @@ -67,9 +67,11 @@ function TopPlatformsInner( { platformDimension }: TopPlatformsInnerProps ) { return { id: `${ index }-${ item.key }`, label: ( - - { item.label } - + ), currentValue: item.views, currentShare: sharePercentage( item.views, maxViews ), diff --git a/projects/packages/premium-analytics/widgets/top-platforms/style.module.css b/projects/packages/premium-analytics/widgets/top-platforms/style.module.css index cd8e2c0a4822..5a943c435006 100644 --- a/projects/packages/premium-analytics/widgets/top-platforms/style.module.css +++ b/projects/packages/premium-analytics/widgets/top-platforms/style.module.css @@ -13,7 +13,3 @@ flex: 1 1 0; min-height: 0; } - -.itemLabel { - padding: var(--wpds-dimension-padding-sm); -} diff --git a/projects/packages/premium-analytics/widgets/video-detail-embeds/style.module.css b/projects/packages/premium-analytics/widgets/video-detail-embeds/style.module.css index 1046179fc116..1b5f03ec5b7b 100644 --- a/projects/packages/premium-analytics/widgets/video-detail-embeds/style.module.css +++ b/projects/packages/premium-analytics/widgets/video-detail-embeds/style.module.css @@ -42,7 +42,7 @@ display: block; min-block-size: 36px; padding-block: var(--wpds-dimension-padding-sm); - padding-inline: var(--wpds-dimension-padding-sm); + padding-inline: var(--wpds-dimension-padding-md); overflow: hidden; color: var(--wpds-color-foreground-interactive-brand); text-overflow: ellipsis; diff --git a/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-remaining-labels b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-remaining-labels new file mode 100644 index 000000000000..c70ef09eda71 --- /dev/null +++ b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-remaining-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Premium Analytics: Match the remaining leaderboard row labels to the other widgets. diff --git a/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels new file mode 100644 index 000000000000..e78493d77075 --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-remaining-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Leaderboards: Match the remaining row labels to the other leaderboard widgets. From ee5036dc8569d4195eb0c0e4a7f2dd06350b455c Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 18:07:10 +0800 Subject: [PATCH 2/6] docs(premium-analytics): narrow the custom-label note and trim the icon media docs Also builds the File downloads rows through `buildLeaderboardRow`, like every other migrated widget. --- projects/packages/premium-analytics/AGENTS.md | 6 +++--- .../src/components/chart-leaderboard/README.md | 7 +------ .../widgets/file-downloads/render.tsx | 14 ++++++-------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/projects/packages/premium-analytics/AGENTS.md b/projects/packages/premium-analytics/AGENTS.md index 3e4d7f351c0f..23e11012f88e 100644 --- a/projects/packages/premium-analytics/AGENTS.md +++ b/projects/packages/premium-analytics/AGENTS.md @@ -760,9 +760,9 @@ wire a handler in `routeStatsReport()` inside `register-report-mocks.ts`. See widgets. - View count format: `dataFormat={ { type: 'number', options: { useMultipliers: true, decimals: 0 } } }` - Leaderboard rows: prefer `LeaderboardRow` / `buildLeaderboardRow` / `LeaderboardPostLabel` over - a hand-written label — they carry the shared row box, and a copy drifts from it. A label that - must be custom should match `.row` in `chart-leaderboard/leaderboard-label.module.scss`; - several widgets predate that and have not been migrated yet. + a hand-written label — a copy drifts from the shared row box. `utm-insights` and + `video-detail-embeds` are the last two that still draw their own, and match `.row` in + `chart-leaderboard/leaderboard-label.module.scss` by hand. - Loading / error / empty state: render through `` (see "Loading / error / empty state" above), not `LeaderboardChart`'s `emptyStateText` or a hand-rolled `data.length === 0` branch. Empty uses a neutral glyph distinct from the error icon. diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md index e945f7be88a8..bdaa266a064c 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/README.md @@ -175,12 +175,7 @@ fallback, and default alt-text policy: | `icon` | 20 × 20px | No image; takes a glyph | | `none` | No media | Renders text only | -`icon` takes a `@wordpress/icons` glyph rather than a URL, for rows whose media is a symbol -instead of a picture: - -```tsx -media: { kind: 'icon', icon: category }, -``` +`icon` takes a `@wordpress/icons` glyph rather than a URL: `media: { kind: 'icon', icon: category }`. Use `resolveLeaderboardRowAction` when raw data can contain both an external URL and children. It applies the shared precedence: drill-down for rows with children, external links for diff --git a/projects/packages/premium-analytics/widgets/file-downloads/render.tsx b/projects/packages/premium-analytics/widgets/file-downloads/render.tsx index 176479cfa591..e97e1c1a3a3d 100644 --- a/projects/packages/premium-analytics/widgets/file-downloads/render.tsx +++ b/projects/packages/premium-analytics/widgets/file-downloads/render.tsx @@ -18,9 +18,9 @@ import { getCombinedPeriodMax, safeHttpUrl, LeaderboardChart, - LeaderboardRow, LeaderboardSkeleton, ReportLink, + buildLeaderboardRow, resolveLeaderboardRowAction, sharePercentage, WidgetFooter, @@ -78,13 +78,11 @@ function buildLeaderboardData( return { id: `${ index }-${ row.href ?? row.label }`, - label: ( - - ), + ...buildLeaderboardRow( { + label: row.label, + media: { kind: 'none' }, + action: resolveLeaderboardRowAction( { href: row.href, hasChildren: false } ), + } ), currentValue: row.value, currentShare: sharePercentage( row.value, maxValue ), previousValue, From cf20f0f7339ffb4ae594623d4dfe28f93a6d569f Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 18:12:10 +0800 Subject: [PATCH 3/6] refactor(premium-analytics): move the UTM value rows onto the shared leaderboard row The last leaderboard widget drawing its own label. Its box already matched the shared row, so only the markup changes; `LeaderboardLabel` sets the same font size the dropped `variant="body-sm"` did. --- projects/packages/premium-analytics/AGENTS.md | 6 +++--- .../widgets/utm-insights/render.tsx | 12 ++++++------ .../widgets/utm-insights/style.module.css | 16 ---------------- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/projects/packages/premium-analytics/AGENTS.md b/projects/packages/premium-analytics/AGENTS.md index 23e11012f88e..ecb9aa2a75c3 100644 --- a/projects/packages/premium-analytics/AGENTS.md +++ b/projects/packages/premium-analytics/AGENTS.md @@ -759,9 +759,9 @@ wire a handler in `routeStatsReport()` inside `register-report-mocks.ts`. See add a second in-widget ` }>` title for framed Stats widgets. - View count format: `dataFormat={ { type: 'number', options: { useMultipliers: true, decimals: 0 } } }` -- Leaderboard rows: prefer `LeaderboardRow` / `buildLeaderboardRow` / `LeaderboardPostLabel` over - a hand-written label — a copy drifts from the shared row box. `utm-insights` and - `video-detail-embeds` are the last two that still draw their own, and match `.row` in +- Leaderboard rows: build them with `LeaderboardRow` / `buildLeaderboardRow` / + `LeaderboardPostLabel` — a hand-written copy drifts from the shared row box. `video-detail-embeds` + is the one exception, a plain list rather than a leaderboard, and matches `.row` in `chart-leaderboard/leaderboard-label.module.scss` by hand. - Loading / error / empty state: render through `` (see "Loading / error / empty state" above), not `LeaderboardChart`'s `emptyStateText` or a hand-rolled `data.length === 0` diff --git a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx index 35fc257e10fd..d4e591c82669 100644 --- a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx +++ b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx @@ -3,7 +3,6 @@ */ import { useEffect, useMemo } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; -import { Text } from '@jetpack-premium-analytics/externals'; import { WIDGET_ROW_LIMIT, calculateDelta, @@ -12,6 +11,7 @@ import { LeaderboardChart, LeaderboardSkeleton, LeaderboardPostLabel, + LeaderboardRow, ReportLink, WidgetBackLink, WidgetFooter, @@ -139,11 +139,11 @@ function UtmInsightsInner( { utmDimension, showReportLink }: UtmInsightsInnerPro link={ postRow.href } /> ) : ( - - - { item.label } - - + ), currentValue: item.value, currentShare: sharePercentage( item.value, maxValue ), diff --git a/projects/packages/premium-analytics/widgets/utm-insights/style.module.css b/projects/packages/premium-analytics/widgets/utm-insights/style.module.css index 61d3a7f3cf76..d38deb672ae8 100644 --- a/projects/packages/premium-analytics/widgets/utm-insights/style.module.css +++ b/projects/packages/premium-analytics/widgets/utm-insights/style.module.css @@ -19,19 +19,3 @@ min-inline-size: 0; margin-block-end: 0; } - -/* Matches the shared leaderboard row so these sit level with the post rows, - which render through `LeaderboardPostLabel`. */ -.itemLabel { - display: flex; - align-items: center; - min-block-size: 36px; - min-inline-size: 0; - padding-inline: var(--wpds-dimension-padding-md); -} - -.itemLabelText { - overflow: hidden; - white-space: nowrap; - text-overflow: ellipsis; -} From 9e5487be56d2d2c3b0b4942cbc7275626bbed2dd Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 27 Aug 2026 11:36:12 +0800 Subject: [PATCH 4/6] refactor(premium-analytics): build every chart row through buildLeaderboardRow --- .../widgets/search-terms/render.tsx | 14 +++-- .../widgets/shares/render.tsx | 14 +++-- .../widgets/top-platforms/render.tsx | 14 +++-- .../widgets/utm-insights/render.tsx | 52 ++++++++++--------- 4 files changed, 46 insertions(+), 48 deletions(-) diff --git a/projects/packages/premium-analytics/widgets/search-terms/render.tsx b/projects/packages/premium-analytics/widgets/search-terms/render.tsx index 57ff444c3e02..0457f8f16495 100644 --- a/projects/packages/premium-analytics/widgets/search-terms/render.tsx +++ b/projects/packages/premium-analytics/widgets/search-terms/render.tsx @@ -7,12 +7,12 @@ import { describeError, getCombinedPeriodMax, LeaderboardChart, - LeaderboardRow, LeaderboardSkeleton, ReportLink, WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, sharePercentage, useWidgetRootContext, type LeaderboardChartData, @@ -56,13 +56,11 @@ function SearchTermsInner() { return { id: `${ index }-${ term.label }`, - label: ( - - ), + ...buildLeaderboardRow( { + label: term.label, + media: { kind: 'none' }, + action: { kind: 'static' }, + } ), currentValue: term.views, previousValue: previousViews, currentShare: sharePercentage( term.views, maxValue ), diff --git a/projects/packages/premium-analytics/widgets/shares/render.tsx b/projects/packages/premium-analytics/widgets/shares/render.tsx index c86aa1e5939c..a71ca043477c 100644 --- a/projects/packages/premium-analytics/widgets/shares/render.tsx +++ b/projects/packages/premium-analytics/widgets/shares/render.tsx @@ -3,11 +3,11 @@ */ import { LeaderboardChart, - LeaderboardRow, LeaderboardSkeleton, WIDGET_ROW_LIMIT, WidgetRoot, WidgetState, + buildLeaderboardRow, sharePercentage, type LeaderboardChartData, type ReportParamsFieldAttributes, @@ -41,13 +41,11 @@ function SharesInner() { return data.map( ( service, index ) => ( { id: `${ index }-${ service.service }`, - label: ( - - ), + ...buildLeaderboardRow( { + label: service.label, + media: { kind: 'none' }, + action: { kind: 'static' }, + } ), currentValue: service.value, currentShare: sharePercentage( service.value, maxValue ), } ) ); diff --git a/projects/packages/premium-analytics/widgets/top-platforms/render.tsx b/projects/packages/premium-analytics/widgets/top-platforms/render.tsx index adc534b3d392..4c511f770efc 100644 --- a/projects/packages/premium-analytics/widgets/top-platforms/render.tsx +++ b/projects/packages/premium-analytics/widgets/top-platforms/render.tsx @@ -12,8 +12,8 @@ import { describeError, getCombinedPeriodMax, LeaderboardChart, - LeaderboardRow, LeaderboardSkeleton, + buildLeaderboardRow, sharePercentage, WidgetRoot, WidgetState, @@ -66,13 +66,11 @@ function TopPlatformsInner( { platformDimension }: TopPlatformsInnerProps ) { return { id: `${ index }-${ item.key }`, - label: ( - - ), + ...buildLeaderboardRow( { + label: item.label, + media: { kind: 'none' }, + action: { kind: 'static' }, + } ), currentValue: item.views, currentShare: sharePercentage( item.views, maxViews ), previousValue, diff --git a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx index d4e591c82669..858a54bea5a2 100644 --- a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx +++ b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx @@ -11,12 +11,13 @@ import { LeaderboardChart, LeaderboardSkeleton, LeaderboardPostLabel, - LeaderboardRow, ReportLink, WidgetBackLink, WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, + resolveLeaderboardRowAction, sharePercentage, useWidgetDrillDown, useWidgetRootContext, @@ -129,22 +130,35 @@ function UtmInsightsInner( { utmDimension, showReportLink }: UtmInsightsInnerPro return activeData.map( ( item, index ) => { const previousValue = item.previousValue; const postRow = 'postId' in item ? item : null; + const hasChildren = ! isDrillDown && 'children' in item && Boolean( item.children?.length ); return { id: `${ index }-${ item.label }`, - label: postRow ? ( - - ) : ( - - ), + ...( postRow + ? { + label: ( + + ), + } + : buildLeaderboardRow( { + label: item.label, + media: { kind: 'none' }, + action: resolveLeaderboardRowAction( { + hasChildren, + drillDown: { + onClick: () => selectUtmLabel( item.label ), + ariaLabel: sprintf( + /* translators: %s is the UTM value label. */ + __( 'View posts for %s', 'jetpack-premium-analytics-pkg' ), + item.label + ), + }, + } ), + } ) ), currentValue: item.value, currentShare: sharePercentage( item.value, maxValue ), previousValue, @@ -156,16 +170,6 @@ function UtmInsightsInner( { utmDimension, showReportLink }: UtmInsightsInnerPro withComparison && previousValue !== undefined ? calculateDelta( item.value, previousValue ) : undefined, - ...( ! isDrillDown && - 'children' in item && - item.children?.length && { - onClick: () => selectUtmLabel( item.label ), - ariaLabel: sprintf( - /* translators: %s is the UTM value label. */ - __( 'View posts for %s', 'jetpack-premium-analytics-pkg' ), - item.label - ), - } ), }; } ); }, [ activeData, isDrillDown, selectUtmLabel, withComparison ] ); From eff56a3c5a26368da0da3f2e9a6bb44467514941 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 27 Aug 2026 11:36:22 +0800 Subject: [PATCH 5/6] docs(premium-analytics): correct the leaderboard row and video-detail-embeds notes --- projects/packages/premium-analytics/AGENTS.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/projects/packages/premium-analytics/AGENTS.md b/projects/packages/premium-analytics/AGENTS.md index ecb9aa2a75c3..5a8d7fc17c2f 100644 --- a/projects/packages/premium-analytics/AGENTS.md +++ b/projects/packages/premium-analytics/AGENTS.md @@ -759,10 +759,12 @@ wire a handler in `routeStatsReport()` inside `register-report-mocks.ts`. See add a second in-widget ` }>` title for framed Stats widgets. - View count format: `dataFormat={ { type: 'number', options: { useMultipliers: true, decimals: 0 } } }` -- Leaderboard rows: build them with `LeaderboardRow` / `buildLeaderboardRow` / - `LeaderboardPostLabel` — a hand-written copy drifts from the shared row box. `video-detail-embeds` - is the one exception, a plain list rather than a leaderboard, and matches `.row` in - `chart-leaderboard/leaderboard-label.module.scss` by hand. +- Leaderboard rows: spread `buildLeaderboardRow()` into the chart entry — it carries the + drill-down `onClick`/`ariaLabel` that a bare `` label silently drops. Use + `` directly only outside a chart, as `widgets/tags` does for its drilled-in + member list. A hand-written copy drifts from the shared row box. `video-detail-embeds` is the + one exception, a plain list rather than a leaderboard, and matches the shared row's 36px height + and `padding-inline` by hand — not the rest of `.row`. - Loading / error / empty state: render through `` (see "Loading / error / empty state" above), not `LeaderboardChart`'s `emptyStateText` or a hand-rolled `data.length === 0` branch. Empty uses a neutral glyph distinct from the error icon. From e65eddd238ea584f2ba971adcb6a204c73ee076b Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 27 Aug 2026 11:49:55 +0800 Subject: [PATCH 6/6] test(premium-analytics): name the leaderboard glyph instead of counting one --- .../__tests__/leaderboard-row.test.tsx | 31 ++++- .../widgets-shared-route-mock-part2.test.tsx | 1 + .../widgets/tags/__tests__/tags.test.tsx | 128 ++++++++++++++++++ 3 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 projects/packages/premium-analytics/widgets/tags/__tests__/tags.test.tsx diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx index 51278f3063ec..a5cf1c5abf5f 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test.tsx @@ -2,13 +2,13 @@ * External dependencies */ import { fireEvent, render, screen } from '@testing-library/react'; -import { category } from '@wordpress/icons'; +import { category, tag } from '@wordpress/icons'; /** * Internal dependencies */ import { LeaderboardLabel } from '../leaderboard-label'; import { buildLeaderboardRow, resolveLeaderboardRowAction } from '../leaderboard-row'; -import type { AnchorHTMLAttributes, ReactNode } from 'react'; +import type { AnchorHTMLAttributes, ReactElement, ReactNode } from 'react'; type MockRouteLinkProps = { to: string; @@ -46,6 +46,20 @@ jest.mock( '@wordpress/route', () => { }; } ); +function glyphPath( root: Element | null | undefined ) { + return root?.querySelector( 'path' )?.getAttribute( 'd' ); +} + +// `@wordpress/icons` exports elements, so naming a glyph means rendering that +// icon on its own and comparing the two paths. +function iconPath( icon: ReactElement ) { + const { container, unmount } = render( icon ); + const path = glyphPath( container ); + + unmount(); + return path; +} + describe( 'LeaderboardLabel', () => { it( 'renders media and text without adding row actions', () => { render( @@ -68,16 +82,19 @@ describe( 'LeaderboardLabel', () => { expect( screen.queryByRole( 'img' ) ).not.toBeInTheDocument(); } ); - it( 'renders a glyph for icon media without announcing it', () => { + it( 'renders the glyph it was handed, hidden from assistive technology', () => { const { container } = render( ); - // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access -- the glyph is aria-hidden by design, so the DOM is the only place to assert it. - const glyphPath = container.querySelector( 'svg path' )?.getAttribute( 'd' ); + // eslint-disable-next-line testing-library/no-container, testing-library/no-node-access -- the glyph is aria-hidden by design, so the DOM is the only place to reach it. + const glyph = container.querySelector( 'svg' ); expect( screen.getByText( 'Recipes' ) ).toBeInTheDocument(); - expect( glyphPath ).toBeTruthy(); - expect( screen.queryByRole( 'img' ) ).not.toBeInTheDocument(); + expect( glyph ).toHaveAttribute( 'aria-hidden', 'true' ); + // Naming the glyph rather than just counting one: an icon kind exists so a + // widget can pick between glyphs, so the wrong one has to fail here. + expect( glyphPath( glyph ) ).toBe( iconPath( category ) ); + expect( glyphPath( glyph ) ).not.toBe( iconPath( tag ) ); } ); } ); diff --git a/projects/packages/premium-analytics/tests/groups/widgets-shared-route-mock-part2.test.tsx b/projects/packages/premium-analytics/tests/groups/widgets-shared-route-mock-part2.test.tsx index 931dc53c6d1d..a1f78e8096f2 100644 --- a/projects/packages/premium-analytics/tests/groups/widgets-shared-route-mock-part2.test.tsx +++ b/projects/packages/premium-analytics/tests/groups/widgets-shared-route-mock-part2.test.tsx @@ -2,6 +2,7 @@ import '../../widgets/site-overview/__tests__/site-overview.test'; import '../../widgets/subscriber-highlights/__tests__/subscriber-highlights.test'; +import '../../widgets/tags/__tests__/tags.test'; import '../../widgets/video-detail-embeds/__tests__/video-detail-embeds.test'; import '../../widgets/videopress/__tests__/videopress.test'; import '../../widgets/wordads-highlights/__tests__/wordads-highlights.test'; diff --git a/projects/packages/premium-analytics/widgets/tags/__tests__/tags.test.tsx b/projects/packages/premium-analytics/widgets/tags/__tests__/tags.test.tsx new file mode 100644 index 000000000000..8cbf7a308ea0 --- /dev/null +++ b/projects/packages/premium-analytics/widgets/tags/__tests__/tags.test.tsx @@ -0,0 +1,128 @@ +/** + * External dependencies + */ +import { getDefaultQueryParams, queryClient } from '@jetpack-premium-analytics/data'; +import { fireEvent, render, screen } from '@testing-library/react'; +import apiFetch from '@wordpress/api-fetch'; +import { category, tag } from '@wordpress/icons'; +/** + * Internal dependencies + */ +import TagsWidget from '../render'; +import type { ReactElement } from 'react'; + +jest.mock( '@wordpress/api-fetch', () => jest.fn() ); + +jest.mock( '@wordpress/route', () => jest.requireActual( '../../test-utils' ).mockWordPressRoute ); + +const mockApiFetch = apiFetch as unknown as jest.Mock; + +// `type: 'category'` is the only value the sanitizer turns into the `folder` +// glyph key, so the fixture pairs one of each against a grouped row. +const TAGS_RESPONSE = { + date: '2026-06-22', + period: 'month', + tags: [ + { + tags: [ + { type: 'category', name: 'Recipes', link: 'https://example.com/category/recipes/' }, + ], + views: 1240, + }, + { + tags: [ { type: 'tag', name: 'vegan', link: 'https://example.com/tag/vegan/' } ], + views: 980, + }, + { + tags: [ + { type: 'category', name: 'Desserts', link: 'https://example.com/category/desserts/' }, + { type: 'tag', name: 'chocolate', link: 'https://example.com/tag/chocolate/' }, + ], + views: 760, + }, + ], +}; + +function glyphPath( root: Element | null | undefined ) { + return root?.querySelector( 'svg path' )?.getAttribute( 'd' ); +} + +// `@wordpress/icons` exports elements, so the only way to name the glyph a row +// drew is to compare it against that icon rendered on its own. +function iconPath( icon: ReactElement ) { + const { container, unmount } = render( icon ); + const path = glyphPath( container ); + + unmount(); + return path; +} + +// The label text sits next to the glyph inside the row's media wrapper. +function rowGlyphPath( label: string ) { + return glyphPath( screen.getByText( label ).parentElement ); +} + +// Queried through the label rather than the accessible name, which the design +// system extends with its own "opens in a new tab" copy. +function rowLink( label: string ) { + return screen.getByText( label ).closest( 'a' ); +} + +describe( 'TagsWidget', () => { + beforeEach( () => { + queryClient.clear(); + mockApiFetch.mockReset(); + mockApiFetch.mockResolvedValue( TAGS_RESPONSE ); + } ); + + it( 'draws the category glyph on a category row and the tag glyph on a tag row', async () => { + render( ); + + await expect( screen.findByText( 'Recipes' ) ).resolves.toBeInTheDocument(); + + const categoryPath = iconPath( category ); + const tagPath = iconPath( tag ); + + expect( categoryPath ).not.toBe( tagPath ); + expect( rowGlyphPath( 'Recipes' ) ).toBe( categoryPath ); + expect( rowGlyphPath( 'vegan' ) ).toBe( tagPath ); + } ); + + it( 'links a single tag row and wraps its glyph in the anchor', async () => { + render( ); + + await expect( screen.findByText( 'vegan' ) ).resolves.toBeInTheDocument(); + + const link = rowLink( 'vegan' ); + + expect( link ).toHaveAttribute( 'href', 'https://example.com/tag/vegan/' ); + expect( glyphPath( link ) ).toBe( iconPath( tag ) ); + } ); + + it( 'drills a grouped row into its members and back', async () => { + render( ); + + const groupButton = await screen.findByRole( 'button', { + name: /view the tags and categories in desserts, chocolate/i, + } ); + + // A grouped row has no combined archive URL, so it must not be a link. + expect( rowLink( 'Desserts, chocolate' ) ).toBeNull(); + + fireEvent.click( groupButton ); // eslint-disable-line testing-library/prefer-user-event -- @testing-library/user-event is not a direct dep of this package. + + // Members carry their own glyph and archive URL, unlike the group row. + await expect( screen.findByText( 'Desserts' ) ).resolves.toBeInTheDocument(); + expect( rowLink( 'Desserts' ) ).toHaveAttribute( + 'href', + 'https://example.com/category/desserts/' + ); + expect( rowGlyphPath( 'Desserts' ) ).toBe( iconPath( category ) ); + expect( rowGlyphPath( 'chocolate' ) ).toBe( iconPath( tag ) ); + + fireEvent.click( screen.getByRole( 'button', { name: /all tags & categories/i } ) ); // eslint-disable-line testing-library/prefer-user-event -- @testing-library/user-event is not a direct dep of this package. + + await expect( screen.findByText( 'Recipes' ) ).resolves.toBeInTheDocument(); + expect( screen.queryByText( 'chocolate' ) ).not.toBeInTheDocument(); + } ); +} );