-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
feat(core,theme): useRouteContext + HtmlClassNameProvider #6933
Changes from all commits
ee9a213
840237c
032516e
fb5d766
a52601e
ef38c6c
89978ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {type ReactNode} from 'react'; | ||
import Head from '@docusaurus/Head'; | ||
import clsx from 'clsx'; | ||
import useRouteContext from '@docusaurus/useRouteContext'; | ||
|
||
const HtmlClassNameContext = React.createContext<string | undefined>(undefined); | ||
|
||
// This wrapper is necessary because Helmet does not "merge" classes | ||
// See https://github.com/staylor/react-helmet-async/issues/161 | ||
export function HtmlClassNameProvider({ | ||
className: classNameProp, | ||
children, | ||
}: { | ||
className: string; | ||
children: ReactNode; | ||
}): JSX.Element { | ||
const classNameContext = React.useContext(HtmlClassNameContext); | ||
const className = clsx(classNameContext, classNameProp); | ||
return ( | ||
<HtmlClassNameContext.Provider value={className}> | ||
<Head> | ||
<html className={className} /> | ||
</Head> | ||
{children} | ||
</HtmlClassNameContext.Provider> | ||
); | ||
} | ||
|
||
function pluginNameToClassName(pluginName: string) { | ||
return `plugin-${pluginName.replace( | ||
/docusaurus-(?:plugin|theme)-(?:content-)?/gi, | ||
'', | ||
)}`; | ||
} | ||
|
||
export function PluginHtmlClassNameProvider({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}): JSX.Element { | ||
const routeContext = useRouteContext(); | ||
const nameClass = pluginNameToClassName(routeContext.plugin.name); | ||
const idClass = `plugin-id-${routeContext.plugin.id}`; | ||
return ( | ||
<HtmlClassNameProvider className={clsx(nameClass, idClass)}> | ||
{children} | ||
</HtmlClassNameProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import Loading from '@theme/Loading'; | |
import routesChunkNames from '@generated/routesChunkNames'; | ||
import registry from '@generated/registry'; | ||
import flat from '../flat'; | ||
import {RouteContextProvider} from '../routeContext'; | ||
|
||
type OptsLoader = Record<string, typeof registry[keyof typeof registry][0]>; | ||
|
||
|
@@ -22,7 +23,16 @@ export default function ComponentCreator( | |
if (path === '*') { | ||
return Loadable({ | ||
loading: Loading, | ||
loader: () => import('@theme/NotFound'), | ||
loader: async () => { | ||
const NotFound = (await import('@theme/NotFound')).default; | ||
return (props) => ( | ||
// Is there a better API for this? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤪 good catch There's also a native "Loading" screen but afaik it's never displayed so 🤷♂️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the "native" name isn't the best, but I don't think this will be targeted anyways... |
||
<RouteContextProvider | ||
value={{plugin: {name: 'native', id: 'default'}}}> | ||
<NotFound {...(props as never)} /> | ||
</RouteContextProvider> | ||
); | ||
}, | ||
}); | ||
} | ||
|
||
|
@@ -84,7 +94,18 @@ export default function ComponentCreator( | |
|
||
const Component = loadedModules.component; | ||
delete loadedModules.component; | ||
return <Component {...loadedModules} {...props} />; | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
const routeContextModule = loadedModules.__routeContextModule; | ||
delete loadedModules.__routeContextModule; | ||
/* eslint-enable no-underscore-dangle */ | ||
|
||
// Is there any way to put this RouteContextProvider upper in the tree? | ||
return ( | ||
<RouteContextProvider value={routeContextModule}> | ||
<Component {...loadedModules} {...props} />; | ||
</RouteContextProvider> | ||
); | ||
}, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import type {PluginRouteContext} from '@docusaurus/types'; | ||
import {Context} from '../routeContext'; | ||
|
||
export default function useRouteContext(): PluginRouteContext { | ||
const context = React.useContext(Context); | ||
if (!context) { | ||
throw new Error( | ||
'Unexpected: no Docusaurus parent/current route context found', | ||
); | ||
} | ||
return context; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {useMemo, type ReactNode} from 'react'; | ||
import type {PluginRouteContext, RouteContext} from '@docusaurus/types'; | ||
|
||
export const Context = React.createContext<PluginRouteContext | null>(null); | ||
|
||
function mergeContexts({ | ||
parent, | ||
value, | ||
}: { | ||
parent: PluginRouteContext | null; | ||
value: RouteContext | null; | ||
}): PluginRouteContext { | ||
if (!parent) { | ||
if (!value) { | ||
throw new Error( | ||
'Unexpected: no Docusaurus parent/current route context found', | ||
); | ||
} else if (!('plugin' in value)) { | ||
throw new Error( | ||
'Unexpected: Docusaurus parent route context has no plugin attribute', | ||
); | ||
} | ||
return value; | ||
} | ||
|
||
// TODO deep merge this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure we really want to deep merge In case there are multiple layers I'd rather ensure that laters do not override each others, there's no good use-case to do so that I can think of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it would be immediately useful for content plugins, but maybe in userland? Deep merging sounds more natural about how these stores should behave |
||
const data = {...parent.data, ...value?.data}; | ||
|
||
return { | ||
// nested routes are not supposed to override plugin attribute | ||
plugin: parent.plugin, | ||
data, | ||
}; | ||
} | ||
|
||
export function RouteContextProvider({ | ||
children, | ||
value, | ||
}: { | ||
children: ReactNode; | ||
value: PluginRouteContext | null; | ||
}): JSX.Element { | ||
const parent = React.useContext(Context); | ||
|
||
const mergedValue = useMemo( | ||
() => mergeContexts({parent, value}), | ||
[parent, value], | ||
); | ||
|
||
return <Context.Provider value={mergedValue}>{children}</Context.Provider>; | ||
} |
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.
temp because the other PR also have created a similarly named file