create <ThemeProvider> to apply <TitleFormatterProvider> consistently + apply to website

This commit is contained in:
sebastien 2025-04-11 17:46:46 +02:00
parent 7afade4199
commit cf8952bd4b
7 changed files with 117 additions and 4 deletions

View file

@ -122,6 +122,15 @@ declare module '@theme/Root' {
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' {
import type {ReactNode} from 'react';

View file

@ -1569,6 +1569,17 @@ declare module '@theme/ThemedImage' {
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' {
import {Details, type DetailsProps} from '@docusaurus/theme-common/Details';

View file

@ -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>
);
}

View file

@ -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>;
}

View file

@ -12,6 +12,7 @@ import routes from '@generated/routes';
import {useLocation} from '@docusaurus/router';
import renderRoutes from '@docusaurus/renderRoutes';
import Root from '@theme/Root';
import ThemeProvider from '@theme/ThemeProvider';
import SiteMetadata from '@theme/SiteMetadata';
import normalizeLocation from './normalizeLocation';
import {BrowserContextProvider} from './browserContext';
@ -43,10 +44,12 @@ export default function App(): ReactNode {
<DocusaurusContextProvider>
<BrowserContextProvider>
<Root>
<SiteMetadataDefaults />
<SiteMetadata />
<BaseUrlIssueBanner />
<AppNavigation />
<ThemeProvider>
<SiteMetadataDefaults />
<SiteMetadata />
<BaseUrlIssueBanner />
<AppNavigation />
</ThemeProvider>
</Root>
<HasHydratedDataAttribute />
</BrowserContextProvider>

View file

@ -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}</>;
}

View 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>
);
}