Skip to content

Commit

Permalink
Site Editor: Make current theme and editor settings available to rout…
Browse files Browse the repository at this point in the history
…e area resolvers (#69299)

* Router: Add context parameter to useMatch

* Change `context` prop to `matchResolverArgs`

* Remove `getThemeSupports` selector

* Memorize matchResolverArgs value

* Add editor settings to route area resolvers

* Add comment for editorSettings

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: stokesman <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: carolinan <[email protected]>
  • Loading branch information
5 people authored Mar 6, 2025
1 parent a793830 commit 877c45b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
21 changes: 18 additions & 3 deletions packages/edit-site/src/components/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*/
import { useSelect } from '@wordpress/data';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useCallback } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -30,9 +31,15 @@ function AppLayout() {

export default function App() {
useRegisterSiteEditorRoutes();
const routes = useSelect( ( select ) => {
return unlock( select( editSiteStore ) ).getRoutes();
const { routes, currentTheme, editorSettings } = useSelect( ( select ) => {
return {
routes: unlock( select( editSiteStore ) ).getRoutes(),
currentTheme: select( coreStore ).getCurrentTheme(),
// This is a temp solution until the has_theme_json value is available for the current theme.
editorSettings: select( editSiteStore ).getSettings(),
};
}, [] );

const beforeNavigate = useCallback( ( { path, query } ) => {
if ( ! isPreviewingTheme() ) {
return { path, query };
Expand All @@ -50,11 +57,19 @@ export default function App() {
};
}, [] );

const matchResolverArgsValue = useMemo(
() => ( {
siteData: { currentTheme, editorSettings },
} ),
[ currentTheme, editorSettings ]
);

return (
<RouterProvider
routes={ routes }
pathArg="p"
beforeNavigate={ beforeNavigate }
matchResolverArgs={ matchResolverArgsValue }
>
<AppLayout />
</RouterProvider>
Expand Down
15 changes: 11 additions & 4 deletions packages/router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export function useHistory() {
export default function useMatch(
location: LocationWithQuery,
matcher: RouteRecognizer,
pathArg: string
pathArg: string,
matchResolverArgs: Record< string, any >
): Match {
const { query: rawQuery = {} } = location;

Expand All @@ -178,7 +179,11 @@ export default function useMatch(
if ( typeof value === 'function' ) {
return [
key,
value( { query, params: result.params } ),
value( {
query,
params: result.params,
...matchResolverArgs,
} ),
];
}
return [ key, value ];
Expand All @@ -193,19 +198,21 @@ export default function useMatch(
query,
path: addQueryArgs( path, query ),
};
}, [ matcher, rawQuery, pathArg ] );
}, [ matcher, rawQuery, pathArg, matchResolverArgs ] );
}

export function RouterProvider( {
routes,
pathArg,
beforeNavigate,
children,
matchResolverArgs,
}: {
routes: Route[];
pathArg: string;
beforeNavigate?: BeforeNavigate;
children: React.ReactNode;
matchResolverArgs: Record< string, any >;
} ) {
const location = useSyncExternalStore(
history.listen,
Expand All @@ -221,7 +228,7 @@ export function RouterProvider( {
} );
return ret;
}, [ routes ] );
const match = useMatch( location, matcher, pathArg );
const match = useMatch( location, matcher, pathArg, matchResolverArgs );
const config = useMemo(
() => ( { beforeNavigate, pathArg } ),
[ beforeNavigate, pathArg ]
Expand Down

1 comment on commit 877c45b

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 877c45b.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/13698294986
📝 Reported issues:

Please sign in to comment.