mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-07 06:07:42 +02:00
create <ThemeProvider> to apply <TitleFormatterProvider> consistently + apply to website
This commit is contained in:
parent
7afade4199
commit
cf8952bd4b
7 changed files with 117 additions and 4 deletions
|
@ -122,6 +122,15 @@ declare module '@theme/Root' {
|
||||||
export default function Root({children}: Props): ReactNode;
|
export default function Root({children}: Props): ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module '@theme/ThemeProvider' {
|
||||||
|
import type {ReactNode} from 'react';
|
||||||
|
|
||||||
|
export interface Props {
|
||||||
|
readonly children: ReactNode;
|
||||||
|
}
|
||||||
|
export default function ThemeProvider({children}: Props): ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
declare module '@theme/SiteMetadata' {
|
declare module '@theme/SiteMetadata' {
|
||||||
import type {ReactNode} from 'react';
|
import type {ReactNode} from 'react';
|
||||||
|
|
||||||
|
|
|
@ -1569,6 +1569,17 @@ declare module '@theme/ThemedImage' {
|
||||||
export default function ThemedImage(props: Props): ReactNode;
|
export default function ThemedImage(props: Props): ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare module '@theme/ThemeProvider/TitleFormatter' {
|
||||||
|
import type {ReactNode} from 'react';
|
||||||
|
|
||||||
|
export interface Props {
|
||||||
|
readonly children: ReactNode;
|
||||||
|
}
|
||||||
|
export default function ThemeProviderTitleFormatter({
|
||||||
|
children,
|
||||||
|
}: Props): ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
declare module '@theme/Details' {
|
declare module '@theme/Details' {
|
||||||
import {Details, type DetailsProps} from '@docusaurus/theme-common/Details';
|
import {Details, type DetailsProps} from '@docusaurus/theme-common/Details';
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
/**
|
||||||
|
* 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 ComponentProps, type ReactNode} from 'react';
|
||||||
|
import {TitleFormatterProvider} from '@docusaurus/theme-common/internal';
|
||||||
|
import type {Props} from '@theme/ThemeProvider/TitleFormatter';
|
||||||
|
|
||||||
|
type FormatterProp = ComponentProps<typeof TitleFormatterProvider>['formatter'];
|
||||||
|
|
||||||
|
const formatter: FormatterProp = (params) => {
|
||||||
|
// Add your own title formatting logic here!
|
||||||
|
return params.defaultFormatter(params);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function ThemeProviderTitleFormatter({
|
||||||
|
children,
|
||||||
|
}: Props): ReactNode {
|
||||||
|
return (
|
||||||
|
<TitleFormatterProvider formatter={formatter}>
|
||||||
|
{children}
|
||||||
|
</TitleFormatterProvider>
|
||||||
|
);
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
/**
|
||||||
|
* 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 TitleFormatterProvider from '@theme/ThemeProvider/TitleFormatter';
|
||||||
|
import type {Props} from '@theme/ThemeProvider';
|
||||||
|
|
||||||
|
export default function ThemeProvider({children}: Props): ReactNode {
|
||||||
|
return <TitleFormatterProvider>{children}</TitleFormatterProvider>;
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ import routes from '@generated/routes';
|
||||||
import {useLocation} from '@docusaurus/router';
|
import {useLocation} from '@docusaurus/router';
|
||||||
import renderRoutes from '@docusaurus/renderRoutes';
|
import renderRoutes from '@docusaurus/renderRoutes';
|
||||||
import Root from '@theme/Root';
|
import Root from '@theme/Root';
|
||||||
|
import ThemeProvider from '@theme/ThemeProvider';
|
||||||
import SiteMetadata from '@theme/SiteMetadata';
|
import SiteMetadata from '@theme/SiteMetadata';
|
||||||
import normalizeLocation from './normalizeLocation';
|
import normalizeLocation from './normalizeLocation';
|
||||||
import {BrowserContextProvider} from './browserContext';
|
import {BrowserContextProvider} from './browserContext';
|
||||||
|
@ -43,10 +44,12 @@ export default function App(): ReactNode {
|
||||||
<DocusaurusContextProvider>
|
<DocusaurusContextProvider>
|
||||||
<BrowserContextProvider>
|
<BrowserContextProvider>
|
||||||
<Root>
|
<Root>
|
||||||
<SiteMetadataDefaults />
|
<ThemeProvider>
|
||||||
<SiteMetadata />
|
<SiteMetadataDefaults />
|
||||||
<BaseUrlIssueBanner />
|
<SiteMetadata />
|
||||||
<AppNavigation />
|
<BaseUrlIssueBanner />
|
||||||
|
<AppNavigation />
|
||||||
|
</ThemeProvider>
|
||||||
</Root>
|
</Root>
|
||||||
<HasHydratedDataAttribute />
|
<HasHydratedDataAttribute />
|
||||||
</BrowserContextProvider>
|
</BrowserContextProvider>
|
||||||
|
|
|
@ -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, {type ReactNode} from 'react';
|
||||||
|
import type {Props} from '@theme/ThemeProvider';
|
||||||
|
|
||||||
|
// Wrapper component expected to be implemented by a theme
|
||||||
|
// Unlike <Layout>, it applies to all sites routes and never unmounts
|
||||||
|
//
|
||||||
|
// Unlike <Root>, the theme is expected to provide an implementation
|
||||||
|
// <Root> is empty and the implementation is expected to be provided by the user
|
||||||
|
//
|
||||||
|
// Tree order: Root > ThemeProvider > Layout
|
||||||
|
export default function ThemeProvider({children}: Props): ReactNode {
|
||||||
|
return <>{children}</>;
|
||||||
|
}
|
29
website/src/theme/ThemeProvider/TitleFormatter/index.tsx
Normal file
29
website/src/theme/ThemeProvider/TitleFormatter/index.tsx
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
/**
|
||||||
|
* 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 ComponentProps, type ReactNode} from 'react';
|
||||||
|
import {TitleFormatterProvider} from '@docusaurus/theme-common/internal';
|
||||||
|
import type {Props} from '@theme/ThemeProvider/TitleFormatter';
|
||||||
|
|
||||||
|
type FormatterProp = ComponentProps<typeof TitleFormatterProvider>['formatter'];
|
||||||
|
|
||||||
|
const formatter: FormatterProp = (params) => {
|
||||||
|
if (params.title) {
|
||||||
|
return 'my custom title';
|
||||||
|
}
|
||||||
|
return params.defaultFormatter(params);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function ThemeProviderTitleFormatter({
|
||||||
|
children,
|
||||||
|
}: Props): ReactNode {
|
||||||
|
return (
|
||||||
|
<TitleFormatterProvider formatter={formatter}>
|
||||||
|
{children}
|
||||||
|
</TitleFormatterProvider>
|
||||||
|
);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue