Repository navigation
Dashboard Widgets: let the host decide which component renders an action link - #81740
Conversation
hosts inject route recognition and their router link primitive
doc page in primitives; HostLinks story in the dashboard
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: +110 B (0%) Total Size: 7.91 MB 📦 View Changed
|
props and refs reach the custom link; unmatched, download, and new-tab actions stay plain anchors
|
|
||
| const path = url.searchParams.get( 'p' ) || '/'; | ||
|
|
||
| if ( path.includes( '?' ) || path.includes( '#' ) ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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< |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. : )
|
Thanks for the second pass @chihsuan. Both points are addressed: the matcher now accepts only root-relative paths in @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. |
|
@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 |
| export function getActionRoute( | ||
| links: WidgetHostLinks | undefined, | ||
| action: WidgetAction | ||
| ): string | null { | ||
| if ( ! links || action.download || action.openInNewTab ) { | ||
| return null; | ||
| } | ||
|
|
||
| return links.match( action.href ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| { | ||
| id: 'export', | ||
| label: 'Export data', | ||
| relevance: 'medium', | ||
| icon: <svg />, | ||
| href: 'files/export.csv', | ||
| download: 'export.csv', | ||
| }, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@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 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. 😅 |
What
Adds the widget host seam to
@wordpress/widget-primitivesand 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
Detailsaction 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/useWidgetHostinpackages/widget-primitives/src/widget-host/.WidgetHostis 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
linkscapability carries two things that travel together:match( href ), recognizing the application's own routes, and the router'sLinkprimitive, 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
linksshape, and the consumption rules link actions follow.The chrome
The actions menu and the footer resolve each action using a shared
getActionRoutehelper.downloadandopenInNewTabalways use the plain anchor: both open a new document.In a match, the host link is injected via the existing
renderprop 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:
DashboardWidgetHostProviderwraps the stage, andmatchDashboardHrefrecognizes hrefs that target the same document and the samepagequery arg, resolving thepparam as the route.Screen.Recording.2026-08-19.at.8.41.42.AM.mov
The demo
The
HostLinksStorybook 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
Detailsaction from #81729 upgrades to client-side navigation with no manifest change.Testing
Detailsin the widget footer: it lands on the Site Health page without a full page load.Details: it is a real anchor.StatusandInfostill point at the classic screen and full-load as before.HostLinksstory shows the route link beside the plain anchor and the download, with a status line confirming the document never reloaded: