-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make current theme and editor settings available to route area resolvers #69299
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +89 B (0%) Total Size: 1.84 MB
ℹ️ View Unchanged
|
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. |
I haven't tested this, but can we resolve the wrapper issue by returning I don't have a strong opinion on Router package changes, but I'll vote for the simplest solution for this particular case. |
I have tried the code below: function HomePreview() {
return undefined;
}
export const homeRoute = {
name: 'home',
path: '/',
areas: {
sidebar: <SidebarNavigationScreenMain />,
preview: <HomePreview />,
mobile: <SidebarNavigationScreenMain />,
},
}; I was hoping that the preview area wouldn't be rendered, but at this point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% certain that this couldn’t some day be made unnecessary but it seems a sensible extensibility feature to add to the router and it’s a simple enough change. The router is also entirely private so I think we should go with it for now.
I have a few opinions on it though. I happened to explore this same concept recently when thinking about a solution for #69108.
Flaky tests detected in 9bcca28. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/13544266093
|
Thanks for the feedback!
Although the route registration API is private and therefore they can remove this parameter in the future if they wish, I am also concerned about this. If we proceed more cautiously without making changes to the router package, I would like to suggest a consistent UI like the following:
I don't have a strong opinion on which approach to take, but I just want to avoid UI like this (e.g. unnatural resize handles) 😅 |
I am less concerned about empty screens or resize handles, than about the user accessing functionality that is broken for classic theme users. |
In time, isn't the plan that all of the Site Editor will work for all theme types? There will be only one theme type. So yes it should eventually be unnecessary, it will take time though. |
This is true, but the site editor will likely eventually be required to be made available to more than just administrators. In that case, we may want to control which screens are accessible based on user roles. This PR is also necessary to control access to stylebook based on theme support. Either way, I think a mechanism for controlling areas based on some conditions will be necessary in the future. |
If I remember correctly, the router is still private/internal API; if we find a better alternative to |
Yes.
|
matchResolverArgs={ { | ||
siteData: { currentTheme }, | ||
} } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each time this component re-renders, the matchResolverArgs
will be a new value, even though the currentTheme
value never changes. This will invalidate memoization in useMatch
, resulting in a new match
.
I'm unsure what consequences this can have on the Site Editor app; it would be hard to tell without proper testing. This makes me hesitant to approve this; just days away from Beta 1.
We could try and memoize (useMemo
) the matchResolverArgs
value, just like beforeNavigate
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had noted this too—latter part of #69299 (comment).
The value could be memoized before passing it as a simple safeguard. However, right now, it doesn’t seem to be an issue. App
doesn’t rerender any more often so matchResolverArgs
remains practically stable. I could be overlooking something but testing with this diff is what I did to try proving it:
Log on change of matchResolverArgs
diff --git a/packages/router/src/router.tsx b/packages/router/src/router.tsx
index 8fbf6e609f..1750989f4f 100644
--- a/packages/router/src/router.tsx
+++ b/packages/router/src/router.tsx
@@ -12,6 +12,7 @@ import {
useContext,
useSyncExternalStore,
useMemo,
+ useInsertionEffect,
} from '@wordpress/element';
import {
addQueryArgs,
@@ -157,7 +158,14 @@ export default function useMatch(
matchResolverArgs: Record< string, any >
): Match {
const { query: rawQuery = {} } = location;
-
+ useInsertionEffect(
+ () =>
+ console.log(
+ 'using match: matchResolverArgs changed!',
+ matchResolverArgs
+ ),
+ [ matchResolverArgs ]
+ );
return useMemo( () => {
const { [ pathArg ]: path = '/', ...query } = rawQuery;
const result = matcher.recognize( path )?.[ 0 ];
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it’s safer to memoize now. It can be missed later and can have unexpected side effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the research everyone! I'll try out your suggestions tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we freeze this PR and try the alternative approach suggested in this comment? In other words, we try a similar approach to #69005:
If navigation within the site editor is properly controlled, users should never see this screen unless they access it directly via a URL. I think this approach can be worked on even after the beta release, because it's a "bug fix" that doesn't add any new APIs. What do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Maybe the title should be updated before merging. "Make current theme available to route area resolvers" would be my suggestion. Maybe prefixed by "Site Editor:"
080f230
to
6a1bfeb
Compare
6a1bfeb
to
00766d0
Compare
Or, you could let enable the theme support if theme.json exists, and still:
|
Ideally, we would add a new field to the REST API endpoint. This is about here. if ( rest_is_field_included( 'has_theme_json', $fields ) ) {
$data['has_theme_json'] = wp_theme_has_theme_json();
} However, I am hesitant to add such a new field just before the Beta release. |
Preparation to move #69005 and #69043 forward
What?
This PR adds a new
context
matchResolverArgs
parameter to theuseMatch
hook.This will allow us to control whether or not to render a particular area based on the theme, for example.
Why?
Currently, the site editor determines what to render in which area based on the path, and if the area is not
undefined
, it renders the component in some wrapper element (example).As attempted in #69005 (review), there are cases where we don't want to render a specific area itself based on a condition. For example, whether it's a block theme.
However, even if a component returns null based on a certain condition, like this, the result is not equal to
undefined
, so the area wrapper itself will be rendered:How?
context
matchResolverArgs
parameter to theuseMatch
hook. This context can be used to pass any information through theRouterProvider
.Testing Instructions
For example, make the following changes: You should now have access to the information provided by your router provider:
In #69005, we are trying to control which screens are exposed to the classic theme. For example, to not expose the navigation screen to the classic theme, we should be able to disable the specific area itself as follows:
In #69043, we are trying to control access to the StyleBook based on theme support. For example, we should be able to disable the specific area itself as follows: