Skip to content

Premium Analytics: Bring out a source in Requests by source when its total is clicked - #55390

Open
dognose24 wants to merge 2 commits into
trunkfrom
add/uni-1105-highlight-source
Open

dognose24 wants to merge 2 commits into
trunkfrom
add/uni-1105-highlight-source

Conversation

@dognose24

@dognose24 dognose24 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Part of UNI-1105. Builds on the Requests by source widget from #55331.

Proposed changes

  • The four totals above the Requests by source chart are now buttons. Clicking one brings out that source: the other three dim in the chart and in the totals. Clicking it again resets, and clicking another total moves the emphasis. The choice is kept when switching between the line and the bar view.
  • All four sources stay on the chart and in the tooltip, so they can still be compared. The totals read much like the metric tabs of other Stats widgets, which is why they should respond; switching to one chart per source would lose the comparison.
  • StackedAreaChart takes emphasizedIndex and ComparativeBarChart takes emphasizedSeries. Both dim the other series with a rule scoped to the chart, since @automattic/charts has no option for this yet: the areas are matched by their position in the stack, the bars by their fill color.
  • ComparativeBarChart takes showLegend. The widget turns it off, since its totals already name the sources; the bar view used to repeat them in a legend below the chart.

The tab stays behind the jetpack-stats-ai-tab flag, so nothing changes for any site.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No.

Testing instructions

There is no site to test against yet, so use Storybook.

  • cd projects/js-packages/storybook && pnpm run storybook:dev
  • Open Packages / Premium Analytics / Widgets / Requests by source / Default.
  • Click AI bots. Its layer keeps its color, the other three layers and totals dim. The tooltip still lists all four sources.
  • Click Blocked. The emphasis moves to it. Click it again: everything is back to normal.
  • Use Tab to reach a total and press Enter or Space. It behaves like a click, and the focus ring is visible.
  • Open Bars and repeat. In each bar only the picked source keeps its color, and there is no legend below the chart.

Recording

2026-11-26.10.46.01.mov

What I ran:

  • The widget, comparative bar chart and stacked area chart suites pass (56 tests). tsgo --noEmit, ESLint and Stylelint are clean.
  • On a local site with mocked data, in Chrome: picking each of the four sources in the line view and in the bar view dims exactly the other three, the choice survives the switch between views, a second click resets, and the bar view has no legend.

Not checked:

  • The stories above, in Storybook itself.
  • The chart-level rules have no automated test: jsdom draws the charts at zero width, so there are no areas or bars to read. The widget test covers the buttons' pressed state only.
  • Other widgets that use ComparativeBarChart. They pass neither new option, and the only change they get is an opacity transition on their bars.

🤖 Generated with Claude Code

@dognose24 dognose24 added Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] In Progress labels Nov 24, 2026
@dognose24 dognose24 self-assigned this Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/uni-1105-highlight-source branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/uni-1105-highlight-source

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@jp-launch-control

jp-launch-control Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-comparative-bar/comparative-bar-chart.tsx 92/95 (96.84%) -0.96% 1 ❤️‍🩹
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-stacked-area/stacked-area-chart.tsx 14/17 (82.35%) 2.35% 0 💚
projects/packages/premium-analytics/widgets/requests-by-source/render.tsx 14/14 (100.00%) 0.00% 0 💚

1 file is newly checked for coverage.

File Coverage
projects/packages/premium-analytics/packages/widgets-toolkit/src/components/chart-emphasis.ts 1/1 (100.00%) 💚

Full summary · PHP report · JS report

Base automatically changed from add/uni-1085-requests-by-source to trunk November 25, 2026 15:02
…total is clicked

The totals read like the metric tabs of other widgets, so they now respond:
a click dims the other sources in the chart and in the totals, and a second
click lets go. The stacked area and comparative bar charts each take an
option for it, and the bar chart can leave its legend to the widget.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@dognose24
dognose24 force-pushed the add/uni-1105-highlight-source branch from 614e543 to efe7aaf Compare November 26, 2026 01:57
… source test

The lint gate allows no warnings, and `fireEvent.click` carries one.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@dognose24
dognose24 marked this pull request as ready for review November 26, 2026 02:42
@dognose24
dognose24 requested a review from a team as a code owner November 26, 2026 02:42
@dognose24 dognose24 added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Nov 26, 2026
@dognose24
dognose24 requested a review from a team November 26, 2026 02:45

@chihsuan chihsuan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! @dognose24 I left a few inline notes, mainly about moving the dimming into @automattic/charts and two accessibility issues.

Comment on lines +85 to +90
const emphasisRule =
emphasizedIndex === null
? null
: `[data-chart-scope="${ scope }"] .visx-area-stack > path:not(:nth-child(${
series.length
}n + ${ emphasizedIndex + 1 })) { opacity: ${ DIMMED_SERIES_OPACITY }; }`;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the dimming could live in @automattic/charts instead? This rule relies on visx drawing the areas and then the lines in series order, so a visx update could silently stop it. visx already accepts a per-series opacity, so an emphasizedSeries label option on the charts could replace both rules and the index/label split here.


.total:focus-visible {
outline: var(--wpds-border-width-focus) solid var(--wpds-color-stroke-focus);
outline-offset: var(--wpds-dimension-gap-xs);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we draw the focus ring inside the button? On the first total, .root's overflow: hidden clips the ring's top and start edges. Other focus rings in this package use the inset offset.

Suggested change
outline-offset: var(--wpds-dimension-gap-xs);
outline-offset: calc(-1 * var(--wpds-border-width-focus));

Comment on lines +58 to +60
.isDimmed {
opacity: 0.45;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we dim only the color marker and keep the text at full color? At 0.45 the dimmed numbers drop to about 2.8:1 and the share line to about 1.9:1, below WCAG AA, while those totals are still clickable.

Comment on lines +84 to +102
it( 'brings out one source at a time, and lets it go on a second click', async () => {
mockUseStatsEdgeBotRequests.mockReturnValue(
requestsResult( { human: 600, ai: 250, other: 100, blocked: 50 } )
);
renderWidget( { from: '2026-11-01', to: '2026-11-07', interval: 'day' } );
const pressed = () =>
screen
.getAllByRole( 'button', { pressed: true } )
.map( button => button.textContent?.match( /^\D+/ )?.[ 0 ] );

await userEvent.click( screen.getByRole( 'button', { name: /AI bots/ } ) );
expect( pressed() ).toEqual( [ 'AI bots' ] );

await userEvent.click( screen.getByRole( 'button', { name: /Blocked/ } ) );
expect( pressed() ).toEqual( [ 'Blocked' ] );

await userEvent.click( screen.getByRole( 'button', { name: /Blocked/ } ) );
expect( screen.queryByRole( 'button', { pressed: true } ) ).not.toBeInTheDocument();
} );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth covering the dimming itself? The PR notes jsdom draws the charts at zero width, but @automattic/charts mocks the element size in its own jest setup (setup-element-size-mock.js). This test only checks aria-pressed, so the chart wiring and the dimmed totals go untested.

const pressed = () =>
screen
.getAllByRole( 'button', { pressed: true } )
.map( button => button.textContent?.match( /^\D+/ )?.[ 0 ] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we assert aria-pressed on the two buttons directly? Pulling the label out with /^\D+/ would break if a label ever held a digit.

@media not (prefers-reduced-motion) {

.total {
transition: opacity 150ms ease-out;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we use the WPDS motion tokens here, like the charts legend does for the same fade? 150ms is repeated in three files and isn't on the token scale.

Suggested change
transition: opacity 150ms ease-out;
transition: opacity var(--wpds-motion-duration-md) var(--wpds-motion-easing-subtle);

Comment on lines +471 to +477
const emphasizedColor =
emphasizedSeries === null
? undefined
: seriesStyles[ alignedSeries.findIndex( s => s.label === emphasizedSeries ) ]?.stroke;
const emphasisRule = emphasizedColor
? `[data-chart-scope="${ scope }"] .visx-bar:not([fill="${ emphasizedColor }"]) { opacity: ${ DIMMED_SERIES_OPACITY }; }`
: null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn in development when emphasizedSeries meets a previous period? In the grouped layout the comparison bars aren't .visx-bar, so they stay fully opaque, and in overlap the emphasized series' own previous bars dim. Only the README guards it today.

isDimmed,
onToggle,
}: SourceTotalProps ) {
const toggle = useCallback( () => onToggle( source.key ), [ onToggle, source.key ] );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is the useCallback needed here? SourceTotal isn't memoized and the handler goes to a DOM button, so an inline arrow should do.

onPointerUp,
onDatumActivate,
}: ComparativeBarChartProps ) {
const scope = useId();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could scope double as the fallback chart id? Both only need a unique per-instance string, so the second useId() could go.

@dognose24

Copy link
Copy Markdown
Contributor Author

Agreed, the dimming belongs in the charts. #55501 adds emphasizedSeries to the area and bar charts, comparison bars included, so the warning in your last note becomes moot. Once it lands I'll switch this PR over and take the rest of the notes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Docs Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Package] Premium Analytics [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants