Skip to content
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

RFC: allow routes to declare a React context #6885

Open
2 tasks done
slorber opened this issue Mar 9, 2022 · 2 comments
Open
2 tasks done

RFC: allow routes to declare a React context #6885

slorber opened this issue Mar 9, 2022 · 2 comments
Labels
apprentice Issues that are good candidates to be handled by a Docusaurus apprentice / trainee domain: content plugin Related to content plugin emitting metadata for theme consumption proposal This issue is a proposal, usually non-trivial change
Milestone

Comments

@slorber
Copy link
Collaborator

slorber commented Mar 9, 2022

Have you read the Contributing Guidelines on issues?

Motivation

It is convenient to be able to access page metadata as a hook, from anywhere in the tree, without any props drilling.

Some use-cases:

I think it could make sense to have a more generic Docusaurus core API to provide an official route context feature?

addRoute({
  path: "/docs/",
  component: "DocVersion",
  context: {sidebars}, // Something here
  routes: [
    addRoute({
	    path: "/docs/",
	    component: "DocItem",
	    context: {docMetadata}, // Something here
	}}
  ],
}}

We could expose a generic useRouteContext(): unknown, and content plugins could expose consuming hooks to read it in a typesafe way.

If the route has multiple layers like above, we could merge the context (similar to MDXProvider components), eventually throw an error if some key is overridden.

This also means that we could remove the regular props from the existing layout components: now all the static data can always be read from the context hooks (apart from the dynamic props related to the UI state)

IMHO having "props-less" theme components also present some advantages. It would notably allow more flexibility to refactor theme components without any props breaking change.

Using context hooks also helps clarify the API surface to the context, and provides an additional layer for us to try to provide retro compatible changes in case the underlying data structures need to be updated.


I wonder what you think of this idea?

Of course, it would be a quite significant breaking change for users that have a lot of swizzled components, as props would have to be replaced by hooks calls in many places.

Self-service

  • I'd be willing to do some initial work on this proposal myself.
@slorber slorber added proposal This issue is a proposal, usually non-trivial change status: needs triage This issue has not been triaged by maintainers labels Mar 9, 2022
@slorber slorber removed the status: needs triage This issue has not been triaged by maintainers label Mar 9, 2022
@Josh-Cena
Copy link
Collaborator

Yes 👍 I'm also thinking about refactoring props to context.

However, there are certain places where props make more sense. For example, the paginators should probably receive next and prev to remain general-purpose and not tied to a specific plugin.

@slorber
Copy link
Collaborator Author

slorber commented Mar 10, 2022

Yes I definitively don't mean that we should use context/hooks everywhere and remove all props, just that to access page data it might be more convenient to use hooks rather than props.

@Josh-Cena Josh-Cena added the domain: content plugin Related to content plugin emitting metadata for theme consumption label Apr 12, 2022
@slorber slorber added this to the 3.0 milestone Aug 17, 2023
@slorber slorber added the apprentice Issues that are good candidates to be handled by a Docusaurus apprentice / trainee label Sep 25, 2023
@slorber slorber modified the milestones: 3.0, 3.x Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apprentice Issues that are good candidates to be handled by a Docusaurus apprentice / trainee domain: content plugin Related to content plugin emitting metadata for theme consumption proposal This issue is a proposal, usually non-trivial change
Projects
None yet
Development

No branches or pull requests

2 participants