From ab391e7d6160f6bf1dc39ec97a0e57122a3fe576 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 16:04:33 +0800 Subject: [PATCH 1/8] fix(premium-analytics): unify the shared leaderboard row height, radius, and hover --- projects/packages/premium-analytics/AGENTS.md | 10 +++--- .../wooa7s-1989-leaderboard-row-consolidation | 4 +++ .../components/chart-leaderboard/README.md | 18 ++-------- .../src/components/chart-leaderboard/index.ts | 1 - .../leaderboard-label.module.scss | 12 ++----- .../chart-leaderboard/leaderboard-row.tsx | 34 ++++--------------- .../widgets-toolkit/src/components/index.ts | 7 +--- .../leaderboard-post-label/index.ts | 6 +--- .../leaderboard-post-label.tsx | 17 +--------- .../src/hooks/use-chart-theme.ts | 2 +- .../packages/widgets-toolkit/src/index.ts | 2 -- .../widgets/utm-insights/render.tsx | 2 -- .../widgets/utm-insights/style.module.css | 16 +++------ .../wooa7s-1989-leaderboard-row-consolidation | 4 +++ .../wooa7s-1989-leaderboard-row-consolidation | 4 +++ 15 files changed, 35 insertions(+), 104 deletions(-) create mode 100644 projects/packages/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation create mode 100644 projects/plugins/jetpack/changelog/wooa7s-1989-leaderboard-row-consolidation create mode 100644 projects/plugins/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation diff --git a/projects/packages/premium-analytics/AGENTS.md b/projects/packages/premium-analytics/AGENTS.md index cad7ff3635a2..c55b03aa4476 100644 --- a/projects/packages/premium-analytics/AGENTS.md +++ b/projects/packages/premium-analytics/AGENTS.md @@ -660,7 +660,7 @@ area. Notes: - `isFetching` draws nothing — it only marks the widget `aria-busy`. A revalidation of unchanged params leaves the right numbers on screen, and blanking them reports a refresh nobody asked for (WOOA7S-1934). Nothing unmounts, so children keep their own state and keyboard focus. -- Every other branch *does* unmount the children, and a drill-down reaches the skeleton by +- Every other branch _does_ unmount the children, and a drill-down reaches the skeleton by definition (it changes the params). `` catches the focus that would otherwise fall to `` and parks it on its own root, so the next Tab continues from the widget instead of the top of the page. Widgets need do nothing for this, but drill-down rows must be real @@ -759,10 +759,10 @@ 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 row height: custom labels should produce a stable 36px row height. For the common - `` label case, `padding: var(--wpds-dimension-padding-sm)` is enough when the text - line-height plus vertical padding yields 36px. Use `min-height: 36px` when the label content - or typography does not naturally produce that height. +- 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 that box: `min-block-size: 36px` and + `padding-inline: var(--wpds-dimension-padding-md)`, with no block padding. - 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/changelog/wooa7s-1989-leaderboard-row-consolidation b/projects/packages/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation new file mode 100644 index 000000000000..7c2921355666 --- /dev/null +++ b/projects/packages/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Leaderboards: Give shared rows one row height, bar radius, and link hover. 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 05c72ecce185..4094c0b2151b 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 @@ -168,22 +168,8 @@ It applies the shared precedence: drill-down for rows with children, external li childless rows, and static content otherwise. Use `LeaderboardLabel` directly for media plus truncating text outside chart rows, such as a -DataViews table cell. It deliberately does not add the chart row's 36px minimum block size. - -### Row layout - -`LeaderboardRow` takes two optional layout props. `LeaderboardPostLabel` accepts both and -forwards them. - -| Prop | Default | Description | -| ----------- | ----------- | ----------------------------------------------------------------------------------------------------------- | -| `variant` | `'compact'` | `'compact'` is the standard 36px row. `'overlay'` drops that floor and takes its height from block padding. | -| `className` | - | Extra class on the row, for per-widget spacing. | - -Pick `variant` to match the sibling rows in the same widget, not the chart's `withOverlayLabel` -prop. That prop only tints the bar fill; it sets no row height. Use `'overlay'` where the -neighbouring rows also take their height from block padding, and `'compact'` where they come -from `buildLeaderboardRow`. +DataViews table cell. It deliberately does not add the chart row's 36px minimum block size or its +inline padding. ## Props diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/index.ts b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/index.ts index e49713c292ff..f93ecbe7a572 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/index.ts +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/index.ts @@ -19,5 +19,4 @@ export type { LeaderboardRowActionOptions, LeaderboardRowChartProps, LeaderboardRowProps, - LeaderboardRowVariant, } from './leaderboard-row'; 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 cc9db3fe17bf..5b09c0585dd6 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 @@ -61,7 +61,7 @@ min-block-size: 36px; min-inline-size: 0; max-inline-size: 100%; - padding-inline: var(--wpds-dimension-padding-sm); + padding-inline: var(--wpds-dimension-padding-md); overflow: hidden; color: inherit; text-decoration: none; @@ -72,17 +72,9 @@ gap: var(--wpds-dimension-gap-xs); } -/* Block padding sets the bar height when the label sits on top of the bar, so - the 36px floor is dropped. Doubled with the base class to beat it on - specificity rather than on stylesheet order. */ -.overlay.row, -.overlay.rowLink { - min-block-size: 0; - padding-block: var(--wpds-dimension-padding-md); -} - .rowLink:hover .label, .rowLink:focus .label { + color: var(--wpds-color-foreground-interactive-brand); text-decoration: underline; } diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx index 1d2afcfa0b1b..59f237556128 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx @@ -2,7 +2,6 @@ * External dependencies */ import { Link } from '@jetpack-premium-analytics/externals'; -import clsx from 'clsx'; /** * Internal dependencies */ @@ -34,13 +33,6 @@ export type LeaderboardRowAction = } | { kind: 'static' }; -/** - * How tall the row sits. `compact` is the standard 36px row. `overlay` suits - * leaderboards that draw the label on top of the bar, where block padding sets - * the bar height because the row has no image to size it. - */ -export type LeaderboardRowVariant = 'compact' | 'overlay'; - export type LeaderboardRowActionOptions = { /** An external destination used only when the row has no children. */ href?: string; @@ -57,10 +49,6 @@ export type LeaderboardRowProps = { media: LeaderboardRowMedia; /** The mutually exclusive action applied to this chart row. */ action: LeaderboardRowAction; - /** Row height. Defaults to `compact`. */ - variant?: LeaderboardRowVariant; - /** Extra class for the row, for per-widget spacing. */ - className?: string; }; export type LeaderboardRowChartProps = @@ -105,17 +93,7 @@ export function resolveLeaderboardRowAction( * * @return A single label element accepted by `LeaderboardEntry.label`. */ -export function LeaderboardRow( { - label, - media, - action, - variant = 'compact', - className, -}: LeaderboardRowProps ): ReactElement { - const variantClass = variant === 'overlay' ? styles.overlay : undefined; - const rowClassName = clsx( styles.row, variantClass, className ); - const linkClassName = clsx( styles.rowLink, variantClass, className ); - +export function LeaderboardRow( { label, media, action }: LeaderboardRowProps ): ReactElement { // Post rows carry no media, so the row chrome goes on the anchor itself. if ( action.kind === 'postLink' ) { return ( @@ -126,9 +104,9 @@ export function LeaderboardRow( { search={ action.search } title={ label } classNames={ { - internal: linkClassName, - external: linkClassName, - plain: rowClassName, + internal: styles.rowLink, + external: styles.rowLink, + plain: styles.row, text: styles.label, } } /> @@ -142,7 +120,7 @@ export function LeaderboardRow( { if ( action.kind === 'link' ) { return ( + { content } ); diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/index.ts b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/index.ts index 6df056fe3483..a5cd54ce9bff 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/index.ts +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/index.ts @@ -54,7 +54,6 @@ export { type LeaderboardRowChartProps, type LeaderboardRowMedia, type LeaderboardRowProps, - type LeaderboardRowVariant, } from './chart-leaderboard'; export { BarChart, @@ -86,11 +85,7 @@ export { WidgetFooter, type WidgetFooterProps } from './widget-footer'; export { ReportLink, type ReportLinkProps } from './report-link'; export { PostTitleLink, POST_URL_SEARCH_PARAM, type PostTitleLinkProps } from './post-title-link'; export { PostDetailLink, type PostDetailLinkProps } from './post-detail-link'; -export { - LeaderboardPostLabel, - type LeaderboardPostLabelProps, - type LeaderboardPostLabelVariant, -} from './leaderboard-post-label'; +export { LeaderboardPostLabel, type LeaderboardPostLabelProps } from './leaderboard-post-label'; export { PostHighlightCard, PostHighlightCardSkeleton, diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/index.ts b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/index.ts index 397012593a4c..33e8184b83d2 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/index.ts +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/index.ts @@ -1,5 +1 @@ -export { - LeaderboardPostLabel, - type LeaderboardPostLabelProps, - type LeaderboardPostLabelVariant, -} from './leaderboard-post-label'; +export { LeaderboardPostLabel, type LeaderboardPostLabelProps } from './leaderboard-post-label'; diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/leaderboard-post-label.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/leaderboard-post-label.tsx index d0e685e1ccdc..b7d9c507935b 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/leaderboard-post-label.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/leaderboard-post-label/leaderboard-post-label.tsx @@ -2,12 +2,9 @@ * Internal dependencies */ import { useWidgetNavigationSearch } from '../../hooks/use-widget-navigation-search'; -import { LeaderboardRow, type LeaderboardRowVariant } from '../chart-leaderboard/leaderboard-row'; +import { LeaderboardRow } from '../chart-leaderboard/leaderboard-row'; import type { ReactElement } from 'react'; -/** How tall the row sits. */ -export type LeaderboardPostLabelVariant = LeaderboardRowVariant; - export type LeaderboardPostLabelProps = { /** * Post or page ID. Rows carrying one link to the internal detail route. @@ -22,14 +19,6 @@ export type LeaderboardPostLabelProps = { * Optional detail-page tab to open, e.g. `email-opens`. */ section?: string; - /** - * Row height. Defaults to `compact`. - */ - variant?: LeaderboardPostLabelVariant; - /** - * Extra class for the row, for per-widget spacing. - */ - className?: string; }; /** @@ -42,8 +31,6 @@ export function LeaderboardPostLabel( { label, link, section, - variant = 'compact', - className, }: LeaderboardPostLabelProps ): ReactElement { const search = useWidgetNavigationSearch( section ); @@ -52,8 +39,6 @@ export function LeaderboardPostLabel( { label={ label } media={ { kind: 'none' } } action={ { kind: 'postLink', id, href: link, search } } - variant={ variant } - className={ className } /> ); } diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/hooks/use-chart-theme.ts b/projects/packages/premium-analytics/packages/widgets-toolkit/src/hooks/use-chart-theme.ts index 3f250dfc8ef8..cc5da87a28f6 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/hooks/use-chart-theme.ts +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/hooks/use-chart-theme.ts @@ -70,7 +70,7 @@ export function useChartTheme(): WooChartTheme { rowGap: 4, columnGap: 4, labelSpacing: 'xs', - barBorderRadius: 'var(--wpds-border-radius-md)', + barBorderRadius: 'var(--wpds-border-radius-lg)', deltaColors: [ 'var(--wpds-color-stroke-surface-error-strong)', 'var(--wpds-color-foreground-content-neutral-weak)', diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/index.ts b/projects/packages/premium-analytics/packages/widgets-toolkit/src/index.ts index 04dfe3010dbd..5c55391a8111 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/index.ts +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/index.ts @@ -51,7 +51,6 @@ export { type LeaderboardRowChartProps, type LeaderboardRowMedia, type LeaderboardRowProps, - type LeaderboardRowVariant, BarChart, BarChartSkeleton, type BarChartProps, @@ -85,7 +84,6 @@ export { type PostDetailLinkProps, LeaderboardPostLabel, type LeaderboardPostLabelProps, - type LeaderboardPostLabelVariant, PostHighlightCard, PostHighlightCardSkeleton, type PostHighlightCardMetric, diff --git a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx index db4126cbdaf2..98b2d5baa5e1 100644 --- a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx +++ b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx @@ -137,8 +137,6 @@ function UtmInsightsInner( { utmDimension, showReportLink }: UtmInsightsInnerPro id={ postRow.postId } label={ postRow.label } link={ postRow.href } - variant="overlay" - className={ styles.itemLabelInset } /> ) : ( 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 5859a2c01fff..61d3a7f3cf76 100644 --- a/projects/packages/premium-analytics/widgets/utm-insights/style.module.css +++ b/projects/packages/premium-analytics/widgets/utm-insights/style.module.css @@ -20,22 +20,14 @@ margin-block-end: 0; } -/* The UTM value rows. Post rows use `LeaderboardPostLabel` instead and take - only the inline-end inset below. */ +/* 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-block: var(--wpds-dimension-padding-md); - padding-inline-start: var(--wpds-dimension-padding-sm); - padding-inline-end: var(--wpds-dimension-padding-lg); -} - -/* Keeps post-row text short of the value column, like `.itemLabel`. Doubled to - outrank the shared row's own `padding-inline` on specificity, because the two - are single classes in different CSS modules and would tie on order. */ -.itemLabelInset.itemLabelInset { - padding-inline-end: var(--wpds-dimension-padding-lg); + padding-inline: var(--wpds-dimension-padding-md); } .itemLabelText { diff --git a/projects/plugins/jetpack/changelog/wooa7s-1989-leaderboard-row-consolidation b/projects/plugins/jetpack/changelog/wooa7s-1989-leaderboard-row-consolidation new file mode 100644 index 000000000000..d6e0fbbe7b0b --- /dev/null +++ b/projects/plugins/jetpack/changelog/wooa7s-1989-leaderboard-row-consolidation @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Premium Analytics: Give shared leaderboard rows one row height, bar radius, and link hover. diff --git a/projects/plugins/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation b/projects/plugins/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation new file mode 100644 index 000000000000..7c2921355666 --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/wooa7s-1989-leaderboard-row-consolidation @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Leaderboards: Give shared rows one row height, bar radius, and link hover. From 7cde3a3024bdcfbdc8d75453173d6d6a4cb5ee44 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 17:38:43 +0800 Subject: [PATCH 2/8] fix(premium-analytics): carry the row hover colour to the external-link arrow The design system renders the arrow as a sibling of the label, so a colour on .label never reached it and hover showed blue text beside a dark arrow. Move the colour to the anchor and leave the underline on the label. Also size the UTM value rows with body-sm so they match the post rows they drill into, and stop AGENTS.md restating the shared row's values. --- projects/packages/premium-analytics/AGENTS.md | 4 ++-- .../chart-leaderboard/leaderboard-label.module.scss | 8 +++++++- .../premium-analytics/widgets/utm-insights/render.tsx | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/projects/packages/premium-analytics/AGENTS.md b/projects/packages/premium-analytics/AGENTS.md index c55b03aa4476..3e4d7f351c0f 100644 --- a/projects/packages/premium-analytics/AGENTS.md +++ b/projects/packages/premium-analytics/AGENTS.md @@ -761,8 +761,8 @@ wire a handler in `routeStatsReport()` inside `register-report-mocks.ts`. See - 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 that box: `min-block-size: 36px` and - `padding-inline: var(--wpds-dimension-padding-md)`, with no block padding. + must be custom should match `.row` in `chart-leaderboard/leaderboard-label.module.scss`; + several widgets predate that and have not been migrated yet. - 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/leaderboard-label.module.scss b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-label.module.scss index 5b09c0585dd6..ca09fea316f8 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 @@ -72,9 +72,15 @@ gap: var(--wpds-dimension-gap-xs); } +/* The design system renders the external-link arrow as a sibling of the + label, so the colour has to go on the anchor to reach it. */ +.rowLink:hover, +.rowLink:focus { + color: var(--wpds-color-foreground-interactive-brand); +} + .rowLink:hover .label, .rowLink:focus .label { - color: var(--wpds-color-foreground-interactive-brand); text-decoration: underline; } diff --git a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx index 98b2d5baa5e1..35fc257e10fd 100644 --- a/projects/packages/premium-analytics/widgets/utm-insights/render.tsx +++ b/projects/packages/premium-analytics/widgets/utm-insights/render.tsx @@ -140,7 +140,9 @@ function UtmInsightsInner( { utmDimension, showReportLink }: UtmInsightsInnerPro /> ) : ( - { item.label } + + { item.label } + ), currentValue: item.value, From 8690a9db59c71e042958a6c88d61341f49bddbd8 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 16:37:23 +0800 Subject: [PATCH 3/8] refactor(premium-analytics): move Most viewed row labels onto the shared leaderboard row --- .../wooa7s-1989-migrate-custom-title-labels | 4 + .../widgets/top-posts/render.tsx | 74 +++++++++---------- .../widgets/top-posts/style.module.css | 52 ------------- .../wooa7s-1989-migrate-custom-title-labels | 4 + .../wooa7s-1989-migrate-custom-title-labels | 4 + 5 files changed, 47 insertions(+), 91 deletions(-) create mode 100644 projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels create mode 100644 projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels create mode 100644 projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels diff --git a/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels new file mode 100644 index 000000000000..f0a19a6a8f01 --- /dev/null +++ b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Most viewed: Match the leaderboard row labels to the other leaderboard widgets. diff --git a/projects/packages/premium-analytics/widgets/top-posts/render.tsx b/projects/packages/premium-analytics/widgets/top-posts/render.tsx index 41f3157c460c..a87aced995f3 100644 --- a/projects/packages/premium-analytics/widgets/top-posts/render.tsx +++ b/projects/packages/premium-analytics/widgets/top-posts/render.tsx @@ -12,7 +12,6 @@ import { pickReportDateParams } from '@jetpack-premium-analytics/routing'; import { LeaderboardChart, LeaderboardSkeleton, - PostTitleLink, ReportLink, RowsCsvDownloadButton, WIDGET_ROW_LIMIT, @@ -20,6 +19,7 @@ import { WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, calculateDelta, getCombinedPeriodMax, safeHttpUrl, @@ -29,10 +29,10 @@ import { useWidgetRootContext, type CsvColumn, type LeaderboardChartData, + type LeaderboardRowAction, type ReportParamsFieldAttributes, } from '@jetpack-premium-analytics/widgets-toolkit'; import { __, sprintf } from '@wordpress/i18n'; -import { Text } from '@jetpack-premium-analytics/externals'; import { useCallback, useEffect, useMemo } from 'react'; /** * Internal dependencies @@ -87,6 +87,32 @@ type TopPostsWidgetProps = WidgetRenderProps< TopPostsRenderAttributes >; const DATA_FORMAT = { type: 'number' as const, options: { useMultipliers: true, decimals: 0 } }; +/** + * Pick the one action a top-posts row exposes. Grouped rows become drill-down + * rows, which the chart turns into buttons, so they cannot carry an anchor. + */ +function resolveRowAction( + row: TopPostRow, + detailSearch: Record< string, unknown >, + onDrillDown?: ( row: TopPostRow ) => void +): LeaderboardRowAction { + if ( row.children?.length ) { + return onDrillDown + ? { + kind: 'drillDown', + onClick: () => onDrillDown( row ), + ariaLabel: sprintf( + /* translators: %s is an archive category label, e.g. "Searches". */ + __( 'View %s archive pages', 'jetpack-premium-analytics-pkg' ), + row.label + ), + } + : { kind: 'static' }; + } + + return { kind: 'postLink', id: row.postId, href: row.href, search: detailSearch }; +} + /** * Maps normalized top-posts rows onto the shape `LeaderboardChart` expects. * Current shares are computed relative to the most-viewed row so the overlay @@ -94,12 +120,11 @@ const DATA_FORMAT = { type: 'number' as const, options: { useMultipliers: true, * and per-row deltas are derived from each row's `previousValue`; otherwise * the comparison fields are zeroed. * - * Titles route through `PostTitleLink`: post/page rows navigate to the + * Titles use the shared leaderboard row: post/page rows navigate to the * internal detail page through the router, and rows without a post ID (the * Archives view) fall back to the public URL and take the external-link icon. * Rows with children instead become drill-down rows (per the widget - * drill-down convention they carry no anchors). The label fills its row so - * the leaderboard overlay bar gets its height from it. + * drill-down convention they carry no anchors). */ function buildLeaderboardData( rows: TopPostRow[], @@ -114,34 +139,14 @@ function buildLeaderboardData( return rows.map( ( row, index ) => { const previousValue = row.previousValue; - const hasChildren = !! row.children?.length; return { id: `${ index }-${ row.href ?? row.label }`, - label: ( - - { /* Rows inside a drill-down button cannot carry anchors. */ } - { hasChildren ? ( - - { row.label } - - ) : ( - - ) } - - ), + ...buildLeaderboardRow( { + label: row.label, + media: { kind: 'none' }, + action: resolveRowAction( row, detailSearch, onDrillDown ), + } ), currentValue: row.value, currentShare: sharePercentage( row.value, maxViews ), // Rows without a comparison-period match keep `undefined` so the chart @@ -155,15 +160,6 @@ function buildLeaderboardData( withComparison && previousValue !== undefined ? calculateDelta( row.value, previousValue ) : undefined, - ...( hasChildren && - onDrillDown && { - onClick: () => onDrillDown( row ), - ariaLabel: sprintf( - /* translators: %s is an archive category label, e.g. "Searches". */ - __( 'View %s archive pages', 'jetpack-premium-analytics-pkg' ), - row.label - ), - } ), }; } ); } diff --git a/projects/packages/premium-analytics/widgets/top-posts/style.module.css b/projects/packages/premium-analytics/widgets/top-posts/style.module.css index d724290d4fdd..210d65ca1d7c 100644 --- a/projects/packages/premium-analytics/widgets/top-posts/style.module.css +++ b/projects/packages/premium-analytics/widgets/top-posts/style.module.css @@ -14,55 +14,3 @@ flex: 1 1 0; min-height: 0; } - -/* Vertical padding sets the overlay bar height — the bar fills the row, - whose height is driven by this label (there is no image to size it). - The inline-start inset keeps the text off the bar's rounded left edge: - the chart applies it to its default `.label` in overlay mode, but a - custom label element bypasses that rule, so we mirror it here. */ -.labelRow { - display: flex; - align-items: center; - gap: var(--wpds-dimension-gap-xs); - min-width: 0; - padding-block: var(--wpds-dimension-padding-md); - padding-inline-start: var(--wpds-dimension-padding-sm); -} - -/* `min-width: 0` lets the flex item shrink below its content width so the - ellipsis applies; without it a long title pushes the trailing icon out. The - gap matches the leaderboard rows' `.rowLink`, so the outbound marker sits - at the same distance from the title in every widget. */ -.labelTitle, -.labelTitleLink, -.labelExternalLink { - min-width: 0; - display: inline-flex; - align-items: center; - gap: var(--wpds-dimension-gap-xs); -} - -/* The label text carries the ellipsis in every branch: `text-overflow` needs - an inline formatting context, which the flex containers above do not give - it. */ -.labelText { - min-width: 0; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - -.labelTitleLink, -.labelExternalLink { - color: inherit; - text-decoration: none; -} - -/* Underline only the title text, matching the leaderboard rows' hover rule — - the outbound marker stays clean instead of taking the underline with it. */ -.labelTitleLink:hover .labelText, -.labelTitleLink:focus .labelText, -.labelExternalLink:hover .labelText, -.labelExternalLink:focus .labelText { - text-decoration: underline; -} diff --git a/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels new file mode 100644 index 000000000000..02dbd524bf19 --- /dev/null +++ b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Premium Analytics: Match the Most viewed leaderboard row labels to the other leaderboard widgets. diff --git a/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels new file mode 100644 index 000000000000..f0a19a6a8f01 --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Most viewed: Match the leaderboard row labels to the other leaderboard widgets. From 724c7e03d98d85d20a24f43a6e47567f706d3076 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 16:44:47 +0800 Subject: [PATCH 4/8] refactor(premium-analytics): move Videos row labels onto the shared leaderboard row --- .../wooa7s-1989-migrate-custom-title-labels | 2 +- .../components/chart-leaderboard/README.md | 30 ++++++++++-- .../__tests__/leaderboard-row.test.tsx | 16 +++++++ .../chart-leaderboard/leaderboard-row.tsx | 30 ++++++++---- .../widgets/top-posts/render.tsx | 4 +- .../widgets/videopress/render.tsx | 39 +++++---------- .../widgets/videopress/style.module.css | 47 ------------------- .../wooa7s-1989-migrate-custom-title-labels | 2 +- .../wooa7s-1989-migrate-custom-title-labels | 2 +- 9 files changed, 80 insertions(+), 92 deletions(-) diff --git a/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels index f0a19a6a8f01..93492cf7ed3a 100644 --- a/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels +++ b/projects/packages/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels @@ -1,4 +1,4 @@ Significance: patch Type: changed -Most viewed: Match the leaderboard row labels to the other leaderboard widgets. +Leaderboards: Match the Most viewed and Videos 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 4094c0b2151b..46511cc9dedc 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 @@ -142,15 +142,37 @@ const row = { A `postLink` row carries no media, and never becomes a chart button: a chart row that is a button cannot nest an anchor. -Inside a widget, use `LeaderboardPostLabel` instead of building the action by hand. It reads the -report window from `WidgetRootContext` and passes it as `search`, so the detail page opens on -the range the row was read against: +Video rows use `videoLink`, which behaves the same way but delegates to `VideoTitleLink` so the +row reaches the video detail route rather than the post one. + +```tsx +const row = { + id: 'video-9', + ...buildLeaderboardRow( { + label: 'Launch teaser', + media: { kind: 'none' }, + action: { + kind: 'videoLink', + id: 9, + search: { from: '2026-03-01', to: '2026-03-10' }, + }, + } ), + currentValue: 100, + currentShare: 100, +}; +``` + +Inside a widget, use `LeaderboardPostLabel` instead of building a post action by hand. It reads +the report window from `WidgetRootContext` and passes it as `search`, so the detail page opens on +the range the row was read against. A widget that builds its own action should take the same +window from `useWidgetNavigationSearch()`: ```tsx ``` -Pass `section` to open a named tab on the detail page, such as `email-opens`. +Pass `section` to `LeaderboardPostLabel` to open a named tab on the detail page, such as +`email-opens`. `LeaderboardRowMedia` provides five semantic media variants. The variant owns its size, fallback, and default alt-text policy: 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 b7f1d04a851f..1fe7a67494fe 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 @@ -106,6 +106,22 @@ describe( 'buildLeaderboardRow', () => { expect( row ).not.toHaveProperty( 'onClick' ); } ); + it( 'keeps a video link out of the chart button props', () => { + const row = buildLeaderboardRow( { + label: 'Launch teaser', + media: { kind: 'none' }, + action: { kind: 'videoLink', href: 'https://example.com/launch-teaser/' }, + } ); + + render( row.label ); + + expect( screen.getByRole( 'link', { name: /Launch teaser/ } ) ).toHaveAttribute( + 'href', + 'https://example.com/launch-teaser/' + ); + expect( row ).not.toHaveProperty( 'onClick' ); + } ); + it( 'returns chart button props for a drill-down without nesting an action', () => { const onClick = jest.fn(); const row = buildLeaderboardRow( { diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx index 59f237556128..772f00b73b60 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx @@ -6,6 +6,7 @@ import { Link } from '@jetpack-premium-analytics/externals'; * Internal dependencies */ import { PostTitleLink } from '../post-title-link'; +import { VideoTitleLink } from '../video-title-link'; import { LeaderboardLabel, type LeaderboardRowMedia } from './leaderboard-label'; import styles from './leaderboard-label.module.scss'; import type { MouseEvent, ReactElement } from 'react'; @@ -22,6 +23,16 @@ export type LeaderboardRowAction = /** Search parameters for the detail route, such as the report window. */ search?: Record< string, unknown >; } + | { + /** A video with a detail page inside the dashboard. */ + kind: 'videoLink'; + /** Video ID. Rows carrying one link to the internal detail route. */ + id?: number | string; + /** Public URL. It becomes the link itself when there is no video ID. */ + href?: string | null; + /** Search parameters for the detail route, such as the report window. */ + search?: Record< string, unknown >; + } | { kind: 'drillDown'; onClick: ( event: MouseEvent< HTMLButtonElement > ) => void; @@ -85,19 +96,22 @@ export function resolveLeaderboardRowAction( /** * Render the shared leaderboard row chrome around a label. * - * Link actions own the anchor and its new-tab affordance. Post links delegate - * to `PostTitleLink`, which picks the internal detail route, the public URL, or - * plain text for the row. Drill-down actions stay non-interactive here because - * `LeaderboardChart` turns the whole row into a button; `buildLeaderboardRow` - * passes that action to the chart. + * Link actions own the anchor and its new-tab affordance. Post and video links + * delegate to `PostTitleLink` and `VideoTitleLink`, which pick the internal + * detail route, the public URL, or plain text for the row. Drill-down actions + * stay non-interactive here because `LeaderboardChart` turns the whole row into + * a button; `buildLeaderboardRow` passes that action to the chart. * * @return A single label element accepted by `LeaderboardEntry.label`. */ export function LeaderboardRow( { label, media, action }: LeaderboardRowProps ): ReactElement { - // Post rows carry no media, so the row chrome goes on the anchor itself. - if ( action.kind === 'postLink' ) { + // Title-link rows carry no media, so the row chrome goes on the title element + // itself, which is an anchor for the two linked branches and a span otherwise. + if ( action.kind === 'postLink' || action.kind === 'videoLink' ) { + const TitleLink = action.kind === 'postLink' ? PostTitleLink : VideoTitleLink; + return ( - toTopPostRows( comparisonRows?.rows ?? [] ), [ comparisonRows ] ); - const detailSearch = useMemo( () => pickReportDateParams( reportParams ), [ reportParams ] ); + const detailSearch = useWidgetNavigationSearch(); const withComparison = hasComparison; // Serialize whatever the leaderboard has loaded, mirroring the Jetpack Stats diff --git a/projects/packages/premium-analytics/widgets/videopress/render.tsx b/projects/packages/premium-analytics/widgets/videopress/render.tsx index 29ab9902f2cf..a13e37ab14a7 100644 --- a/projects/packages/premium-analytics/widgets/videopress/render.tsx +++ b/projects/packages/premium-analytics/widgets/videopress/render.tsx @@ -2,12 +2,11 @@ * External dependencies */ import { useStatsVideoPlays } from '@jetpack-premium-analytics/data'; -import { pickReportDateParams } from '@jetpack-premium-analytics/routing'; import { LeaderboardChart, + LeaderboardRow, LeaderboardSkeleton, ReportLink, - VideoTitleLink, WIDGET_ROW_LIMIT, WidgetFooter, WidgetRoot, @@ -15,6 +14,7 @@ import { calculateDelta, getCombinedPeriodMax, sharePercentage, + useWidgetNavigationSearch, useWidgetRootContext, type LeaderboardChartData, type ReportParamsFieldAttributes, @@ -42,28 +42,6 @@ type VideoPressWidgetProps = WidgetRenderProps< VideoPressRenderAttributes > & { setError?: ComponentProps< typeof WidgetRoot >[ 'setError' ]; }; -/** - * Build a video row's title. Attachment rows navigate to the internal detail - * route; rows without an ID retain the original external-link fallback. - */ -function buildVideoTitle( row: VideoPlaysRow, search: Record< string, unknown > ): JSX.Element { - return ( - - ); -} - /** * Maps normalized video rows onto the shape `LeaderboardChart` expects. Shares * are computed against the largest value of either period so the overlay bars @@ -72,7 +50,7 @@ function buildVideoTitle( row: VideoPlaysRow, search: Record< string, unknown > */ function buildLeaderboardData( rows: VideoPlaysRow[], - search: Record< string, unknown > + detailSearch: Record< string, unknown > ): LeaderboardChartData { const maxPlays = getCombinedPeriodMax( rows.map( row => row.plays ), @@ -81,7 +59,13 @@ function buildLeaderboardData( return rows.map( row => ( { id: row.key, - label: buildVideoTitle( row, search ), + label: ( + + ), currentValue: row.plays, currentShare: sharePercentage( row.plays, maxPlays ), previousValue: row.previousPlays, @@ -98,6 +82,7 @@ function buildLeaderboardData( */ function VideoPressReport() { const { reportParams } = useWidgetRootContext(); + const detailSearch = useWidgetNavigationSearch(); const statsParams = useMemo( () => ( { ...reportParams, max: WIDGET_ROW_LIMIT } ), [ reportParams ] @@ -114,8 +99,6 @@ function VideoPressReport() { const isInitialLoading = isLoading || primary.isPending; const rows = useMemo( () => toVideoPlaysRows( comparisonRows?.rows ?? [] ), [ comparisonRows ] ); - const detailSearch = useMemo( () => pickReportDateParams( reportParams ), [ reportParams ] ); - const chartData = useMemo( () => buildLeaderboardData( rows, detailSearch ), [ rows, detailSearch ] diff --git a/projects/packages/premium-analytics/widgets/videopress/style.module.css b/projects/packages/premium-analytics/widgets/videopress/style.module.css index 252e70468a4c..5a943c435006 100644 --- a/projects/packages/premium-analytics/widgets/videopress/style.module.css +++ b/projects/packages/premium-analytics/widgets/videopress/style.module.css @@ -1,50 +1,3 @@ -.labelLink, -.labelText, -.internalLink { - - /* `inline-flex` keeps the outbound marker outside the clipped text: the - inner text span carries the ellipsis, so a long title truncates while - the marker stays visible. `min-width: 0` lets the box shrink below its - content width; `max-width: 100%` keeps it inside the label slot. The gap - matches the leaderboard rows' `.rowLink`, so the marker sits at the same - distance from the title in every widget. */ - display: inline-flex; - align-items: center; - min-width: 0; - max-width: 100%; - gap: var(--wpds-dimension-gap-xs); - - /* Vertical padding sets the overlay bar height — the bar fills the row, - whose height is driven by this label (there is no image to size it). */ - padding-block: var(--wpds-dimension-padding-md); - - /* Inset the text from the bar's rounded left edge. The chart applies this to - its default `.label` in overlay mode (`.is-overlay .label`), but a custom - label element bypasses that rule, so we mirror it here. */ - padding-inline-start: var(--wpds-dimension-padding-sm); - color: inherit; - text-decoration: none; -} - -/* The label text carries the ellipsis in every branch: `text-overflow` needs - an inline formatting context, which the flex containers above do not give - it. */ -.titleText { - min-width: 0; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - -/* Underline only the title text, matching the leaderboard rows' hover rule — - the outbound marker stays clean instead of taking the underline with it. */ -.labelLink:hover .titleText, -.labelLink:focus .titleText, -.internalLink:hover .titleText, -.internalLink:focus .titleText { - text-decoration: underline; -} - .root { container-type: inline-size; display: flex; diff --git a/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels index 02dbd524bf19..b9f497569f31 100644 --- a/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels +++ b/projects/plugins/jetpack/changelog/wooa7s-1989-migrate-custom-title-labels @@ -1,4 +1,4 @@ Significance: patch Type: enhancement -Premium Analytics: Match the Most viewed leaderboard row labels to the other leaderboard widgets. +Premium Analytics: Match the Most viewed and Videos leaderboard row labels to the other widgets. diff --git a/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels index f0a19a6a8f01..93492cf7ed3a 100644 --- a/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels +++ b/projects/plugins/premium-analytics/changelog/wooa7s-1989-migrate-custom-title-labels @@ -1,4 +1,4 @@ Significance: patch Type: changed -Most viewed: Match the leaderboard row labels to the other leaderboard widgets. +Leaderboards: Match the Most viewed and Videos row labels to the other leaderboard widgets. From 1345a5e407e1c4205e0f6a5012dcf478be6c5921 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 17:46:14 +0800 Subject: [PATCH 5/8] refactor(premium-analytics): tighten leaderboard row docs, comments, and video-route test --- .../components/chart-leaderboard/README.md | 16 +++++-- .../__tests__/leaderboard-row.test.tsx | 35 +++++++++++++-- .../leaderboard-label.module.scss | 2 + .../chart-leaderboard/leaderboard-row.tsx | 11 ++--- .../top-posts/__tests__/top-posts.test.tsx | 2 + .../widgets/top-posts/render.tsx | 45 ++++++++----------- .../widgets/videopress/render.tsx | 14 +++--- 7 files changed, 76 insertions(+), 49 deletions(-) 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 46511cc9dedc..b7c2d6bcb5c5 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 @@ -142,8 +142,9 @@ const row = { A `postLink` row carries no media, and never becomes a chart button: a chart row that is a button cannot nest an anchor. -Video rows use `videoLink`, which behaves the same way but delegates to `VideoTitleLink` so the -row reaches the video detail route rather than the post one. +Video rows use `videoLink`, which delegates to `VideoTitleLink` so the row reaches the video +detail route rather than the post one. The same constraints apply: no media, and never a chart +button. ```tsx const row = { @@ -164,8 +165,7 @@ const row = { Inside a widget, use `LeaderboardPostLabel` instead of building a post action by hand. It reads the report window from `WidgetRootContext` and passes it as `search`, so the detail page opens on -the range the row was read against. A widget that builds its own action should take the same -window from `useWidgetNavigationSearch()`: +the range the row was read against: ```tsx @@ -174,6 +174,14 @@ window from `useWidgetNavigationSearch()`: Pass `section` to `LeaderboardPostLabel` to open a named tab on the detail page, such as `email-opens`. +There is no `LeaderboardVideoLabel`, so a widget that builds a `videoLink` action takes the same +window from `useWidgetNavigationSearch()` and passes it as `search`: + +```tsx +const detailSearch = useWidgetNavigationSearch(); +const action = { kind: 'videoLink', id: row.id, href: row.link, search: detailSearch }; +``` + `LeaderboardRowMedia` provides five semantic media variants. The variant owns its size, fallback, and default alt-text policy: 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 1fe7a67494fe..abe7551f7e5d 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 @@ -7,6 +7,35 @@ import { fireEvent, render, screen } from '@testing-library/react'; */ import { LeaderboardLabel } from '../leaderboard-label'; import { buildLeaderboardRow, resolveLeaderboardRowAction } from '../leaderboard-row'; +import type { AnchorHTMLAttributes, ReactNode } from 'react'; + +type MockRouteLinkProps = { + to: string; + params?: Record< string, unknown >; + children: ReactNode; +} & Omit< AnchorHTMLAttributes< HTMLAnchorElement >, 'href' >; + +// `forwardRef`, because the design system link that renders this forwards a ref. +jest.mock( '@wordpress/route', () => { + const { forwardRef } = jest.requireActual( 'react' ) as typeof import('react'); + + return { + Link: forwardRef< HTMLAnchorElement, MockRouteLinkProps >( + ( { to, params, children, ...props }, ref ) => ( + result.replace( `$${ key }`, String( value ) ), + to + ) } + { ...props } + > + { children } + + ) + ), + }; +} ); describe( 'LeaderboardLabel', () => { it( 'renders media and text without adding row actions', () => { @@ -106,18 +135,18 @@ describe( 'buildLeaderboardRow', () => { expect( row ).not.toHaveProperty( 'onClick' ); } ); - it( 'keeps a video link out of the chart button props', () => { + it( 'routes a video link to the video detail page, not the post one', () => { const row = buildLeaderboardRow( { label: 'Launch teaser', media: { kind: 'none' }, - action: { kind: 'videoLink', href: 'https://example.com/launch-teaser/' }, + action: { kind: 'videoLink', id: 9, href: 'https://example.com/launch-teaser/' }, } ); render( row.label ); expect( screen.getByRole( 'link', { name: /Launch teaser/ } ) ).toHaveAttribute( 'href', - 'https://example.com/launch-teaser/' + '/video/9' ); expect( row ).not.toHaveProperty( 'onClick' ); } ); 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 ca09fea316f8..f9fa7aa9fcb3 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 @@ -52,6 +52,8 @@ --jpa-leaderboard-image-fit: cover; } +/* The chart insets its own `.label` in overlay mode, but an element label + * bypasses that rule — and the overlay bar takes its height from this row. */ .row, .rowLink { display: flex; diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx index 772f00b73b60..0ccb01145972 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx @@ -96,17 +96,14 @@ export function resolveLeaderboardRowAction( /** * Render the shared leaderboard row chrome around a label. * - * Link actions own the anchor and its new-tab affordance. Post and video links - * delegate to `PostTitleLink` and `VideoTitleLink`, which pick the internal - * detail route, the public URL, or plain text for the row. Drill-down actions - * stay non-interactive here because `LeaderboardChart` turns the whole row into - * a button; `buildLeaderboardRow` passes that action to the chart. + * Drill-down actions stay non-interactive here because `LeaderboardChart` turns + * the whole row into a button; `buildLeaderboardRow` passes that action to the + * chart. * * @return A single label element accepted by `LeaderboardEntry.label`. */ export function LeaderboardRow( { label, media, action }: LeaderboardRowProps ): ReactElement { - // Title-link rows carry no media, so the row chrome goes on the title element - // itself, which is an anchor for the two linked branches and a span otherwise. + // Title-link rows carry no media, so the row chrome goes on the title element. if ( action.kind === 'postLink' || action.kind === 'videoLink' ) { const TitleLink = action.kind === 'postLink' ? PostTitleLink : VideoTitleLink; diff --git a/projects/packages/premium-analytics/widgets/top-posts/__tests__/top-posts.test.tsx b/projects/packages/premium-analytics/widgets/top-posts/__tests__/top-posts.test.tsx index 89f7204e64f7..fb3b426af1f9 100644 --- a/projects/packages/premium-analytics/widgets/top-posts/__tests__/top-posts.test.tsx +++ b/projects/packages/premium-analytics/widgets/top-posts/__tests__/top-posts.test.tsx @@ -88,6 +88,7 @@ describe( 'TopPostsWidget', () => { const titleLink = await screen.findByRole( 'link', { name: /^Hello World Post$/ } ); expect( titleLink ).toHaveAttribute( 'href', expect.stringContaining( '/post/1' ) ); expect( titleLink ).not.toHaveAttribute( 'target' ); + expect( titleLink ).toHaveAttribute( 'title', 'Hello World Post' ); // A row with a detail page carries no link out to the live post: the // external-link icon marks destinations outside the app, and the detail @@ -529,6 +530,7 @@ describe( 'TopPostsWidget', () => { expect( screen.getByText( 'About Page' ) ).toBeInTheDocument(); // The homepage entry has no URL — it must not render as a link. expect( screen.queryByRole( 'link', { name: /Homepage/ } ) ).not.toBeInTheDocument(); + expect( screen.getByTitle( 'Homepage (Latest posts)' ) ).toBeInTheDocument(); } ); it( 'gates archive comparison UI on overlapping archive types', async () => { diff --git a/projects/packages/premium-analytics/widgets/top-posts/render.tsx b/projects/packages/premium-analytics/widgets/top-posts/render.tsx index 20f73f825f31..f5e709e4d9c3 100644 --- a/projects/packages/premium-analytics/widgets/top-posts/render.tsx +++ b/projects/packages/premium-analytics/widgets/top-posts/render.tsx @@ -87,44 +87,35 @@ type TopPostsWidgetProps = WidgetRenderProps< TopPostsRenderAttributes >; const DATA_FORMAT = { type: 'number' as const, options: { useMultipliers: true, decimals: 0 } }; -/** - * Pick the one action a top-posts row exposes. Grouped rows become drill-down - * rows, which the chart turns into buttons, so they cannot carry an anchor. - */ +/** Pick the one action a top-posts row exposes. */ function resolveRowAction( row: TopPostRow, detailSearch: Record< string, unknown >, onDrillDown?: ( row: TopPostRow ) => void ): LeaderboardRowAction { - if ( row.children?.length ) { - return onDrillDown - ? { - kind: 'drillDown', - onClick: () => onDrillDown( row ), - ariaLabel: sprintf( - /* translators: %s is an archive category label, e.g. "Searches". */ - __( 'View %s archive pages', 'jetpack-premium-analytics-pkg' ), - row.label - ), - } - : { kind: 'static' }; + if ( ! row.children?.length ) { + return { kind: 'postLink', id: row.postId, href: row.href, search: detailSearch }; } - return { kind: 'postLink', id: row.postId, href: row.href, search: detailSearch }; + if ( ! onDrillDown ) { + return { kind: 'static' }; + } + + return { + kind: 'drillDown', + onClick: () => onDrillDown( row ), + ariaLabel: sprintf( + /* translators: %s is an archive category label, e.g. "Searches". */ + __( 'View %s archive pages', 'jetpack-premium-analytics-pkg' ), + row.label + ), + }; } /** * Maps normalized top-posts rows onto the shape `LeaderboardChart` expects. - * Current shares are computed relative to the most-viewed row so the overlay - * bars are proportional. When `withComparison` is set, previous-period shares - * and per-row deltas are derived from each row's `previousValue`; otherwise - * the comparison fields are zeroed. - * - * Titles use the shared leaderboard row: post/page rows navigate to the - * internal detail page through the router, and rows without a post ID (the - * Archives view) fall back to the public URL and take the external-link icon. - * Rows with children instead become drill-down rows (per the widget - * drill-down convention they carry no anchors). + * Shares use the largest value across both periods as one denominator, so + * equal-width bars represent equal values. */ function buildLeaderboardData( rows: TopPostRow[], diff --git a/projects/packages/premium-analytics/widgets/videopress/render.tsx b/projects/packages/premium-analytics/widgets/videopress/render.tsx index a13e37ab14a7..e7fbe02723a9 100644 --- a/projects/packages/premium-analytics/widgets/videopress/render.tsx +++ b/projects/packages/premium-analytics/widgets/videopress/render.tsx @@ -4,13 +4,13 @@ import { useStatsVideoPlays } from '@jetpack-premium-analytics/data'; import { LeaderboardChart, - LeaderboardRow, LeaderboardSkeleton, ReportLink, WIDGET_ROW_LIMIT, WidgetFooter, WidgetRoot, WidgetState, + buildLeaderboardRow, calculateDelta, getCombinedPeriodMax, sharePercentage, @@ -59,13 +59,11 @@ function buildLeaderboardData( return rows.map( row => ( { id: row.key, - label: ( - - ), + ...buildLeaderboardRow( { + label: row.label, + media: { kind: 'none' }, + action: { kind: 'videoLink', id: row.id, href: row.link, search: detailSearch }, + } ), currentValue: row.plays, currentShare: sharePercentage( row.plays, maxPlays ), previousValue: row.previousPlays, From 33d0c7281e15c0bd4934bbd2f306f55bb81c0dc8 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 18:01:36 +0800 Subject: [PATCH 6/8] test(premium-analytics): share the route mock so leaderboard-row can join a group The suite gained a `@wordpress/route` mock but stayed in the no-mocks group, so `test-groups` failed on the mismatched signature. --- .../__tests__/leaderboard-row.test.tsx | 32 ++++++++++++------- .../widgets-toolkit-no-mocks-part1.test.tsx | 1 - .../widgets-toolkit-route-link.test.tsx | 1 + 3 files changed, 21 insertions(+), 13 deletions(-) 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 abe7551f7e5d..4c45ec6aeed3 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 @@ -12,6 +12,7 @@ import type { AnchorHTMLAttributes, ReactNode } from 'react'; type MockRouteLinkProps = { to: string; params?: Record< string, unknown >; + search?: Record< string, unknown >; children: ReactNode; } & Omit< AnchorHTMLAttributes< HTMLAnchorElement >, 'href' >; @@ -21,18 +22,25 @@ jest.mock( '@wordpress/route', () => { return { Link: forwardRef< HTMLAnchorElement, MockRouteLinkProps >( - ( { to, params, children, ...props }, ref ) => ( - result.replace( `$${ key }`, String( value ) ), - to - ) } - { ...props } - > - { children } - - ) + ( { to, params, search, children, ...props }, ref ) => { + const path = Object.entries( params ?? {} ).reduce( + ( result, [ key, value ] ) => result.replace( `$${ key }`, String( value ) ), + to + ); + const query = new URLSearchParams(); + Object.entries( search ?? {} ).forEach( ( [ key, value ] ) => { + if ( value !== undefined && value !== null ) { + query.set( key, String( value ) ); + } + } ); + const queryString = query.toString(); + + return ( + + { children } + + ); + } ), }; } ); diff --git a/projects/packages/premium-analytics/tests/groups/widgets-toolkit-no-mocks-part1.test.tsx b/projects/packages/premium-analytics/tests/groups/widgets-toolkit-no-mocks-part1.test.tsx index f1d48e1cd20e..2d745a7021e9 100644 --- a/projects/packages/premium-analytics/tests/groups/widgets-toolkit-no-mocks-part1.test.tsx +++ b/projects/packages/premium-analytics/tests/groups/widgets-toolkit-no-mocks-part1.test.tsx @@ -5,7 +5,6 @@ import '../../packages/widgets-toolkit/src/components/calendar-heatmap/__tests__ import '../../packages/widgets-toolkit/src/components/chart-bar/__tests__/bar-chart-skeleton.test'; import '../../packages/widgets-toolkit/src/components/chart-comparative-line/utils/align-series-dates.test'; import '../../packages/widgets-toolkit/src/components/chart-donut/__tests__/donut-chart-skeleton.test'; -import '../../packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test'; import '../../packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-skeleton.test'; import '../../packages/widgets-toolkit/src/components/download-csv/__tests__/to-download-report-params.test'; import '../../packages/widgets-toolkit/src/components/metric-tabs-chart/__tests__/metric-tabs-chart-skeleton.test'; diff --git a/projects/packages/premium-analytics/tests/groups/widgets-toolkit-route-link.test.tsx b/projects/packages/premium-analytics/tests/groups/widgets-toolkit-route-link.test.tsx index 0ccf1c8cac1a..75803732a5a3 100644 --- a/projects/packages/premium-analytics/tests/groups/widgets-toolkit-route-link.test.tsx +++ b/projects/packages/premium-analytics/tests/groups/widgets-toolkit-route-link.test.tsx @@ -1,4 +1,5 @@ // See README.md before adding a suite to this group. +import '../../packages/widgets-toolkit/src/components/chart-leaderboard/__tests__/leaderboard-row.test'; import '../../packages/widgets-toolkit/src/components/post-highlight-card/__tests__/post-highlight-card.test'; import '../../packages/widgets-toolkit/src/components/post-title-link/__tests__/post-title-link.test'; From db39b8f0005a6005f1a1528fc6f01d57816b76ce Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 18:03:44 +0800 Subject: [PATCH 7/8] fix(premium-analytics): require the report window on detail-route row actions `LeaderboardPostLabel` reads the window from context, but a row whose action varies per row has to build it by hand and could omit `search` silently: the detail page then opened on its own default range with nothing to show for it. Making `search` required turns that into a compile error. Pass `{}` to navigate without a window on purpose. --- .../__tests__/leaderboard-row.test.tsx | 13 +++++++++---- .../chart-leaderboard/leaderboard-row.tsx | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) 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 4c45ec6aeed3..aa1dc4ac9ac5 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 @@ -131,7 +131,7 @@ describe( 'buildLeaderboardRow', () => { const row = buildLeaderboardRow( { label: 'Pricing', media: { kind: 'none' }, - action: { kind: 'postLink', href: 'https://example.com/pricing/' }, + action: { kind: 'postLink', href: 'https://example.com/pricing/', search: {} }, } ); render( row.label ); @@ -143,18 +143,23 @@ describe( 'buildLeaderboardRow', () => { expect( row ).not.toHaveProperty( 'onClick' ); } ); - it( 'routes a video link to the video detail page, not the post one', () => { + it( 'routes a video link to the video detail page with the report window', () => { const row = buildLeaderboardRow( { label: 'Launch teaser', media: { kind: 'none' }, - action: { kind: 'videoLink', id: 9, href: 'https://example.com/launch-teaser/' }, + action: { + kind: 'videoLink', + id: 9, + href: 'https://example.com/launch-teaser/', + search: { date_start: '2026-08-01', date_end: '2026-08-26' }, + }, } ); render( row.label ); expect( screen.getByRole( 'link', { name: /Launch teaser/ } ) ).toHaveAttribute( 'href', - '/video/9' + '/video/9?date_start=2026-08-01&date_end=2026-08-26' ); expect( row ).not.toHaveProperty( 'onClick' ); } ); diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx index 0ccb01145972..4a0ff7863b83 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx @@ -20,8 +20,12 @@ export type LeaderboardRowAction = id?: number | string; /** Public URL. It becomes the link itself when there is no post ID. */ href?: string | null; - /** Search parameters for the detail route, such as the report window. */ - search?: Record< string, unknown >; + /** + * Search parameters for the detail route. Required so a row cannot reach + * the detail page without the window it was read against; pass `{}` to + * navigate deliberately without one. + */ + search: Record< string, unknown >; } | { /** A video with a detail page inside the dashboard. */ @@ -30,8 +34,12 @@ export type LeaderboardRowAction = id?: number | string; /** Public URL. It becomes the link itself when there is no video ID. */ href?: string | null; - /** Search parameters for the detail route, such as the report window. */ - search?: Record< string, unknown >; + /** + * Search parameters for the detail route. Required so a row cannot reach + * the detail page without the window it was read against; pass `{}` to + * navigate deliberately without one. + */ + search: Record< string, unknown >; } | { kind: 'drillDown'; From c872d7435cd018f84972628bf3f4a86f8e61dd6c Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 26 Aug 2026 18:05:30 +0800 Subject: [PATCH 8/8] docs(premium-analytics): trim the leaderboard row docs to what the code cannot say --- .../components/chart-leaderboard/README.md | 27 +++---------------- .../chart-leaderboard/leaderboard-row.tsx | 10 +++---- 2 files changed, 8 insertions(+), 29 deletions(-) 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 b7c2d6bcb5c5..49be6a854378 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 @@ -143,24 +143,10 @@ A `postLink` row carries no media, and never becomes a chart button: a chart row button cannot nest an anchor. Video rows use `videoLink`, which delegates to `VideoTitleLink` so the row reaches the video -detail route rather than the post one. The same constraints apply: no media, and never a chart -button. +detail route instead of the post one. Same constraints: no media, never a chart button. ```tsx -const row = { - id: 'video-9', - ...buildLeaderboardRow( { - label: 'Launch teaser', - media: { kind: 'none' }, - action: { - kind: 'videoLink', - id: 9, - search: { from: '2026-03-01', to: '2026-03-10' }, - }, - } ), - currentValue: 100, - currentShare: 100, -}; +action: { kind: 'videoLink', id: 9, search: { from: '2026-03-01', to: '2026-03-10' } }, ``` Inside a widget, use `LeaderboardPostLabel` instead of building a post action by hand. It reads @@ -174,13 +160,8 @@ the range the row was read against: Pass `section` to `LeaderboardPostLabel` to open a named tab on the detail page, such as `email-opens`. -There is no `LeaderboardVideoLabel`, so a widget that builds a `videoLink` action takes the same -window from `useWidgetNavigationSearch()` and passes it as `search`: - -```tsx -const detailSearch = useWidgetNavigationSearch(); -const action = { kind: 'videoLink', id: row.id, href: row.link, search: detailSearch }; -``` +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, fallback, and default alt-text policy: diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx index 4a0ff7863b83..8440ba158ffa 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-leaderboard/leaderboard-row.tsx @@ -21,9 +21,8 @@ export type LeaderboardRowAction = /** Public URL. It becomes the link itself when there is no post ID. */ href?: string | null; /** - * Search parameters for the detail route. Required so a row cannot reach - * the detail page without the window it was read against; pass `{}` to - * navigate deliberately without one. + * Required, so a row cannot silently open the detail page on its own + * default range. Pass `{}` to navigate without a window on purpose. */ search: Record< string, unknown >; } @@ -35,9 +34,8 @@ export type LeaderboardRowAction = /** Public URL. It becomes the link itself when there is no video ID. */ href?: string | null; /** - * Search parameters for the detail route. Required so a row cannot reach - * the detail page without the window it was read against; pass `{}` to - * navigate deliberately without one. + * Required, so a row cannot silently open the detail page on its own + * default range. Pass `{}` to navigate without a window on purpose. */ search: Record< string, unknown >; }