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
10 changes: 6 additions & 4 deletions projects/packages/premium-analytics/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,12 @@ wire a handler in `routeStatsReport()` inside `register-report-mocks.ts`. See
add a second in-widget `<Text variant="heading-md" render={ <h3 /> }>` 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 — 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.
- Leaderboard rows: spread `buildLeaderboardRow()` into the chart entry — it carries the
drill-down `onClick`/`ariaLabel` that a bare `<LeaderboardRow>` label silently drops. Use
`<LeaderboardRow>` 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 `<WidgetState>` (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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Leaderboards: Match the remaining row labels to the other leaderboard widgets.
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -172,8 +172,11 @@ 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: `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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* External dependencies
*/
import { fireEvent, render, screen } from '@testing-library/react';
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;
Expand Down Expand Up @@ -45,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(
Expand All @@ -66,6 +81,21 @@ describe( 'LeaderboardLabel', () => {
expect( screen.getByText( 'Desktop' ) ).toBeInTheDocument();
expect( screen.queryByRole( 'img' ) ).not.toBeInTheDocument();
} );

it( 'renders the glyph it was handed, hidden from assistive technology', () => {
const { container } = render(
<LeaderboardLabel label="Recipes" media={ { kind: 'icon', icon: category } } />
);
// 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( 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 ) );
} );
} );

describe( 'resolveLeaderboardRowAction', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@
height: var(--jpa-leaderboard-image-height);
object-fit: var(--jpa-leaderboard-image-fit);
}

.icon {
flex: none;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/**
* 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';
/**
* 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 = {
Expand All @@ -30,7 +32,9 @@ export type LeaderboardLabelProps = {
const DEFAULT_IMAGE_URL =
'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50"><rect width="50" height="50" fill="%23e5e7eb"/></svg>';

function getMediaDetails( media: Exclude< LeaderboardRowMedia, { kind: 'none' } > ) {
function getMediaDetails(
media: Exclude< LeaderboardRowMedia, { kind: 'none' } | { kind: 'icon' } >
) {
switch ( media.kind ) {
case 'avatar':
return {
Expand Down Expand Up @@ -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 ) ) &&
Expand All @@ -95,6 +100,9 @@ export function LeaderboardLabel( {
align="center"
className={ clsx( styles.container, mediaDetails?.className ) }
>
{ media.kind === 'icon' && (
<Icon icon={ media.icon } size={ 20 } className={ styles.icon } />
) }
{ shouldRenderImage && (
<img
src={ mediaDetails.url || DEFAULT_IMAGE_URL }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { download } from '@wordpress/icons';
import { Link } from '@jetpack-premium-analytics/externals';
import {
WIDGET_ROW_LIMIT,
calculateDelta,
Expand All @@ -21,6 +20,8 @@ import {
LeaderboardChart,
LeaderboardSkeleton,
ReportLink,
buildLeaderboardRow,
resolveLeaderboardRowAction,
sharePercentage,
WidgetFooter,
WidgetRoot,
Expand Down Expand Up @@ -77,21 +78,11 @@ function buildLeaderboardData(

return {
id: `${ index }-${ row.href ?? row.label }`,
label: row.href ? (
<Link
className={ styles.labelLink }
href={ row.href }
variant="unstyled"
openInNewTab
title={ row.label }
>
{ row.label }
</Link>
) : (
<span className={ styles.labelText } title={ row.label }>
{ row.label }
</span>
),
...buildLeaderboardRow( {
label: row.label,
media: { kind: 'none' },
action: resolveLeaderboardRowAction( { href: row.href, hasChildren: false } ),
} ),
currentValue: row.value,
currentShare: sharePercentage( row.value, maxValue ),
previousValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
WidgetFooter,
WidgetRoot,
WidgetState,
buildLeaderboardRow,
sharePercentage,
useWidgetRootContext,
type LeaderboardChartData,
Expand All @@ -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
*/
Expand Down Expand Up @@ -55,11 +56,11 @@ function SearchTermsInner() {

return {
id: `${ index }-${ term.label }`,
label: (
<Stack align="center" className={ styles.itemLabel }>
<Text className={ styles.itemLabelText }>{ term.label }</Text>
</Stack>
),
...buildLeaderboardRow( {
label: term.label,
media: { kind: 'none' },
action: { kind: 'static' },
} ),
currentValue: term.views,
previousValue: previousViews,
currentShare: sharePercentage( term.views, maxValue ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 7 additions & 6 deletions projects/packages/premium-analytics/widgets/shares/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
WIDGET_ROW_LIMIT,
WidgetRoot,
WidgetState,
buildLeaderboardRow,
sharePercentage,
type LeaderboardChartData,
type ReportParamsFieldAttributes,
} from '@jetpack-premium-analytics/widgets-toolkit';
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
*/
Expand All @@ -40,11 +41,11 @@ function SharesInner() {

return data.map( ( service, index ) => ( {
id: `${ index }-${ service.service }`,
label: (
<Stack align="center" className={ styles.itemLabel }>
<Text className={ styles.itemLabelText }>{ service.label }</Text>
</Stack>
),
...buildLeaderboardRow( {
label: service.label,
media: { kind: 'none' },
action: { kind: 'static' },
} ),
currentValue: service.value,
currentShare: sharePercentage( service.value, maxValue ),
} ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading
Loading