-
-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee9a213
add useRouteContext
slorber 840237c
add HtmlClassNameProvider component + make it work for docs versions
slorber 032516e
create html className for doc id
slorber fb5d766
refactors
Josh-Cena a52601e
remove generic
Josh-Cena ef38c6c
refactor...
Josh-Cena 89978ea
fix build failure
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/docusaurus-theme-common/src/utils/metadataUtilsTemp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* 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 annoying 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( | ||
new RegExp( | ||
[ | ||
'docusaurus-plugin-content-', | ||
'docusaurus-plugin-', | ||
'docusaurus-theme-', | ||
].join('|'), | ||
'gi', | ||
), | ||
'', | ||
)}`; | ||
} | ||
|
||
export function PluginHtmlClassNameProvider({children}: {children: ReactNode}) { | ||
const routeContext = useRouteContext(); | ||
const nameClass = pluginNameToClassName(routeContext.plugin.name); | ||
const idClass = `plugin-id-${routeContext.plugin.id}`; | ||
return ( | ||
<HtmlClassNameProvider className={clsx(nameClass, idClass)}> | ||
{children} | ||
</HtmlClassNameProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/docusaurus/src/client/exports/useRouteContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 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< | ||
Data = unknown, | ||
>(): PluginRouteContext<Data> { | ||
const context = React.useContext(Context); | ||
if (!context) { | ||
throw new Error( | ||
'Unexpected: no Docusaurus parent/current route context found', | ||
); | ||
} | ||
return context as PluginRouteContext<Data>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* 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: PluginRouteContext | 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', | ||
); | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
// See TS issue https://stackoverflow.com/a/51193091/82609 | ||
// eslint-disable-next-line prefer-object-spread | ||
const data = Object.assign({}, 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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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