Skip to content

Dashboard Widgets: let the host decide which component renders an action link - #81740

Merged
retrofox merged 34 commits into
trunkfrom
add/widget-host-links
Aug 25, 2026
Merged

retrofox merged 34 commits into
trunkfrom
add/widget-host-links

Conversation

@retrofox

@retrofox retrofox commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds the widget host seam to @wordpress/widget-primitives and wires its first capability, links: an action href the application recognizes as one of its own routes renders through that application's router link and navigates client-side.

Everything else keeps today's plain anchor.

Closes #81725. Part of #81723. The site-health Details action from #81729, now on trunk, is the in-repo case that upgrades.

Why

Action links render as plain anchors regardless of target, so following an action that points at a dashboard route reloads the whole admin page.

The widget declaration should stay portable data; which component materializes the link is the host's decision.

How

The contract

WidgetHostProvider / useWidgetHost in packages/widget-primitives/src/widget-host/.

WidgetHost is a bag of optional capabilities: an absent capability degrades to the host-agnostic behavior, so the seam is inert for hosts that do not provide it.

The links capability carries two things that travel together: match( href ), recognizing the application's own routes, and the router's Link primitive, which must render a real anchor.
The provider merges over the inherited value, so capabilities can layer per subtree.

A new Widget Host page in the package's Storybook documents the contract: the seam, the links shape, and the consumption rules link actions follow.

image image

The chrome

The actions menu and the footer resolve each action using a shared getActionRoute helper. download and openInNewTab always use the plain anchor: both open a new document.

In a match, the host link is injected via the existing render prop of the UI primitives (Link, LinkButton), so the visual treatment remains identical and only the underlying anchor changes.

The dashboard

The dashboard provides the capability at its route layer: DashboardWidgetHostProvider wraps the stage, and matchDashboardHref recognizes hrefs that target the same document and the same page query arg, resolving the p param as the route.

Screen.Recording.2026-08-19.at.8.41.42.AM.mov

The demo

The HostLinks Storybook story mounts a demo host and shows the three materializations side by side in the widget footer: a client-side route link, a plain external anchor, and a download.

Screen.Recording.2026-08-19.at.8.39.39.AM.mov

On the dashboard, the site-health Details action from #81729 upgrades to client-side navigation with no manifest change.

Testing

  1. Check out the branch and rebuild:
npm run build
  1. Open Dashboard (Beta) with the Site Health widget on the grid. Follow Details in the widget footer: it lands on the Site Health page without a full page load.
  2. Middle-click and copy-address still work on Details: it is a real anchor.
  3. Status and Info still point at the classic screen and full-load as before.
  4. The Storybook HostLinks story shows the route link beside the plain anchor and the download, with a status line confirming the document never reloaded:
npm run storybook:dev
  1. Unit coverage for the seam and the matcher:
npx jest --config test/unit/jest.config.js packages/widget-primitives/src/widget-host routes/dashboard/widget-host

@retrofox retrofox added [Type] Enhancement A suggestion for improvement. [Feature] Dashboard WordPress admin dashboard, widget framework, and layout customization [Package] Widget primitives /packages/widget-primitives [Package] Widget Dashboard /packages/widget-dashboard labels Aug 17, 2026
@retrofox retrofox self-assigned this Aug 17, 2026
@github-actions

github-actions Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: retrofox <retrofox@git.wordpress.org>
Co-authored-by: chihsuan <chihsuan@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

Size Change: +110 B (0%)

Total Size: 7.91 MB

📦 View Changed
Filename Size Change
build/modules/widget-primitives/index.min.js 1.64 kB +110 B (+7.21%) 🔍

compressed-size-action


const path = url.searchParams.get( 'p' ) || '/';

if ( path.includes( '?' ) || path.includes( '#' ) ) {

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 make sense to only accept internal path-shaped values here? It's probably low risk, but a p such as https://example.com or mailto:… currently passes these checks. TanStack then treats it as an external link, while the plain-anchor version remains under admin.php?page=dashboard&p=…, so the two materializations behave differently.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, and the divergence is real: TanStack treats any to that new URL() accepts as an external link, so the router link would leave the site while the full load stays under admin.php?page=dashboard&p=… and shows the not-found screen.

A relative p also diverges, since the router resolves it against the current route.

Fixed in 205144f: p must be a root-relative path, / not followed by /, which is the shape the router's own createHref writes into it. Absolute URLs, mailto:, protocol-relative URLs, and relative values all remain plain anchors now, with a test for each.

* where menus and tooltips reach their anchor through the ref.
* `path` replaces `href`; the two never coexist.
*/
Link: ComponentType<

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 strengthening how we communicate or test the ref requirement?

The current ComponentType<…RefAttributes> also accepts a plain function component, so a third-party host can satisfy the type while silently dropping the ref. In React 18, that could breakmenu focus or tooltip behavior.

I’m not sure ForwardRefExoticComponent is the right constraint given React 19’s ref model, but a user-facing focus or tooltip test may help protect this contract.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed on the diagnosis, and I would not go to ForwardRefExoticComponent either: under React 19, ref arrives as a plain prop, so a function component that spreads its props onto the anchor is a correct host, and the constraint would reject it (along with memo and class components) while still not expressing the real requirement, which is that the ref reaches the anchor.

So the protection is user-facing tests plus clearer communication:

cb7e72d and bc77f0be16125d1 add behavior tests to the chrome compositions: in the menu, ArrowDown from the trigger lands on the matched item, and ArrowDown/ArrowUp traverse it; in the footer, hovering the matched medium action opens its tooltip.

On a link that drops the ref, both fail as you describe (I checked with a plain function component before writing them): the first ArrowDown skips the item, ArrowUp cannot return to it, and the hover never opens the tooltip.

f67f5e5 states the consequences for a host author who reads it: the Link JSDoc, the README, and the Storybook page now name the two symptoms (keyboard navigation skips the item and the tooltip loses its anchor), note that React 19 satisfies the rule with ref as a prop, and give the one-line test a host can run against its own link.

A development-only runtime check in Chrome (warning when the composed ref never receives an anchor) would be the one thing that reaches a third-party host at the point of failure.
I left it out to keep the seam small, but I'm happy to open it as a follow-up if you think it earns its place.

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 ran a quick check. React 18 already warns when the link drops it. That leaves forwardRef-but-misused, which the JSDoc already names. I think it's fine to skip the runtime check for now. : )

@retrofox

Copy link
Copy Markdown
Contributor Author

Thanks for the second pass @chihsuan.

Both points are addressed: the matcher now accepts only root-relative paths in p (205144f), and the ref requirement is pinned by a keyboard test on menu composition, plus docs that name the failure modes (cb7e72d, f67f5e5). Details in the threads.

@ciampo, the peer dependency and duplicate-copy tests from the single-copy thread will land in a separate PR; the diagram thread has been addressed as well.

@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 addressing my comments. @retrofox LGTM 🚀

@ciampo

ciampo commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

@retrofox I see this PR was merged while we were still discussing feedback/approach, especially about adding Context and APIs we don't seem to need at the moment and introducing complexity.

I'd prefer to coordinate better next time; I was not under the impression that this PR had to be merged quickly

@ciampo ciampo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More (minor) feedback

Comment on lines +15 to +23
export function getActionRoute(
links: WidgetHostLinks | undefined,
action: WidgetAction
): string | null {
if ( ! links || action.download || action.openInNewTab ) {
return null;
}

return links.match( action.href );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's a small bug here:

an empty string is valid download value, and the current truthiness check will wrongly skip that scenario, dropping link download behaviour.

We should probably treat every value except undefined and false as a download, and add an empty-string case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right 👍 , download="" is a valid download.

Fixed in #82073: every value but false keeps the plain anchor, with a unit test for the guard and the empty-string case in both compositions.

Comment on lines +73 to +80
{
id: 'export',
label: 'Export data',
relevance: 'medium',
icon: <svg />,
href: 'files/export.csv',
download: 'export.csv',
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These download and new-tab actions use URLs that createHost().match already rejects. The tests therefore stay green if the download / openInNewTab guard is removed from getActionRoute().

Could these fixtures use MATCHED_HREF? Then the assertions prove that the modifiers—not an unrelated matcher miss—keep the plain anchor. An empty-string download case would also cover the valid edge case above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch; the guard was untested.

Done in #82073: the download, empty-string download, and new-tab fixtures now use MATCHED_HREF in the footer and the menu, so the assertions prove the modifiers keep the plain anchor; against the old guard, the empty-string cases fail.

@retrofox

Copy link
Copy Markdown
Contributor Author

@retrofox I see this PR was merged while we were still discussing feedback/approach, especially about adding Context and APIs we don't seem to need at the moment and introducing complexity.

I'd prefer to coordinate better next time; I was not under the impression that this PR had to be merged quickly

@ciampo Thanks for the review and the follow-up.

On the merge: you're right, it went in with your threads open. Every thread had a reply, and I read the days without a follow-up as signs that the discussion had settled; it hadn't. Next time I'll ask before merging.

On the npm release: the seam ships in this RC. Both packages are 0.x and experimental by design, precisely so the API can change while the dashboard is being built; the seam is tested, documented, and was reviewed and approved.

The follow-ups land in the next cycle: the single-copy contract is filed as #82063, and the widget-body consumer that motivates the context comes next.

The API-shape discussion continues in its thread; I'm answering the remaining threads today.

Side note: I wrote the issue, but it wasn't created due to a connection issue. 😅

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

Labels

[Feature] Dashboard WordPress admin dashboard, widget framework, and layout customization [Package] Widget Dashboard /packages/widget-dashboard [Package] Widget primitives /packages/widget-primitives [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dashboard Widgets: let the host decide which component renders an action link

3 participants