Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Widgets: link post and email titles to their detail pages in the dashboard.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,41 @@ An explicit drill-down `ariaLabel` replaces the accessible name otherwise comput
the media alt text and visible label, which can cause a screen reader to announce the label
twice.

For a post, page, or email with a detail page inside the dashboard, use the `postLink` action.
`PostTitleLink` chooses the destination: the internal detail route when the row carries a post
ID, the public URL when it does not, and plain text when neither is usable.

```tsx
const row = {
id: 'post-41',
...buildLeaderboardRow( {
label: 'Hello world',
media: { kind: 'none' },
action: {
kind: 'postLink',
id: 41,
href: 'https://example.com/hello-world/',
search: { from: '2026-03-01', to: '2026-03-10' },
},
} ),
currentValue: 100,
currentShare: 100,
};
```

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:

```tsx
<LeaderboardPostLabel id={ row.postId } label={ row.label } link={ row.link } />
```

Pass `section` 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:

Expand All @@ -135,6 +170,21 @@ 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`.

## Props

| Prop | Type | Default | Description |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe( 'buildLeaderboardRow', () => {
expect( row ).not.toHaveProperty( 'onClick' );
} );

it( 'keeps a post link out of the chart button props', () => {
const row = buildLeaderboardRow( {
label: 'Pricing',
media: { kind: 'none' },
action: { kind: 'postLink', href: 'https://example.com/pricing/' },
} );

render( row.label );

expect( screen.getByRole( 'link', { name: /Pricing/ } ) ).toHaveAttribute(
'href',
'https://example.com/pricing/'
);
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( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export type {
LeaderboardRowActionOptions,
LeaderboardRowChartProps,
LeaderboardRowProps,
LeaderboardRowVariant,
} from './leaderboard-row';
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
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 {
text-decoration: underline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
* External dependencies
*/
import { Link } from '@jetpack-premium-analytics/externals';
import clsx from 'clsx';
/**
* Internal dependencies
*/
import { PostTitleLink } from '../post-title-link';
import { LeaderboardLabel, type LeaderboardRowMedia } from './leaderboard-label';
import styles from './leaderboard-label.module.scss';
import type { MouseEvent, ReactElement } from 'react';

export type LeaderboardRowAction =
| { kind: 'link'; href: string }
| {
/** A post, page, or email with a detail page inside the dashboard. */
kind: 'postLink';
/** Post or page ID. Rows carrying one link to the internal detail route. */
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 >;
}
| {
kind: 'drillDown';
onClick: ( event: MouseEvent< HTMLButtonElement > ) => void;
Expand All @@ -22,6 +34,13 @@ 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;
Expand All @@ -38,6 +57,10 @@ 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 =
Expand Down Expand Up @@ -75,25 +98,58 @@ export function resolveLeaderboardRowAction(
/**
* Render the shared leaderboard row chrome around a label.
*
* Link actions own the anchor and its new-tab affordance. 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 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.
*
* @param props - Component props.
* @param props.label - Label text.
* @param props.media - Media rendered before the label.
* @param props.action - The row action.
* @param props - Component props.
* @param props.label - Label text.
* @param props.media - Media rendered before the label.
* @param props.action - The row action.
* @param props.variant - Row height.
* @param props.className - Extra class for the row.
* @return A single label element accepted by `LeaderboardEntry.label`.
*/
export function LeaderboardRow( { label, media, action }: LeaderboardRowProps ): ReactElement {
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 );

// Post rows carry no media, so the row chrome goes on the anchor itself.
if ( action.kind === 'postLink' ) {
return (
<PostTitleLink
id={ action.id }
label={ label }
link={ action.href }
search={ action.search }
title={ label }
classNames={ {
internal: linkClassName,
external: linkClassName,
plain: rowClassName,
text: styles.label,
} }
/>
);
}

const content = (
<LeaderboardLabel label={ label } media={ media } decorativeMedia={ action.kind === 'link' } />
);

if ( action.kind === 'link' ) {
return (
<Link
className={ styles.rowLink }
className={ linkClassName }
href={ action.href }
variant="unstyled"
openInNewTab
Expand All @@ -105,7 +161,7 @@ export function LeaderboardRow( { label, media, action }: LeaderboardRowProps ):
}

return (
<span className={ styles.row } title={ label }>
<span className={ rowClassName } title={ label }>
{ content }
</span>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export {
type LeaderboardRowChartProps,
type LeaderboardRowMedia,
type LeaderboardRowProps,
type LeaderboardRowVariant,
} from './chart-leaderboard';
export { BarChart, type BarChartProps, type BarChartData, type BarChartStyle } from './chart-bar';
export { ChartEmptyState, type ChartEmptyStateProps } from './chart-empty-state';
Expand All @@ -53,6 +54,11 @@ export { WidgetBackLink, type WidgetBackLinkProps } from './widget-back-link';
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 {
LeaderboardPostLabel,
type LeaderboardPostLabelProps,
type LeaderboardPostLabelVariant,
} from './leaderboard-post-label';
export { VideoTitleLink, type VideoTitleLinkProps } from './video-title-link';
export {
SubscriberList,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
/**
* Internal dependencies
*/
import { useWidgetRootContext } from '../../widget-root';
import { LeaderboardPostLabel } from '../leaderboard-post-label';
import type { AnchorHTMLAttributes, ReactNode } from 'react';

type MockRouteLinkProps = {
to: string;
params?: Record< string, unknown >;
search?: Record< string, unknown >;
children: ReactNode;
} & Omit< AnchorHTMLAttributes< HTMLAnchorElement >, 'href' >;

jest.mock( '@wordpress/route', () => ( {
Link: ( { to, params, search, children, ...props }: MockRouteLinkProps ) => {
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 (
<a href={ queryString ? `${ path }?${ queryString }` : path } { ...props }>
{ children }
</a>
);
},
} ) );

jest.mock( '../../widget-root', () => ( {
useWidgetRootContext: jest.fn(),
} ) );

const mockUseWidgetRootContext = useWidgetRootContext as jest.Mock;

beforeEach( () => {
mockUseWidgetRootContext.mockReturnValue( { reportParams: { from: '2026-06-01' } } );
} );

describe( 'LeaderboardPostLabel', () => {
it( 'links a post to its detail route and carries the report window', () => {
render(
<LeaderboardPostLabel id={ 12 } label="Hello world" link="https://example.com/hello/" />
);

const link = screen.getByRole( 'link', { name: 'Hello world' } );
const url = new URL( link.getAttribute( 'href' ) ?? '', 'https://example.com' );

expect( url.pathname ).toBe( '/post/12' );
expect( url.searchParams.get( 'from' ) ).toBe( '2026-06-01' );
expect( url.searchParams.get( 'post_url' ) ).toBe( 'https://example.com/hello/' );
expect( link ).not.toHaveAttribute( 'target', '_blank' );
} );

it( 'opens the requested detail tab when a section is given', () => {
render( <LeaderboardPostLabel id={ 12 } label="Newsletter" section="email-opens" /> );

expect( screen.getByRole( 'link', { name: 'Newsletter' } ) ).toHaveAttribute(
'href',
'/post/12?from=2026-06-01&section=email-opens'
);
} );

it( 'falls back to the public URL when there is no post ID', () => {
render( <LeaderboardPostLabel label="Untracked page" link="https://example.com/untracked/" /> );

const link = screen.getByRole( 'link', { name: /Untracked page/ } );
expect( link ).toHaveAttribute( 'href', 'https://example.com/untracked/' );
expect( link ).toHaveAttribute( 'target', '_blank' );
} );

it( 'renders plain text when the row has neither a post ID nor a safe URL', () => {
render( <LeaderboardPostLabel label="Untitled" link="javascript:alert(1)" /> );

expect( screen.getByText( 'Untitled' ) ).toBeInTheDocument();
expect( screen.queryByRole( 'link' ) ).not.toBeInTheDocument();
} );
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
LeaderboardPostLabel,
type LeaderboardPostLabelProps,
type LeaderboardPostLabelVariant,
} from './leaderboard-post-label';
Loading
Loading